@@ -5,8 +5,7 @@ import {createApp} from 'vue';
5
5
import {toggleElem } from ' ../utils/dom.js' ;
6
6
import {getCurrentLocale } from ' ../utils.js' ;
7
7
import {renderAnsi } from ' ../render/ansi.js' ;
8
-
9
- const {csrfToken } = window .config ;
8
+ import {POST } from ' ../modules/fetch.js' ;
10
9
11
10
const sfc = {
12
11
name: ' RepoActionView' ,
@@ -145,11 +144,11 @@ const sfc = {
145
144
},
146
145
// cancel a run
147
146
cancelRun () {
148
- this . fetchPost (` ${ this .run .link } /cancel` );
147
+ POST (` ${ this .run .link } /cancel` );
149
148
},
150
149
// approve a run
151
150
approveRun () {
152
- this . fetchPost (` ${ this .run .link } /approve` );
151
+ POST (` ${ this .run .link } /approve` );
153
152
},
154
153
155
154
createLogLine (line , startTime , stepIndex ) {
@@ -196,17 +195,21 @@ const sfc = {
196
195
}
197
196
},
198
197
198
+ async fetchArtifacts () {
199
+ const resp = await POST (` ${ this .actionsURL } /runs/${ this .runIndex } /artifacts` );
200
+ return await resp .json ();
201
+ },
202
+
199
203
async fetchJob () {
200
204
const logCursors = this .currentJobStepsStates .map ((it , idx ) => {
201
205
// cursor is used to indicate the last position of the logs
202
206
// it's only used by backend, frontend just reads it and passes it back, it and can be any type.
203
207
// for example: make cursor=null means the first time to fetch logs, cursor=eof means no more logs, etc
204
208
return {step: idx, cursor: it .cursor , expanded: it .expanded };
205
209
});
206
- const resp = await this .fetchPost (
207
- ` ${ this .actionsURL } /runs/${ this .runIndex } /jobs/${ this .jobIndex } ` ,
208
- JSON .stringify ({logCursors}),
209
- );
210
+ const resp = await POST (` ${ this .actionsURL } /runs/${ this .runIndex } /jobs/${ this .jobIndex } ` , {
211
+ data: {logCursors},
212
+ });
210
213
return await resp .json ();
211
214
},
212
215
@@ -215,16 +218,21 @@ const sfc = {
215
218
try {
216
219
this .loading = true ;
217
220
218
- // refresh artifacts if upload-artifact step done
219
- const resp = await this .fetchPost (` ${ this .actionsURL } /runs/${ this .runIndex } /artifacts` );
220
- const artifacts = await resp .json ();
221
- this .artifacts = artifacts[' artifacts' ] || [];
221
+ let job, artifacts;
222
+ try {
223
+ [job, artifacts] = await Promise .all ([
224
+ this .fetchJob (),
225
+ this .fetchArtifacts (), // refresh artifacts if upload-artifact step done
226
+ ]);
227
+ } catch (err) {
228
+ if (! (err instanceof TypeError )) throw err; // avoid network error while unloading page
229
+ }
222
230
223
- const response = await this . fetchJob () ;
231
+ this . artifacts = artifacts[ ' artifacts ' ] || [] ;
224
232
225
233
// save the state to Vue data, then the UI will be updated
226
- this .run = response .state .run ;
227
- this .currentJob = response .state .currentJob ;
234
+ this .run = job .state .run ;
235
+ this .currentJob = job .state .currentJob ;
228
236
229
237
// sync the currentJobStepsStates to store the job step states
230
238
for (let i = 0 ; i < this .currentJob .steps .length ; i++ ) {
@@ -234,7 +242,7 @@ const sfc = {
234
242
}
235
243
}
236
244
// append logs to the UI
237
- for (const logs of response .logs .stepsLog ) {
245
+ for (const logs of job .logs .stepsLog ) {
238
246
// save the cursor, it will be passed to backend next time
239
247
this .currentJobStepsStates [logs .step ].cursor = logs .cursor ;
240
248
this .appendLogs (logs .step , logs .lines , logs .started );
@@ -249,18 +257,6 @@ const sfc = {
249
257
}
250
258
},
251
259
252
-
253
- fetchPost (url , body ) {
254
- return fetch (url, {
255
- method: ' POST' ,
256
- headers: {
257
- ' Content-Type' : ' application/json' ,
258
- ' X-Csrf-Token' : csrfToken,
259
- },
260
- body,
261
- });
262
- },
263
-
264
260
isDone (status ) {
265
261
return [' success' , ' skipped' , ' failure' , ' cancelled' ].includes (status);
266
262
},
0 commit comments