Add authorization header to GitHub API call when COMPOSER_TOKEN is set

This commit is contained in:
Shivam Mathur 2021-07-06 08:16:53 +05:30
parent c6b6dabbb9
commit 39491a0fba
No known key found for this signature in database
GPG Key ID: 3E13E4C8591ACC2A
3 changed files with 16 additions and 2 deletions

View File

@ -41,6 +41,10 @@ describe('Utils tests', () => {
expect(await utils.fetch('test_url')).toBe( expect(await utils.fetch('test_url')).toBe(
'{ "latest": "8.0", "5.x": "5.6", "url": "test_url" }' '{ "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 () => { it('checking parseVersion', async () => {

7
dist/index.js vendored
View File

@ -874,10 +874,15 @@ exports.getInput = getInput;
async function fetch(input_url) { async function fetch(input_url) {
const fetch_promise = new Promise(resolve => { const fetch_promise = new Promise(resolve => {
const url_object = new url.URL(input_url); 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 = { const options = {
hostname: url_object.hostname, hostname: url_object.hostname,
path: url_object.pathname, path: url_object.pathname,
headers: { 'User-Agent': 'setup-php' } headers: {
authorization: auth_header,
'User-Agent': 'setup-php'
}
}; };
const req = https.get(options, (res) => { const req = https.get(options, (res) => {
res.setEncoding('utf8'); res.setEncoding('utf8');

View File

@ -52,10 +52,15 @@ export async function getInput(
export async function fetch(input_url: string): Promise<string> { export async function fetch(input_url: string): Promise<string> {
const fetch_promise: Promise<string> = new Promise(resolve => { const fetch_promise: Promise<string> = new Promise(resolve => {
const url_object: url.UrlObject = new url.URL(input_url); 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 = { const options: https.RequestOptions = {
hostname: url_object.hostname, hostname: url_object.hostname,
path: url_object.pathname, path: url_object.pathname,
headers: {'User-Agent': 'setup-php'} headers: {
authorization: auth_header,
'User-Agent': 'setup-php'
}
}; };
const req = https.get(options, (res: IncomingMessage) => { const req = https.get(options, (res: IncomingMessage) => {
res.setEncoding('utf8'); res.setEncoding('utf8');