Skip to content

Commit e0cd149

Browse files
committed
ci: Extract the pre-commit step in a dedicated workflow that would comment on the PR what the user should do next
Ideally, we could automatically push changes to the PR branch, but this would require elevated permissions (like running in a pull_request_target trigger), which come with certain restrictions (like the fact that the PR checks will not be re-triggered on a push by the GH Actions bot) Signed-off-by: Armel Soro <[email protected]>
1 parent 572167c commit e0cd149

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

.github/workflows/pre-commit.yaml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
name: Pre-commit
2+
3+
on:
4+
pull_request:
5+
6+
concurrency:
7+
group: ${{ github.workflow }}-${{ github.event.number }}
8+
cancel-in-progress: true
9+
10+
jobs:
11+
pre-commit:
12+
name: Pre-commit
13+
runs-on: ubuntu-latest
14+
permissions:
15+
pull-requests: write # Required to post comments
16+
env:
17+
GO111MODULE: on
18+
steps:
19+
- name: Checkout
20+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # pin@v3
21+
with:
22+
fetch-depth: 0
23+
24+
- uses: actions/setup-python@e797f83bcb11b83ae66e0230d6156d7c80228e7c # pin@v4
25+
with:
26+
python-version: 3.12
27+
28+
- uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # pin@v3
29+
with:
30+
go-version: ^1
31+
32+
- name: Setup helm-docs
33+
run: go install github.com/norwoodj/helm-docs/cmd/helm-docs@latest
34+
35+
- name: Run pre-commit
36+
uses: pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd # [email protected]
37+
continue-on-error: true # Don't fail immediately; we'll handle it below
38+
with:
39+
extra_args: --verbose --all-files --show-diff-on-failure
40+
41+
- name: Check for changes after pre-commit
42+
id: diff-checker
43+
run: |
44+
echo "CHANGED=$(if git diff --quiet; then echo "false"; else echo "true"; fi)" >> $GITHUB_OUTPUT
45+
46+
- name: Comment PR with diff
47+
if: ${{ steps.diff-checker.outputs.CHANGED == 'true' }}
48+
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8
49+
with:
50+
script: |
51+
const {
52+
exitCode,
53+
stdout,
54+
stderr
55+
} = await exec.getExecOutput('git', ['diff']);
56+
console.log(exitCode, stderr);
57+
58+
const body = `
59+
## Pre-commit Hook Failures
60+
<details>
61+
<summary>View diff</summary>
62+
63+
\`\`\`diff
64+
${stdout}
65+
\`\`\`
66+
67+
</details>
68+
Please apply the above diff in your PR branch (or run `pre-commit run --all-files` locally) and push the changes.
69+
`;
70+
github.rest.issues.createComment({
71+
issue_number: context.issue.number,
72+
owner: context.repo.owner,
73+
repo: context.repo.repo,
74+
body: body
75+
});

0 commit comments

Comments
 (0)