Skip to content

Commit 8eadfd7

Browse files
ZhongRuoyuBo98
andcommitted
find-related-workflow-run-id: migrate to JS and add test
As part of #549. Co-authored-by: Bo Anderson <[email protected]>
1 parent f3fe7e5 commit 8eadfd7

File tree

3 files changed

+145
-39
lines changed

3 files changed

+145
-39
lines changed

find-related-workflow-run-id/action.yml

+7-39
Original file line numberDiff line numberDiff line change
@@ -15,43 +15,11 @@ inputs:
1515
description: Authentication token for `gh`
1616
required: false
1717
default: ${{ github.token }}
18+
outputs:
19+
workflow-run-id:
20+
description: >
21+
ID of the related workflow run.
22+
Also available as `${{ env.workflow_run_id }}`.
1823
runs:
19-
using: "composite"
20-
steps:
21-
- shell: bash
22-
id: result
23-
env:
24-
GH_TOKEN: ${{ inputs.token }}
25-
WORKFLOW_NAME: ${{ inputs.workflow-name }}
26-
WORKFLOW_RUN_URL: ${{ github.server_url }}/${{ inputs.repository }}/actions/runs/${{ inputs.run-id }}
27-
QUERY: >-
28-
query($url: URI!) {
29-
resource(url: $url) {
30-
... on WorkflowRun {
31-
checkSuite {
32-
commit {
33-
checkSuites(last: 100) {
34-
nodes {
35-
workflowRun {
36-
databaseId
37-
workflow {
38-
name
39-
}
40-
}
41-
}
42-
}
43-
}
44-
}
45-
}
46-
}
47-
}
48-
run: |
49-
run_id="$(
50-
gh api graphql \
51-
--field url="$WORKFLOW_RUN_URL" \
52-
--raw-field query="$QUERY" \
53-
--jq ".data.resource.checkSuite.commit.checkSuites.nodes |
54-
map(select(.workflowRun.workflow.name == \"$WORKFLOW_NAME\")) |
55-
last | .workflowRun.databaseId"
56-
)"
57-
echo "workflow_run_id=$run_id" >> "$GITHUB_ENV"
24+
using: node20
25+
main: main.mjs

find-related-workflow-run-id/main.mjs

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import core from "@actions/core";
2+
import github from "@actions/github";
3+
4+
async function main() {
5+
try {
6+
const runId = core.getInput("run-id", { required: true });
7+
const workflowName = core.getInput("workflow-name", { required: true });
8+
const repository = core.getInput("repository", { required: true });
9+
const token = core.getInput("token", { required: true });
10+
11+
const client = github.getOctokit(token);
12+
13+
const serverUrl = github.context.serverUrl;
14+
const runUrl = `${serverUrl}/${repository}/actions/runs/${runId}`;
15+
const response = await client.graphql(
16+
`
17+
query($runUrl: URI!) {
18+
resource(url: $runUrl) {
19+
... on WorkflowRun {
20+
checkSuite {
21+
commit {
22+
checkSuites(last: 100) {
23+
nodes {
24+
workflowRun {
25+
databaseId
26+
workflow {
27+
name
28+
}
29+
}
30+
}
31+
}
32+
}
33+
}
34+
}
35+
}
36+
}
37+
`,
38+
{ runUrl }
39+
);
40+
41+
const relatedRunId =
42+
response.resource.checkSuite.commit.checkSuites.nodes
43+
.reverse()
44+
.find(node => node.workflowRun?.workflow.name === workflowName)
45+
?.workflowRun.databaseId;
46+
47+
if (relatedRunId === undefined) {
48+
core.setFailed(`No related run found for workflow ${workflowName}`);
49+
return;
50+
}
51+
52+
core.setOutput("workflow-run-id", relatedRunId);
53+
core.exportVariable("workflow_run_id", relatedRunId);
54+
} catch (error) {
55+
core.setFailed(error.message);
56+
}
57+
}
58+
59+
await main();
+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import util from "node:util";
2+
3+
test("find-related-workflow-run-id", async () => {
4+
const GITHUB_SERVER_URL = "https://github.com";
5+
process.env.GITHUB_SERVER_URL = GITHUB_SERVER_URL;
6+
process.env.GITHUB_ENV = "/dev/null";
7+
8+
const mockPool = githubMockPool();
9+
10+
const runId = 12345;
11+
const workflowName = "Some workflow";
12+
const repository = "fake-owner/fake-repo";
13+
const token = "fake-token";
14+
15+
const runUrl = `${GITHUB_SERVER_URL}/${repository}/actions/runs/${runId}`;
16+
17+
mockInput("run-id", runId.toString());
18+
mockInput("workflow-name", workflowName);
19+
mockInput("repository", repository);
20+
mockInput("token", token);
21+
22+
mockPool.intercept({
23+
method: "POST",
24+
path: "/graphql",
25+
headers: {
26+
Authorization: `token ${token}`,
27+
},
28+
body: (body) => util.isDeepStrictEqual(JSON.parse(body), {
29+
query: `
30+
query($runUrl: URI!) {
31+
resource(url: $runUrl) {
32+
... on WorkflowRun {
33+
checkSuite {
34+
commit {
35+
checkSuites(last: 100) {
36+
nodes {
37+
workflowRun {
38+
databaseId
39+
workflow {
40+
name
41+
}
42+
}
43+
}
44+
}
45+
}
46+
}
47+
}
48+
}
49+
}
50+
`,
51+
variables: { runUrl },
52+
}),
53+
}).defaultReplyHeaders({
54+
"Content-Type": "application/json",
55+
}).reply(200, {
56+
data: {
57+
resource: {
58+
checkSuite: {
59+
commit: {
60+
checkSuites: {
61+
nodes: [
62+
{
63+
workflowRun: {
64+
databaseId: 123,
65+
workflow: {
66+
name: "Some workflow"
67+
}
68+
}
69+
}
70+
]
71+
}
72+
}
73+
}
74+
}
75+
}
76+
});
77+
78+
await loadMain();
79+
});

0 commit comments

Comments
 (0)