mit-lint 3.4.0

Lints for commits parsed with mit-commit.
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
use std::sync::LazyLock;

use mit_commit::CommitMessage;
use regex::Regex;

use crate::model::{Code, Problem, ProblemBuilder};

/// Canonical lint ID
pub const CONFIG: &str = "pivotal-tracker-id-missing";

/// Advice on how to correct the problem
pub const HELP_MESSAGE: &str =
    "It's important to add the ID because it allows code to be linked back to the stories it was \
done for, it can provide a chain of custody for code for audit purposes, and it can give \
future explorers of the codebase insight into the wider organisational need behind the \
change. We may also use it for automation purposes, like generating changelogs or \
notification emails.

You can fix this by adding the Id in one of the styles below to the commit message
[Delivers #12345678]
[fixes #12345678]
[finishes #12345678]
[#12345884 #12345678]
[#12345884,#12345678]
[#12345678],[#12345884]
This will address [#12345884]";

/// Description of the problem
pub const ERROR: &str = "Your commit message is missing a Pivotal Tracker ID";

static RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"(?i)\[(((finish|fix)(ed|es)?|complete[ds]?|deliver(s|ed)?) )?#\d+([, ]#\d+)*]")
        .unwrap()
});

/// Configuration for Pivotal Tracker ID linting
#[derive(Debug, Default)]
pub struct PivotalTrackerIdConfig;

/// Checks if the commit message contains a Pivotal Tracker ID
///
/// # Arguments
///
/// * `commit_message` - The commit message to check
///
/// # Returns
///
/// * `Some(Problem)` - If the commit message does not contain a Pivotal Tracker ID
/// * `None` - If the commit message contains a Pivotal Tracker ID
///
/// # Examples
///
/// ```rust
/// use mit_commit::CommitMessage;
/// use mit_lint::Lint::PivotalTrackerIdMissing;
///
/// // This should pass
/// let passing = CommitMessage::from("Subject\n\nBody\n\n[#12345678]");
/// assert!(PivotalTrackerIdMissing.lint(&passing).is_none());
///
/// // This should fail
/// let failing = CommitMessage::from("Subject\n\nBody");
/// assert!(PivotalTrackerIdMissing.lint(&failing).is_some());
/// ```
///
/// # Errors
///
/// This function will never return an error, only an Option<Problem>
pub fn lint(commit_message: &CommitMessage<'_>) -> Option<Problem> {
    lint_with_config(commit_message, &PivotalTrackerIdConfig)
}

fn lint_with_config(
    commit_message: &CommitMessage<'_>,
    _config: &PivotalTrackerIdConfig,
) -> Option<Problem> {
    Some(commit_message)
        .filter(|commit| has_problem(commit, &RE))
        .map(create_problem)
}

fn has_problem(commit_message: &CommitMessage<'_>, pattern: &Regex) -> bool {
    !commit_message.matches_pattern(pattern)
}

fn create_problem(commit_message: &CommitMessage) -> Problem {
    ProblemBuilder::new(
        ERROR,
        HELP_MESSAGE,
        Code::PivotalTrackerIdMissing,
        commit_message,
    )
    .with_label_at_last_line("No Pivotal Tracker ID")
    .with_url("https://www.pivotaltracker.com/help/api?version=v5#Tracker_Updates_in_SCM_Post_Commit_Hooks")
    .build()
}

#[cfg(test)]
mod tests {
    use std::option::Option::None;

    use miette::{GraphicalReportHandler, GraphicalTheme, Report};
    use mit_commit::CommitMessage;
    use quickcheck::TestResult;

    use super::*;
    use crate::model::{Code, Problem};

    #[test]
    fn test_commit_with_pivotal_tracker_id_passes() {
        test_has_missing_pivotal_tracker_id(
            "An example commit

This is an example commit

[#12345678]
# Some comment
",
            None,
        );
    }

    fn test_has_missing_pivotal_tracker_id(message: &str, expected: Option<&Problem>) {
        let actual = lint(&CommitMessage::from(message));
        assert_eq!(
            actual.as_ref(),
            expected,
            "Message {message:?} should have returned {expected:?}, found {actual:?}"
        );
    }

    #[test]
    fn test_commit_with_multiple_pivotal_tracker_ids_passes() {
        test_has_missing_pivotal_tracker_id(
            "An example commit

This is an example commit

[#12345678,#87654321]
# some comment
",
            None,
        );
        test_has_missing_pivotal_tracker_id(
            "An example commit

This is an example commit

[#12345678,#87654321,#11223344]
# some comment
",
            None,
        );
        test_has_missing_pivotal_tracker_id(
            "An example commit

This is an example commit

[#12345678 #87654321 #11223344]
# some comment
",
            None,
        );
    }

    #[test]
    fn test_commit_with_fix_state_change_passes() {
        test_has_missing_pivotal_tracker_id(
            "An example commit

This is an example commit

[fix #12345678]
# some comment
",
            None,
        );
        test_has_missing_pivotal_tracker_id(
            "An example commit

This is an example commit

[FIX #12345678]
# some comment
",
            None,
        );
        test_has_missing_pivotal_tracker_id(
            "An example commit

This is an example commit

[Fix #12345678]
# some comment
",
            None,
        );
        test_has_missing_pivotal_tracker_id(
            "An example commit

This is an example commit

[fixed #12345678]
# some comment
",
            None,
        );
        test_has_missing_pivotal_tracker_id(
            "An example commit

This is an example commit

[fixes #12345678]
# some comment
",
            None,
        );
    }

    #[test]
    fn test_commit_with_complete_state_change_passes() {
        test_has_missing_pivotal_tracker_id(
            "An example commit

This is an example commit

[complete #12345678]
# some comment
",
            None,
        );

        test_has_missing_pivotal_tracker_id(
            "An example commit

This is an example commit

[completed #12345678]
# some comment
",
            None,
        );

        test_has_missing_pivotal_tracker_id(
            "An example commit

This is an example commit

[Completed #12345678]
# some comment
",
            None,
        );

        test_has_missing_pivotal_tracker_id(
            "An example commit

This is an example commit

[completes #12345678]
# some comment
",
            None,
        );
    }

    #[test]
    fn test_commit_with_finish_state_change_passes() {
        test_has_missing_pivotal_tracker_id(
            "An example commit

This is an example commit

[finish #12345678]
# some comment
",
            None,
        );

        test_has_missing_pivotal_tracker_id(
            "An example commit

This is an example commit

[finished #12345678]
# some comment
",
            None,
        );
        test_has_missing_pivotal_tracker_id(
            "An example commit

This is an example commit

[finishes #12345678]
# some comment
",
            None,
        );
    }

    #[test]
    fn test_commit_with_deliver_state_change_passes() {
        test_has_missing_pivotal_tracker_id(
            "An example commit

This is an example commit

[deliver #12345678]
# some comment
",
            None,
        );

        test_has_missing_pivotal_tracker_id(
            "An example commit

This is an example commit

[delivered #12345678]
# some comment
",
            None,
        );
        test_has_missing_pivotal_tracker_id(
            "An example commit

This is an example commit

[delivers #12345678]
# some comment
",
            None,
        );
    }

    #[test]
    fn test_commit_with_state_change_and_multiple_ids_passes() {
        test_has_missing_pivotal_tracker_id(
            "An example commit

This is an example commit

[fix #12345678 #12345678]
# some comment
",
            None,
        );
    }

    #[test]
    fn test_commit_with_prefixed_text_passes() {
        test_has_missing_pivotal_tracker_id(
            "An example commit

This is an example commit

Finally [fix #12345678 #12345678]
",
            None,
        );
    }

    #[test]
    fn test_commit_with_invalid_state_change_fails() {
        let message = "An example commit

This is an example commit

[fake #12345678]
# some comment
";
        test_has_missing_pivotal_tracker_id(
            message,
            Some(Problem::new(
                ERROR.into(),
                HELP_MESSAGE.into(),
                Code::PivotalTrackerIdMissing,
                &message.into(),
                Some(vec![("No Pivotal Tracker ID".to_string(), 63, 14)]),
                Some("https://www.pivotaltracker.com/help/api?version=v5#Tracker_Updates_in_SCM_Post_Commit_Hooks".parse().unwrap()),
            )).as_ref(),
        );
    }

    #[test]
    fn test_commit_without_pivotal_tracker_id_fails() {
        let message_1 = "An example commit

This is an example commit
";
        test_has_missing_pivotal_tracker_id(
            message_1,
            Some(&Problem::new(
                ERROR.into(),
                HELP_MESSAGE.into(),
                Code::PivotalTrackerIdMissing,
                &message_1.into(),
                Some(vec![("No Pivotal Tracker ID".to_string(), 19, 25)]),
                Some("https://www.pivotaltracker.com/help/api?version=v5#Tracker_Updates_in_SCM_Post_Commit_Hooks".parse().unwrap()),
            )),
        );

        let message_2 = "An example commit

This is an example commit

[#]
# some comment
";
        test_has_missing_pivotal_tracker_id(
            message_2,
            Some(Problem::new(
                ERROR.into(),
                HELP_MESSAGE.into(),
                Code::PivotalTrackerIdMissing,
                &message_2.into(),
                Some(vec![("No Pivotal Tracker ID".to_string(), 50, 14)]),
                Some("https://www.pivotaltracker.com/help/api?version=v5#Tracker_Updates_in_SCM_Post_Commit_Hooks".parse().unwrap()),
            )).as_ref(),
        );
    }

    #[test]
    fn test_error_report_formatting() {
        let message = "An example commit

This is an example commit
";
        let problem = lint(&CommitMessage::from(message.to_string()));
        let actual = fmt_report(&Report::new(problem.unwrap()));
        let expected = "PivotalTrackerIdMissing (https://www.pivotaltracker.com/help/api?version=v5#Tracker_Updates_in_SCM_Post_Commit_Hooks)

  x Your commit message is missing a Pivotal Tracker ID
   ,-[3:1]
 2 | 
 3 | This is an example commit
   : ^^^^^^^^^^^^|^^^^^^^^^^^^
   :             `-- No Pivotal Tracker ID
   `----
  help: It's important to add the ID because it allows code to be linked back
        to the stories it was done for, it can provide a chain of custody for
        code for audit purposes, and it can give future explorers of the
        codebase insight into the wider organisational need behind the change.
        We may also use it for automation purposes, like generating changelogs
        or notification emails.
        
        You can fix this by adding the Id in one of the styles below to the
        commit message
        [Delivers #12345678]
        [fixes #12345678]
        [finishes #12345678]
        [#12345884 #12345678]
        [#12345884,#12345678]
        [#12345678],[#12345884]
        This will address [#12345884]
"        .to_string();
        assert_eq!(
            actual, expected,
            "Message {message:?} should have returned {expected:?}, found {actual:?}"
        );
    }

    fn fmt_report(diag: &Report) -> String {
        let mut out = String::new();
        GraphicalReportHandler::new_themed(GraphicalTheme::none())
            .with_width(80)
            .with_links(false)
            .render_report(&mut out, diag.as_ref())
            .unwrap();
        out
    }

    #[allow(clippy::needless_pass_by_value)]
    #[quickcheck]
    fn test_quickcheck_commits_without_pivotal_tracker_id_fail(commit: String) -> TestResult {
        let message = CommitMessage::from(commit);
        let result = lint(&message);
        TestResult::from_bool(result.is_some())
    }

    #[allow(clippy::needless_pass_by_value)]
    #[quickcheck]
    fn test_quickcheck_commits_with_pivotal_tracker_id_pass(
        commit: String,
        commit_suffix: String,
        ids: Vec<usize>,
        prefix: Option<usize>,
    ) -> TestResult {
        if commit.starts_with('#') {
            return TestResult::discard();
        }
        if commit.ends_with("\n#") {
            return TestResult::discard();
        }
        if ids.is_empty() {
            return TestResult::discard();
        }

        let prefixes = [
            "finished ",
            "finishes ",
            "finish ",
            "fix ",
            "fixed ",
            "fixes ",
            "complete ",
            "completed ",
            "completes ",
            "delivered ",
            "delivers ",
        ];
        let prefix = prefix
            .and_then(|x| prefixes.get(x % prefixes.len()))
            .map(|x| (*x).to_string())
            .unwrap_or_default();
        let id_str: String = ids
            .iter()
            .map(|x| format!("#{x}"))
            .collect::<Vec<_>>()
            .join(",");

        let message = CommitMessage::from(format!(
            "{commit}\n[{prefix}{id_str}]\n{commit_suffix}\n# comment"
        ));
        let result = lint(&message);
        TestResult::from_bool(result.is_none())
    }
}