forked from evvanErb/get-github-email-by-username-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
80 lines (63 loc) · 2.39 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import fetch from "node-fetch";
const { Octokit } = require("@octokit/core");
const core = require('@actions/core');
function findEmailCommitAPI(apiData) {
const emailPosition = apiData.indexOf("\"email\":\"");
if (emailPosition < 0) {
return null;
}
const email = apiData.substring((emailPosition + 9), (emailPosition + 9 + (apiData.substring(emailPosition + 9).indexOf('\"'))));
//if found a bot email, continue searching
if (email.indexOf("users.noreply.github.com") >= 0) {
return findEmailCommitAPI(apiData.substring(emailPosition + 9));
}
else {
return email;
}
}
try {
//inputs defined in action metadata file
const usernameForEmail = core.getInput('github-username');
const token = core.getInput('token');
console.log(`[*] Getting ${usernameForEmail}\'s GitHub email`);
//attempt to use auth token to get email via accessing the user's API page
let userAPIData = null;
try {
const octokit = new Octokit({ auth: `${token}` });
userAPIData = await octokit.request(`GET /users/${usernameForEmail}`, {});
} catch (error) {
console.log("[!] " + error.message);
}
// Extract the email if the user's API was accessed successfully
let emailUserpage = null;
if (userAPIData != null && userAPIData.data != null && userAPIData.data.email != null && userAPIData.data.email != "") {
emailUserpage = userAPIData.data.email;
}
//email not found on user's API page or failed to authenticate with token, fallback to old method to attempt email retrieval
if (emailUserpage == null) {
console.log(`[*] Falling back to old API retrieval method`);
//fetch user's public events page
fetch(`https://api.github.com/users/${usernameForEmail}/events/public`)
.then(function(response) {
// When the page is loaded convert it to text
return response.text()
})
.then((apiData) => {
const emailEventsPage = findEmailCommitAPI(apiData);
if (emailEventsPage == null) {
throw Error('[!!!] Could not find email in API Data');
}
console.log(`[*] Found ${usernameForEmail}\'s email: ${emailEventsPage}`)
core.setOutput("email", emailEventsPage);
})
.catch((error) => {
core.setFailed(error.message);
});
}
else {
console.log(`[*] Found ${usernameForEmail}\'s email: ${emailUserpage}`)
core.setOutput("email", emailUserpage);
}
} catch (error) {
core.setFailed(error.message);
}