11import FormData from 'form-data' ;
22
3+ import { BASIC_TREE } from './jenkins_constants.js' ;
4+ import { TestBuild } from './build-types/test_build.js' ;
35import {
46 CI_DOMAIN ,
57 CI_TYPES ,
@@ -9,14 +11,16 @@ import {
911export const CI_CRUMB_URL = `https://${ CI_DOMAIN } /crumbIssuer/api/json` ;
1012const CI_PR_NAME = CI_TYPES . get ( CI_TYPES_KEYS . PR ) . jobName ;
1113export const CI_PR_URL = `https://${ CI_DOMAIN } /job/${ CI_PR_NAME } /build` ;
14+ export const CI_PR_RESUME_URL = `https://${ CI_DOMAIN } /job/${ CI_PR_NAME } /` ;
1215
1316export class RunPRJob {
14- constructor ( cli , request , owner , repo , prid ) {
17+ constructor ( cli , request , owner , repo , prid , jobid ) {
1518 this . cli = cli ;
1619 this . request = request ;
1720 this . owner = owner ;
1821 this . repo = repo ;
1922 this . prid = prid ;
23+ this . jobid = jobid ;
2024 }
2125
2226 async getCrumb ( ) {
@@ -43,18 +47,28 @@ export class RunPRJob {
4347 return payload ;
4448 }
4549
46- async start ( ) {
50+ async #validateJenkinsCredentials ( ) {
4751 const { cli } = this ;
4852 cli . startSpinner ( 'Validating Jenkins credentials' ) ;
4953 const crumb = await this . getCrumb ( ) ;
5054
5155 if ( crumb === false ) {
5256 cli . stopSpinner ( 'Jenkins credentials invalid' ,
5357 this . cli . SPINNER_STATUS . FAILED ) ;
54- return false ;
58+ return { crumb , success : false } ;
5559 }
5660 cli . stopSpinner ( 'Jenkins credentials valid' ) ;
5761
62+ return { crumb, success : true } ;
63+ }
64+
65+ async start ( ) {
66+ const { cli } = this ;
67+ const { crumb, success } = await this . #validateJenkinsCredentials( ) ;
68+ if ( success === false ) {
69+ return false ;
70+ }
71+
5872 try {
5973 cli . startSpinner ( 'Starting PR CI job' ) ;
6074 const response = await this . request . fetch ( CI_PR_URL , {
@@ -64,7 +78,7 @@ export class RunPRJob {
6478 } ,
6579 body : this . payload
6680 } ) ;
67- if ( response . status !== 201 ) {
81+ if ( response . status !== 200 ) {
6882 cli . stopSpinner (
6983 `Failed to start PR CI: ${ response . status } ${ response . statusText } ` ,
7084 this . cli . SPINNER_STATUS . FAILED ) ;
@@ -77,4 +91,46 @@ export class RunPRJob {
7791 }
7892 return true ;
7993 }
94+
95+ async resume ( ) {
96+ const { cli, request, jobid } = this ;
97+ const { crumb, success } = await this . #validateJenkinsCredentials( ) ;
98+ if ( success === false ) {
99+ return false ;
100+ }
101+
102+ try {
103+ cli . startSpinner ( 'Resuming PR CI job' ) ;
104+ const path = `job/${ CI_PR_NAME } /${ jobid } /` ;
105+ const testBuild = new TestBuild ( cli , request , path , BASIC_TREE ) ;
106+ const { result } = await testBuild . getBuildData ( ) ;
107+
108+ if ( result !== 'FAILURE' ) {
109+ cli . stopSpinner (
110+ `CI Job is in status ${ result ?? 'RUNNING' } , skipping resume` ,
111+ this . cli . SPINNER_STATUS . FAILED ) ;
112+ return false ;
113+ }
114+
115+ const resume_url = `${ CI_PR_RESUME_URL } ${ jobid } /resume` ;
116+ const response = await this . request . fetch ( resume_url , {
117+ method : 'POST' ,
118+ headers : {
119+ 'Jenkins-Crumb' : crumb
120+ }
121+ } ) ;
122+ if ( response . status !== 201 ) {
123+ cli . stopSpinner (
124+ `Failed to resume PR CI: ${ response . status } ${ response . statusText } ` ,
125+ this . cli . SPINNER_STATUS . FAILED ) ;
126+ return false ;
127+ }
128+
129+ cli . stopSpinner ( 'PR CI job successfully resumed' ) ;
130+ } catch ( err ) {
131+ cli . stopSpinner ( 'Failed to resume CI' , this . cli . SPINNER_STATUS . FAILED ) ;
132+ return false ;
133+ }
134+ return true ;
135+ }
80136}
0 commit comments