-
Notifications
You must be signed in to change notification settings - Fork 24
108 lines (92 loc) · 3.77 KB
/
pr-comment.yml
File metadata and controls
108 lines (92 loc) · 3.77 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
name: PR Spec Preview Comment
on:
workflow_run:
workflows: ["Check Specs"]
types: [completed]
permissions:
actions: read
pull-requests: write
jobs:
comment:
name: Post spec preview
runs-on: ubuntu-latest
if: >
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success'
steps:
- name: Download PR metadata
uses: dawidd6/action-download-artifact@bf251b5aa9c2f7eeb574a96ee720e24f801b7c11 # v6
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
run_id: ${{ github.event.workflow_run.id }}
name: pr-metadata
- name: Read PR number
id: pr
run: echo "number=$(cat pr-number.txt)" >> "$GITHUB_OUTPUT"
- name: Download spec artifacts
uses: dawidd6/action-download-artifact@bf251b5aa9c2f7eeb574a96ee720e24f801b7c11 # v6
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
run_id: ${{ github.event.workflow_run.id }}
name_is_regexp: true
name: pr-${{ steps.pr.outputs.number }}-.*
path: /tmp/pr-artifacts
continue-on-error: true
- name: Comment on PR
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
with:
script: |
const fs = require('fs');
const path = require('path');
const prNumber = parseInt('${{ steps.pr.outputs.number }}');
const runUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${{ github.event.workflow_run.id }}`;
// Find spec artifacts
const specsDir = fs.readdirSync('/tmp/pr-artifacts').find(d => d.includes('specs'));
const diffsDir = fs.readdirSync('/tmp/pr-artifacts').find(d => d.includes('diffs'));
let table = '| Spec | Changed |\n|------|--------|\n';
if (specsDir) {
const specsPath = path.join('/tmp/pr-artifacts', specsDir);
const htmlFiles = fs.readdirSync(specsPath)
.filter(f => f.startsWith('draft-') && f.endsWith('.html'))
.sort();
for (const file of htmlFiles) {
const name = file.replace('.html', '');
let changed = '-';
if (diffsDir) {
const diffFile = path.join('/tmp/pr-artifacts', diffsDir, `${name}-diff.txt`);
if (fs.existsSync(diffFile)) {
const content = fs.readFileSync(diffFile, 'utf8');
if (content.trim() && !content.includes('New spec')) {
changed = 'Yes';
}
}
}
table += `| \`${name}\` | ${changed} |\n`;
}
}
const body = `<!-- ietf-spec-preview -->
## Spec Preview
${table}
**[Download spec artifacts](${runUrl}#artifacts)** (HTML, TXT, XML, PDF)
`;
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const existingComment = comments.find(c => c.body.includes('<!-- ietf-spec-preview -->'));
if (existingComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existingComment.id,
body: body
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: body
});
}