Repository navigation
Expand file tree
/
Copy pathpr-comment.yml
More file actions
119 lines (102 loc) · 3.83 KB
/
Copy pathpr-comment.yml
File metadata and controls
119 lines (102 loc) · 3.83 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
name: Validate and comment on Co-authored-by trailers
on:
pull_request:
jobs:
validate-commits:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-python@v5
with:
python-version: "3.13"
- name: Install coauthorcheck
run: pip install coauthorcheck==0.7.0
- name: Validate PR commits
id: validate
shell: bash
run: |
set +e
coauthorcheck --format json "${{ github.event.pull_request.base.sha }}..${{ github.event.pull_request.head.sha }}" > result.json
status=$?
echo "exit_code=$status" >> "$GITHUB_OUTPUT"
cat result.json
exit 0
- name: Comment on pull request when validation fails
if: steps.validate.outputs.exit_code == '1'
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const result = JSON.parse(fs.readFileSync('result.json', 'utf8'));
const invalidResults = result.results.filter(item => !item.is_valid);
const marker = '<!-- coauthorcheck-comment -->';
const lines = [
marker,
'## coauthorcheck',
'',
`Found ${result.summary.issue_count} issue(s) across ${result.summary.invalid_commit_count} commit(s).`,
'',
];
for (const item of invalidResults) {
lines.push(`### ${item.source}`);
for (const issue of item.issues) {
lines.push(`- line ${issue.line_number}: [${issue.code}] ${issue.message}`);
}
lines.push('');
}
const body = lines.join('\n');
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const existing = comments.find(comment =>
comment.user?.type === 'Bot' && comment.body?.includes(marker)
);
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body,
});
}
- name: Remove previous coauthorcheck comment when validation passes
if: steps.validate.outputs.exit_code == '0'
uses: actions/github-script@v7
with:
script: |
const marker = '<!-- coauthorcheck-comment -->';
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
});
const existing = comments.find(comment =>
comment.user?.type === 'Bot' && comment.body?.includes(marker)
);
if (existing) {
await github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
});
}
- name: Fail workflow on validation issues
if: steps.validate.outputs.exit_code == '1'
run: exit 1
- name: Fail workflow on execution errors
if: steps.validate.outputs.exit_code != '0' && steps.validate.outputs.exit_code != '1'
run: exit 2