Skip to content

Commit dec276d

Browse files
authored
chore(request): mutualize GH API headers (#1046)
1 parent 3070852 commit dec276d

File tree

2 files changed

+28
-54
lines changed

2 files changed

+28
-54
lines changed

lib/request.js

Lines changed: 15 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ export default class Request {
3232
if (url.startsWith(`https://${CI_DOMAIN}`)) {
3333
options.headers = options.headers || {};
3434
Object.assign(options.headers, this.getJenkinsHeaders());
35+
} else if (!url.startsWith('https://')) {
36+
url = new URL(url, 'https://api.github.com/').href;
37+
options.headers = {
38+
Authorization: `Basic ${this.credentials.github}`,
39+
'User-Agent': 'node-core-utils',
40+
Accept: 'application/vnd.github+json',
41+
...options.headers
42+
};
3543
}
3644
return wrappedFetch(url, options);
3745
}
@@ -65,14 +73,9 @@ export default class Request {
6573
}
6674

6775
async createIssue(title, body, { owner, repo }) {
68-
const url = `https://api.github.com/repos/${owner}/${repo}/issues`;
76+
const url = `/repos/${owner}/${repo}/issues`;
6977
const options = {
7078
method: 'POST',
71-
headers: {
72-
Authorization: `Basic ${this.credentials.github}`,
73-
'User-Agent': 'node-core-utils',
74-
Accept: 'application/vnd.github+json'
75-
},
7679
body: JSON.stringify({
7780
title,
7881
body
@@ -82,15 +85,9 @@ export default class Request {
8285
}
8386

8487
async commentIssue(fullUrl, comment) {
85-
const commentUrl = fullUrl.replace('https://github.com/', 'https://api.github.com/repos/') +
86-
'/comments';
88+
const commentUrl = fullUrl.replace('https://github.com/', '/repos/') + '/comments';
8789
const options = {
8890
method: 'POST',
89-
headers: {
90-
Authorization: `Basic ${this.credentials.github}`,
91-
'User-Agent': 'node-core-utils',
92-
Accept: 'application/vnd.github+json'
93-
},
9491
body: JSON.stringify({
9592
body: comment,
9693
})
@@ -99,27 +96,14 @@ export default class Request {
9996
}
10097

10198
async getPullRequest(fullUrl) {
102-
const prUrl = fullUrl.replace('https://github.com/', 'https://api.github.com/repos/').replace('pull', 'pulls');
103-
const options = {
104-
method: 'GET',
105-
headers: {
106-
Authorization: `Basic ${this.credentials.github}`,
107-
'User-Agent': 'node-core-utils',
108-
Accept: 'application/vnd.github+json'
109-
}
110-
};
111-
return this.json(prUrl, options);
99+
const prUrl = fullUrl.replace('https://github.com/', '/repos/').replace('pull', 'pulls');
100+
return this.json(prUrl);
112101
}
113102

114103
async createPullRequest(title, body, { owner, repo, head, base }) {
115-
const url = `https://api.github.com/repos/${owner}/${repo}/pulls`;
104+
const url = `/repos/${owner}/${repo}/pulls`;
116105
const options = {
117106
method: 'POST',
118-
headers: {
119-
Authorization: `Basic ${this.credentials.github}`,
120-
'User-Agent': 'node-core-utils',
121-
Accept: 'application/vnd.github+json'
122-
},
123107
body: JSON.stringify({
124108
title,
125109
body,
@@ -131,13 +115,10 @@ export default class Request {
131115
}
132116

133117
async closePullRequest(id, { owner, repo }) {
134-
const url = `https://api.github.com/repos/${owner}/${repo}/pulls/${id}`;
118+
const url = `/repos/${owner}/${repo}/pulls/${id}`;
135119
const options = {
136120
method: 'POST',
137121
headers: {
138-
Authorization: `Basic ${this.credentials.github}`,
139-
'User-Agent': 'node-core-utils',
140-
Accept: 'application/vnd.github+json',
141122
'Content-Type': 'application/json'
142123
},
143124
body: JSON.stringify({
@@ -299,13 +280,11 @@ export default class Request {
299280
throw new Error('The request has not been ' +
300281
'authenticated with a GitHub token');
301282
}
302-
const url = 'https://api.github.com/graphql';
283+
const url = '/graphql';
303284
const options = {
304285
agent: this.proxyAgent,
305286
method: 'POST',
306287
headers: {
307-
Authorization: `Basic ${githubCredentials}`,
308-
'User-Agent': 'node-core-utils',
309288
Accept: 'application/vnd.github.antiope-preview+json'
310289
},
311290
body: JSON.stringify({

lib/voting_session.js

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,10 @@ export default class VotingSession extends Session {
7272
const subPath = `${prInfo.headRef.name}/vote.yml`;
7373
this.cli.startSpinner('Downloading vote file from remote...');
7474
const yamlString = await this.req.text(
75-
`https://api.github.com/repos/${this.owner}/${this.repo}/contents/${encodeURIComponent(subPath)}?ref=${prInfo.commits.nodes[0].commit.oid}`, {
76-
agent: this.req.proxyAgent,
77-
headers: {
78-
Authorization: `Basic ${this.req.credentials.github}`,
79-
'User-Agent': 'node-core-utils',
80-
Accept: 'application/vnd.github.raw'
81-
}
82-
});
75+
`/repos/${this.owner}/${this.repo}/contents/${
76+
encodeURIComponent(subPath)}?ref=${prInfo.commits.nodes[0].commit.oid}`,
77+
{ agent: this.req.proxyAgent }
78+
);
8379
this.cli.stopSpinner('Download complete');
8480

8581
const { shares } = yaml.load(yamlString);
@@ -114,16 +110,15 @@ export default class VotingSession extends Session {
114110
const body = 'I would like to close this vote, and for this effect, I\'m revealing my ' +
115111
`key part:\n\n${'```'}\n${keyPart}\n${'```'}\n`;
116112
if (this.postComment) {
117-
const { message, html_url } = await this.req.json(`https://api.github.com/repos/${this.owner}/${this.repo}/issues/${this.prid}/comments`, {
118-
agent: this.req.proxyAgent,
119-
method: 'POST',
120-
headers: {
121-
Authorization: `Basic ${this.req.credentials.github}`,
122-
'User-Agent': 'node-core-utils',
123-
Accept: 'application/vnd.github.antiope-preview+json'
124-
},
125-
body: JSON.stringify({ body })
126-
});
113+
const { message, html_url } = await this.req.json(
114+
`/repos/${this.owner}/${this.repo}/issues/${this.prid}/comments`,
115+
{
116+
agent: this.req.proxyAgent,
117+
method: 'POST',
118+
headers: { Accept: 'application/vnd.github.antiope-preview+json' },
119+
body: JSON.stringify({ body })
120+
}
121+
);
127122
if (html_url) {
128123
this.cli.log(`Comment posted at: ${html_url}`);
129124
return;

0 commit comments

Comments
 (0)