Skip to content

Commit 9e95e07

Browse files
authored
Migrate to @octokit/rest (#12)
1 parent 2640b0d commit 9e95e07

File tree

2 files changed

+16
-28
lines changed

2 files changed

+16
-28
lines changed

index.js

+15-27
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,14 @@
11
'use strict';
2-
const ghGot = require('gh-got');
3-
4-
async function searchCommits(email, token) {
5-
const result = await ghGot('search/commits', {
6-
token,
7-
query: {
8-
q: `author-email:${email}`,
9-
sort: 'author-date',
10-
// eslint-disable-next-line camelcase
11-
per_page: 1
12-
},
13-
headers: {
14-
accept: 'application/vnd.github.cloak-preview',
15-
'user-agent': 'https://github.com/sindresorhus/github-username'
16-
}
2+
const {Octokit} = require('@octokit/rest');
3+
4+
async function searchCommits(octokit, email) {
5+
const {data} = await octokit.search.commits({
6+
q: `author-email:${email}`,
7+
sort: 'author-date',
8+
// eslint-disable-next-line camelcase
9+
per_page: 1
1710
});
1811

19-
const {body: data} = result;
20-
2112
if (data.total_count === 0) {
2213
throw new Error(`Couldn't find username for \`${email}\``);
2314
}
@@ -30,20 +21,17 @@ module.exports = async (email, token) => {
3021
throw new Error('Email required');
3122
}
3223

33-
const result = await ghGot('search/users', {
34-
token,
35-
query: {
36-
q: `${email} in:email`
37-
},
38-
headers: {
39-
'user-agent': 'https://github.com/sindresorhus/github-username'
40-
}
24+
const octokit = new Octokit({
25+
auth: token,
26+
userAgent: 'https://github.com/sindresorhus/github-username'
4127
});
4228

43-
const {body: data} = result;
29+
const {data} = await octokit.search.users({
30+
q: `${email} in:email`
31+
});
4432

4533
if (data.total_count === 0) {
46-
return searchCommits(email, token);
34+
return searchCommits(octokit, email);
4735
}
4836

4937
return data.items[0].login;

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"git"
3030
],
3131
"dependencies": {
32-
"gh-got": "^8.1.0"
32+
"@octokit/rest": "^18.0.6"
3333
},
3434
"devDependencies": {
3535
"ava": "^1.4.1",

0 commit comments

Comments
 (0)