Skip to content

Commit 14d2fb5

Browse files
Merge pull request #1 from fullstackzach/new-branch
test
2 parents fddfa38 + 1eeee74 commit 14d2fb5

File tree

3 files changed

+54
-46
lines changed

3 files changed

+54
-46
lines changed

.github/scripts/commit-checker.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#!/bin/bash
2+
3+
# Checkout branch
4+
git checkout -q $1
5+
6+
# Set variables
7+
BASE_BRANCH=$2
8+
msg_regex='(AAA|BBB|CCC)\-[0-9]+'
9+
10+
# Initialize invalidCommit as false, will be set to true by any invalid commits
11+
invalidCommit=false
12+
# Find current branch name
13+
CURRENT_BRANCH=$(git branch --show-current)
14+
15+
#echo "Current branch is:" $CURRENT_BRANCH
16+
# Find hash of commit most common ancestor, e.g. where branch began
17+
BRANCH_MERGE_BASE=$(git merge-base ${BASE_BRANCH} ${CURRENT_BRANCH})
18+
#echo "Branch merge base hash is:" $BRANCH_MERGE_BASE
19+
# Find all commits since common ancestor
20+
BRANCH_COMMITS=$(git rev-list ${BRANCH_MERGE_BASE}..HEAD)
21+
22+
#echo $BRANCH_COMMITS
23+
24+
# Check every commit message since ancestor for regex match
25+
for commit in $BRANCH_COMMITS; do
26+
if git log --max-count=1 --format=%B $commit | tr '[a-z]' '[A-Z]' | grep -iqE "$msg_regex"; then
27+
: #If commit matches regex, commit is valid, do nothing
28+
else
29+
# If commit doesn't match regex, commit isn't valid, print commit info
30+
echo "************"
31+
printf "Invalid commit message: \"%s\" and hash: %s\n" "$(git log --max-count=1 --format=%B $commit)" "$commit"
32+
echo "************"
33+
34+
# Set this variable to trigger rejection if any commit fails regex
35+
invalidCommit=true
36+
fi
37+
done
38+
# If any commit are invalid, print reject message
39+
if [ "$invalidCommit" == true ]; then
40+
echo "Your push was rejected because at least one commit message on this branch is invalid"
41+
echo "Please fix the commit message(s) and push again."
42+
echo "https://help.github.com/en/articles/changing-a-commit-message"
43+
echo "************"
44+
exit 1
45+
elif [ "$invalidCommit" == false ]; then
46+
echo "************"
47+
echo "All commits are valid"
48+
echo "************"
49+
exit 0
50+
fi

.github/workflows/commit-checker.yml

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -14,49 +14,5 @@ jobs:
1414
ref: '${{ github.event.pull_request.base.ref }}'
1515

1616
- name: Commit validation
17-
run: |
18-
# Checkout branch
19-
git checkout -q ${{ github.event.pull_request.head.ref }}
20-
21-
# Set variables
22-
BASE_BRANCH=${{ github.event.pull_request.base.ref }}
23-
msg_regex='(AAA|BBB|CCC)\-[0-9]+'
24-
25-
# Initialize invalidCommit as false, will be set to true by any invalid commits
26-
invalidCommit=false
27-
# Find current branch name
28-
CURRENT_BRANCH=$(git branch | grep ^\* | cut -d "*" -f 2 | cut -d " " -f 2)
29-
#echo "Current branch is:" $CURRENT_BRANCH
30-
# Find hash of commit most common ancestor, e.g. where branch began
31-
BRANCH_MERGE_BASE=$(git merge-base ${BASE_BRANCH} ${CURRENT_BRANCH})
32-
#echo "Branch merge base hash is:" $BRANCH_MERGE_BASE
33-
# Find all commits since common ancestor
34-
BRANCH_COMMITS=$(git rev-list ${BRANCH_MERGE_BASE}..HEAD)
35-
#echo $BRANCH_COMMITS
36-
# Check every commit message since ancestor for regex match
37-
for commit in $BRANCH_COMMITS; do
38-
if git log --max-count=1 --format=%B $commit | tr '[a-z]' '[A-Z]' | grep -iqE "$msg_regex"; then
39-
: #If commit matches regex, commit is valid, do nothing
40-
else
41-
# If commit doesn't match regex, commit isn't valid, print commit info
42-
echo "************"
43-
printf "Invalid commit message: \"%s\" and hash: %s\n" "$(git log --max-count=1 --format=%B $commit)" "$commit"
44-
echo "************"
45-
46-
# Set this variable to trigger rejection if any commit fails regex
47-
invalidCommit=true
48-
fi
49-
done
50-
# If any commit are invalid, print reject message
51-
if [ "$invalidCommit" == true ]; then
52-
echo "Your push was rejected because at least one commit message on this branch is invalid"
53-
echo "Please fix the commit message(s) and push again."
54-
echo "https://help.github.com/en/articles/changing-a-commit-message"
55-
echo "************"
56-
exit 1
57-
elif [ "$invalidCommit" == false ]; then
58-
echo "************"
59-
echo "All commits are valid"
60-
echo "************"
61-
exit 0
62-
fi
17+
run: bash ${GITHUB_WORKSPACE}/.github/scripts/commit-checker.sh ${{ github.event.pull_request.head.ref }} ${{ github.event.pull_request.base.ref }}
18+

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
# gh-actions-playground
2+
3+
test

0 commit comments

Comments
 (0)