-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.js
37 lines (29 loc) · 852 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import {Octokit} from '@octokit/rest';
async function searchCommits(octokit, email) {
const {data} = await octokit.search.commits({
q: `author-email:${email}`,
sort: 'author-date',
// eslint-disable-next-line camelcase
per_page: 1,
});
if (data.total_count === 0) {
throw new Error(`Couldn't find username for \`${email}\``);
}
return data.items[0].author.login;
}
export default async function githubUsername(email, {token} = {}) {
if (!(typeof email === 'string' && email.includes('@'))) {
throw new Error('Email required');
}
const octokit = new Octokit({
auth: token,
userAgent: 'https://github.com/sindresorhus/github-username',
});
const {data} = await octokit.search.users({
q: `${email} in:email`,
});
if (data.total_count === 0) {
return searchCommits(octokit, email);
}
return data.items[0].login;
}