-
-
Notifications
You must be signed in to change notification settings - Fork 39
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.
- Loading branch information
1 parent
f3fe7e5
commit 08912fa
Showing
3 changed files
with
147 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,61 @@ | ||
import fs from "fs"; | ||
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); | ||
await fs.promises.appendFile(process.env.GITHUB_ENV, | ||
`workflow_run_id=${relatedRunId}\n`); | ||
} 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(); | ||
}); |