Skip to content

Add CI check for issue references in commit messages#419

Merged
ebrahimebrahim merged 1 commit into
mainfrom
add-precommit-check-for-issue-number
Feb 25, 2026
Merged

Add CI check for issue references in commit messages#419
ebrahimebrahim merged 1 commit into
mainfrom
add-precommit-check-for-issue-number

Conversation

@ebrahimebrahim
Copy link
Copy Markdown
Collaborator

@ebrahimebrahim ebrahimebrahim commented Feb 23, 2026

This makes it a CI requirement to reference in issue number in each commit message (in the title or body of the message).

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new CI job to enforce that pull request commit messages reference a GitHub issue (with a few allowed exceptions), helping keep commit history consistently traceable to issues.

Changes:

  • Add a check-commit-messages job that scans all commits in a PR and fails CI if any commit message lacks an issue reference.
  • Define allowed reference patterns (#123 or full issue URL) and exception prefixes (e.g., Bump, Merge, Revert, fixup!).
Comments suppressed due to low confidence (3)

.github/workflows/ci.yml:40

  • Using echo "$msg" can misbehave for certain commit messages (e.g., ones starting with -n), since echo may treat that as an option and change output. Use printf '%s\n' "$msg" (or similar) to reliably pass the message to grep.
            msg=$(git log -1 --format="%B" "$sha")
            first_line=$(git log -1 --format="%s" "$sha")
            if ! echo "$msg" | grep -qP "$pattern"; then
              echo "::error::Commit message missing issue reference: $first_line"

.github/workflows/ci.yml:37

  • The commit range "$BASE_SHA".."$HEAD_SHA" assumes the base SHA is an ancestor of the head SHA. If the PR branch is behind base (or history diverged), this range can miss commits or include an unexpected set. Consider computing the merge-base first (e.g., git merge-base "$BASE_SHA" "$HEAD_SHA") and using that as the start of the range to reliably get all PR commits.
          for sha in $(git log --format="%H" "$BASE_SHA".."$HEAD_SHA"); do
            msg=$(git log -1 --format="%B" "$sha")

.github/workflows/ci.yml:40

  • The regex is applied to the full multi-line commit message (%B). Because ^ matches the start of any line, a commit could pass by having an exception token (e.g., Merge ) in the body even if the subject doesn't qualify. If the intent is to allow exceptions only based on the subject, check the exception patterns against first_line (or use grep -qPz with \A anchors).
          pattern='(#[0-9]+|github\.com/[^/]+/[^/]+/issues/[0-9]+|^Bump |^Merge |^Revert |^fixup! |^squash! |^amend! )'
          failed=0
          for sha in $(git log --format="%H" "$BASE_SHA".."$HEAD_SHA"); do
            msg=$(git log -1 --format="%B" "$sha")
            first_line=$(git log -1 --format="%s" "$sha")
            if ! echo "$msg" | grep -qP "$pattern"; then
              echo "::error::Commit message missing issue reference: $first_line"

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@ebrahimebrahim ebrahimebrahim force-pushed the add-precommit-check-for-issue-number branch from e97a14d to d8c02d8 Compare February 24, 2026 03:41
@ebrahimebrahim ebrahimebrahim merged commit 4ff3be8 into main Feb 25, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants