From 23441b70d7b7fe24ab0726db5510076206fc2659 Mon Sep 17 00:00:00 2001 From: Fredrik Arvidsson Date: Thu, 29 Dec 2022 13:50:34 +0100 Subject: [PATCH] feat(rate limiting): use the github bearer token to authenticate requests to increase the rate limiting of the github api --- fetch_artifact.js | 107 ++++++++++++++++++++++------------------------ 1 file changed, 52 insertions(+), 55 deletions(-) diff --git a/fetch_artifact.js b/fetch_artifact.js index 1c857f6..2901935 100644 --- a/fetch_artifact.js +++ b/fetch_artifact.js @@ -22,58 +22,6 @@ const core = new (class { } })(); -const getAsync = async (url, asStream = false) => { - const getAsyncCalback = (url, resolve, reject) => { - https.get( - url, - { - headers: { - Accept: asStream ? 'text/html' : 'application/vnd.github.v3+json', - 'User-Agent': 'artifact-downloader' - } - }, - (res) => { - if (res.statusCode === 301 || res.statusCode === 302) { - return getAsyncCalback(res.headers.location, resolve, reject); - } - - if (asStream) { - return resolve({ - status: res.statusCode, - statusText: res.statusMessage, - data: res - }); - } - - let rawData = ''; - res.on('data', (chunk) => { - rawData += chunk; - }); - res.on('end', () => { - resolve({ - status: res.statusCode, - statusText: res.statusMessage, - data: JSON.parse(rawData) - }); - }); - res.on('error', (err) => { - reject({ - error: { - status: res.statusCode, - statusText: res.statusMessage, - data: err - } - }); - }); - } - ); - }; - - return new Promise((resolve, reject) => - getAsyncCalback(url, resolve, reject) - ); -}; - const getEnvironmentVariable = (name) => { const value = process.env[name]; if (['', null, undefined].includes(value)) { @@ -92,9 +40,58 @@ try { const api_url = getEnvironmentVariable('GITHUB_API_URL'); const token = getEnvironmentVariable('INPUT_GITHUB_TOKEN'); - if (token) { - core.info('Token set'); - } + const getAsync = async (url, asStream = false) => { + const getAsyncCalback = (url, resolve, reject) => { + https.get( + url, + { + headers: { + Accept: asStream ? 'text/html' : 'application/vnd.github.v3+json', + 'User-Agent': 'artifact-downloader', + Authorization: `Bearer ${token}` + } + }, + (res) => { + if (res.statusCode === 301 || res.statusCode === 302) { + return getAsyncCalback(res.headers.location, resolve, reject); + } + + if (asStream) { + return resolve({ + status: res.statusCode, + statusText: res.statusMessage, + data: res + }); + } + + let rawData = ''; + res.on('data', (chunk) => { + rawData += chunk; + }); + res.on('end', () => { + resolve({ + status: res.statusCode, + statusText: res.statusMessage, + data: JSON.parse(rawData) + }); + }); + res.on('error', (err) => { + reject({ + error: { + status: res.statusCode, + statusText: res.statusMessage, + data: err + } + }); + }); + } + ); + }; + + return new Promise((resolve, reject) => + getAsyncCalback(url, resolve, reject) + ); + }; const ensureSuccessStatusCode = (response) => { if (response.status !== 200) {