forked from janus-idp/helm-backstage
-
Notifications
You must be signed in to change notification settings - Fork 30
103 lines (86 loc) · 3.36 KB
/
pre-commit-comment.yaml
File metadata and controls
103 lines (86 loc) · 3.36 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
name: Pre-commit comment
on:
workflow_run:
workflows: ["Pre-commit"]
types:
- completed
jobs:
comment:
name: Comment on PR
runs-on: ubuntu-latest
if: github.event.workflow_run.event == 'pull_request'
permissions:
pull-requests: write
steps:
- name: Get the PR number from the workflow run
id: pr-number
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
with:
script: |
const prs = context.payload.workflow_run.pull_requests;
if (prs.length > 0) {
const num = prs[0].number;
if (Number.isInteger(num)) {
core.setOutput('number', num);
} else {
core.setFailed(`Invalid PR number detected: ${num}`);
}
}
- name: Delete previous pre-commit failure comments
if: steps.pr-number.outputs.number
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
env:
PR_NUMBER: ${{ steps.pr-number.outputs.number }}
with:
script: |
const prNumber = parseInt(process.env.PR_NUMBER, 10);
// 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}`);
const botComments = comments.filter(comment => {
return comment.user.login === 'github-actions[bot]' &&
comment.body &&
comment.body.includes('⚠️ Pre-commit hook failures');
});
for (const comment of botComments) {
try {
await github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: comment.id,
});
} catch (error) {
console.warn(`Failed to delete comment ${comment.id}:`, error.message);
}
}
- name: Post comment
if: steps.pr-number.outputs.number && github.event.workflow_run.conclusion == 'failure'
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9
env:
PR_NUMBER: ${{ steps.pr-number.outputs.number }}
with:
script: |
const prNumber = parseInt(process.env.PR_NUMBER, 10);
const body = `
## ⚠️ Pre-commit hook failures
The pre-commit hooks detected issues that need to be fixed.
**To fix these issues:**
1. Install the required dependencies:
- [pre-commit](https://pre-commit.com/#install)
- [helm-docs](https://github.com/norwoodj/helm-docs#installation)
2. Run pre-commit on all files:
\`\`\`bash
pre-commit run --all-files
\`\`\`
3. Commit and push any resulting changes
`.trim().replace(/^ {12}/gm, '');
await github.rest.issues.createComment({
issue_number: prNumber,
owner: context.repo.owner,
repo: context.repo.repo,
body: body
});