@@ -17,6 +17,16 @@ import { URL } from 'url';
17
17
import prepareImportScript from './bundler.js' ;
18
18
import chalk from 'chalk' ;
19
19
import { uploadZipFromS3ToSharePoint } from './sharepoint-uploader.js' ;
20
+ import { makeRequest } from '../utils/http-utils.js' ;
21
+
22
+ function getApiBaseUrl ( stage ) {
23
+ const alias = stage ? 'ci' : 'v1' ;
24
+ return `https://spacecat.experiencecloud.live/api/${ alias } /tools/import/jobs` ;
25
+ }
26
+
27
+ async function getJobResult ( jobId , stage ) {
28
+ return makeRequest ( `${ getApiBaseUrl ( stage ) } /${ jobId } /result` , 'POST' ) ;
29
+ }
20
30
21
31
/**
22
32
* Run the import job and begin polling for the result. Logs progress & result to the console.
@@ -27,46 +37,20 @@ import { uploadZipFromS3ToSharePoint } from './sharepoint-uploader.js';
27
37
* @param {boolean } stage - Set to true if stage APIs should be used
28
38
* @returns {Promise<void> }
29
39
*/
30
- async function runImportJobAndPoll ( {
40
+ export async function runImportJobAndPoll ( {
31
41
urls,
32
42
importJsPath,
33
43
options,
34
44
sharePointUploadUrl,
35
45
stage = false
36
46
} ) {
37
47
// Determine the base URL
38
- const baseURL = stage
39
- ? 'https://spacecat.experiencecloud.live/api/ci/tools/import/jobs'
40
- : 'https://spacecat.experiencecloud.live/api/v1/tools/import/jobs' ;
48
+ const baseURL = getApiBaseUrl ( stage ) ;
41
49
42
50
function hasProvidedSharePointUrl ( ) {
43
51
return typeof sharePointUploadUrl === 'string' ;
44
52
}
45
53
46
- // Function to make HTTP requests
47
- async function makeRequest ( url , method , data ) {
48
- const parsedUrl = new URL ( url ) ;
49
- const headers = new Headers ( {
50
- 'Content-Type' : data ? 'application/json' : '' ,
51
- 'x-api-key' : process . env . AEM_IMPORT_API_KEY ,
52
- } ) ;
53
- if ( data instanceof FormData ) {
54
- headers . delete ( 'Content-Type' ) ;
55
- }
56
- const res = await fetch ( parsedUrl , {
57
- method,
58
- headers,
59
- body : data ,
60
- } ) ;
61
- if ( res . ok ) {
62
- return res . json ( ) ;
63
- }
64
- const body = await res . text ( ) ;
65
- throw new Error ( `Request failed with status code ${ res . status } . `
66
- + `x-error header: ${ res . headers . get ( 'x-error' ) } , x-invocation-id: ${ res . headers . get ( 'x-invocation-id' ) } , `
67
- + `Body: ${ body } ` ) ;
68
- }
69
-
70
54
// Function to poll job status
71
55
async function pollJobStatus ( jobId ) {
72
56
const url = `${ baseURL } /${ jobId } ` ;
@@ -80,7 +64,7 @@ async function runImportJobAndPoll( {
80
64
console . log ( chalk . green ( 'Job completed:' ) , jobStatus ) ;
81
65
82
66
// Print the job result's downloadUrl
83
- const jobResult = await makeRequest ( ` ${ url } /result` , 'POST' ) ;
67
+ const jobResult = await getJobResult ( jobId , stage ) ;
84
68
console . log ( chalk . green ( 'Download the import archive:' ) , jobResult . downloadUrl ) ;
85
69
86
70
if ( hasProvidedSharePointUrl ( ) ) {
@@ -132,4 +116,10 @@ async function runImportJobAndPoll( {
132
116
return startJob ( ) ;
133
117
}
134
118
135
- export default runImportJobAndPoll ;
119
+ export async function uploadJobResult ( { jobId, sharePointUploadUrl, stage = false } ) {
120
+ // Fetch the job result
121
+ const jobResult = await getJobResult ( jobId , stage ) ;
122
+
123
+ // Upload the import archive to SharePoint
124
+ await uploadZipFromS3ToSharePoint ( jobResult . downloadUrl , sharePointUploadUrl ) ;
125
+ }
0 commit comments