1
1
'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
17
10
} ) ;
18
11
19
- const { body : data } = result ;
20
-
21
12
if ( data . total_count === 0 ) {
22
13
throw new Error ( `Couldn't find username for \`${ email } \`` ) ;
23
14
}
@@ -30,20 +21,17 @@ module.exports = async (email, token) => {
30
21
throw new Error ( 'Email required' ) ;
31
22
}
32
23
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'
41
27
} ) ;
42
28
43
- const { body : data } = result ;
29
+ const { data} = await octokit . search . users ( {
30
+ q : `${ email } in:email`
31
+ } ) ;
44
32
45
33
if ( data . total_count === 0 ) {
46
- return searchCommits ( email , token ) ;
34
+ return searchCommits ( octokit , email ) ;
47
35
}
48
36
49
37
return data . items [ 0 ] . login ;
0 commit comments