-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
find-related-workflow-run-id: migrate to JS and add test
As part of #549. Co-authored-by: Bo Anderson <[email protected]>
- Loading branch information
1 parent
f3fe7e5
commit 8eadfd7
Showing
3 changed files
with
145 additions
and
39 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
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; | ||
} | ||
|
||
core.setOutput("workflow-run-id", relatedRunId); | ||
core.exportVariable("workflow_run_id", relatedRunId); | ||
} catch (error) { | ||
core.setFailed(error.message); | ||
} | ||
} | ||
|
||
await main(); |
This file contains 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
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(); | ||
}); |