-
Notifications
You must be signed in to change notification settings - Fork 41
Fix artifact comment posting for fork PRs #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
LibretroAdmin
merged 1 commit into
libretro:master
from
Provenance-Emu:libretro/fix/artifact-comments
Apr 3, 2026
+106
−75
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| name: Post build artifact links | ||
|
|
||
| # Runs in the context of the base (upstream) repo after the build workflow | ||
| # completes — this gives the GITHUB_TOKEN write access to PRs even when | ||
| # the build was triggered by a fork PR. | ||
| on: | ||
| workflow_run: | ||
| workflows: ["C/C++ CI"] | ||
| types: [completed] | ||
|
|
||
| permissions: | ||
| actions: read | ||
| pull-requests: write | ||
|
|
||
| jobs: | ||
| post-artifacts: | ||
| if: > | ||
| github.event.workflow_run.event == 'pull_request' && | ||
| github.event.workflow_run.conclusion == 'success' | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Comment artifact links on PR | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| const run = context.payload.workflow_run; | ||
| const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${run.id}`; | ||
|
|
||
| // Find associated PR number from the triggering workflow run | ||
| const prs = run.pull_requests; | ||
| if (!prs || prs.length === 0) { | ||
| // Fallback: search for open PR matching the head branch/SHA | ||
| const { data: pulls } = await github.rest.pulls.list({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| state: 'open', | ||
| head: `${run.head_repository.owner.login}:${run.head_branch}`, | ||
| }); | ||
| if (pulls.length === 0) { | ||
| console.log('No associated PR found, skipping.'); | ||
| return; | ||
| } | ||
| prs.push(pulls[0]); | ||
| } | ||
| const prNumber = prs[0].number; | ||
|
|
||
| // Fetch artifacts from the completed workflow run | ||
| const { data: { artifacts } } = await github.rest.actions.listWorkflowRunArtifacts({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| run_id: run.id, | ||
| }); | ||
|
|
||
| let rows = ''; | ||
| for (const a of artifacts) { | ||
| const dlUrl = `${runUrl}/artifacts/${a.id}`; | ||
| rows += `| ${a.name} | [${a.name}](${dlUrl}) | ${(a.size_in_bytes / 1024).toFixed(0)} KB |\n`; | ||
| } | ||
|
|
||
| if (!rows) { | ||
| rows = `| - | No artifacts found | - |\n`; | ||
| } | ||
|
|
||
| // Check for existing comment to update (upsert pattern) | ||
| const comments = await github.paginate( | ||
| github.rest.issues.listComments, | ||
| { | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: prNumber, | ||
| per_page: 100, | ||
| } | ||
| ); | ||
| const marker = '<!-- build-artifacts -->'; | ||
| const existing = comments.find(c => c.body.includes(marker)); | ||
|
|
||
| const body = [ | ||
| marker, | ||
| '### Build Artifacts', | ||
| '', | ||
| '| Platform | Download | Size |', | ||
| '|----------|----------|------|', | ||
| rows.trim(), | ||
| '', | ||
| `> [Full workflow run](${runUrl})`, | ||
| '', | ||
| `<sub>Updated by CI at ${new Date().toISOString()}</sub>`, | ||
| ].join('\n'); | ||
|
|
||
| if (existing) { | ||
| await github.rest.issues.updateComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| comment_id: existing.id, | ||
| body, | ||
| }); | ||
| console.log(`Updated comment ${existing.id} on PR #${prNumber}`); | ||
| } else { | ||
| await github.rest.issues.createComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: prNumber, | ||
| body, | ||
| }); | ||
| console.log(`Created new comment on PR #${prNumber}`); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const prs = run.pull_requests;is followed by a guard that allowsprsto be falsy (if (!prs || prs.length === 0)), but the fallback later callsprs.push(...). Ifrun.pull_requestsis everundefined/null, this will throw and prevent artifact comments from being posted. Initialize a local array (e.g.,let prs = run.pull_requests ?? []) and push into that (or assignprs = pulls).