Skip to content

Commit 53f62f2

Browse files
committed
feat: Log the remote run URL when found.
1 parent 09c8b51 commit 53f62f2

File tree

5 files changed

+104
-4
lines changed

5 files changed

+104
-4
lines changed

dist/index.js

Lines changed: 27 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/api.spec.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
getWorkflowId,
88
getWorkflowRunIds,
99
getWorkflowRunJobSteps,
10+
getWorkflowRunUrl,
1011
init,
1112
retryOrDie,
1213
} from "./api";
@@ -22,6 +23,9 @@ const mockOctokit = {
2223
createWorkflowDispatch: async (_req?: any): Promise<MockResponse> => {
2324
throw new Error("Should be mocked");
2425
},
26+
getWorkflowRun: async (_req?: any): Promise<MockResponse> => {
27+
throw new Error("Should be mocked");
28+
},
2529
listRepoWorkflows: async (_req?: any): Promise<MockResponse> => {
2630
throw new Error("Should be mocked");
2731
},
@@ -360,6 +364,37 @@ describe("API", () => {
360364
});
361365
});
362366

367+
describe("getWorkflowRunUrl", () => {
368+
it("should return the workflow run state for a given run ID", async () => {
369+
const mockData = {
370+
html_url: "master sword",
371+
};
372+
jest.spyOn(mockOctokit.rest.actions, "getWorkflowRun").mockReturnValue(
373+
Promise.resolve({
374+
data: mockData,
375+
status: 200,
376+
})
377+
);
378+
379+
const url = await getWorkflowRunUrl(123456);
380+
expect(url).toStrictEqual(mockData.html_url);
381+
});
382+
383+
it("should throw if a non-200 status is returned", async () => {
384+
const errorStatus = 401;
385+
jest.spyOn(mockOctokit.rest.actions, "getWorkflowRun").mockReturnValue(
386+
Promise.resolve({
387+
data: undefined,
388+
status: errorStatus,
389+
})
390+
);
391+
392+
await expect(getWorkflowRunUrl(0)).rejects.toThrow(
393+
`Failed to get Workflow Run state, expected 200 but received ${errorStatus}`
394+
);
395+
});
396+
});
397+
363398
describe("retryOrDie", () => {
364399
it("should return a populated array", async () => {
365400
const attempt = async () => {

src/api.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,40 @@ export async function getWorkflowId(workflowFilename: string): Promise<number> {
8989
}
9090
}
9191

92+
export async function getWorkflowRunUrl(runId: number): Promise<string> {
93+
try {
94+
// https://docs.github.com/en/rest/reference/actions#get-a-workflow-run
95+
const response = await octokit.rest.actions.getWorkflowRun({
96+
owner: config.owner,
97+
repo: config.repo,
98+
run_id: runId,
99+
});
100+
101+
if (response.status !== 200) {
102+
throw new Error(
103+
`Failed to get Workflow Run state, expected 200 but received ${response.status}`
104+
);
105+
}
106+
107+
core.debug(
108+
`Fetched Run:\n` +
109+
` Repository: ${config.owner}/${config.repo}\n` +
110+
` Run ID: ${runId}\n` +
111+
` URL: ${response.data.html_url}`
112+
);
113+
114+
return response.data.html_url;
115+
} catch (error) {
116+
if (error instanceof Error) {
117+
core.error(
118+
`getWorkflowRunUrl: An unexpected error has occurred: ${error.message}`
119+
);
120+
error.stack && core.debug(error.stack);
121+
}
122+
throw error;
123+
}
124+
}
125+
92126
export async function getWorkflowRunIds(workflowId: number): Promise<number[]> {
93127
try {
94128
const branchName = getBranchName(config.ref);

src/main.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ async function run(): Promise<void> {
6060

6161
for (const step of steps) {
6262
if (idRegex.test(step)) {
63-
core.info(`Successfully identified remote Run ID: ${id}`);
63+
const url = await api.getWorkflowRunUrl(id);
64+
core.info(
65+
"Successfully identified remote Run:\n" +
66+
` Run ID: ${id}\n` +
67+
` URL: ${url}`
68+
);
6469
core.setOutput(ActionOutputs.runId, id);
6570
return;
6671
}

0 commit comments

Comments
 (0)