Repository navigation
test: verify a single acknowledgement on docs PR 238 #1
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
| name: Verify the acknowledgement on docs PR 238 | |
| on: | |
| push: | |
| branches: [qa/cla-comment-pr-238] | |
| permissions: | |
| actions: read | |
| pull-requests: write | |
| concurrency: | |
| group: qa-cla-comment-pr-238 | |
| cancel-in-progress: false | |
| jobs: | |
| verify: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Run the reviewed acknowledgement twice for the same verified PR | |
| uses: actions/github-script@v8 | |
| with: | |
| script: | | |
| const assert = require("node:assert/strict"); | |
| const sha = "461751b2182538160372126de882322a4a0defad"; | |
| const target = {owner: "phcode-dev", repo: "docs", issue_number: 238}; | |
| const text = "## Thank You for making this pull request."; | |
| assert.equal(context.repo.owner, target.owner); | |
| assert.equal(context.repo.repo, target.repo); | |
| assert.equal(context.ref, "refs/heads/qa/cla-comment-pr-238"); | |
| const {data: run} = await github.rest.actions.getWorkflowRun({...context.repo, run_id: 46121494434}); | |
| const {data: pr} = await github.rest.pulls.get({...context.repo, pull_number: target.issue_number}); | |
| assert.equal(run.event, "pull_request"); | |
| assert.equal(run.path, ".github/workflows/verify_cla_signature_pr.yml"); | |
| assert.equal(run.conclusion, "success"); | |
| assert.equal(run.head_sha, sha); | |
| assert.ok(run.pull_requests.some(pull => pull.number === target.issue_number)); | |
| assert.equal(pr.state, "open"); | |
| assert.equal(pr.head.sha, sha); | |
| assert.equal(pr.head.ref, run.head_branch); | |
| assert.equal(pr.head.repo.id, run.head_repository.id); | |
| assert.equal(pr.base.repo.full_name, "phcode-dev/docs"); | |
| assert.equal(pr.base.ref, context.payload.repository.default_branch); | |
| context.payload.workflow_run = run; | |
| async function acknowledge(github) { | |
| const run = context.payload.workflow_run; | |
| const source = run.head_repository; | |
| if (!source || !run.head_branch) { | |
| core.info("The verified source branch is no longer available."); | |
| return; | |
| } | |
| const {data: pulls} = await github.rest.pulls.list({ | |
| ...context.repo, | |
| state: "open", | |
| base: context.payload.repository.default_branch, | |
| head: source.owner.login + ":" + run.head_branch | |
| }); | |
| const pull = pulls.find(pr => pr.head.repo?.id === source.id && pr.head.sha === run.head_sha); | |
| if (!pull) { | |
| core.info("The verified commit no longer heads an open pull request."); | |
| return; | |
| } | |
| const target = {...context.repo, issue_number: pull.number}; | |
| const body = "## Thank You for making this pull request."; | |
| const pages = github.paginate.iterator(github.rest.issues.listComments, {...target, per_page: 100}); | |
| for await (const {data: comments} of pages) { | |
| if (comments.some(comment => comment.user?.login === "github-actions[bot]" && comment.body?.trim() === body)) { | |
| core.info("This pull request already has an acknowledgement."); | |
| return; | |
| } | |
| } | |
| await github.rest.issues.createComment({...target, body}); | |
| } | |
| let writes = 0; | |
| const client = { | |
| paginate: github.paginate, | |
| rest: { | |
| pulls: {list: github.rest.pulls.list}, | |
| issues: { | |
| listComments: github.rest.issues.listComments, | |
| async createComment(args) { | |
| assert.deepEqual(args, {...target, body: text}); | |
| assert.equal(writes, 0, "Only one comment is authorized"); | |
| writes++; | |
| return github.rest.issues.createComment(args); | |
| } | |
| } | |
| } | |
| }; | |
| async function acknowledgements() { | |
| const comments = await github.paginate(github.rest.issues.listComments, {...target, per_page: 100}); | |
| return comments.filter(c => c.user?.login === "github-actions[bot]" && c.body?.trim() === text); | |
| } | |
| const before = await acknowledgements(); | |
| assert.ok(before.length <= 1); | |
| await acknowledge(client); | |
| const first = await acknowledgements(); | |
| assert.equal(first.length, 1); | |
| await acknowledge(client); | |
| const second = await acknowledgements(); | |
| assert.equal(second.length, 1); | |
| assert.equal(second[0].id, first[0].id); | |
| assert.equal(writes, before.length === 0 ? 1 : 0); | |
| core.info("Confirmed one acknowledgement and no duplicate on retry: " + second[0].html_url); | |
| await core.summary.addLink("Acknowledgement on docs PR 238", second[0].html_url).write(); |