Skip to content

Commit 43348dd

Browse files
committed
Check for JIRA ID or "no jira" marker in commit message.
1 parent cf559f7 commit 43348dd

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ description: Check changed files for compliance
44
runs:
55
using: "composite"
66
steps:
7-
- run: python3 $GITHUB_ACTION_PATH/main.py
7+
- run: python3 $GITHUB_ACTION_PATH/main.py ${{ github.event.head_commit.message }}
88
shell: bash
99
branding:
1010
icon: 'check-square'

main.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414

1515
if __name__ == '__main__':
1616

17+
message = sys.argv[1]
18+
print(f'Commit message: {message}')
19+
1720
print(f'Checking commit {githooks.get_sha()} by {githooks.get_user()} in {githooks.get_branch()}')
1821

22+
1923
files = githooks.get_commit_files()
2024
print(f'Checking {githooks.get_event()} modified files:')
2125
print(' ' + '\n '.join(files['M']))
@@ -24,6 +28,8 @@
2428

2529
retval = 0
2630

31+
retval += githooks.check_commit_msg(message, files['M'] + files['A'])
32+
2733
if githooks._is_pull_request():
2834
retval += githooks.check_do_not_merge(files['M'])
2935
retval += githooks.check_do_not_merge(files['A'], new_files=True)

main/githooks.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
SOFT_SIZE_THRESHOLD = 5.0
2222
# Large file marker in commit message
2323
LARGE_FILE_MARKER = 'LARGE_FILE'
24+
# No jira marker in commit message
25+
NO_JIRA_MARKER = 'NO_JIRA'
2426
# Check file content if it has these extensions
2527
CHECKED_EXTS = [
2628
'.bat',
@@ -762,6 +764,12 @@ def check_commit_msg(message, files):
762764
does not contain required marker.
763765
764766
'''
767+
if NO_JIRA_MARKER not in message:
768+
if jira_id_pattern.search(message) is None:
769+
_fail('Every commit should contain a Jira issue ID or the text '
770+
f'{NO_JIRA_MARKER}')
771+
return 1
772+
765773
for filename in files:
766774
size = Path(filename).stat().st_size / 1024**2
767775
if size > HARD_SIZE_THRESHOLD:
@@ -773,6 +781,24 @@ def check_commit_msg(message, files):
773781
return 1
774782

775783
return 0
784+
jira_id_pattern = re.compile(r'\b[A-Z]{2,5}-[0-9]{1,4}\b')
785+
786+
787+
class TestJiraIDPattern(unittest.TestCase):
788+
def test_various_strings(self):
789+
def _test(input, is_jira=True):
790+
m = jira_id_pattern.search(input)
791+
self.assertEqual(bool(m), is_jira)
792+
_test('BLD-5704')
793+
_test("BLD-1234 fixed some builds")
794+
_test("fixed some builds BLD-1234 ")
795+
_test("fixed some builds (Jira BLD-1234)")
796+
_test("fixed some builds\n some more text BLD-1234")
797+
_test('lower-1234', False)
798+
_test('A-1234', False)
799+
_test('ABCDEF-1234', False)
800+
_test('BLD-12345', False)
801+
_test('wordBLD-1234word', False)
776802

777803

778804
def commit_hook(merge=False):

0 commit comments

Comments
 (0)