|
| 1 | +/- |
| 2 | +Copyright (c) 2024 Michael Rothgang. All rights reserved. |
| 3 | +Released under Apache 2.0 license as described in the file LICENSE. |
| 4 | +Authors: Michael Rothgang |
| 5 | +-/ |
| 6 | + |
| 7 | +module |
| 8 | + |
| 9 | +import Mathlib.Init |
| 10 | +import Std.Internal.Parsec.String |
| 11 | + |
| 12 | +/-! |
| 13 | +# Checker for well-formed title and labels |
| 14 | +This script checks if a PR title matches |
| 15 | +[mathlib's commit conventions](https://leanprover-community.github.io/contribute/commit.html). |
| 16 | +Not all checks from the commit conventions are implemented: for instance, no effort is made to |
| 17 | +verify whether the title or body are written in present imperative tense. |
| 18 | +-/ |
| 19 | + |
| 20 | +open Std.Internal.Parsec String |
| 21 | + |
| 22 | +/-- Basic parser for PR titles: given a title `feat(scope): main title` or `feat: title`, |
| 23 | +extracts the `feat` and `scope` components. In the future, this will be extended to also parse |
| 24 | +the main PR title. -/ |
| 25 | +-- TODO: also parse and return the main PR title |
| 26 | +def prTitle : Parser (String × Option String) := |
| 27 | + Prod.mk |
| 28 | + <$> (["feat", "chore", "perf", "refactor", "style", "fix", "doc", "test", "ci"].firstM pstring) |
| 29 | + <*> ( |
| 30 | + (skipString "(" *> some <$> manyChars (notFollowedBy (skipString "):") *> any) |
| 31 | + <* skipString "): ") |
| 32 | + <|> (skipString ": " *> pure none) |
| 33 | + ) |
| 34 | + |
| 35 | +-- Some self-tests for the parser. |
| 36 | +/-- info: Except.ok ("feat", some "x") -/ |
| 37 | +#guard_msgs in |
| 38 | +#eval Parser.run prTitle "feat(x): foo" |
| 39 | +/-- info: Except.ok ("feat", none) -/ |
| 40 | +#guard_msgs in |
| 41 | +#eval Parser.run prTitle "feat: foo" |
| 42 | +/-- info: Except.error "offset 10: expected: ): " -/ |
| 43 | +#guard_msgs in |
| 44 | +#eval Parser.run prTitle "feat(: foo" |
| 45 | +/-- info: Except.error "offset 4: expected: : " -/ |
| 46 | +#guard_msgs in |
| 47 | +#eval Parser.run prTitle "feat): foo" |
| 48 | +/-- info: Except.error "offset 4: expected: : " -/ |
| 49 | +#guard_msgs in |
| 50 | +#eval Parser.run prTitle "feat)(: foo" |
| 51 | +/-- info: Except.error "offset 4: expected: : " -/ |
| 52 | +#guard_msgs in |
| 53 | +#eval Parser.run prTitle "feat)(sdf): foo" |
| 54 | +/-- info: Except.ok ("feat", some "sdf") -/ |
| 55 | +#guard_msgs in |
| 56 | +#eval Parser.run prTitle "feat(sdf): foo:" |
| 57 | +/-- info: Except.error "offset 4: expected: : " -/ |
| 58 | +#guard_msgs in |
| 59 | +#eval Parser.run prTitle "feat foo" |
| 60 | +/-- info: Except.ok ("chore", none) -/ |
| 61 | +#guard_msgs in |
| 62 | +#eval Parser.run prTitle "chore: test" |
| 63 | + |
| 64 | +/-- |
| 65 | +Check if `title` matches the mathlib conventions for PR titles |
| 66 | +(documented at <https://leanprover-community.github.io/contribute/commit.html>). |
| 67 | +
|
| 68 | +Not all checks are implemented: for instance, no effort is made to verify if the title or body |
| 69 | +are written in present imperative tense. |
| 70 | +Return all error messages for violations found. |
| 71 | +-/ |
| 72 | +public def validateTitle (title : String) : Array String := Id.run do |
| 73 | + -- The title should be of the form "abbrev: main title" or "abbrev(scope): main title". |
| 74 | + -- We use the parser above to extract abbrev and scope ignoring the main title, |
| 75 | + -- but give some custom errors in some easily detectable cases. |
| 76 | + if !title.contains ':' then |
| 77 | + return #["error: the PR title does not contain a colon"] |
| 78 | + |
| 79 | + match Parser.run prTitle title with |
| 80 | + | Except.error _ => |
| 81 | + return #[s!"error: the PR title should be of the form\n abbrev: main title\nor\n \ |
| 82 | + abbrev(scope): main title"] |
| 83 | + | Except.ok (kind, _scope?) => |
| 84 | + -- Future: also check scope (and the main PR title) |
| 85 | + let mut errors := #[] |
| 86 | + let knownKinds := ["feat", "chore", "perf", "refactor", "style", "fix", "doc", "test", "ci"] |
| 87 | + let mut isFine := false |
| 88 | + for k in knownKinds do |
| 89 | + isFine := isFine || kind.startsWith k |
| 90 | + if isFine == false then |
| 91 | + errors := errors.push s!"error: the PR title should be of the form \ |
| 92 | + \"kind: main title\" or \"kind(scope): main title\"\n |
| 93 | + Known PR title kinds are {knownKinds}" |
| 94 | + return errors |
0 commit comments