From 8eadfd792142f8aedb98e7230010320c0e2d1b6b Mon Sep 17 00:00:00 2001 From: Ruoyu Zhong Date: Sat, 21 Sep 2024 21:49:46 +0800 Subject: [PATCH] find-related-workflow-run-id: migrate to JS and add test As part of #549. Co-authored-by: Bo Anderson --- find-related-workflow-run-id/action.yml | 46 ++----------- find-related-workflow-run-id/main.mjs | 59 ++++++++++++++++ find-related-workflow-run-id/main.test.mjs | 79 ++++++++++++++++++++++ 3 files changed, 145 insertions(+), 39 deletions(-) create mode 100644 find-related-workflow-run-id/main.mjs create mode 100644 find-related-workflow-run-id/main.test.mjs diff --git a/find-related-workflow-run-id/action.yml b/find-related-workflow-run-id/action.yml index 158d5c68..9f73b7c0 100644 --- a/find-related-workflow-run-id/action.yml +++ b/find-related-workflow-run-id/action.yml @@ -15,43 +15,11 @@ inputs: description: Authentication token for `gh` required: false default: ${{ github.token }} +outputs: + workflow-run-id: + description: > + ID of the related workflow run. + Also available as `${{ env.workflow_run_id }}`. runs: - using: "composite" - steps: - - shell: bash - id: result - env: - GH_TOKEN: ${{ inputs.token }} - WORKFLOW_NAME: ${{ inputs.workflow-name }} - WORKFLOW_RUN_URL: ${{ github.server_url }}/${{ inputs.repository }}/actions/runs/${{ inputs.run-id }} - QUERY: >- - query($url: URI!) { - resource(url: $url) { - ... on WorkflowRun { - checkSuite { - commit { - checkSuites(last: 100) { - nodes { - workflowRun { - databaseId - workflow { - name - } - } - } - } - } - } - } - } - } - run: | - run_id="$( - gh api graphql \ - --field url="$WORKFLOW_RUN_URL" \ - --raw-field query="$QUERY" \ - --jq ".data.resource.checkSuite.commit.checkSuites.nodes | - map(select(.workflowRun.workflow.name == \"$WORKFLOW_NAME\")) | - last | .workflowRun.databaseId" - )" - echo "workflow_run_id=$run_id" >> "$GITHUB_ENV" + using: node20 + main: main.mjs diff --git a/find-related-workflow-run-id/main.mjs b/find-related-workflow-run-id/main.mjs new file mode 100644 index 00000000..41f8f907 --- /dev/null +++ b/find-related-workflow-run-id/main.mjs @@ -0,0 +1,59 @@ +import core from "@actions/core"; +import github from "@actions/github"; + +async function main() { + try { + const runId = core.getInput("run-id", { required: true }); + const workflowName = core.getInput("workflow-name", { required: true }); + const repository = core.getInput("repository", { required: true }); + const token = core.getInput("token", { required: true }); + + const client = github.getOctokit(token); + + const serverUrl = github.context.serverUrl; + const runUrl = `${serverUrl}/${repository}/actions/runs/${runId}`; + const response = await client.graphql( + ` + query($runUrl: URI!) { + resource(url: $runUrl) { + ... on WorkflowRun { + checkSuite { + commit { + checkSuites(last: 100) { + nodes { + workflowRun { + databaseId + workflow { + name + } + } + } + } + } + } + } + } + } + `, + { runUrl } + ); + + const relatedRunId = + response.resource.checkSuite.commit.checkSuites.nodes + .reverse() + .find(node => node.workflowRun?.workflow.name === workflowName) + ?.workflowRun.databaseId; + + if (relatedRunId === undefined) { + core.setFailed(`No related run found for workflow ${workflowName}`); + return; + } + + core.setOutput("workflow-run-id", relatedRunId); + core.exportVariable("workflow_run_id", relatedRunId); + } catch (error) { + core.setFailed(error.message); + } +} + +await main(); diff --git a/find-related-workflow-run-id/main.test.mjs b/find-related-workflow-run-id/main.test.mjs new file mode 100644 index 00000000..b6e0a393 --- /dev/null +++ b/find-related-workflow-run-id/main.test.mjs @@ -0,0 +1,79 @@ +import util from "node:util"; + +test("find-related-workflow-run-id", async () => { + const GITHUB_SERVER_URL = "https://github.com"; + process.env.GITHUB_SERVER_URL = GITHUB_SERVER_URL; + process.env.GITHUB_ENV = "/dev/null"; + + const mockPool = githubMockPool(); + + const runId = 12345; + const workflowName = "Some workflow"; + const repository = "fake-owner/fake-repo"; + const token = "fake-token"; + + const runUrl = `${GITHUB_SERVER_URL}/${repository}/actions/runs/${runId}`; + + mockInput("run-id", runId.toString()); + mockInput("workflow-name", workflowName); + mockInput("repository", repository); + mockInput("token", token); + + mockPool.intercept({ + method: "POST", + path: "/graphql", + headers: { + Authorization: `token ${token}`, + }, + body: (body) => util.isDeepStrictEqual(JSON.parse(body), { + query: ` + query($runUrl: URI!) { + resource(url: $runUrl) { + ... on WorkflowRun { + checkSuite { + commit { + checkSuites(last: 100) { + nodes { + workflowRun { + databaseId + workflow { + name + } + } + } + } + } + } + } + } + } + `, + variables: { runUrl }, + }), + }).defaultReplyHeaders({ + "Content-Type": "application/json", + }).reply(200, { + data: { + resource: { + checkSuite: { + commit: { + checkSuites: { + nodes: [ + { + workflowRun: { + databaseId: 123, + workflow: { + name: "Some workflow" + } + } + } + ] + } + } + } + } + } + }); + + await loadMain(); +});