Skip to content

Commit 962cf5e

Browse files
Merge pull request #9 from fullstackzach/commit-checker
bad commit
2 parents dc05ce7 + 5bb174b commit 962cf5e

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/bin/bash
2+
3+
# Reqires atleast one jira ticket in the format AUS-1234 on either a branch or commit message
4+
5+
# Checkout branch
6+
git checkout -q $1
7+
8+
# Set variables
9+
BASE_BRANCH=$2
10+
msg_regex='(AAA|BBB|CCC)\-[0-9]+'
11+
skip_regex='\[SKIP JIRA\]'
12+
13+
# Initialize invalidCommit as false, will be set to true by any invalid commits
14+
# Find current branch name
15+
CURRENT_BRANCH=$(git branch --show-current)
16+
17+
#echo "Current branch is:" $CURRENT_BRANCH
18+
# Find hash of commit most common ancestor, e.g. where branch began
19+
BRANCH_MERGE_BASE=$(git merge-base ${BASE_BRANCH} ${CURRENT_BRANCH})
20+
# echo "Branch merge base hash is:" $BRANCH_MERGE_BASE
21+
# Find all commits since common ancestor
22+
BRANCH_COMMITS=$(git rev-list ${BRANCH_MERGE_BASE}..HEAD)
23+
24+
# Check every commit message since ancestor for regex matchs
25+
for commit in $BRANCH_COMMITS; do
26+
COMMIT_MSG_UPPER=$(git log --max-count=1 --format=%B $commit | tr '[a-z]' '[A-Z]')
27+
28+
if echo $COMMIT_MSG_UPPER | grep -iqE "$skip_regex"; then
29+
echo "************"
30+
echo "[skip jira] detected, skipping commit linting"
31+
echo "************"
32+
exit 0
33+
fi
34+
35+
if echo $COMMIT_MSG_UPPER | grep -iqE "$msg_regex"; then
36+
echo "************"
37+
echo "Jira ticket # found in a commit mesage 👍🏻"
38+
echo "************"
39+
exit 0
40+
fi
41+
done
42+
43+
CURRENT_BRANCH_UPPER=$(echo $CURRENT_BRANCH | tr '[a-z]' '[A-Z]')
44+
45+
if echo $CURRENT_BRANCH_UPPER | grep -iqE "$skip_regex"; then
46+
echo "************"
47+
echo "[skip jira] detected, skipping commit linting"
48+
echo "************"
49+
exit 0
50+
fi
51+
52+
if echo $CURRENT_BRANCH_UPPER | grep -iqE "$msg_regex"; then
53+
echo "************"
54+
echo "Jira ticket # found in a branch name 👍🏻"
55+
echo "************"
56+
exit 0
57+
fi
58+
59+
# If we made it thsis far, no JIRA ticket # was detected in a commit or branch, print the reject message and fail the job
60+
61+
echo "⛔️ Atleast one commit messages OR your branch name must include a JIRA ticket number e.g. \"AAA-1234\". This can be anywhere in your commit"
62+
echo "You can skip this whole step if necessary by running \"git commit --amend\" and add \"[skip jira]\" in your last commit message, and force-push"
63+
echo "Please fix the commit message and push again."
64+
echo "https://help.github.com/en/articles/changing-a-commit-message"
65+
echo "************"
66+
exit 1

.github/scripts/commit-linting.sh renamed to .github/scripts/commit-linting-strict.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#!/bin/bash
22

3+
# Reqires every commit includes a jira ticket in the format AUS-1234
4+
35
# Checkout branch
46
git checkout -q $1
57

68
# Set variables
79
BASE_BRANCH=$2
8-
msg_regex='(AAA|BBB|CCC)\-[0-9]+'
10+
msg_regex='(AUS)\-[0-9]+'
911
skip_regex='\[SKIP JIRA\]'
1012

1113
# Initialize invalidCommit as false, will be set to true by any invalid commits

.github/workflows/commit-linting.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Commit messages contain a Jira issue
1+
name: Includes Jira issue
22

33
# Run on push to any branch
44
on: [pull_request]
@@ -13,5 +13,5 @@ jobs:
1313
fetch-depth: 0
1414
ref: '${{ github.event.pull_request.base.ref }}'
1515

16-
- name: Commit messages contain a Jira issue
17-
run: bash ${GITHUB_WORKSPACE}/.github/scripts/commit-checker.sh ${{ github.event.pull_request.head.ref }} ${{ github.event.pull_request.base.ref }}
16+
- name: Branch or Commits includes a Jira issue
17+
run: bash ${GITHUB_WORKSPACE}/.github/scripts/commit-linting-loose.sh ${{ github.event.pull_request.head.ref }} ${{ github.event.pull_request.base.ref }}

0 commit comments

Comments
 (0)