Skip to content

Commit 8b7a7ee

Browse files
committed
Add action to validate PR & commit messages
This commit adds a GitHub workflow action that validates the following: - The pull request body references an issue or ticket identifier - The commit messages for commits contained within the pull request all reference an issue or ticket identifier - The reference to an issue or ticket is in one of the following formats: - references: PROJ-1234 - references: #123 (for GitHub issues, specifically) - resolves: PROJ-1234 - resolves: #123 (for GitHub issues, specifically) references: HACBS-2653 Signed-off-by: Rob Nester <rnester@redhat.com>
1 parent dcef451 commit 8b7a7ee

2 files changed

Lines changed: 122 additions & 2 deletions

File tree

CONTRIBUTING.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,31 @@ Before contributing code or documentation to this project, make sure you read th
5353

5454
### Commit message standards
5555

56-
The commit message should contain an overall explanation about the change and the motivation behind it. Please note that mentioning a Jira ticket ID or a GitHub issue, isn't a replacement for that.
56+
The commit message should contain an overall explanation about the change and the motivation behind it.
57+
58+
In addition to an explanation about the change, please ensure that each commit contains a line similar to one of the following:
59+
60+
|if your commit|use this keyword|value|
61+
|-|-------|-----|
62+
|resolves an issue/ticket|resolves, res, resolved| #123(only for GitHub Issues), PROJ-1234 (any other issue tracker)|
63+
|only references an issue/ticket|ref, refs, references, refers to| #123(only for GitHub Issues), PROJ-1234 (any other issue tracker)|
64+
65+
*Example git commit message*:
66+
```
67+
Fixed the pesky bug.
68+
69+
This commit fixed that pesky bug that we all hated.
70+
71+
resolves: PROJ-1234
72+
Signed-off-by: John Doe <jdoe@example.com>
73+
74+
# Please enter the commit message for your changes. Lines starting
75+
# with '#' will be ignored, and an empty message aborts the commit.
76+
#
77+
# Date: Fri Jan 10 00:00:00 2025 -0400
78+
#
79+
# On branch PROJ-1234
80+
```
5781

5882
### Signing Commits
5983

@@ -64,7 +88,11 @@ All commits must be signed-off
6488
All changes must come from a pull request (PR) and cannot be directly committed. While anyone can engage in activity on a PR, pull requests are only approved by team members.
6589

6690
Before a pull request can be merged:
67-
91+
* The PR body should contain a line similar to one of the following:
92+
|if your pull request|use this keyword|value|
93+
|-|-------|-----|
94+
|resolves an issue/ticket|resolves, res, resolved| #123(only for GitHub Issues), PROJ-1234 (any other issue tracker)|
95+
|only references an issue/ticket|ref, refs, references, refers to| #123(only for GitHub Issues), PROJ-1234 (any other issue tracker)|
6896
* The content of the PR has to be relevant to the PR itself
6997
* The contribution must follow the style guidelines of this project
7098
* Multiple commits should be used if the PR is complex and clarity can be improved, but they should still relate to a single topic
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
name: Check Commit Message
2+
3+
on:
4+
pull_request:
5+
types: [opened, synchronize, edited, reopened]
6+
7+
jobs:
8+
check-commit-message:
9+
runs-on: ubuntu-latest
10+
env:
11+
REGEX_PATTERN: '^(?i)(ref|refs|reference|references|res|resolve|resolves)[ \t]*:[ \t]*(gh-|\#|\!|[A-Za-z]+-)\d+\s*$'
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v3
15+
16+
- name: Install jq
17+
run: sudo apt-get install jq
18+
19+
- name: Fetch PR data
20+
id: pr_data
21+
run: |
22+
PR_DATA=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
23+
"https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}")
24+
echo "PR_DATA<<EOF" >> $GITHUB_ENV
25+
echo "$PR_DATA" >> $GITHUB_ENV
26+
echo "EOF" >> $GITHUB_ENV
27+
28+
- name: Check PR body
29+
run: |
30+
# Extract the PR body from the PR_DATA environment variable
31+
PR_BODY=$(echo "$PR_DATA" | jq -r '.body')
32+
echo -e "$PR_BODY" > temp_message.txt
33+
echo -e "PR Body:\n$PR_BODY"
34+
if grep -Pq "$REGEX_PATTERN" temp_message.txt; then
35+
echo -e "PR body meets the requirements.\n"
36+
exit 0
37+
else
38+
echo """
39+
PR body does not meet the requirements.
40+
41+
If PR resolves an issue or ticket, amend the PR body to
42+
include a reference to the issue or ticket like so:
43+
44+
resolves: PROJ-1234
45+
or
46+
resolves: #123 (if resolving a GitHub issue)
47+
48+
If PR does not resolve an issue or ticket, but is part of
49+
work done on an issue, amend the PR body to include a
50+
reference to the issue or ticket like so:
51+
52+
reference: PROJ-1234
53+
or
54+
reference: #123 (if referencing a GitHub issue)
55+
56+
"""
57+
exit 1
58+
fi
59+
60+
61+
- name: Check PR commit messages
62+
run: |
63+
COMMIT_DATA=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
64+
"https://api.github.com/repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/commits")
65+
echo "$COMMIT_DATA" | jq -c '.[]' | while read -r commit; do
66+
COMMIT_MESSAGE=$(echo "$commit" | jq -r '.commit.message')
67+
echo -e "$COMMIT_MESSAGE" > temp_commit_message.txt
68+
echo -e "Evaluating commit message:\n$COMMIT_MESSAGE"
69+
if grep -Pq "$REGEX_PATTERN" temp_commit_message.txt; then
70+
echo -e "Commit message meets the requirements.\n"
71+
else
72+
echo -e """
73+
Commit message does not meet the requirements.\
74+
75+
If commit resolves an issue or ticket, amend the commit to
76+
include a reference to the issue or ticket like so:
77+
78+
resolves: PROJ-1234
79+
or
80+
resolves: #123 (if resolving a GitHub issue)
81+
82+
If commit does not resolve an issue or ticket, but was
83+
done while working on an issue, amend the commit to include
84+
a reference to the issue or ticket like so:
85+
86+
reference: PROJ-1234
87+
or
88+
reference: #123 (if referencing a GitHub issue)
89+
"""
90+
exit 1
91+
fi
92+
done

0 commit comments

Comments
 (0)