11import { GitHubRepoContext } from "../git/repository" ;
2- import { logDebug } from "../log" ;
2+ import { log , logDebug } from "../log" ;
33import * as model from "../model" ;
44import { WorkflowJob } from "./WorkflowJob" ;
55
6- export class WorkflowRun {
7- private _gitHubRepoContext : GitHubRepoContext ;
8- private _run : model . WorkflowRun ;
6+ abstract class WorkflowRunBase {
7+ protected _gitHubRepoContext : GitHubRepoContext ;
8+ protected _run : model . WorkflowRun ;
9+
910 private _jobs : Promise < WorkflowJob [ ] > | undefined ;
1011
1112 constructor ( gitHubRepoContext : GitHubRepoContext , run : model . WorkflowRun ) {
@@ -17,6 +18,10 @@ export class WorkflowRun {
1718 return this . _run ;
1819 }
1920
21+ get hasPreviousAttempts ( ) : boolean {
22+ return ( this . run . run_attempt || 1 ) > 1 ;
23+ }
24+
2025 updateRun ( run : model . WorkflowRun ) {
2126 if ( this . _run . updated_at !== run . updated_at ) {
2227 // Run has changed, reset jobs. Note: this doesn't work in all cases, there might be race conditions
@@ -37,7 +42,17 @@ export class WorkflowRun {
3742 return this . _jobs ;
3843 }
3944
40- private async fetchJobs ( ) : Promise < WorkflowJob [ ] > {
45+ protected abstract fetchJobs ( ) : Promise < WorkflowJob [ ] > ;
46+ }
47+
48+ export class WorkflowRun extends WorkflowRunBase {
49+ private _attempts : Promise < WorkflowRunAttempt [ ] > | undefined ;
50+
51+ constructor ( gitHubRepoContext : GitHubRepoContext , run : model . WorkflowRun ) {
52+ super ( gitHubRepoContext , run ) ;
53+ }
54+
55+ override async fetchJobs ( ) : Promise < WorkflowJob [ ] > {
4156 logDebug ( "Getting workflow jobs" ) ;
4257
4358 const result = await this . _gitHubRepoContext . client . actions . listJobsForWorkflowRun ( {
@@ -50,4 +65,69 @@ export class WorkflowRun {
5065 const jobs : model . WorkflowJob [ ] = resp . jobs ;
5166 return jobs . map ( j => new WorkflowJob ( this . _gitHubRepoContext , j ) ) ;
5267 }
68+
69+ attempts ( ) : Promise < WorkflowRunAttempt [ ] > {
70+ if ( ! this . _attempts ) {
71+ this . _attempts = this . _updateAttempts ( ) ;
72+ }
73+
74+ return this . _attempts ;
75+ }
76+
77+ private async _updateAttempts ( ) : Promise < WorkflowRunAttempt [ ] > {
78+ const attempts : WorkflowRunAttempt [ ] = [ ] ;
79+
80+ const attempt = this . run . run_attempt || 1 ;
81+ if ( attempt > 1 ) {
82+ for ( let i = 1 ; i < attempt ; i ++ ) {
83+ const runAttemptResp = await this . _gitHubRepoContext . client . actions . getWorkflowRunAttempt ( {
84+ owner : this . _gitHubRepoContext . owner ,
85+ repo : this . _gitHubRepoContext . name ,
86+ run_id : this . _run . id ,
87+ attempt_number : i
88+ } ) ;
89+ if ( runAttemptResp . status !== 200 ) {
90+ log (
91+ "Failed to get workflow run attempt" ,
92+ this . _run . id ,
93+ "for attempt" ,
94+ i ,
95+ runAttemptResp . status ,
96+ runAttemptResp . data
97+ ) ;
98+ continue ;
99+ }
100+
101+ const runAttempt = runAttemptResp . data ;
102+ attempts . push ( new WorkflowRunAttempt ( this . _gitHubRepoContext , runAttempt , i ) ) ;
103+ }
104+ }
105+
106+ return attempts ;
107+ }
108+ }
109+
110+ export class WorkflowRunAttempt extends WorkflowRunBase {
111+ public readonly attempt : number ;
112+
113+ constructor ( gitHubRepoContext : GitHubRepoContext , run : model . WorkflowRunAttempt , attempt : number ) {
114+ super ( gitHubRepoContext , run ) ;
115+
116+ this . attempt = attempt ;
117+ }
118+
119+ override async fetchJobs ( ) : Promise < WorkflowJob [ ] > {
120+ logDebug ( "Getting workflow run attempt jobs" , this . _run . id , "for attempt" , this . attempt ) ;
121+
122+ const result = await this . _gitHubRepoContext . client . actions . listJobsForWorkflowRunAttempt ( {
123+ owner : this . _gitHubRepoContext . owner ,
124+ repo : this . _gitHubRepoContext . name ,
125+ run_id : this . _run . id ,
126+ attempt_number : this . attempt
127+ } ) ;
128+
129+ const resp = result . data ;
130+ const jobs : model . WorkflowJob [ ] = resp . jobs ;
131+ return jobs . map ( j => new WorkflowJob ( this . _gitHubRepoContext , j ) ) ;
132+ }
53133}
0 commit comments