From 39491a0fbaa5fc771fba3c599b0990de3530ad8d Mon Sep 17 00:00:00 2001 From: Shivam Mathur Date: Tue, 6 Jul 2021 08:16:53 +0530 Subject: [PATCH] Add authorization header to GitHub API call when COMPOSER_TOKEN is set --- __tests__/utils.test.ts | 4 ++++ dist/index.js | 7 ++++++- src/utils.ts | 7 ++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/__tests__/utils.test.ts b/__tests__/utils.test.ts index 852e3bf2..d218a79b 100644 --- a/__tests__/utils.test.ts +++ b/__tests__/utils.test.ts @@ -41,6 +41,10 @@ describe('Utils tests', () => { expect(await utils.fetch('test_url')).toBe( '{ "latest": "8.0", "5.x": "5.6", "url": "test_url" }' ); + process.env['COMPOSER_TOKEN'] = 'GITHUB_TOKEN'; + expect(await utils.fetch('test_url')).toBe( + '{ "latest": "8.0", "5.x": "5.6", "url": "test_url" }' + ); }); it('checking parseVersion', async () => { diff --git a/dist/index.js b/dist/index.js index d79ac632..08923045 100644 --- a/dist/index.js +++ b/dist/index.js @@ -874,10 +874,15 @@ exports.getInput = getInput; async function fetch(input_url) { const fetch_promise = new Promise(resolve => { const url_object = new url.URL(input_url); + const auth_token = process.env['COMPOSER_TOKEN'] || ''; + const auth_header = auth_token ? 'Bearer' + auth_token : ''; const options = { hostname: url_object.hostname, path: url_object.pathname, - headers: { 'User-Agent': 'setup-php' } + headers: { + authorization: auth_header, + 'User-Agent': 'setup-php' + } }; const req = https.get(options, (res) => { res.setEncoding('utf8'); diff --git a/src/utils.ts b/src/utils.ts index f45edc9f..56739fa1 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -52,10 +52,15 @@ export async function getInput( export async function fetch(input_url: string): Promise { const fetch_promise: Promise = new Promise(resolve => { const url_object: url.UrlObject = new url.URL(input_url); + const auth_token: string = process.env['COMPOSER_TOKEN'] || ''; + const auth_header: string = auth_token ? 'Bearer' + auth_token : ''; const options: https.RequestOptions = { hostname: url_object.hostname, path: url_object.pathname, - headers: {'User-Agent': 'setup-php'} + headers: { + authorization: auth_header, + 'User-Agent': 'setup-php' + } }; const req = https.get(options, (res: IncomingMessage) => { res.setEncoding('utf8');