11import * as core from "@actions/core" ;
2- import * as github from "@actions/github" ;
2+ import { getOctokit } from "@actions/github" ;
33import { DefaultArtifactClient } from "@actions/artifact" ;
44import * 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+
617async 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
8697async 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}
128145main ( ) ;
0 commit comments