Repository navigation
144 lines (125 loc) · 5.16 KB
/
Copy pathpr-comment.yml
File metadata and controls
144 lines (125 loc) · 5.16 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
144
name: PR Comment
on:
workflow_run:
workflows: ["PR Build"]
types: [completed]
permissions:
pull-requests: write
actions: read
jobs:
comment:
if: >-
github.event.workflow_run.conclusion == 'success' &&
github.event.workflow_run.event == 'pull_request'
runs-on: ubuntu-latest
steps:
- name: Download binaries artifact
uses: actions/download-artifact@v4
with:
name: uploadcare-cli-binaries
path: binaries
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ secrets.GITHUB_TOKEN }}
- name: Comment on PR
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const headSha = context.payload.workflow_run.head_sha;
const headRepo = context.payload.workflow_run.head_repository;
// Try workflow_run.pull_requests first (populated for same-repo PRs)
let prNumber = context.payload.workflow_run.pull_requests?.[0]?.number;
// For forked PRs, pull_requests is empty — look up by head owner:branch
if (!prNumber) {
const { data: pulls } = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
head: `${headRepo.owner.login}:${context.payload.workflow_run.head_branch}`,
});
if (pulls.length === 0) {
core.warning(`No open PR found for ${headRepo.owner.login}:${context.payload.workflow_run.head_branch} — skipping comment`);
return;
}
prNumber = pulls[0].number;
}
// Skip if this build is no longer the PR head (stale build race)
const { data: pr } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: prNumber,
});
if (pr.head.sha !== headSha) {
core.info(`Build is for ${headSha.substring(0, 7)} but PR head is now ${pr.head.sha.substring(0, 7)} — skipping stale comment`);
return;
}
// List files from the downloaded artifact, sanitize names
const safeNamePattern = /^[\w.\-]+\.(tar\.gz|zip)$/;
const files = fs.readdirSync('binaries')
.filter(f => safeNamePattern.test(f))
.sort();
const buildRunId = context.payload.workflow_run.id;
const runUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/runs/${buildRunId}#artifacts`;
const shortSha = headSha.substring(0, 7);
const now = new Date().toISOString().replace('T', ' ').replace(/\.\d+Z$/, ' UTC');
const newRow = `| \`${shortSha}\` | [Download](${runUrl}) | ${now} |`;
// Find existing comment
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const botComment = comments.find(c =>
c.user.type === 'Bot' && c.body.includes('PR Build Artifacts')
);
let historyRows = [];
if (botComment) {
// Extract existing table rows (skip header and separator)
const lines = botComment.body.split('\n');
historyRows = lines.filter(l =>
l.startsWith('|') && !l.startsWith('| Commit') && !l.startsWith('| ---')
);
}
// Prepend new build (latest first)
historyRows.unshift(newRow);
const platformList = files.map(f => `- \`${f}\``).join('\n');
const body = [
'## PR Build Artifacts',
'',
'| Commit | Build | Time (UTC) |',
'| --- | --- | --- |',
...historyRows,
'',
'<details>',
'<summary>Available platforms</summary>',
'',
platformList,
'',
'</details>',
'',
'<details>',
'<summary>How to install</summary>',
'',
'1. Click **Download** for the build you want',
'2. Download **uploadcare-cli-binaries** zip',
'3. Unzip it — you\'ll find per-platform archives inside',
'4. Extract the archive for your platform (e.g. `tar xzf uploadcare-cli_*_darwin_arm64.tar.gz`)',
'5. Run `./uploadcare`',
'',
'</details>',
].join('\n');
if (botComment) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: botComment.id,
body,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body,
});
}