Skip to content

Commit d948fc2

Browse files
committed
Add extra options for what to check, and reorganize the code
1 parent 0cf0aeb commit d948fc2

File tree

3 files changed

+167
-46
lines changed

3 files changed

+167
-46
lines changed

README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,35 @@ A simple python scripts, that checks if the commit message in the pull request m
44

55
Just requires a plain Github workflow to run, on ubuntu-latest (or anything that has python3)
66

7-
## Example status check Github Action workflow
7+
There is a few options you can specify:
8+
| Input | Description | Required? | Default |
9+
|-----------------------|---------------------------------------------------------------|-----------|---------|
10+
| pattern | The regex pattern to use, to check for a valid commit message | Yes | N/A |
11+
| check_commit_title | Check for the pattern in the commit title (first line) | No | false |
12+
| check_commit_body | Check for thee pattern in the rest of the commit message | No | false |
13+
| check_commit_message | Check for the pattern in the full commit message (title+body) | No | true |
14+
| check_all_commits | Check all individual commit message in the pull request | No | false |
15+
| github_token | The GITHUB_TOKEN to use for authenticating to github | No | N/A |
16+
17+
Using check_all_commits, requires you to pass the {{ secrets.GITHUB_TOKEN }}, so it has access to all the commits in the pull request
18+
19+
## Example status check Github Action workflow simple
20+
```yml
21+
name: Check commit message
22+
on: [pull_request]
23+
24+
jobs:
25+
check-commit-message:
26+
name: Check commit message
27+
runs-on: ubuntu-latest
28+
steps:
29+
- name: Check commit message
30+
uses: mnj/github-status-checks-commit-message-regex@v1
31+
with:
32+
pattern: '([A-Z][A-Z0-9]+-[0-9]+)' # Match all upper casee Jira ticket ids
33+
```
34+
35+
## Example status check Github Action workflow, full example
836
```yml
937
name: Check commit message
1038
on: [pull_request]
@@ -18,6 +46,12 @@ jobs:
1846
uses: mnj/github-status-checks-commit-message-regex@v1
1947
with:
2048
pattern: '([A-Z][A-Z0-9]+-[0-9]+)' # Match all upper casee Jira ticket ids
49+
check_commit_title: 'false'
50+
check_commit_body: 'false'
51+
check_commit_message: 'true'
52+
check_all_commits: 'true'
53+
54+
github_token: ${{ secrets.GITHUB_TOKEN }}
2155
```
2256
2357
## License

action.yml

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
11
name: 'github-status-checks-commit-message-regex'
22
description: 'Check if commit message matches a regex pattern'
33
author: 'Michael N. Jensen <[email protected]>'
4+
45
inputs:
56
pattern:
67
description: 'A regex pattern to check if a commit message is valid'
78
required: true
9+
check_commit_title:
10+
description: 'Check the commit title (first line)'
11+
required: false
12+
default: 'true'
13+
check_commit_body:
14+
description: 'Check the rest of the commit message, except title'
15+
default: 'true'
16+
check_commit_message:
17+
description: 'Check if its in any part of the commit message'
18+
default: 'false'
19+
check_all_commits:
20+
required: false
21+
description: 'Check all indivual commit messages in the pull request'
22+
default: 'false'
23+
github_token:
24+
required: false
25+
description: 'The GITHUB_TOKEN required if checking all the commits in the pull request'
26+
827
runs:
928
using: "composite"
1029
steps:
11-
- run: python3 ${{ github.action_path }}/main.py "${{ inputs.pattern }}"
30+
- run: python3 -m pip install PyGithub
31+
shell: bash
32+
- run: python3 ${{ github.action_path }}/main.py "${{ inputs.pattern }}" "${{ inputs.check_commit_title }}" "${{ inputs.check_commit_body }}" "${{ inputs.check_commit_message }}" "${{ inputs.check_all_commits }}" "${{ inputs.github_token }}"
1233
shell: bash

main.py

Lines changed: 110 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,115 @@
1+
##### Imports #####
12
import re
23
import sys
34
import os
45
import json
6+
from distutils.util import strtobool
7+
# import Github
58

6-
re_match_title = None
7-
re_match_body = None
8-
9-
try:
10-
# Try to open the event file (json) that is specified with the environment
11-
# varible: GITHUB_EVENT_PATH, this will contain the pull request details
12-
# but it will not list each individual commit message, for that we need to
13-
# use the python github client, with required the GITHUB_TOKEN to work.
14-
15-
# Check if the GITHUB_EVENT_PATH environment variable has been set
16-
if "GITHUB_EVENT_PATH" in os.environ:
17-
18-
# Open the event json file, and parse it with json
19-
with open(os.environ['GITHUB_EVENT_PATH'], 'r') as event_file:
20-
event_data = json.load(event_file)
21-
22-
# Check that we are actually running on a pull request status check
23-
if "pull_request" in event_data:
24-
25-
# Check if there is a title element (first line of the git commit message)
26-
if "title" in event_data['pull_request']:
27-
28-
# Check that it's not set to null/empty
29-
if event_data['pull_request']['title'] is not None:
30-
31-
# Get the match result, for the regex search, based on the pattern in argv[1]
32-
re_match_title = re.search(sys.argv[1], event_data['pull_request']['title'])
33-
34-
# Check if there is a body element (the rest of the commit message, excl the first line)
35-
if "body" in event_data['pull_request']:
36-
37-
# Check that it's not set to null/empty
38-
if event_data['pull_request']['body'] is not None:
39-
40-
# Get the match result, for the regex search, based on the pattern in argv[1]
41-
re_match_body = re.search(sys.argv[1], event_data['pull_request']['body'])
42-
except:
43-
raise SystemExit('Unable to parse the pull request!')
44-
45-
# Return success if there was a regex match in either the title or body or both
46-
if (re_match_title is not None) or (re_match_body is not None):
47-
sys.exit(0)
48-
else:
49-
raise SystemExit('You did not specify a Jira ticket number in this pull request!')
9+
# Script args:
10+
# sys.argv[1] - Pattern (string)
11+
# sys.argv[2] - Check commit title (bool)
12+
# sys.argv[3] - Check commit body (bool)
13+
# sys.argv[5] - Check commit message (bool)
14+
# sys.argv[6] - Check all commits (bool)
15+
# sys.argv[7] - Github token (string)
16+
17+
# Check if the argument exists, (and convert them to Python booleans),
18+
# if they are not present, then set them to some sane defaults
19+
pattern = sys.argv[1] if (len(sys.argv) >= 2) else None
20+
check_commit_title = bool(strtobool(sys.argv[2])) if len(sys.argv) >= 3 else False
21+
check_commit_body = bool(strtobool(sys.argv[3])) if len(sys.argv) >= 4 else False
22+
check_commit_message = bool(strtobool(sys.argv[4])) if len(sys.argv) >= 5 else True
23+
check_all_commits = bool(strtobool(sys.argv[5])) if len(sys.argv) >= 6 else False
24+
github_token = sys.argv[6] if len(sys.argv) >= 7 else None
25+
26+
##### Functions #####
27+
def check_valid_title(pull_request_json):
28+
# Check if there is a title element (first line of the git commit message)
29+
if "title" in pull_request_json:
30+
# Check that it's not set to null/empty
31+
if pull_request_json['title'] is not None:
32+
regex_match = re.search(pattern, pull_request_json['title'])
33+
return regex_match
34+
else:
35+
return False # Didn't contain anything/exist, so cant be valid
36+
else:
37+
raise SystemExit('Did not find the expected title elemment in the pull request event data!')
38+
39+
def check_valid_body(pull_request_json):
40+
# Check if there is a body element (the rest of the commit message, excl the first line)
41+
if "body" in pull_request_json:
42+
# Check that it's not set to null/empty
43+
if pull_request_json['body'] is not None:
44+
regex_match = re.search(pattern, pull_request_json['body'])
45+
return regex_match
46+
else:
47+
return False # Didn't contain anything/exist, so cant be valid
48+
else:
49+
raise SystemExit('Did not find the expected body elemment in the pull request event data!')
50+
51+
def check_valid_message(pull_request_json):
52+
full_message = ""
53+
54+
if "title" in pull_request_json:
55+
if pull_request_json['title'] is not None:
56+
full_message += pull_request_json['title']
57+
if "body" in pull_request_json:
58+
if pull_request_json['body'] is not None:
59+
full_message += "\n" + pull_request_json['body']
60+
61+
if full_message == "":
62+
raise SystemExit('Both the title and body of the commit message can not be empty!')
63+
else:
64+
return re.search(pattern, full_message)
65+
66+
def get_pull_request_json():
67+
try:
68+
# Try to open the event file (json) that is specified with the environment
69+
# varible: GITHUB_EVENT_PATH, this will contain the pull request details
70+
# but it will not list each individual commit message, for that we need to
71+
# use the python github client, with required the GITHUB_TOKEN to work.
72+
73+
# Check if the GITHUB_EVENT_PATH environment variable has been set
74+
if "GITHUB_EVENT_PATH" in os.environ:
75+
76+
# Open the event json file, and parse it with json
77+
with open(os.environ['GITHUB_EVENT_PATH'], 'r') as event_file:
78+
event_data = json.load(event_file)
79+
80+
# Check that we are actually running on a pull request status check
81+
if "pull_request" in event_data:
82+
return event_data['pull_request']
83+
else:
84+
raise SystemExit('There was no pull request data in the triggered event!')
85+
else:
86+
raise SystemExit('Missing the GITHUB_EVENT_PATH environment variable!')
87+
except:
88+
raise SystemExit('Unable to parse the pull request!')
89+
90+
##### Main #####
91+
if __name__ == '__main__':
92+
valid_pull_request = True # Assume true, unless checks fail
93+
94+
pull_request_json = get_pull_request_json()
95+
96+
if check_all_commits:
97+
pass
98+
else:
99+
# We can rely on just the pull request json without having to use PyGithub
100+
if check_commit_title:
101+
if not check_valid_title(pull_request_json):
102+
valid_pull_request = False
103+
104+
if check_commit_body:
105+
if not check_valid_body(pull_request_json):
106+
valid_pull_request = False
107+
108+
if check_commit_message:
109+
if not check_valid_message(pull_request_json):
110+
valid_pull_request = False
111+
112+
if valid_pull_request:
113+
sys.exit(0)
114+
else:
115+
raise SystemExit('You did not specify a Jira ticket number in this pull request!')

0 commit comments

Comments
 (0)