Skip to content

Commit

Permalink
find-related-workflow-run-id: migrate to JS and add test
Browse files Browse the repository at this point in the history
As part of #549.

Co-authored-by: Bo Anderson <[email protected]>
  • Loading branch information
ZhongRuoyu and Bo98 committed Sep 21, 2024
1 parent f3fe7e5 commit 8eadfd7
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 39 deletions.
46 changes: 7 additions & 39 deletions find-related-workflow-run-id/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
59 changes: 59 additions & 0 deletions find-related-workflow-run-id/main.mjs
Original file line number Diff line number Diff line change
@@ -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;
}

Check warning on line 50 in find-related-workflow-run-id/main.mjs

View check run for this annotation

Codecov / codecov/patch

find-related-workflow-run-id/main.mjs#L48-L50

Added lines #L48 - L50 were not covered by tests

core.setOutput("workflow-run-id", relatedRunId);
core.exportVariable("workflow_run_id", relatedRunId);
} catch (error) {
core.setFailed(error.message);
}

Check warning on line 56 in find-related-workflow-run-id/main.mjs

View check run for this annotation

Codecov / codecov/patch

find-related-workflow-run-id/main.mjs#L55-L56

Added lines #L55 - L56 were not covered by tests
}

await main();
79 changes: 79 additions & 0 deletions find-related-workflow-run-id/main.test.mjs
Original file line number Diff line number Diff line change
@@ -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();
});

0 comments on commit 8eadfd7

Please sign in to comment.