Skip to content

Commit d24aec3

Browse files
authored
fix(get-latest-workflow-artifact): suggested fixes (#1026)
... from #1022
1 parent cfaf759 commit d24aec3

File tree

3 files changed

+50
-29
lines changed

3 files changed

+50
-29
lines changed

actions/get-latest-workflow-artifact/bun.lock

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"@actions/github": "^6.0.1",
1010
},
1111
"devDependencies": {
12+
"@octokit/openapi-types": "^25.1.0",
1213
"@types/bun": "latest",
1314
"@types/node": "^22.15.18",
1415
},
@@ -63,7 +64,7 @@
6364

6465
"@octokit/graphql": ["@octokit/[email protected]", "", { "dependencies": { "@octokit/request": "^5.6.0", "@octokit/types": "^6.0.3", "universal-user-agent": "^6.0.0" } }, "sha512-0gv+qLSBLKF0z8TKaSKTsS39scVKF9dbMxJpj3U0vC7wjNWFuIpL/z76Qe2fiuCbDRcJSavkXsVtMS6/dtQQsg=="],
6566

66-
"@octokit/openapi-types": ["@octokit/openapi-types@12.11.0", "", {}, "sha512-VsXyi8peyRq9PqIz/tpqiL2w3w80OgVMwBHltTml3LmVvXiphgeqmY9mvBw9Wu7e0QWk/fqD37ux8yP5uVekyQ=="],
67+
"@octokit/openapi-types": ["@octokit/openapi-types@25.1.0", "", {}, "sha512-idsIggNXUKkk0+BExUn1dQ92sfysJrje03Q0bv0e+KPLrvyqZF8MnBpFz8UNfYDwB3Ie7Z0TByjWfzxt7vseaA=="],
6768

6869
"@octokit/plugin-paginate-rest": ["@octokit/[email protected]", "", { "dependencies": { "@octokit/types": "^12.6.0" }, "peerDependencies": { "@octokit/core": "5" } }, "sha512-u3KYkGF7GcZnSD/3UP0S7K5XUFT2FkOQdcfXZGZQPGv3lm4F2Xbf71lvjldr8c1H3nNbF+33cLEkWYbokGWqiQ=="],
6970

@@ -315,6 +316,8 @@
315316

316317
"@octokit/request-error/@octokit/types": ["@octokit/[email protected]", "", { "dependencies": { "@octokit/openapi-types": "^24.2.0" } }, "sha512-ifLaO34EbbPj0Xgro4G5lP5asESjwHracYJvVaPIyXMuiuXLlhic3S47cBdTb+jfODkTE5YtGCLt3Ay3+J97sA=="],
317318

319+
"@octokit/types/@octokit/openapi-types": ["@octokit/[email protected]", "", {}, "sha512-VsXyi8peyRq9PqIz/tpqiL2w3w80OgVMwBHltTml3LmVvXiphgeqmY9mvBw9Wu7e0QWk/fqD37ux8yP5uVekyQ=="],
320+
318321
"glob/minimatch": ["[email protected]", "", { "dependencies": { "brace-expansion": "^2.0.1" } }, "sha512-G6T0ZX48xgozx7587koeX9Ys2NYy6Gmv//P89sEte9V9whIapMNF4idKxnW2QtCcLiTWlb/wfCabAtAFWhhBow=="],
319322

320323
"lazystream/readable-stream": ["[email protected]", "", { "dependencies": { "core-util-is": "~1.0.0", "inherits": "~2.0.3", "isarray": "~1.0.0", "process-nextick-args": "~2.0.0", "safe-buffer": "~5.1.1", "string_decoder": "~1.1.1", "util-deprecate": "~1.0.1" } }, "sha512-8p0AUk4XODgIewSi0l8Epjs+EVnWiK7NoDIEGU0HhE7+ZyY8D1IMY7odu5lRrFXGg71L15KG8QrPmum45RTtdA=="],

actions/get-latest-workflow-artifact/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"@actions/github": "^6.0.1"
1414
},
1515
"devDependencies": {
16+
"@octokit/openapi-types": "^25.1.0",
1617
"@types/bun": "latest",
1718
"@types/node": "^22.15.18"
1819
},

actions/get-latest-workflow-artifact/src/main.ts

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
import * as core from "@actions/core";
2-
import * as github from "@actions/github";
2+
import { getOctokit } from "@actions/github";
33
import { DefaultArtifactClient } from "@actions/artifact";
44
import * as filepath from "node:path";
55

6+
import { components } from "@octokit/openapi-types";
7+
8+
type WorkflowRunStatus = "in_progress" | "completed";
9+
type Artifact = components["schemas"]["artifact"];
10+
type GitHubClient = ReturnType<typeof getOctokit>;
11+
type WorkflowRun = components["schemas"]["workflow-run"];
12+
type GetLatestArtifactResponse = {
13+
artifact: Artifact;
14+
run: WorkflowRun;
15+
} | null;
16+
617
async function main() {
718
const ghToken = core.getInput("github-token", { required: true });
819
const workflowId = core.getInput("workflow-id", { required: true });
@@ -24,7 +35,7 @@ async function main() {
2435

2536
path = filepath.join(path, artifactName);
2637

27-
const client = github.getOctokit(ghToken);
38+
const client = getOctokit(ghToken);
2839

2940
const { data } = await client.rest.pulls.get({
3041
owner: repoOwner,
@@ -36,7 +47,7 @@ async function main() {
3647

3748
// Now let's get all workflows associated with this sha:
3849
// We start with in-progress ones if those are to be considered.
39-
let found = null;
50+
let found: GetLatestArtifactResponse = null;
4051

4152
if (considerInProgress) {
4253
found = await getLatestArtifact(
@@ -84,45 +95,51 @@ async function main() {
8495
}
8596

8697
async function getLatestArtifact(
87-
client,
88-
repoOwner,
89-
repoName,
90-
workflowId,
98+
client: GitHubClient,
99+
repoOwner: string,
100+
repoName: string,
101+
workflowId: string,
91102
headSha: string,
92-
status: string,
103+
status: WorkflowRunStatus,
93104
artifactName: string,
94-
): Promise<{ artifact: any; run: any } | null> {
95-
const { data } = await client.rest.actions.listWorkflowRuns({
96-
repo: repoName,
97-
owner: repoOwner,
98-
head_sha: headSha,
99-
workflow_id: workflowId,
100-
status: status,
101-
});
102-
if (data.workflow_runs.length === 0) {
105+
): Promise<GetLatestArtifactResponse> {
106+
const {
107+
data: { workflow_runs: workflowRuns },
108+
}: { data: { workflow_runs: WorkflowRun[] } } =
109+
await client.rest.actions.listWorkflowRuns({
110+
repo: repoName,
111+
owner: repoOwner,
112+
head_sha: headSha,
113+
workflow_id: workflowId,
114+
status: status,
115+
});
116+
if (workflowRuns.length === 0) {
103117
console.log(`No ${status} runs found`);
104118
return null;
105119
}
106-
const run = data.workflow_runs[0];
107-
// Since these are pending workflows, the artifact might not be there yet. For this scenario, let's try this a couple of times:
108-
for (let attempt = 0; attempt < 5; attempt++) {
109-
const { data: artifacts } =
120+
const run = workflowRuns[0];
121+
// For in-progress workflows the artifact might not be available yet. For this scenario we do a bit of retrying:
122+
const maxAttempts = status === "in_progress" ? 5 : 1;
123+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
124+
const {
125+
data: { artifacts },
126+
}: { data: { artifacts: Artifact[] } } =
110127
await client.rest.actions.listWorkflowRunArtifacts({
111128
owner: repoOwner,
112129
repo: repoName,
113130
run_id: run.id,
114131
});
115-
const artifact = artifacts.artifacts.find(
116-
(art) => art.name == artifactName,
117-
);
132+
const artifact = artifacts.find((art) => art.name == artifactName);
118133
if (artifact) {
119134
console.log(`Found ${status} artifact`);
120135
return { artifact, run };
121136
}
122-
console.log("No artifact found, retrying in 10s");
123-
await new Promise((resolve) => {
124-
setTimeout(() => resolve(), 10000);
125-
});
137+
if (attempt < maxAttempts) {
138+
console.log("No artifact found, retrying in 10s");
139+
await new Promise((resolve) => setTimeout(resolve, 10_000));
140+
}
126141
}
142+
console.log("No artifact found");
143+
return null;
127144
}
128145
main();

0 commit comments

Comments
 (0)