Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 15 additions & 36 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand All @@ -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,
})
Expand All @@ -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,
Expand All @@ -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({
Expand Down Expand Up @@ -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({
Expand Down
31 changes: 13 additions & 18 deletions lib/voting_session.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
Loading