Skip to content

Commit 68265f9

Browse files
Merge pull request #103 from Provenance-Emu/libretro/fix/artifact-comments
Fix artifact comment posting for fork PRs
2 parents 6cf0794 + 57da3a0 commit 68265f9

2 files changed

Lines changed: 106 additions & 75 deletions

File tree

.github/workflows/artifacts.yml

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
name: Post build artifact links
2+
3+
# Runs in the context of the base (upstream) repo after the build workflow
4+
# completes — this gives the GITHUB_TOKEN write access to PRs even when
5+
# the build was triggered by a fork PR.
6+
on:
7+
workflow_run:
8+
workflows: ["C/C++ CI"]
9+
types: [completed]
10+
11+
permissions:
12+
actions: read
13+
pull-requests: write
14+
15+
jobs:
16+
post-artifacts:
17+
if: >
18+
github.event.workflow_run.event == 'pull_request' &&
19+
github.event.workflow_run.conclusion == 'success'
20+
runs-on: ubuntu-latest
21+
steps:
22+
- name: Comment artifact links on PR
23+
uses: actions/github-script@v7
24+
with:
25+
script: |
26+
const run = context.payload.workflow_run;
27+
const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${run.id}`;
28+
29+
// Find associated PR number from the triggering workflow run
30+
const prs = run.pull_requests;
31+
if (!prs || prs.length === 0) {
32+
// Fallback: search for open PR matching the head branch/SHA
33+
const { data: pulls } = await github.rest.pulls.list({
34+
owner: context.repo.owner,
35+
repo: context.repo.repo,
36+
state: 'open',
37+
head: `${run.head_repository.owner.login}:${run.head_branch}`,
38+
});
39+
if (pulls.length === 0) {
40+
console.log('No associated PR found, skipping.');
41+
return;
42+
}
43+
prs.push(pulls[0]);
44+
}
45+
const prNumber = prs[0].number;
46+
47+
// Fetch artifacts from the completed workflow run
48+
const { data: { artifacts } } = await github.rest.actions.listWorkflowRunArtifacts({
49+
owner: context.repo.owner,
50+
repo: context.repo.repo,
51+
run_id: run.id,
52+
});
53+
54+
let rows = '';
55+
for (const a of artifacts) {
56+
const dlUrl = `${runUrl}/artifacts/${a.id}`;
57+
rows += `| ${a.name} | [${a.name}](${dlUrl}) | ${(a.size_in_bytes / 1024).toFixed(0)} KB |\n`;
58+
}
59+
60+
if (!rows) {
61+
rows = `| - | No artifacts found | - |\n`;
62+
}
63+
64+
// Check for existing comment to update (upsert pattern)
65+
const comments = await github.paginate(
66+
github.rest.issues.listComments,
67+
{
68+
owner: context.repo.owner,
69+
repo: context.repo.repo,
70+
issue_number: prNumber,
71+
per_page: 100,
72+
}
73+
);
74+
const marker = '<!-- build-artifacts -->';
75+
const existing = comments.find(c => c.body.includes(marker));
76+
77+
const body = [
78+
marker,
79+
'### Build Artifacts',
80+
'',
81+
'| Platform | Download | Size |',
82+
'|----------|----------|------|',
83+
rows.trim(),
84+
'',
85+
`> [Full workflow run](${runUrl})`,
86+
'',
87+
`<sub>Updated by CI at ${new Date().toISOString()}</sub>`,
88+
].join('\n');
89+
90+
if (existing) {
91+
await github.rest.issues.updateComment({
92+
owner: context.repo.owner,
93+
repo: context.repo.repo,
94+
comment_id: existing.id,
95+
body,
96+
});
97+
console.log(`Updated comment ${existing.id} on PR #${prNumber}`);
98+
} else {
99+
await github.rest.issues.createComment({
100+
owner: context.repo.owner,
101+
repo: context.repo.repo,
102+
issue_number: prNumber,
103+
body,
104+
});
105+
console.log(`Created new comment on PR #${prNumber}`);
106+
}

.github/workflows/c-cpp.yml

Lines changed: 0 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ on:
66
pull_request:
77
branches: [ master ]
88

9-
permissions:
10-
actions: read
11-
contents: read
12-
pull-requests: write
13-
149
jobs:
1510
build:
1611
strategy:
@@ -80,73 +75,3 @@ jobs:
8075
name: ${{ matrix.config.displayTargetName }}
8176
path: ${{ matrix.config.artifact }}
8277
if-no-files-found: error
83-
84-
post-artifacts:
85-
if: github.event_name == 'pull_request'
86-
needs: build
87-
runs-on: ubuntu-latest
88-
steps:
89-
- name: Comment artifact links on PR
90-
continue-on-error: true
91-
uses: actions/github-script@v7
92-
with:
93-
script: |
94-
const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`;
95-
96-
// Fetch actual artifact list from this workflow run
97-
const { data: { artifacts } } = await github.rest.actions.listWorkflowRunArtifacts({
98-
owner: context.repo.owner,
99-
repo: context.repo.repo,
100-
run_id: context.runId,
101-
});
102-
103-
let rows = '';
104-
for (const a of artifacts) {
105-
const dlUrl = `${runUrl}/artifacts/${a.id}`;
106-
rows += `| ${a.name} | [${a.name}](${dlUrl}) | ${(a.size_in_bytes / 1024).toFixed(0)} KB |\n`;
107-
}
108-
109-
if (!rows) {
110-
rows = `| - | No artifacts found | - |\n`;
111-
}
112-
113-
const comments = await github.paginate(
114-
github.rest.issues.listComments,
115-
{
116-
owner: context.repo.owner,
117-
repo: context.repo.repo,
118-
issue_number: context.issue.number,
119-
per_page: 100,
120-
}
121-
);
122-
const marker = '<!-- build-artifacts -->';
123-
const existing = comments.find(c => c.body.includes(marker));
124-
125-
const body = [
126-
marker,
127-
'### Build Artifacts',
128-
'',
129-
'| Platform | Download | Size |',
130-
'|----------|----------|------|',
131-
rows.trim(),
132-
'',
133-
`> [Full workflow run](${runUrl})`,
134-
'',
135-
`<sub>Updated by CI at ${new Date().toISOString()}</sub>`,
136-
].join('\n');
137-
138-
if (existing) {
139-
await github.rest.issues.updateComment({
140-
owner: context.repo.owner,
141-
repo: context.repo.repo,
142-
comment_id: existing.id,
143-
body,
144-
});
145-
} else {
146-
await github.rest.issues.createComment({
147-
owner: context.repo.owner,
148-
repo: context.repo.repo,
149-
issue_number: context.issue.number,
150-
body,
151-
});
152-
}

0 commit comments

Comments
 (0)