File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- name : Test Single Commit
1+ name : Check single commit
22
33on :
44 pull_request :
88 check-single-commit :
99 runs-on : ubuntu-latest
1010 steps :
11- - name : Checkout code
12- uses : actions/checkout@v4
13- with :
14- fetch-depth : 0
15-
16- - name : Count commits
11+ - name : Count commits in PR
1712 id : commits
1813 run : |
19- BASE_BRANCH="${{ github.event.pull_request.base.ref }}"
20- COMMIT_COUNT=$(git rev-list --count --no-merges origin/$BASE_BRANCH..HEAD)
21- echo "count=$COMMIT_COUNT" >> $GITHUB_OUTPUT
14+ PR_NUMBER=${{ github.event.pull_request.number }}
15+ REPO_OWNER=${{ github.repository_owner }}
16+ REPO_NAME=${{ github.event.repository.name }}
17+
18+ COMMIT_COUNT=$(curl -s \
19+ -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \
20+ -H "Accept: application/vnd.github+json" \
21+ "https://api.github.com/repos/$REPO_OWNER/$REPO_NAME/pulls/$PR_NUMBER/commits" \
22+ | jq '. | length')
23+
24+ echo "commit_count=$COMMIT_COUNT" >> $GITHUB_OUTPUT
2225
2326 - name : Enforce single commit
2427 run : |
25- if [ "${{ steps.commits.outputs.count }}" -gt 1 ]; then
26- echo "❌ PR has more than one commit (${{ steps.commits.outputs.count }} commits)."
28+ if [ "${{ steps.commits.outputs.commit_count }}" -gt 1 ]; then
29+ echo "❌ PR has more than one commit (${{
30+ steps.commits.outputs.commit_count }})"
2731 exit 1
2832 else
29- echo "✅ PR has 1 commit. "
33+ echo "✅ PR has a single commit"
3034 fi
Original file line number Diff line number Diff line change 1+ This is the second commit and should trigger the single commit check to fail.
You can’t perform that action at this time.
0 commit comments