This repository was archived by the owner on Jan 5, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
134 lines (118 loc) · 3.68 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const {promisify} = require("util")
const crypto = require("crypto")
// API connection to GitHub
// https://github.com/octokit/rest.js
const octokit = require("@octokit/rest")()
octokit.authenticate({
type: "token",
token: process.env.GH_TOKEN
})
// API connection to mailgun
// https://github.com/bojand/mailgun-js
const mailgun = require("mailgun-js")({
apiKey: process.env.MG_API,
domain: process.env.MG_DOMAIN
})
const redis = require("redis")
const client = redis.createClient(process.env.REDIS_URL)
// Setup promise redis classes
const getAsync = promisify(client.get).bind(client)
const setAsync = promisify(client.set).bind(client)
// Creating a redis key hex sha
const redisKey = crypto.createHmac("sha256", process.env.GH_USERNAME)
.update("githubthings")
.digest("hex")
const assignedType = () => {
switch (process.env.ASSIGNED_TYPE) {
case "issue":
return "is:issue"
break;
case "pull":
return "is:pull"
break;
case "both":
return ""
break;
default:
return "is:issue"
}
}
const queries = [
// Assigned issues
{
"name": "Assigned",
"q": `is:open ${assignedType()} assignee:${process.env.GH_USERNAME} archived:false`,
"enabled": (process.env.ASSIGNED || "true")
},
// Review requested
{
"name": "Review Requested",
"q": `is:open is:pr review-requested:${process.env.GH_USERNAME} archived:false`,
"enabled": (process.env.REVIEWS || "true")
}
]
const debug = (...args) => {
if (process.env.DEBUG == "true") {
if (args.length > 0) {
console.info(args.join("\n"))
}
return true
}
return false
}
// Construct the email and send it.
const emailItem = (item, queryType) => {
// Email subject
const subject = `<${queryType}> ${item.title} · ${item.repository_url.replace("https://api.github.com/repos/", "")}`
// Email body
const body = `${item.html_url}\n\n` +
`Author: ${item.user.login}` +
`\nOpened: ${(new Date(item.created_at)).toLocaleDateString("en-US", { "timeZone": "America/Los_Angeles" })}` +
(item.labels.length > 0 ? `\nLabels: ${item.labels.map(l => { return `<${l.name}>`}).join(" ")}` : "")
if (debug(subject + "\n--\n" + body + "\n\n-------------------------------\n\n")) {
return Promise.resolve(`${item.id}`)
}
return Promise.resolve(mailgun.messages().send({
from: `GitHub Things <github-things@${process.env.MG_DOMAIN}>`,
to: process.env.THINGS_EMAIL,
subject: subject,
text: body
}))
.then(() => `${item.id}`)
}
// Get all the queried items
Promise.all(queries.map(q => {
if (q["enabled"] == "false") {
return { "items": [], "type": q["name"] }
}
return octokit.search.issues({
q: q["q"]
}).then( results => {
return { "items": results.data.items, "type": q["name"] }
})
})).then( resultsArray => {
return getAsync(redisKey).then(oldItems => {
if (oldItems == null) {
oldItems = []
} else {
oldItems = debug() ? [] : oldItems.split(",")
}
debug("OLD ITEMS: ", oldItems);
return Promise.all(resultsArray.map( results => {
const queryType = results["type"]
// Remove any already sent
return Promise.resolve(results["items"].filter(item => !oldItems.includes(`${item.id}`)))
.then( newItems => {
// Send issues, return ids
return Promise.all(newItems.map(item => {
return emailItem(item, queryType)
}))
}).then( sentItems => {
if(debug("NEW ITEMS: ", sentItems)) {
return
}
return setAsync(redisKey, oldItems.concat(sentItems).toString())
})
}))
})
}).then(() => client.quit())