forked from seisvelas/comment-email-address-alerts
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
86 lines (70 loc) · 2.87 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
81
82
83
84
85
86
const core = require('@actions/core');
const github = require('@actions/github');
const https = require('https');
/*
> There were [N] email addresses found in the above comment. Please:
>
> 1) click `three dots` -> `edit` to remove the email addresses
> 2) click `edited` in the comment header, and click on the previous revision of the comment
> 3) when viewing the old revision with an email in it, click `options` -> `delete this revision from history`
*/
try {
const payload = github.context.payload;
const comment = payload.comment ? payload.comment.body : payload.issue.body;
console.log(`comment: ${comment}`)
const exemptDomainsInput = core.getInput('exemptions')
const exemptDomains = exemptDomainsInput.split(',').map(d => d.toLowerCase());
const ignoredEmailsInput = core.getInput('ignoredEmails');
const ignoredEmails = ignoredEmailsInput.split(',').map(e => e.toLowerCase());
console.log(`Exempt domains: ${exemptDomains}`);
console.log(`Ignored emails: ${ignoredEmails}`);
let emails = comment
? comment.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi)
: null;
if (emails === null) emails = [];
emails = emails.map(e => e.toLowerCase());
if (exemptDomains && exemptDomains.length && emails.length) {
emails = emails.filter(addr => !exemptDomains.some(d => addr.endsWith('@' + d) || addr.endsWith('.' + d)));
}
if (ignoredEmails && ignoredEmails.length && emails.length) {
emails = emails.filter(addr => !ignoredEmails.some(e => addr === e));
}
console.log(`emails: ${emails}`)
if (emails.length) {
const repoToken = core.getInput('repo-token');
const notice = "There were " + emails.length + " email addresses found in the above comment. Please:\n\n" +
"1) Click `three dots` -> `edit` to remove the email addresses.\n" +
"2) Click `edited` in the comment header, and click on the previous revision of the comment.\n" +
"3) When viewing the old revision with an email in it, click `options` -> `delete this revision from history`.";
// make comment
const data = JSON.stringify({
"body": notice
})
const options = {
hostname: 'api.github.com',
port: 443,
path: `/repos/${payload.repository.full_name}/issues/${payload.issue.number}/comments`,
method: 'POST',
headers: {
'Content-Type': 'application/json',
"Authorization": `Bearer ${repoToken}`,
"User-Agent": "Email Comment GitHub Action"
}
}
const req = https.request(options, res => {
console.log(`statusCode: ${res.statusCode}`)
res.on('data', d => {
process.stdout.write(d)
})
})
req.on('error', error => {
console.error(error)
})
req.write(data)
req.end()
} else {
console.log("No emails found in comment");
}
} catch (error) {
core.setFailed(error.message);
}