diff --git a/lib/request.js b/lib/request.js index 289f13fa..c28e92a0 100644 --- a/lib/request.js +++ b/lib/request.js @@ -32,6 +32,14 @@ export default class Request { if (url.startsWith(`https://${CI_DOMAIN}`)) { options.headers = options.headers || {}; Object.assign(options.headers, this.getJenkinsHeaders()); + } else if (!url.startsWith('https://')) { + url = new URL(url, 'https://api.github.com/').href; + options.headers = { + Authorization: `Basic ${this.credentials.github}`, + 'User-Agent': 'node-core-utils', + Accept: 'application/vnd.github+json', + ...options.headers + }; } return wrappedFetch(url, options); } @@ -65,14 +73,9 @@ export default class Request { } async createIssue(title, body, { owner, repo }) { - const url = `https://api.github.com/repos/${owner}/${repo}/issues`; + const url = `/repos/${owner}/${repo}/issues`; const options = { method: 'POST', - headers: { - Authorization: `Basic ${this.credentials.github}`, - 'User-Agent': 'node-core-utils', - Accept: 'application/vnd.github+json' - }, body: JSON.stringify({ title, body @@ -82,15 +85,9 @@ export default class Request { } async commentIssue(fullUrl, comment) { - const commentUrl = fullUrl.replace('https://github.com/', 'https://api.github.com/repos/') + - '/comments'; + const commentUrl = fullUrl.replace('https://github.com/', '/repos/') + '/comments'; const options = { method: 'POST', - headers: { - Authorization: `Basic ${this.credentials.github}`, - 'User-Agent': 'node-core-utils', - Accept: 'application/vnd.github+json' - }, body: JSON.stringify({ body: comment, }) @@ -99,27 +96,14 @@ export default class Request { } async getPullRequest(fullUrl) { - const prUrl = fullUrl.replace('https://github.com/', 'https://api.github.com/repos/').replace('pull', 'pulls'); - const options = { - method: 'GET', - headers: { - Authorization: `Basic ${this.credentials.github}`, - 'User-Agent': 'node-core-utils', - Accept: 'application/vnd.github+json' - } - }; - return this.json(prUrl, options); + const prUrl = fullUrl.replace('https://github.com/', '/repos/').replace('pull', 'pulls'); + return this.json(prUrl); } async createPullRequest(title, body, { owner, repo, head, base }) { - const url = `https://api.github.com/repos/${owner}/${repo}/pulls`; + const url = `/repos/${owner}/${repo}/pulls`; const options = { method: 'POST', - headers: { - Authorization: `Basic ${this.credentials.github}`, - 'User-Agent': 'node-core-utils', - Accept: 'application/vnd.github+json' - }, body: JSON.stringify({ title, body, @@ -131,13 +115,10 @@ export default class Request { } async closePullRequest(id, { owner, repo }) { - const url = `https://api.github.com/repos/${owner}/${repo}/pulls/${id}`; + const url = `/repos/${owner}/${repo}/pulls/${id}`; const options = { method: 'POST', headers: { - Authorization: `Basic ${this.credentials.github}`, - 'User-Agent': 'node-core-utils', - Accept: 'application/vnd.github+json', 'Content-Type': 'application/json' }, body: JSON.stringify({ @@ -299,13 +280,11 @@ export default class Request { throw new Error('The request has not been ' + 'authenticated with a GitHub token'); } - const url = 'https://api.github.com/graphql'; + const url = '/graphql'; const options = { agent: this.proxyAgent, method: 'POST', headers: { - Authorization: `Basic ${githubCredentials}`, - 'User-Agent': 'node-core-utils', Accept: 'application/vnd.github.antiope-preview+json' }, body: JSON.stringify({ diff --git a/lib/voting_session.js b/lib/voting_session.js index e3d349fe..bc6d4cae 100644 --- a/lib/voting_session.js +++ b/lib/voting_session.js @@ -72,14 +72,10 @@ export default class VotingSession extends Session { const subPath = `${prInfo.headRef.name}/vote.yml`; this.cli.startSpinner('Downloading vote file from remote...'); const yamlString = await this.req.text( - `https://api.github.com/repos/${this.owner}/${this.repo}/contents/${encodeURIComponent(subPath)}?ref=${prInfo.commits.nodes[0].commit.oid}`, { - agent: this.req.proxyAgent, - headers: { - Authorization: `Basic ${this.req.credentials.github}`, - 'User-Agent': 'node-core-utils', - Accept: 'application/vnd.github.raw' - } - }); + `/repos/${this.owner}/${this.repo}/contents/${ + encodeURIComponent(subPath)}?ref=${prInfo.commits.nodes[0].commit.oid}`, + { agent: this.req.proxyAgent } + ); this.cli.stopSpinner('Download complete'); const { shares } = yaml.load(yamlString); @@ -114,16 +110,15 @@ export default class VotingSession extends Session { const body = 'I would like to close this vote, and for this effect, I\'m revealing my ' + `key part:\n\n${'```'}\n${keyPart}\n${'```'}\n`; if (this.postComment) { - const { message, html_url } = await this.req.json(`https://api.github.com/repos/${this.owner}/${this.repo}/issues/${this.prid}/comments`, { - agent: this.req.proxyAgent, - method: 'POST', - headers: { - Authorization: `Basic ${this.req.credentials.github}`, - 'User-Agent': 'node-core-utils', - Accept: 'application/vnd.github.antiope-preview+json' - }, - body: JSON.stringify({ body }) - }); + const { message, html_url } = await this.req.json( + `/repos/${this.owner}/${this.repo}/issues/${this.prid}/comments`, + { + agent: this.req.proxyAgent, + method: 'POST', + headers: { Accept: 'application/vnd.github.antiope-preview+json' }, + body: JSON.stringify({ body }) + } + ); if (html_url) { this.cli.log(`Comment posted at: ${html_url}`); return;