Skip to content

Commit 35edaab

Browse files
fix(scripts): addition of new commit linter and restrictions on commit messages via workflows (#2840)
Co-authored-by: pranav-new-relic <[email protected]>
1 parent b7b06f1 commit 35edaab

File tree

8 files changed

+184
-28
lines changed

8 files changed

+184
-28
lines changed

.githooks/commit-msg

+8-24
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,10 @@
11
#!/bin/sh
22

3-
RED="\033[0;31m"
4-
END_COLOR="\033[0m"
5-
6-
commit_types="(chore|docs|feat|fix|refactor|tests?)"
7-
conventional_commit_regex="^${commit_types}(\([a-z \-]+\))?!?: .+$"
8-
9-
commit_message=$(cat "$1")
10-
11-
if [[ "$commit_message" =~ $conventional_commit_regex ]]; then
12-
echo "Commit message meets Conventional Commit standards..."
13-
exit 0
14-
fi
15-
echo
16-
echo "${RED}Commit lint failed. Please update your commit message format. ${END_COLOR}"
17-
echo "Example commit messages:"
18-
echo " feat(scope): add your feature description here"
19-
echo " fix(scope): add your fix description here"
20-
echo " chore(scope): add your chore description here"
21-
echo " docs(scope): add your docs description here"
22-
echo " refactor(scope): add your refactor description here"
23-
echo " tests(scope): add your tests description here"
24-
echo
25-
26-
exit 1
3+
echo
4+
echo "Checking commit message format..."
5+
echo
6+
if ! make lint-commit; then
7+
echo "See: https://www.conventionalcommits.org"
8+
echo "\033[31m============================================================================="
9+
exit 1
10+
fi
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
name: PR Commits Conventional Check
2+
3+
on:
4+
pull_request:
5+
types: [opened, edited, synchronize, reopened]
6+
7+
jobs:
8+
# THIS IS SUPPOSED TO GO BEFORE validate-commits
9+
# commenting this for now as this needs rework
10+
11+
# check-merge-commits:
12+
# runs-on: ubuntu-latest
13+
# steps:
14+
# - uses: actions/checkout@v3
15+
# with:
16+
# fetch-depth: 0
17+
# token: ${{ secrets.GITHUB_TOKEN }}
18+
#
19+
# - name: Check and Fix Merge Commits
20+
# id: check_merge_commits
21+
# run: |
22+
# # Get the PR branch and base branch
23+
# PR_BRANCH="${{ github.event.pull_request.head.ref }}"
24+
# BASE_BRANCH="${{ github.event.pull_request.base.ref }}"
25+
#
26+
# # Get all merge commits in the PR
27+
#
28+
# git config --global user.name ${{ secrets.NEW_RELIC_GITHUB_SERVICE_ACCOUNT_USERNAME }}
29+
# git config --global user.email ${{ secrets.NEW_RELIC_GITHUB_SERVICE_ACCOUNT_EMAIL }}
30+
#
31+
# git fetch origin $PR_BRANCH
32+
# git fetch origin $BASE_BRANCH
33+
#
34+
# MERGE_COMMITS=$(git log --merges "origin/$BASE_BRANCH..origin/$PR_BRANCH" --format="%H")
35+
#
36+
# if [ ! -z "$MERGE_COMMITS" ]; then
37+
# echo "Found merge commits in PR, converting to conventional commits..."
38+
#
39+
# for commit in $MERGE_COMMITS; do
40+
# # Get the merge commit message
41+
# echo "Processing commit: $commit"
42+
# MERGE_MSG=$(git log -1 --format="%B" $commit)
43+
#
44+
# # Extract meaningful information from merge commit
45+
# TITLE=$(echo "$MERGE_MSG" | head -n 1)
46+
# echo "$commit current title: $TITLE"
47+
#
48+
# # Create conventional commit format
49+
# # Default to 'chore' type if cannot determine
50+
# NEW_MSG="chore: $TITLE"
51+
# echo "$commit new title: $NEW_MSG"
52+
#
53+
# # Amend the commit with new message
54+
# echo "Amending commit: $commit"
55+
# git checkout $commit
56+
# echo "$commit: checked out"
57+
# git commit --amend -m "$NEW_MSG" --no-edit
58+
# echo "$commit: amended"
59+
# done
60+
#
61+
# # Push the changes back to the PR branch
62+
# # very risky, but just want to give this a try
63+
# git push origin HEAD:$PR_BRANCH -f
64+
# echo "$commit: force pushed with the new title '$NEW_MSG'"
65+
# fi
66+
67+
validate-commits:
68+
# needs: check-merge-commits
69+
runs-on: ubuntu-latest
70+
steps:
71+
- name: Checkout code
72+
uses: actions/checkout@v3
73+
with:
74+
fetch-depth: 0
75+
76+
- name: Get commits
77+
id: get-commits
78+
run: |
79+
# Get the base and head SHAs
80+
base_sha=$(git merge-base ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }})
81+
head_sha=${{ github.event.pull_request.head.sha }}
82+
83+
# Store invalid commits in a variable
84+
invalid_commits=""
85+
86+
# Check each commit
87+
while read -r commit; do
88+
commit_msg=$(git log -1 --format=%B "$commit")
89+
# Conventional commit regex pattern
90+
if ! echo "$commit_msg" | grep -qP '^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\([a-z0-9-]+\))?: .+$'; then
91+
commit_info="$(git log -1 --format='%H - %s' $commit)"
92+
invalid_commits="- $invalid_commits\n$commit_info"
93+
fi
94+
done < <(git rev-list $base_sha..$head_sha)
95+
96+
# Set output
97+
if [ ! -z "$invalid_commits" ]; then
98+
echo "invalid_commits<<EOF" >> $GITHUB_OUTPUT
99+
echo -e "$invalid_commits" >> $GITHUB_OUTPUT
100+
echo "EOF" >> $GITHUB_OUTPUT
101+
echo "has_invalid=true" >> $GITHUB_OUTPUT
102+
else
103+
echo "has_invalid=false" >> $GITHUB_OUTPUT
104+
fi
105+
- name: Comment PR
106+
if: steps.get-commits.outputs.has_invalid == 'true'
107+
uses: actions/github-script@v6
108+
with:
109+
script: |
110+
const invalidCommits = `${{ steps.get-commits.outputs.invalid_commits }}`;
111+
const message = `### ❌ Invalid Conventional Commits Detected
112+
113+
The following commits do not follow the [Conventional Commits](https://www.conventionalcommits.org/) format:
114+
115+
${invalidCommits}
116+
117+
118+
Please update these commit messages to follow the format:
119+
\`<type>[optional scope]: <description>\`
120+
121+
Valid types: build, chore, ci, docs, feat, fix, perf, refactor, revert, style, test`;
122+
123+
github.rest.issues.createComment({
124+
issue_number: context.issue.number,
125+
owner: context.repo.owner,
126+
repo: context.repo.repo,
127+
body: message
128+
});
129+
core.setFailed('Some commits do not follow conventional commit format');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: Pull Request Title Conventional Check
2+
on:
3+
pull_request:
4+
types: [opened, edited, synchronize, reopened]
5+
env:
6+
GO_VERSION: 1.22.x
7+
jobs:
8+
test:
9+
name: Pull Request Title
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v4
13+
- name: Go pull request message linter
14+
uses: masahiro331/[email protected]
15+
env:
16+
TITLE: ${{ github.event.pull_request.title }}

CONTRIBUTING.md

+20-3
Original file line numberDiff line numberDiff line change
@@ -194,13 +194,30 @@ client version you want to be using as well.
194194
To keep a style and allow us to automate the generating of a change log, we
195195
require that that commit messages adhere to a standard.
196196

197-
TL;DR The commit message must match this regular expression.
197+
## Commit Message Format
198198

199+
All commits must follow [Conventional Commits](https://www.conventionalcommits.org/) standards.
200+
Use the following format:
201+
`<type>(<scope>): <description>`
199202

200-
(chore|docs|feat|fix|refactor|tests?)(\([^\)]+\))?: .*
203+
**Allowed commit types**:
201204

205+
- **feat**: New feature for end users (*not* build scripts)
206+
- **fix**: Bug fix for end users
207+
- **perf**: Performance improvements
208+
- **docs**: Documentation changes
209+
- **style**: Code formatting, semicolons, etc.
210+
- **refactor**: Code restructuring without changing behavior
211+
- **test**: Test-related changes (no production code)
212+
- **build**: Build system/config changes
213+
- **chore**: Maintenance tasks (e.g., dependency updates)
202214

203-
For more information on commit messages, we mostly follow [this standard][conventional_commits].
215+
Example of valid commit messages:
216+
```
217+
git commit -m "feat(auth): add OAuth2 login support"
218+
git commit -m "docs(readme): update installation instructions"
219+
```
220+
Please make sure your commit messages follow the conventional format. The linter will check your messages before allowing the commit.
204221

205222
[api_explorer]: https://rpm.newrelic.com/api/explore/
206223

build/lint.mk

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ FILES ?= $(shell find ${SRCDIR} -type f | grep -v -e '.git/' -e '/vend
1616
GO_FILES ?= $(shell find $(SRCDIR) -type f -name "*.go" | grep -v -e ".git/" -e '/example/')
1717
PROJECT_MODULE ?= $(shell $(GO) list -m)
1818

19+
COMMIT_LINT_CMD ?= go-commitlinter "$1"
20+
1921
lint: deps spell-check gofmt golangci goimports outdated
2022
lint-fix: deps spell-check-fix gofmt-fix goimports
2123

@@ -42,6 +44,10 @@ goimports: deps
4244
@echo "=== $(PROJECT_NAME) === [ goimports ]: Checking imports with $(GOIMPORTS)..."
4345
@$(GOIMPORTS) -l -w -local $(PROJECT_MODULE) $(GO_FILES)
4446

47+
lint-commit: deps
48+
@echo "=== $(PROJECT_NAME) === [ lint-commit ]: Checking that commit messages are properly formatted ($(COMMIT_LINT_CMD))..."
49+
@$(COMMIT_LINT_CMD)
50+
4551
golangci: deps
4652
@echo "=== $(PROJECT_NAME) === [ golangci-lint ]: Linting using $(GOLINTER) ($(COMMIT_LINT_CMD))..."
4753
@$(GOLINTER) run
@@ -50,4 +56,4 @@ outdated: deps tools-outdated
5056
@echo "=== $(PROJECT_NAME) === [ outdated ]: Finding outdated deps with $(GO_MOD_OUTDATED)..."
5157
@$(GO) list -u -m -json all | $(GO_MOD_OUTDATED) -direct -update
5258

53-
.PHONY: lint spell-check spell-check-fix gofmt gofmt-fix lint-fix outdated goimports
59+
.PHONY: lint spell-check spell-check-fix gofmt gofmt-fix lint-fix lint-commit outdated goimports

tools/go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/git-chglog/git-chglog v0.15.4
1111
github.com/golangci/golangci-lint v1.51.2
1212
github.com/goreleaser/goreleaser v1.26.2
13+
github.com/masahiro331/go-commitlinter v0.0.0-20230912111648-dd369c40beae
1314
github.com/psampaz/go-mod-outdated v0.9.0
1415
github.com/stretchr/testify v1.10.0
1516
golang.org/x/tools v0.23.0

tools/go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,8 @@ github.com/maratori/testableexamples v1.0.0 h1:dU5alXRrD8WKSjOUnmJZuzdxWOEQ57+7s
811811
github.com/maratori/testableexamples v1.0.0/go.mod h1:4rhjL1n20TUTT4vdh3RDqSizKLyXp7K2u6HgraZCGzE=
812812
github.com/maratori/testpackage v1.1.0 h1:GJY4wlzQhuBusMF1oahQCBtUV/AQ/k69IZ68vxaac2Q=
813813
github.com/maratori/testpackage v1.1.0/go.mod h1:PeAhzU8qkCwdGEMTEupsHJNlQu2gZopMC6RjbhmHeDc=
814+
github.com/masahiro331/go-commitlinter v0.0.0-20230912111648-dd369c40beae h1:oz/k1U2fd/lgeVzIWwFixTsbcQpVIouBItNn+1wQT2E=
815+
github.com/masahiro331/go-commitlinter v0.0.0-20230912111648-dd369c40beae/go.mod h1:y5FnwWvGbGTWD1ObWpVPnMH7WXp1ne13WLP5q/Nt7fo=
814816
github.com/matoous/godox v0.0.0-20210227103229-6504466cf951 h1:pWxk9e//NbPwfxat7RXkts09K+dEBJWakUWwICVqYbA=
815817
github.com/matoous/godox v0.0.0-20210227103229-6504466cf951/go.mod h1:1BELzlh859Sh1c6+90blK8lbYy0kwQf1bYlBhBysy1s=
816818
github.com/matryer/is v1.4.0 h1:sosSmIWwkYITGrxZ25ULNDeKiMNzFSr4V/eqBQP0PeE=

tools/tools.go

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
_ "github.com/git-chglog/git-chglog/cmd/git-chglog"
1010
_ "github.com/golangci/golangci-lint/cmd/golangci-lint"
1111
_ "github.com/goreleaser/goreleaser"
12+
_ "github.com/masahiro331/go-commitlinter"
1213
_ "github.com/psampaz/go-mod-outdated"
1314
_ "github.com/stretchr/testify/assert"
1415
_ "golang.org/x/tools/cmd/godoc"

0 commit comments

Comments
 (0)