From f7638778046c83f8feeca64ef3d1ec19d1bbccf0 Mon Sep 17 00:00:00 2001 From: Johannes Spangenberg Date: Sat, 4 May 2024 13:43:51 +0200 Subject: [PATCH] Fix problematic retransmission of authentication token The retransmission of the authentication token to the server providing the artifact caused the following errors when using Artifacts v4: HTTPError: Response code 400 (Authentication information is not given in the correct format. Check the value of Authorization header.) It looks like the service serving the artifacts does not expect the authentication header, and therefore complaines about inproper use of the authentication header. Delegating the redirect-handling to the `got` library fixes the issue according to my tsts. --- dist/index.js | 23 ++++------------------- src/utils/github-utils.ts | 26 +++++--------------------- 2 files changed, 9 insertions(+), 40 deletions(-) diff --git a/dist/index.js b/dist/index.js index dee44688..0befaa4c 100644 --- a/dist/index.js +++ b/dist/index.js @@ -2151,26 +2151,11 @@ function downloadArtifact(octokit, artifactId, fileName, token) { const headers = { Authorization: `Bearer ${token}` }; - const resp = yield (0, got_1.default)(req.url, { - headers, - followRedirect: false - }); - core.info(`Fetch artifact URL: ${resp.statusCode} ${resp.statusMessage}`); - if (resp.statusCode !== 302) { - throw new Error('Fetch artifact URL failed: received unexpected status code'); - } - const url = resp.headers.location; - if (url === undefined) { - const receivedHeaders = Object.keys(resp.headers); - core.info(`Received headers: ${receivedHeaders.join(', ')}`); - throw new Error('Location header was not found in API response'); - } - if (typeof url !== 'string') { - throw new Error(`Location header has unexpected value: ${url}`); - } - const downloadStream = got_1.default.stream(url, { headers }); + const downloadStream = got_1.default.stream(req.url, { headers }); const fileWriterStream = (0, fs_1.createWriteStream)(fileName); - core.info(`Downloading ${url}`); + downloadStream.on('redirect', response => { + core.info(`Downloading ${response.headers.location}`); + }); downloadStream.on('downloadProgress', ({ transferred }) => { core.info(`Progress: ${transferred} B`); }); diff --git a/src/utils/github-utils.ts b/src/utils/github-utils.ts index 1d4add7d..19fe9218 100644 --- a/src/utils/github-utils.ts +++ b/src/utils/github-utils.ts @@ -50,33 +50,17 @@ export async function downloadArtifact( const headers = { Authorization: `Bearer ${token}` } - const resp = await got(req.url, { - headers, - followRedirect: false - }) - - core.info(`Fetch artifact URL: ${resp.statusCode} ${resp.statusMessage}`) - if (resp.statusCode !== 302) { - throw new Error('Fetch artifact URL failed: received unexpected status code') - } - - const url = resp.headers.location - if (url === undefined) { - const receivedHeaders = Object.keys(resp.headers) - core.info(`Received headers: ${receivedHeaders.join(', ')}`) - throw new Error('Location header was not found in API response') - } - if (typeof url !== 'string') { - throw new Error(`Location header has unexpected value: ${url}`) - } - const downloadStream = got.stream(url, {headers}) + const downloadStream = got.stream(req.url, {headers}) const fileWriterStream = createWriteStream(fileName) - core.info(`Downloading ${url}`) + downloadStream.on('redirect', response => { + core.info(`Downloading ${response.headers.location}`) + }) downloadStream.on('downloadProgress', ({transferred}) => { core.info(`Progress: ${transferred} B`) }) + await asyncStream(downloadStream, fileWriterStream) } finally { core.endGroup()