|
| 1 | +##### Imports ##### |
1 | 2 | import re |
2 | 3 | import sys |
3 | 4 | import os |
4 | 5 | import json |
| 6 | +from distutils.util import strtobool |
| 7 | +# import Github |
5 | 8 |
|
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