Skip to content

Commit 5ffae12

Browse files
committed
todo-tags: rename
1 parent 91a6827 commit 5ffae12

3 files changed

Lines changed: 43 additions & 13 deletions

File tree

.pre-commit-hooks.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
- id: todo-tagged-jira
2-
name: 'TODOs have JIRA tags'
3-
entry: todo-jira-check.py
1+
- id: todo-tags
2+
name: 'TODOs have tags'
3+
entry: todo-tags.py
44
types: [text]
55
language: 'script'
66
pass_filenames: true

README.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
This is a set of [`pre-commit`] hooks for development across multiple languages.
44

55
Hooks:
6-
* `todo-tagged-jira`: ensure all TODO comments reference a JIRA ticket
6+
* `todo-tags`: ensure all TODO comments reference a JIRA ticket (or any regex matcher)
77
* `todo-branch-tags`: show all TODOs tagged with the ticket reference in the branch name
88
* `branch-name-check`: checks branch names adhere to the regex `^(feature|bugfix|release|hotfix)\/.+`
99
* `go-test`: runs `go test ./...` at the repo root
@@ -24,9 +24,10 @@ repos:
2424
- repo: https://github.com/domodwyer/pre-commit
2525
rev: master
2626
hooks:
27-
- id: todo-tagged-jira # Requires python
27+
- id: todo-tags # Requires python
2828
stages: [commit, push, manual]
2929
args: ["--tag=DEV"] # JIRA ticket tag (defaults to DEV)
30+
# args: ["--regex='.*'"] # Or specify your own regex
3031

3132
- id: todo-branch-tags
3233
stages: [post-checkout, manual] # Show tags when checking out
@@ -79,10 +80,19 @@ repos:
7980
New TODOs introduced into the codebase should be tracked in JIRA so they're
8081
eventually done!
8182
82-
The `todo-tagged-jira` hook searches changed files for `TODO` comments (lines
83-
prefixed by `#` or `//`) that do not reference a JIRA ticket. A valid TODO is in
84-
the form: `TODO(DEV-4242)`, where the `DEV` tag is configurable, and `4242` is
85-
the ticket number.
83+
The `todo-tags` hook searches changed files for `TODO` comments (lines prefixed
84+
by `#` or `//`) that do not reference a JIRA ticket. A valid TODO is in the
85+
form: `TODO(DEV-4242)`, where the `DEV` tag is configurable, and `4242` is the
86+
ticket number.
87+
88+
Instead for more flexibility, you can specify a custom regex to validate TODO
89+
tags with instead:
90+
91+
```yaml
92+
- id: todo-tags
93+
stages: [commit, push, manual]
94+
args: ["--regex='(dom|clive)'"]
95+
```
8696

8797
The `todo-branch-tags` is useful as a [`post-checkout`] hook to print all TODOs
8898
tagged with the ticket reference in the branch name. If you checkout a branch
Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,38 @@
1313
import re
1414
import sys
1515

16+
DEFAULT_TAG = "DEV"
17+
1618
parser = argparse.ArgumentParser()
17-
parser.add_argument('-t', '--tag', type=str,
18-
help="The JIRA tag (i.e. DEV) to search for", default="DEV", dest='tag')
19+
parser.add_argument('-t', '--tag',
20+
type=str,
21+
help="A JIRA-like tag (i.e. DEV) to search for",
22+
default=DEFAULT_TAG,
23+
dest='tag')
24+
25+
parser.add_argument('-r', '--regex',
26+
type=str,
27+
help="Specify the regex to match inner in TODO(inner)",
28+
dest='regex')
29+
1930
parser.add_argument('files', metavar='FILES', type=str, nargs='+',
2031
help='Files to search')
2132

2233
args = parser.parse_args()
2334

35+
# Do not allow specifying a tag and --regex
36+
if args.regex and args.tag != DEFAULT_TAG:
37+
sys.exit("cannot provide tag with --regex")
38+
39+
# Figure out what regex to use
40+
tag = args.tag + "-[0-9]+"
41+
if args.regex:
42+
tag = args.regex
43+
2444
# This regex matches all TODO comments (prefixed by // or #) that are not
25-
# immediately followed by "($TAG-0000)"
45+
# immediately followed by "($TAG)"
2646
pattern = re.compile(
27-
r"(\/\/|#)+\s?TODO((?!\("+args.tag+r"-[0-9]+\)).)*$", re.IGNORECASE)
47+
r"(\/\/|#)+\s?TODO((?!\("+tag+r"\)).)*$", re.IGNORECASE)
2848

2949
ret = 0
3050
for f in args.files:

0 commit comments

Comments
 (0)