Skip to content

Commit

Permalink
Add a clang-format error to force workflow failure (microsoft#5757)
Browse files Browse the repository at this point in the history
This change _should_ fix the permissions issues and allow the PR format
checker to post comments into PRs on failure. The comments should
include the first 50 lines of the diff that clang-format produces and an
error with a link to the full log.

Unfortunately this can't really be tested until it is merged. The
`pull_request_target` event only occurs once the action is in the target
branch.

Parts of this change have been tested separately, but they haven't all
been tested together.
  • Loading branch information
llvm-beanz authored Sep 20, 2023
1 parent 29e4ca2 commit 314d2ff
Showing 1 changed file with 16 additions and 17 deletions.
33 changes: 16 additions & 17 deletions .github/workflows/clang-format-checker.yml
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
name: 'PR clang-format checker'
on:
pull_request:
pull_request_target:
types:
- opened
- edited
- synchronize
- reopened
permissions:
contents: read
pull-requests: write
issues: write
jobs:
check-pr-formatting:
permissions:
contents: read
pull-requests: write
checks: write
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: install clang-format
run: sudo apt install -y clang-format
- name: Comparing ${{ env.GITHUB_BASE_REF }} vs HEAD
- name: Comparing PR against target branch
env:
PR_NUMBER: ${{ github.event.number }}
GH_TOKEN: ${{ github.token }}
run: |
echo Comparing $GITHUB_BASE_REF vs HEAD
git diff -U0 --no-color origin/$GITHUB_BASE_REF..HEAD | clang-format-diff -p1 | tee format.diff
if [ -s "format.diff" ]
then
then
echo PR contains clang-format violations. First 50 lines of the diff: >> message.txt
echo ``` >> message.txt
cat format.diff | head -n 50 >> message.txt
echo ``` >> message.txt
echo See [action log]\(https://github.com/microsoft/DirectXShaderCompiler/actions/runs/$GITHUB_RUN_ID/job/$GITHUB_JOB\) for the full diff. >> message.txt
gh pr comment $PR_NUMBER --body-file message.txt
exit 1
fi
- if: failure()
uses: actions/github-script@v6
with:
script: |
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: 'PR contains clang-format violations. See [action log](https://github.com/microsoft/DirectXShaderCompiler/actions/runs/${{ env.GITHUB_RUN_ID }}/job/${{ env.GITHUB_JOB }}) for the full diff.'
})

0 comments on commit 314d2ff

Please sign in to comment.