GitPolicyEnforcer 0.3.0

GitPolicyEnforcer is a command-line tool that helps you enforce Git policies through Git hooks, both server and client side
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
use crate::loggers::log_to_file;
use crate::structs::{UpdateHookData, UpdateRules, ValidationError};
use regex::Regex;
use std::process::{exit, Command};

// Public functions
pub fn validate_update_rules(
    hook_rules: &UpdateRules,
    hook_data: &UpdateHookData,
) -> Result<(), ValidationError> {
    if hook_rules.branches.is_some() {
        // Do not run any validation if the current branch is not in the list of provided branches.
        if !hook_rules
            .branches
            .as_ref()
            .unwrap()
            .contains(&hook_data.branch)
        {
            return Ok(());
        }
    }

    let commits_range: Vec<String> =
        _get_commits_range(&hook_data.old_commit, &hook_data.new_commit);
    let commits: Vec<String> = _get_commits(&commits_range);
    let commit_titles: Vec<String> = _get_commit_titles(&commits);
    let commit_bodies = _get_commit_bodies(&commits);

    let title_regex_validator = create_regex(&hook_rules.title_format)?;

    // Title related validations.
    validate_title_format(&commit_titles, &title_regex_validator)?;
    _validate_title_max_length(&commit_titles, hook_rules.title_max_length)?;

    if hook_rules.body_required.is_some() {
        _validate_body_required(&commit_bodies)?;
    };

    if hook_rules.body_max_line_length.is_some() {
        _validate_body_max_line_length(&commit_bodies, hook_rules.body_max_line_length.unwrap())?;
    }

    // Todo: Pending.
    // if let Some(true) = hook_rules.enforce_squash_merge {
    //     _validator_enforce_squash_merge(&commits_range)?;
    // }

    Ok(())
}

pub fn create_regex(regex_str: &str) -> Result<Regex, crate::ValidationError> {
    match regex::Regex::new(regex_str) {
        Ok(r) => Ok(r),
        Err(_) => Err(crate::ValidationError::RegexCompilation(
            regex_str.to_owned(),
        )),
    }
}

// Private functions.
pub fn validate_title_format(
    commit_titles: &[String],
    regex_validator: &regex::Regex,
) -> Result<(), ValidationError> {
    for commit_title in commit_titles {
        if !regex_validator.is_match(commit_title) {
            return Err(ValidationError::TitleFormat(format!(
                "{:?}",
                regex_validator
            )));
        }
    }

    Ok(())
}

fn _validate_body_required(commit_bodies: &[Vec<String>]) -> Result<(), ValidationError> {
    for commit_body in commit_bodies {
        if commit_body.is_empty() {
            return Err(ValidationError::BodyRequired);
        }
    }
    Ok(())
}

fn _get_commits(commits_range: &[String]) -> Vec<String> {
    commits_range
        .iter()
        .map(|commit_hash| _get_commit(commit_hash))
        .collect()
}

/// Extracts the full commit, from a commit hash.
fn _get_commit(commit_hash: &str) -> String {
    let output = match Command::new("git")
        .arg("cat-file")
        .arg("commit")
        .arg(commit_hash)
        .output()
    {
        Ok(v) => v,
        Err(_e) => {
            let _ =
                log_to_file("_get_commit(): Failed to execute git cat-file commit <commit_hash>.");
            exit(1);
        }
    };

    match String::from_utf8(output.stdout) {
        Ok(v) => v,
        Err(_e) => {
            let _ =
                log_to_file("_get_commit(): Failed to get utf8 string from git cat-file output");
            exit(1);
        }
    }
}

fn _get_commit_bodies(commits: &[String]) -> Vec<Vec<String>> {
    return commits
        .iter()
        .map(|commit_hash| _get_commit_body(commit_hash))
        .collect();
}

/// Extracts the commit body from a full commit.
fn _get_commit_body(commit: &str) -> Vec<String> {
    let mut body_lines = vec![];
    let mut first_empty_line_found = false;
    let mut second_empty_line_found = false;

    // Start saving the commit lines that come after the second empty line.
    // The second empty line comes after the commit title and before the commit body.
    for line in commit.lines() {
        let line = line.trim();

        if line.is_empty() && !first_empty_line_found {
            first_empty_line_found = true;
            continue;
        }

        if line.is_empty() && first_empty_line_found {
            second_empty_line_found = true;
            continue;
        }

        if !line.is_empty() && second_empty_line_found {
            body_lines.push(line.to_owned());
            continue;
        }
    }

    body_lines
}

fn _get_commit_titles(commits: &[String]) -> Vec<String> {
    commits
        .iter()
        .map(|commit| _get_commit_title(commit))
        .collect()
}

/// Extracts the commit title from a full commit message.
fn _get_commit_title(commit: &str) -> String {
    let mut title = "";
    let mut found_first_empty_line = false;

    for line in commit.lines() {
        let line = line.trim();
        if line.is_empty() && !found_first_empty_line {
            found_first_empty_line = true;
        }
        if !line.is_empty() && found_first_empty_line {
            title = line;
            break;
        }
    }

    title.to_owned()
}

fn _validate_title_max_length(
    commit_titles: &[String],
    max_title_length: u8,
) -> Result<(), ValidationError> {
    for title in commit_titles.iter() {
        let number_of_characters = title.chars().count();
        if number_of_characters > max_title_length as usize {
            return Err(ValidationError::TitleMaxLength(max_title_length));
        }
    }
    Ok(())
}

fn _get_commits_range(old_commit: &str, new_commit: &str) -> Vec<String> {
    // Todo: This implementation does not correctly get all the commits.
    // Todo: Check the correct way to get all the commits.
    // Todo: Get all commits from current branch and remove the ones that exists in the target branch
    // git rev-list HEAD (and remove the ones that exists in the target branch)
    // git rev-list target_branch..HEAD (git rev-list master..HEAD)
    let commit_range = format!("{}..{}", old_commit, new_commit);
    let output = match Command::new("git")
        .arg("rev-list")
        .arg(commit_range)
        .output()
    {
        Ok(v) => v,
        Err(_e) => {
            let _ = log_to_file("_get_commits_range(): Failed to execute git rev-list");
            exit(1);
        }
    };
    let output_string = match String::from_utf8(output.stdout) {
        Ok(v) => v,
        Err(_e) => {
            let _ = log_to_file("_get_commits_range(): Failed to utf8 the git rev result");
            exit(1);
        }
    };
    output_string
        .lines()
        .into_iter()
        .map(|line| line.to_owned())
        .collect()
}

fn _validator_enforce_squash_merge(commits_range: &[String]) -> Result<(), ValidationError> {
    if commits_range.len() > 1 {
        return Err(ValidationError::EnforceSquashMerge);
    }
    Ok(())
}

fn _validate_body_max_line_length(
    commit_bodies: &[Vec<String>],
    body_max_line_length: u8,
) -> Result<(), ValidationError> {
    for commit_body in commit_bodies {
        for line in commit_body {
            let number_of_characters = line.chars().count();
            if number_of_characters > body_max_line_length as usize {
                return Err(ValidationError::BodyMaxLineLength(body_max_line_length));
            }
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_validator_title_format() {
        let regex_string = "^((\\bECSTU\\b)|(\\bINTERSCALE\\b))-\\d{1,}: \\w+.*$".to_owned();
        let regex = regex::Regex::new(&regex_string).unwrap();

        let commit_titles = vec!["ECSTU-123: This is the title description".to_owned()];
        let result = validate_title_format(&commit_titles, &regex);
        assert!(result.is_ok());

        let commit_titles = vec!["ECSTU-: This is the title description".to_owned()];
        let result = validate_title_format(&commit_titles, &regex);
        assert_eq!(
            result.err().unwrap(),
            ValidationError::TitleFormat(regex_string.clone())
        );

        let commit_titles = vec!["ECSTU-1:    ".to_owned()];
        let result = validate_title_format(&commit_titles, &regex);
        assert_eq!(
            result.err().unwrap(),
            ValidationError::TitleFormat(regex_string.clone())
        );

        let commit_titles = vec!["ECSTU-1: a".to_owned()];
        let result = validate_title_format(&commit_titles, &regex);
        assert!(result.is_ok());

        // New regex
        let regex_string = "^[A-Z]+-\\d{1,}: \\w+.*$".to_owned();
        let regex = regex::Regex::new(&regex_string).unwrap();

        let commit_titles = vec!["HELLO-1: a".to_owned()];
        let result = validate_title_format(&commit_titles, &regex);
        assert!(result.is_ok());

        let commit_titles = vec!["HELLo-1: a".to_owned()];
        let result = validate_title_format(&commit_titles, &regex);
        assert_eq!(
            result.err().unwrap(),
            ValidationError::TitleFormat(regex_string.clone())
        );
    }

    #[test]
    fn test_validator_title_max_length() {
        let commit_titles = vec![
            "Title line 1".to_owned(),
            "Title line 2".to_owned(),
            "Title line 3".to_owned(),
        ];
        let result = _validate_title_max_length(&commit_titles, 12);
        assert!(result.is_ok());

        let commit_titles = vec![
            "Bigger title line 1".to_owned(),
            "Title line 2".to_owned(),
            "Title line 3".to_owned(),
        ];
        let result = _validate_title_max_length(&commit_titles, 12);
        assert_eq!(result.err().unwrap(), ValidationError::TitleMaxLength(12));

        let commit_titles = vec![
            "Title line 1".to_owned(),
            "Title line 2".to_owned(),
            "Bigger title line 3".to_owned(),
        ];
        let result = _validate_title_max_length(&commit_titles, 12);
        assert_eq!(result.err().unwrap(), ValidationError::TitleMaxLength(12));

        let commit_titles = vec![];
        let result = _validate_title_max_length(&commit_titles, 12);
        assert!(result.is_ok())
    }

    #[test]
    fn test_get_commit_title() {
        let commit = "tree d6b3dd4b08f63ba13479484508e0679d32a7891a
author John Doe <john.doe@gmail.com>
committer John Doe <john.doe@gmail.com>

This is the commit title 1";
        assert_eq!(
            _get_commit_title(commit),
            "This is the commit title 1".to_owned()
        );

        let commit = "tree d6b3dd4b08f63ba13479484508e0679d32a7891a
author John Doe <john.doe@gmail.com>
committer John Doe <john.doe@gmail.com>

              This is the commit title 2";
        assert_eq!(
            _get_commit_title(commit),
            "This is the commit title 2".to_owned()
        );

        let commit = "tree d6b3dd4b08f63ba13479484508e0679d32a7891a
author John Doe <john.doe@gmail.com>
committer John Doe <john.doe@gmail.com>

This is the commit title 3         ";
        assert_eq!(
            _get_commit_title(commit),
            "This is the commit title 3".to_owned()
        );

        let commit = "tree d6b3dd4b08f63ba13479484508e0679d32a7891a
author John Doe <john.doe@gmail.com>
committer John Doe <john.doe@gmail.com>

This is the commit title 4

";
        assert_eq!(
            _get_commit_title(commit),
            "This is the commit title 4".to_owned()
        );

        let commit = "tree d6b3dd4b08f63ba13479484508e0679d32a7891a
author John Doe <john.doe@gmail.com>
committer John Doe <john.doe@gmail.com>

This is the commit title 5

This is the body
";
        assert_eq!(
            _get_commit_title(commit),
            "This is the commit title 5".to_owned()
        );
    }

    #[test]
    fn test_get_commit_body() {
        let commit = "tree d6b3dd4b08f63ba13479484508e0679d32a7891a
author John Doe <john.doe@gmail.com>
committer John Doe <john.doe@gmail.com>

This is the commit title";
        let actual = _get_commit_body(commit);
        let expected: Vec<String> = vec![];
        assert_eq!(actual, expected);

        let commit = "tree d6b3dd4b08f63ba13479484508e0679d32a7891a
author John Doe <john.doe@gmail.com>
committer John Doe <john.doe@gmail.com>

This is the commit title

This is body line 1";
        let actual = _get_commit_body(commit);
        let expected: Vec<String> = vec!["This is body line 1".to_owned()];
        assert_eq!(actual, expected);

        let commit = "tree d6b3dd4b08f63ba13479484508e0679d32a7891a
author John Doe <john.doe@gmail.com>
committer John Doe <john.doe@gmail.com>

This is the commit title

This is body line 1

";
        let actual = _get_commit_body(commit);
        let expected: Vec<String> = vec!["This is body line 1".to_owned()];
        assert_eq!(actual, expected);

        let commit = "tree d6b3dd4b08f63ba13479484508e0679d32a7891a
author John Doe <john.doe@gmail.com>
committer John Doe <john.doe@gmail.com>

This is the commit title

This is body line 1
This is body line 2
This is body line 3
This is body line 4
This is body line 5";
        let actual = _get_commit_body(commit);
        let expected_length = 5;
        assert_eq!(actual.len(), expected_length);
        assert_eq!(actual.last().unwrap(), "This is body line 5");

        let commit = "tree d6b3dd4b08f63ba13479484508e0679d32a7891a
author John Doe <john.doe@gmail.com>
committer John Doe <john.doe@gmail.com>

This is the commit title

This is body line 1
This is body line 2
This is body line 3
This is body line 4
This is body line 5

              \t";
        let actual = _get_commit_body(commit);
        let expected_length = 5;
        assert_eq!(actual.len(), expected_length);
        assert_eq!(actual.last().unwrap(), "This is body line 5");
    }

    #[test]
    fn test_validator_body_required() {
        let commit_bodies = vec![
            vec!["Body line 1".to_owned()],
            vec!["Body line 1".to_owned()],
            vec!["Body line 1".to_owned()],
        ];
        let result = _validate_body_required(&commit_bodies);
        assert!(result.is_ok());

        let commit_bodies = vec![
            vec!["Body line 1".to_owned()],
            vec!["Body line 1".to_owned()],
            vec![],
        ];
        let result = _validate_body_required(&commit_bodies);
        assert_eq!(result.err().unwrap(), ValidationError::BodyRequired);

        let commit_bodies = vec![vec![]];
        let result = _validate_body_required(&commit_bodies);
        assert_eq!(result.err().unwrap(), ValidationError::BodyRequired);
    }

    #[test]
    fn test_validator_body_max_line_length() {
        let commit_bodies = vec![vec![
            "Body line 1".to_owned(),
            "Body line 2".to_owned(),
            "Body line 3".to_owned(),
        ]];
        let result = _validate_body_max_line_length(&commit_bodies, 11);
        assert!(result.is_ok());

        let commit_bodies = vec![vec![
            "Body line 1".to_owned(),
            "Body line 2".to_owned(),
            "Bigger body line 3".to_owned(),
        ]];
        let result = _validate_body_max_line_length(&commit_bodies, 11);
        assert_eq!(
            result.err().unwrap(),
            ValidationError::BodyMaxLineLength(11)
        );

        let commit_bodies = vec![vec![]];
        let result = _validate_body_max_line_length(&commit_bodies, 11);
        assert!(result.is_ok());
    }
}