|
| 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'); |
0 commit comments