build: update makeRequest parameters (#34782)

This commit is contained in:
Keeley Hammond 2022-06-28 20:26:00 -07:00 committed by GitHub
parent 40fbc05bb2
commit 3310e4039f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -35,19 +35,27 @@ const vstsArmJobs = [
let jobRequestedCount = 0; let jobRequestedCount = 0;
async function makeRequest ({ auth, url, headers, body, method }) { async function makeRequest ({ auth, username, password, url, headers, body, method }) {
const clonedHeaders = { const clonedHeaders = {
...(headers || {}) ...(headers || {})
}; };
if (auth && auth.bearer) { if (auth?.bearer) {
clonedHeaders.Authorization = `Bearer ${auth.bearer}`; clonedHeaders.Authorization = `Bearer ${auth.bearer}`;
} }
const response = await got(url, {
const options = {
headers: clonedHeaders, headers: clonedHeaders,
body, body,
method, method
auth: auth && (auth.username || auth.password) ? `${auth.username}:${auth.password}` : undefined };
});
if (username || password) {
options.username = username;
options.password = password;
}
const response = await got(url, options);
if (response.statusCode < 200 || response.statusCode >= 300) { if (response.statusCode < 200 || response.statusCode >= 300) {
console.error('Error: ', `(status ${response.statusCode})`, response.body); console.error('Error: ', `(status ${response.statusCode})`, response.body);
throw new Error(`Unexpected status code ${response.statusCode} from ${url}`); throw new Error(`Unexpected status code ${response.statusCode} from ${url}`);
@ -170,19 +178,21 @@ async function getCircleCIJobNumber (workflowId) {
} }
async function circleCIRequest (url, method, requestBody) { async function circleCIRequest (url, method, requestBody) {
return makeRequest({ const requestOpts = {
auth: {
username: process.env.CIRCLE_TOKEN, username: process.env.CIRCLE_TOKEN,
password: '' password: '',
},
method, method,
url, url,
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
Accept: 'application/json' Accept: 'application/json'
}, }
body: requestBody ? JSON.stringify(requestBody) : null };
}, true).catch(err => { if (requestBody) {
requestOpts.body = JSON.stringify(requestBody);
}
return makeRequest(requestOpts, true).catch(err => {
console.log('Error calling CircleCI:', err); console.log('Error calling CircleCI:', err);
}); });
} }
@ -269,10 +279,8 @@ async function buildVSTS (targetBranch, options) {
} }
const requestOpts = { const requestOpts = {
url: `${vstsURL}/definitions?api-version=4.1`, url: `${vstsURL}/definitions?api-version=4.1`,
auth: {
user: '', user: '',
password: vstsToken password: vstsToken,
},
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
} }
@ -300,10 +308,8 @@ async function callVSTSBuild (build, targetBranch, environmentVariables, vstsURL
} }
const requestOpts = { const requestOpts = {
url: `${vstsURL}/builds?api-version=4.1`, url: `${vstsURL}/builds?api-version=4.1`,
auth: {
user: '', user: '',
password: vstsToken password: vstsToken,
},
headers: { headers: {
'Content-Type': 'application/json' 'Content-Type': 'application/json'
}, },