From 040f0b44e912fa490546d62db0f2ac6b67b34604 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Sat, 15 Aug 2026 14:29:01 +0800 Subject: [PATCH 1/4] ci: replace third-party comment action with local one --- .../actions/comment-pull-request/action.yml | 85 +++++++++++++++++++ .github/workflows/leave-comment.yml | 3 +- 2 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 .github/actions/comment-pull-request/action.yml diff --git a/.github/actions/comment-pull-request/action.yml b/.github/actions/comment-pull-request/action.yml new file mode 100644 index 00000000..1f52ae90 --- /dev/null +++ b/.github/actions/comment-pull-request/action.yml @@ -0,0 +1,85 @@ +name: Comment on Pull Request +description: Create or update a comment on a pull request, optionally keyed by a tag + +inputs: + message: + description: Comment body + required: false + comment-tag: + description: A tag embedded in the comment that is used to find and update an existing comment instead of creating duplicates + required: false + pr-number: + description: Number of the pull request to comment on + required: false + default: ${{ github.event.pull_request.number || github.event.issue.number || github.event.workflow_run.pull_requests[0].number }} + github-token: + description: GitHub token used to authenticate with the API + required: false + default: ${{ github.token }} + +runs: + using: composite + steps: + - name: Create or update comment + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9 + env: + MESSAGE: ${{ inputs.message }} + COMMENT_TAG: ${{ inputs.comment-tag }} + PR_NUMBER: ${{ inputs.pr-number }} + with: + github-token: ${{ inputs.github-token }} + script: | + const message = process.env.MESSAGE; + const commentTag = process.env.COMMENT_TAG || ''; + const prNumber = Number(process.env.PR_NUMBER); + + if (!prNumber) { + core.setFailed('No pull request number provided (pr-number input) and none could be inferred from context.'); + return; + } + if (!message) { + core.setFailed("The 'message' input must be provided."); + return; + } + + const { owner, repo } = context.repo; + + // Keep the same marker format as the previously used + // thollander/actions-comment-pull-request action so existing comments + // are updated instead of duplicated. + const tagPattern = commentTag + ? `` + : ''; + const body = tagPattern ? `${message}\n${tagPattern}` : message; + + let commentId; + if (tagPattern) { + const comments = await github.paginate(github.rest.issues.listComments, { + owner, + repo, + issue_number: prNumber, + per_page: 100, + }); + const existing = comments.find((comment) => comment.body.includes(tagPattern)); + if (existing) { + commentId = existing.id; + } + } + + if (commentId) { + core.info(`Updating comment ${commentId} on PR #${prNumber}`); + await github.rest.issues.updateComment({ + owner, + repo, + comment_id: commentId, + body, + }); + } else { + core.info(`Creating comment on PR #${prNumber}`); + await github.rest.issues.createComment({ + owner, + repo, + issue_number: prNumber, + body, + }); + } diff --git a/.github/workflows/leave-comment.yml b/.github/workflows/leave-comment.yml index 178fd7a0..a7d74b69 100644 --- a/.github/workflows/leave-comment.yml +++ b/.github/workflows/leave-comment.yml @@ -42,8 +42,7 @@ jobs: - name: Add Comment to PR if: steps.combine.outputs.combined - uses: thollander/actions-comment-pull-request@24bffb9b452ba05a4f3f77933840a6a841d1b32b # v3.0.1 + uses: ./.github/actions/comment-pull-request with: comment-tag: compared message: ${{ steps.combine.outputs.combined }} - pr-number: ${{ github.event.workflow_run.pull_requests[0].number }} From 3ffa8bbc2a33fb75e433323fbb92cfb6a6175161 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Sat, 15 Aug 2026 17:02:53 +0800 Subject: [PATCH 2/4] fix: update --- .github/workflows/leave-comment.yml | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/.github/workflows/leave-comment.yml b/.github/workflows/leave-comment.yml index a7d74b69..b4328a25 100644 --- a/.github/workflows/leave-comment.yml +++ b/.github/workflows/leave-comment.yml @@ -40,9 +40,26 @@ jobs: echo "EOF" } >> "$GITHUB_OUTPUT" - - name: Add Comment to PR + # `github.event.workflow_run.pull_requests` is only populated for PRs from + # branches in this repository; for fork PRs it is empty, so we resolve the + # PR number from the head repository and branch instead. + - name: Resolve PR number + id: pr if: steps.combine.outputs.combined + env: + GH_TOKEN: ${{ github.token }} + HEAD_REPO: ${{ github.event.workflow_run.head_repository.login }} + HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }} + run: | + PR_NUMBER=$(gh api \ + "repos/${GITHUB_REPOSITORY}/pulls?head=${HEAD_REPO}:${HEAD_BRANCH}" \ + --jq '[.[] | select(.state == "open")][0].number // .[0].number // empty') + echo "number=${PR_NUMBER:-}" >> "$GITHUB_OUTPUT" + + - name: Add Comment to PR + if: steps.combine.outputs.combined && steps.pr.outputs.number uses: ./.github/actions/comment-pull-request with: comment-tag: compared message: ${{ steps.combine.outputs.combined }} + pr-number: ${{ steps.pr.outputs.number }} From dc9c2f0e6462d4ece8e07ffaaf90c702c667bc16 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Sat, 15 Aug 2026 23:17:36 +0800 Subject: [PATCH 3/4] ci: update --- .../actions/comment-pull-request/action.yml | 85 ------------------- .github/workflows/leave-comment.yml | 2 +- 2 files changed, 1 insertion(+), 86 deletions(-) delete mode 100644 .github/actions/comment-pull-request/action.yml diff --git a/.github/actions/comment-pull-request/action.yml b/.github/actions/comment-pull-request/action.yml deleted file mode 100644 index 1f52ae90..00000000 --- a/.github/actions/comment-pull-request/action.yml +++ /dev/null @@ -1,85 +0,0 @@ -name: Comment on Pull Request -description: Create or update a comment on a pull request, optionally keyed by a tag - -inputs: - message: - description: Comment body - required: false - comment-tag: - description: A tag embedded in the comment that is used to find and update an existing comment instead of creating duplicates - required: false - pr-number: - description: Number of the pull request to comment on - required: false - default: ${{ github.event.pull_request.number || github.event.issue.number || github.event.workflow_run.pull_requests[0].number }} - github-token: - description: GitHub token used to authenticate with the API - required: false - default: ${{ github.token }} - -runs: - using: composite - steps: - - name: Create or update comment - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9 - env: - MESSAGE: ${{ inputs.message }} - COMMENT_TAG: ${{ inputs.comment-tag }} - PR_NUMBER: ${{ inputs.pr-number }} - with: - github-token: ${{ inputs.github-token }} - script: | - const message = process.env.MESSAGE; - const commentTag = process.env.COMMENT_TAG || ''; - const prNumber = Number(process.env.PR_NUMBER); - - if (!prNumber) { - core.setFailed('No pull request number provided (pr-number input) and none could be inferred from context.'); - return; - } - if (!message) { - core.setFailed("The 'message' input must be provided."); - return; - } - - const { owner, repo } = context.repo; - - // Keep the same marker format as the previously used - // thollander/actions-comment-pull-request action so existing comments - // are updated instead of duplicated. - const tagPattern = commentTag - ? `` - : ''; - const body = tagPattern ? `${message}\n${tagPattern}` : message; - - let commentId; - if (tagPattern) { - const comments = await github.paginate(github.rest.issues.listComments, { - owner, - repo, - issue_number: prNumber, - per_page: 100, - }); - const existing = comments.find((comment) => comment.body.includes(tagPattern)); - if (existing) { - commentId = existing.id; - } - } - - if (commentId) { - core.info(`Updating comment ${commentId} on PR #${prNumber}`); - await github.rest.issues.updateComment({ - owner, - repo, - comment_id: commentId, - body, - }); - } else { - core.info(`Creating comment on PR #${prNumber}`); - await github.rest.issues.createComment({ - owner, - repo, - issue_number: prNumber, - body, - }); - } diff --git a/.github/workflows/leave-comment.yml b/.github/workflows/leave-comment.yml index b4328a25..3f30b970 100644 --- a/.github/workflows/leave-comment.yml +++ b/.github/workflows/leave-comment.yml @@ -58,7 +58,7 @@ jobs: - name: Add Comment to PR if: steps.combine.outputs.combined && steps.pr.outputs.number - uses: ./.github/actions/comment-pull-request + uses: thollander/actions-comment-pull-request@24bffb9b452ba05a4f3f77933840a6a841d1b32b # v3.0.1 with: comment-tag: compared message: ${{ steps.combine.outputs.combined }} From 5cfcc33cbd2440c40aeb811c69c67b6e12889d3f Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Sat, 15 Aug 2026 23:31:57 +0800 Subject: [PATCH 4/4] ci: update --- .github/workflows/leave-comment.yml | 53 +++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/.github/workflows/leave-comment.yml b/.github/workflows/leave-comment.yml index 3f30b970..3bec6069 100644 --- a/.github/workflows/leave-comment.yml +++ b/.github/workflows/leave-comment.yml @@ -8,12 +8,18 @@ on: permissions: contents: read actions: read - pull-requests: write + +concurrency: + group: ${{ github.workflow }}-${{ github.event.workflow_run.id }} + cancel-in-progress: true jobs: aggregate: name: Aggregate Comparison Results runs-on: ubuntu-latest + permissions: + pull-requests: write + steps: - name: Harden Runner uses: step-security/harden-runner@bf7454d06d71f1098171f2acdf0cd4708d7b5920 # v2.20.0 @@ -40,24 +46,41 @@ jobs: echo "EOF" } >> "$GITHUB_OUTPUT" - # `github.event.workflow_run.pull_requests` is only populated for PRs from - # branches in this repository; for fork PRs it is empty, so we resolve the - # PR number from the head repository and branch instead. - - name: Resolve PR number + - name: Resolve Pull Request Number id: pr if: steps.combine.outputs.combined - env: - GH_TOKEN: ${{ github.token }} - HEAD_REPO: ${{ github.event.workflow_run.head_repository.login }} - HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }} - run: | - PR_NUMBER=$(gh api \ - "repos/${GITHUB_REPOSITORY}/pulls?head=${HEAD_REPO}:${HEAD_BRANCH}" \ - --jq '[.[] | select(.state == "open")][0].number // .[0].number // empty') - echo "number=${PR_NUMBER:-}" >> "$GITHUB_OUTPUT" + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 + with: + script: | + const run = context.payload.workflow_run; + + // 1. For same-repo Pull Requests the run is already linked to its PR(s). + if (run.pull_requests && run.pull_requests.length) { + core.setOutput('number', run.pull_requests[0].number); + return; + } + + // 2. For forks that list is empty, so find the open Pull Request who has the + // correct branch information + const match = await github.rest.pulls.list({ + owner: context.repo.owner, + repo: context.repo.repo, + state: 'open', + head: `${run.head_repository.owner.login}:${run.head_branch}`, + sort: 'updated', + direction: 'desc', + per_page: 1, + }).then(r => r.data[0]); + + if (!match) { + core.info(`No open pull request found for HEAD ${run.head_sha}`); + return; + } + + core.setOutput('number', match.number); - name: Add Comment to PR - if: steps.combine.outputs.combined && steps.pr.outputs.number + if: steps.combine.outputs.combined && steps.pr.outputs.number != '' uses: thollander/actions-comment-pull-request@24bffb9b452ba05a4f3f77933840a6a841d1b32b # v3.0.1 with: comment-tag: compared