Skip to content

Commit f4cc68b

Browse files
committed
Initial commit
1 parent ced2662 commit f4cc68b

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

action.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: 'github-status-checks-commit-message-regex'
2+
description: 'Check if commit message matches a regex pattern'
3+
author: 'Michael N. Jensen <michael.jensen@dustin.dk>'
4+
inputs:
5+
pattern:
6+
description: 'A regex pattern to check if a commit message is valid'
7+
required: true
8+
runs:
9+
using: "composite"
10+
steps:
11+
- run: python3 ${{ github.action_path }}/main.py "${{ inputs.pattern }}"
12+
shell: bash

main.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import re
2+
import sys
3+
import os
4+
import json
5+
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!')

0 commit comments

Comments
 (0)