-
Notifications
You must be signed in to change notification settings - Fork 99
143 lines (118 loc) · 5.42 KB
/
Copy pathpre-commit-comment.yaml
File metadata and controls
143 lines (118 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
name: Pre-commit Comment
on:
workflow_run:
workflows: ["Pre-commit"]
types:
- completed
jobs:
comment:
name: Comment on PR
runs-on: ubuntu-latest
# Only run if the pre-commit workflow completed (not necessarily succeeded)
if: github.event.workflow_run.event == 'pull_request'
permissions:
pull-requests: write
steps:
- name: Download artifact
id: download
uses: actions/download-artifact@fa0a91b85d4f404e444e00e005971372dc801d16 # v4
continue-on-error: true
with:
name: pre-commit-diff
github-token: ${{ secrets.GITHUB_TOKEN }}
run-id: ${{ github.event.workflow_run.id }}
- name: Check artifact and extract PR number
id: extract
if: steps.download.outcome == 'success'
run: |
if [ -f pr-number.txt ]; then
echo "PR_NUMBER=$(cat pr-number.txt)" >> $GITHUB_OUTPUT
echo "HAS_DIFF=true" >> $GITHUB_OUTPUT
else
echo "HAS_DIFF=false" >> $GITHUB_OUTPUT
fi
- name: Get PR number from workflow run
id: pr-number
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8
with:
script: |
// Try to get PR number from artifact first, otherwise from the workflow run
let prNumber = ${{ steps.extract.outputs.PR_NUMBER || 'null' }};
if (!prNumber) {
// Get PR number from the workflow run event
const headRepo = context.payload.workflow_run.head_repository.owner.login;
const headBranch = context.payload.workflow_run.head_branch;
console.log(`Looking for PR with head: ${headRepo}:${headBranch}`);
const { data: pullRequests } = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
head: `${headRepo}:${headBranch}`,
});
if (pullRequests.length > 0) {
prNumber = pullRequests[0].number;
console.log(`Found PR #${prNumber} for branch ${headBranch}`);
} else {
console.log('No open PR found for this workflow run');
console.log('This might be a push to a non-PR branch, skipping comment workflow');
return;
}
}
core.setOutput('number', prNumber);
- name: Delete previous pre-commit failure comments
if: steps.pr-number.outputs.number
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8
with:
script: |
const prNumber = ${{ steps.pr-number.outputs.number }};
// Get all comments on the PR
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
console.log(`Found ${comments.length} total comments on PR #${prNumber}`);
// Filter comments created by github-actions bot that contain our marker
const botComments = comments.filter(comment => {
const isBot = comment.user.login === 'github-actions[bot]';
const hasMarker = comment.body && comment.body.includes('⚠️ Pre-commit Hook Failures');
if (isBot && hasMarker) {
console.log(`Found matching comment ${comment.id} to delete`);
}
return isBot && hasMarker;
});
console.log(`Found ${botComments.length} pre-commit failure comment(s) to delete`);
// Delete each matching comment
for (const comment of botComments) {
console.log(`Deleting comment ${comment.id}`);
await github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: comment.id,
});
}
console.log(`Successfully deleted ${botComments.length} previous pre-commit failure comment(s).`);
- name: Post comment
if: steps.extract.outputs.HAS_DIFF == 'true'
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8
with:
script: |
const fs = require('fs');
const prNumber = ${{ steps.pr-number.outputs.number }};
const diff = fs.readFileSync('diff.txt', 'utf8');
const body = `## ⚠️ Pre-commit Hook Failures
<details>
<summary>View diff</summary>
\`\`\`diff
${diff}
\`\`\`
</details>
Please apply the above diff in your PR branch (or run \`pre-commit run --all-files\` locally) and push the changes.
For more information on pre-commit, see the [pre-commit documentation](https://pre-commit.com/#install).`;
await github.rest.issues.createComment({
issue_number: prNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body: body
});
console.log('Successfully posted PR comment with diff.');