Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

find-related-workflow-run-id: migrate to JS and add test #590

Merged
merged 1 commit into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});