Skip to content

Commit 82f08d0

Browse files
authored
Send access token via auth header (#165)
Github is deprecating sending access token via query param. Instead it should be sent over auth header https://developer.github.com/changes/2019-11-05-deprecated-passwords-and-authorizations-api/#authenticating-using-query-parameters
1 parent 4244b66 commit 82f08d0

9 files changed

+59
-18
lines changed

src/options/options.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ describe('getOptions', () => {
3535
it('should check whether access token is valid', () => {
3636
expect(axiosHeadSpy).toHaveBeenCalledTimes(1);
3737
expect(axiosHeadSpy).toHaveBeenCalledWith(
38-
'https://api.github.com/repos/elastic/kibana?access_token=myAccessToken'
38+
'https://api.github.com/repos/elastic/kibana',
39+
{ auth: { password: 'myAccessToken', username: 'sqren' } }
3940
);
4041
});
4142

src/services/github/__snapshots__/fetchCommitBySha.test.ts.snap

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33
exports[`fetchCommitBySha should return single commit with pull request 1`] = `
44
Array [
55
Array [
6-
"https://api.github.com/search/commits?q=hash:sha123456789%20repo:elastic/kibana&per_page=1&access_token=myAccessToken",
6+
"https://api.github.com/search/commits?q=hash:sha123456789%20repo:elastic/kibana&per_page=1",
77
Object {
8+
"auth": Object {
9+
"password": "myAccessToken",
10+
"username": "sqren",
11+
},
812
"headers": Object {
913
"Accept": "application/vnd.github.cloak-preview",
1014
},

src/services/github/addLabelsToPullRequest.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,22 @@ import { handleGithubError } from './handleGithubError';
44
import { logger } from '../logger';
55

66
export async function addLabelsToPullRequest(
7-
{ apiHostname, repoName, repoOwner, accessToken }: BackportOptions,
7+
{ apiHostname, repoName, repoOwner, accessToken, username }: BackportOptions,
88
pullNumber: number,
99
labels: string[]
1010
) {
1111
logger.info(`Adding label "${labels}" to #${pullNumber}`);
1212

1313
try {
1414
return await axios.post(
15-
`https://${apiHostname}/repos/${repoOwner}/${repoName}/issues/${pullNumber}/labels?access_token=${accessToken}`,
16-
labels
15+
`https://${apiHostname}/repos/${repoOwner}/${repoName}/issues/${pullNumber}/labels`,
16+
labels,
17+
{
18+
auth: {
19+
username: username,
20+
password: accessToken
21+
}
22+
}
1723
);
1824
} catch (e) {
1925
throw handleGithubError(e);

src/services/github/createPullRequest.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { handleGithubError } from './handleGithubError';
55
import { logger } from '../logger';
66

77
export async function createPullRequest(
8-
{ apiHostname, repoName, repoOwner, accessToken }: BackportOptions,
8+
{ apiHostname, repoName, repoOwner, accessToken, username }: BackportOptions,
99
payload: {
1010
title: string;
1111
body: string;
@@ -19,8 +19,14 @@ export async function createPullRequest(
1919

2020
try {
2121
const res: AxiosResponse<GithubIssue> = await axios.post(
22-
`https://${apiHostname}/repos/${repoOwner}/${repoName}/pulls?access_token=${accessToken}`,
23-
payload
22+
`https://${apiHostname}/repos/${repoOwner}/${repoName}/pulls`,
23+
payload,
24+
{
25+
auth: {
26+
username: username,
27+
password: accessToken
28+
}
29+
}
2430
);
2531
return {
2632
html_url: res.data.html_url,

src/services/github/fetchCommitBySha.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,22 @@ import { getFormattedCommitMessage } from './commitFormatters';
1010
export async function fetchCommitBySha(
1111
options: BackportOptions & { sha: string }
1212
): Promise<CommitSelected> {
13-
const { apiHostname, repoName, repoOwner, sha, accessToken } = options;
13+
const {
14+
apiHostname,
15+
repoName,
16+
repoOwner,
17+
sha,
18+
accessToken,
19+
username
20+
} = options;
1421
try {
1522
const res = await axios.get<GithubSearch<GithubCommit>>(
16-
`https://${apiHostname}/search/commits?q=hash:${sha}%20repo:${repoOwner}/${repoName}&per_page=1&access_token=${accessToken}`,
23+
`https://${apiHostname}/search/commits?q=hash:${sha}%20repo:${repoOwner}/${repoName}&per_page=1`,
1724
{
25+
auth: {
26+
username: username,
27+
password: accessToken
28+
},
1829
headers: {
1930
Accept: 'application/vnd.github.cloak-preview'
2031
}

src/services/github/verifyAccessToken.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ describe('verifyAccessToken', () => {
1919
await verifyAccessToken(getDefaultOptions(options));
2020

2121
expect(spy).toHaveBeenCalledWith(
22-
'https://api.github.com/repos/elastic/kibana?access_token=myAccessToken'
22+
'https://api.github.com/repos/elastic/kibana',
23+
{ auth: { password: 'myAccessToken', username: 'sqren' } }
2324
);
2425
});
2526

src/services/github/verifyAccessToken.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,21 @@ function getSSOAuthUrl(error: GithubApiError) {
1515
}
1616

1717
export async function verifyAccessToken({
18+
username,
1819
accessToken,
1920
apiHostname,
2021
repoName,
2122
repoOwner
2223
}: ReturnType<typeof validateRequiredOptions>) {
2324
try {
2425
return await axios.head(
25-
`https://${apiHostname}/repos/${repoOwner}/${repoName}?access_token=${accessToken}`
26+
`https://${apiHostname}/repos/${repoOwner}/${repoName}`,
27+
{
28+
auth: {
29+
username: username,
30+
password: accessToken
31+
}
32+
}
2633
);
2734
} catch (e) {
2835
const error = e as GithubApiError;

src/ui/cherrypickAndCreatePullRequest.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe('cherrypickAndCreatePullRequest', () => {
7575
expect(axiosPostMock).toHaveBeenCalledTimes(2);
7676
const [apiEndpoint, payload] = axiosPostMock.mock.calls[0];
7777
expect(apiEndpoint).toBe(
78-
'https://api.github.com/repos/elastic/kibana/pulls?access_token=undefined'
78+
'https://api.github.com/repos/elastic/kibana/pulls'
7979
);
8080
expect(payload.title).toBe(
8181
'[6.x] myCommitMessage (#1000) | myOtherCommitMessage (#2000)'
@@ -95,7 +95,7 @@ myPrSuffix`
9595
const [apiEndpoint, labels] = axiosPostMock.mock.calls[1];
9696

9797
expect(apiEndpoint).toBe(
98-
'https://api.github.com/repos/elastic/kibana/issues/1337/labels?access_token=undefined'
98+
'https://api.github.com/repos/elastic/kibana/issues/1337/labels'
9999
);
100100
expect(labels).toEqual(['backport']);
101101
});
@@ -126,7 +126,7 @@ myPrSuffix`
126126
expect(axiosPostMock).toHaveBeenCalledTimes(2);
127127
const [apiEndpoint, payload] = axiosPostMock.mock.calls[0];
128128
expect(apiEndpoint).toBe(
129-
'https://api.github.com/repos/elastic/kibana/pulls?access_token=undefined'
129+
'https://api.github.com/repos/elastic/kibana/pulls'
130130
);
131131
expect(payload.title).toBe('[6.x] myCommitMessage (mySha)');
132132
expect(payload.body).toBe(
@@ -141,7 +141,7 @@ myPrSuffix`
141141
const [apiEndpoint, labels] = axiosPostMock.mock.calls[1];
142142

143143
expect(apiEndpoint).toBe(
144-
'https://api.github.com/repos/elastic/kibana/issues/1337/labels?access_token=undefined'
144+
'https://api.github.com/repos/elastic/kibana/issues/1337/labels'
145145
);
146146
expect(labels).toEqual(['backport']);
147147
});

src/ui/getCommitBySha.test.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ describe('getCommitBySha', () => {
1010
it('should return a single commit without PR', async () => {
1111
const axiosSpy = mockCommitItems([commitByShaMock]);
1212
const commit = await getCommitBySha({
13+
username: 'sqren',
14+
accessToken: 'myAccessToken',
1315
repoOwner: 'elastic',
1416
repoName: 'kibana',
1517
sha: 'myCommitSha',
@@ -24,8 +26,11 @@ describe('getCommitBySha', () => {
2426
});
2527

2628
expect(axiosSpy).toHaveBeenCalledWith(
27-
'https://api.github.com/search/commits?q=hash:myCommitSha%20repo:elastic/kibana&per_page=1&access_token=undefined',
28-
{ headers: { Accept: 'application/vnd.github.cloak-preview' } }
29+
'https://api.github.com/search/commits?q=hash:myCommitSha%20repo:elastic/kibana&per_page=1',
30+
{
31+
headers: { Accept: 'application/vnd.github.cloak-preview' },
32+
auth: { password: 'myAccessToken', username: 'sqren' }
33+
}
2934
);
3035
});
3136

0 commit comments

Comments
 (0)