-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
122 lines (103 loc) · 3.03 KB
/
index.ts
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
import { parse } from "url";
import { IncomingMessage, ServerResponse } from "http";
import { fetchRecentlyClosedPullRequests } from "weekly-summary-typescript";
import sgMail from "@sendgrid/mail";
import { IPullRequestsForRepos } from "weekly-summary-typescript/dist/github";
import marked from "marked";
export default async function(
req: IncomingMessage,
res: ServerResponse,
pullRequestFetcher = fetchRecentlyClosedPullRequests,
emailer = sgMail
) {
console.log("Running schedule for generating Weekly Summary");
const queryData = parse(req.url, true).query;
console.log(`Received the following query parameters: `);
console.log(queryData);
let organization = queryData.organization || "";
if (organization.length == 0) {
res.statusCode = 400;
res.end("Must provide 'organization' query parameter.");
return;
} else if (Array.isArray(organization)) {
organization = organization[0];
}
let to = queryData.to || "";
if (!Array.isArray(to)) {
to = to.split(",");
}
to = to.filter(email => !email.match(new RegExp(/^\s*$/)));
console.log("Requesting Pull Requests");
let recentlyClosedPullRequests: IPullRequestsForRepos;
try {
recentlyClosedPullRequests = await pullRequestFetcher(
{ organization },
process.env.GITHUB_AUTH_TOKEN
);
} catch (e) {
res.statusCode = 400;
res.end(`Failed to fetch pull requests. Received: ${e}`);
return;
}
console.log("Received Pull Requests. Generating e-mail.");
const markdownBody = convertPullRequestsToMarkdown(
recentlyClosedPullRequests
);
const htmlBody = marked(markdownBody);
await sendEmail({
to: to,
textBody: markdownBody,
htmlBody: htmlBody,
emailer: emailer
});
res.end(htmlBody);
}
function convertPullRequestsToMarkdown(
pullRequests: IPullRequestsForRepos
): string {
console.log("Converting Pull Request limit to Markdown");
return Object.entries(pullRequests).reduce(
(emailBody, [repoName, pullRequests]) => {
emailBody += `# ${repoName}\n\n`;
pullRequests.forEach(pullRequest => {
emailBody += `* ${pullRequest.title} (${pullRequest.url})\n`;
});
emailBody += "\n";
return emailBody;
},
""
);
}
async function sendEmail({
to,
textBody,
htmlBody,
emailer
}: {
to: string[];
textBody: string;
htmlBody: string;
// @sengrid/mail did not export the `MailService` type, but that's what this should be
emailer: any;
}) {
if (!process.env.SENDGRID_API_KEY || to.length == 0) {
console.log(
"SendGrid is not set up, or no `to` addresses specified. " +
"Skipping sending email."
);
return;
}
console.log("Sending email");
emailer.setApiKey(process.env.SENDGRID_API_KEY);
const email = {
to: to,
from: "[email protected]",
subject: `Weekly Summary - ${new Date().toDateString()}`,
text: textBody,
html: htmlBody
};
const emailResponse = await emailer.send(email);
console.log(`Sent email. Response: `);
console.log(emailResponse);
return emailResponse;
}