-
Notifications
You must be signed in to change notification settings - Fork 432
ci: make pkg.pr.new comment flow fork-safe #170
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
kongenpei
merged 5 commits into
larksuite:main
from
kongenpei:codex/pkg-pr-new-fork-safe-comment
Apr 1, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1347997
ci: make pkg.pr.new comment flow fork-safe
kongenpei d8261c3
ci: harden trusted comment workflow inputs
kongenpei 7678b5b
ci: skip comment steps when payload artifact is missing
kongenpei 5e3e156
ci: use artifact PR number when workflow_run pull_requests is empty
kongenpei 8c1969c
ci: allow PR comment workflow to write pull requests
kongenpei 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,149 @@ | ||
| name: PR Preview Package Comment | ||
|
|
||
| on: | ||
| workflow_run: | ||
| workflows: ["PR Preview Package"] | ||
| types: [completed] | ||
|
|
||
| permissions: | ||
| actions: read | ||
| contents: read | ||
| issues: write | ||
| pull-requests: write | ||
|
|
||
| jobs: | ||
| comment: | ||
| if: github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'pull_request' | ||
| runs-on: ubuntu-latest | ||
|
|
||
| steps: | ||
| - name: Check comment payload artifact | ||
| id: payload | ||
| uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 | ||
| with: | ||
| script: | | ||
| const runId = context.payload.workflow_run?.id; | ||
| const { data } = await github.rest.actions.listWorkflowRunArtifacts({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| run_id: runId, | ||
| per_page: 100, | ||
| }); | ||
| const found = Boolean( | ||
| data.artifacts?.some((artifact) => artifact.name === "pkg-pr-new-comment-payload") | ||
| ); | ||
| core.setOutput("found", found ? "true" : "false"); | ||
| if (!found) { | ||
| core.notice("No comment payload artifact found for this run; skipping comment."); | ||
| } | ||
|
|
||
| - name: Download comment payload | ||
| if: steps.payload.outputs.found == 'true' | ||
| uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8 | ||
| with: | ||
| name: pkg-pr-new-comment-payload | ||
| repository: ${{ github.repository }} | ||
| run-id: ${{ github.event.workflow_run.id }} | ||
| github-token: ${{ github.token }} | ||
|
|
||
| - name: Comment install command | ||
| if: steps.payload.outputs.found == 'true' | ||
| uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 | ||
| with: | ||
| script: | | ||
| const fs = require("fs"); | ||
| const payload = JSON.parse(fs.readFileSync("pkg-pr-new-comment-payload.json", "utf8")); | ||
| const url = payload?.url; | ||
| const payloadPr = payload?.pr; | ||
| const sourceRepo = payload?.sourceRepo; | ||
| const sourceBranch = payload?.sourceBranch; | ||
| if (!Number.isInteger(payloadPr)) { | ||
| throw new Error(`Invalid PR number in artifact payload: ${payloadPr}`); | ||
| } | ||
| if (payloadPr <= 0) { | ||
| throw new Error(`Invalid PR number in artifact payload: ${payloadPr}`); | ||
| } | ||
| const issueNumber = payloadPr; | ||
| const runPrNumber = context.payload.workflow_run?.pull_requests?.[0]?.number; | ||
| if (Number.isInteger(runPrNumber) && runPrNumber !== issueNumber) { | ||
| throw new Error( | ||
| `PR number mismatch between workflow_run (${runPrNumber}) and artifact payload (${issueNumber})`, | ||
| ); | ||
| } | ||
|
|
||
| if (typeof url !== "string" || url.trim() !== url || /[\u0000-\u001F\u007F]/.test(url)) { | ||
| throw new Error(`Invalid package URL in payload: ${url}`); | ||
| } | ||
| let parsedUrl; | ||
| try { | ||
| parsedUrl = new URL(url); | ||
| } catch { | ||
| throw new Error(`Invalid package URL in payload: ${url}`); | ||
| } | ||
| if (parsedUrl.protocol !== "https:" || parsedUrl.hostname !== "pkg.pr.new") { | ||
| throw new Error(`Invalid package URL in payload: ${url}`); | ||
| } | ||
|
|
||
| const safeRepoPattern = /^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/; | ||
| const safeBranchPattern = /^[A-Za-z0-9._\/-]+$/; | ||
| const hasSkillSource = | ||
| typeof sourceRepo === "string" && | ||
| typeof sourceBranch === "string" && | ||
| safeRepoPattern.test(sourceRepo) && | ||
| safeBranchPattern.test(sourceBranch); | ||
| const skillSection = hasSkillSource | ||
| ? [ | ||
| "", | ||
| "### 🧩 Skill update", | ||
| "", | ||
| "```bash", | ||
| `npx skills add ${sourceRepo}#${sourceBranch} -y -g`, | ||
greptile-apps[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "```", | ||
| ] | ||
| : [ | ||
| "", | ||
| "### 🧩 Skill update", | ||
| "", | ||
| "_Unavailable for this PR because source repo/branch metadata is missing._", | ||
| ]; | ||
|
|
||
| const body = [ | ||
| "<!-- pkg-pr-new-install-guide -->", | ||
| "## 🚀 PR Preview Install Guide", | ||
| "", | ||
| "### 🧰 CLI update", | ||
| "", | ||
| "```bash", | ||
| `npm i -g ${url}`, | ||
| "```", | ||
| ...skillSection, | ||
| ].join("\n"); | ||
|
|
||
| const comments = await github.paginate(github.rest.issues.listComments, { | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: issueNumber, | ||
| per_page: 100, | ||
| }); | ||
|
|
||
| const existing = comments.find((comment) => | ||
| comment.user?.login === "github-actions[bot]" && | ||
| typeof comment.body === "string" && | ||
| comment.body.includes("<!-- pkg-pr-new-install-guide -->") | ||
| ); | ||
|
|
||
| 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: issueNumber, | ||
| body, | ||
| }); | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.