-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathgithub.js
More file actions
52 lines (44 loc) · 1.19 KB
/
github.js
File metadata and controls
52 lines (44 loc) · 1.19 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'use strict';
const { httpsGetJson } = require('../oauth/http');
const GITHUB_USER_HEADERS = {
'Accept': 'application/vnd.github+json',
'X-GitHub-Api-Version': '2022-11-28',
};
function pickGitHubEmail(list) {
if (!Array.isArray(list)) return null;
const p = list.find(i => i.primary && i.verified) ||
list.find(i => i.primary) ||
list.find(i => i.verified) ||
list[0];
return p ? { email: p.email, verified: !!p.verified } : null;
}
module.exports = {
type: 'github',
matches({ host, name, profile }) {
if (host === 'api.github.com' || /github/.test(name)) return true;
if (profile && (profile.login !== undefined || profile.html_url || profile.gravatar_id)) {
return true;
}
return false;
},
userRouteHeaders() {
return GITHUB_USER_HEADERS;
},
parse(profile) {
return {
id: profile.id,
displayName: profile.login || profile.name,
fullname: profile.name || profile.login,
picture: profile.avatar_url,
email: profile.email || undefined,
username: profile.login,
};
},
async fetchEmail({ accessToken }) {
return pickGitHubEmail(await httpsGetJson({
url: 'https://api.github.com/user/emails',
accessToken,
headers: GITHUB_USER_HEADERS,
}));
},
};