-
Notifications
You must be signed in to change notification settings - Fork 1
/
sendDeployStartMessage.js
99 lines (87 loc) · 2.44 KB
/
sendDeployStartMessage.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
import { Octokit } from "octokit";
import { WebClient } from "@slack/web-api";
import { createDeploymentNotificationMessage } from "./slackMessages.js ";
const octokit = new Octokit({
auth: process.env.GITHUB_TOKEN,
});
const slackClient = new WebClient(process.env.SLACK_TOKEN, {
rejectRateLimitedCalls: true,
});
// TODO: Make this work for a direct push (no pr)
export async function sendDeployStartMessage({
slack_channel,
sha1,
repo_owner,
repo_name,
workflow_url,
}) {
console.log("Preparing deployment message with following params", {
slack_channel,
sha1,
repo_owner,
repo_name,
workflow_url,
});
const results = await octokit.request(
"GET /repos/{owner}/{repo}/commits/{commit_sha}/pulls{?per_page,page}",
{
owner: repo_owner,
repo: repo_name,
commit_sha: sha1,
}
);
const incompletePrData = results.data[0];
const pr = await octokit.request(
"GET /repos/{owner}/{repo}/pulls/{pull_number}",
{
owner: repo_owner,
repo: repo_name,
pull_number: incompletePrData.number,
}
);
const ghIdOfUserPrMerger = pr.data.merged_by.login;
const ghIdOfUserPrAuthor = pr.data.user.login;
const ghUserOfPrAuthor = await octokit.request("GET /users/{username}", {
username: ghIdOfUserPrAuthor,
});
const ghUserOfPrMerger = await octokit.request("GET /users/{username}", {
username: ghIdOfUserPrMerger,
});
const slackUsers = await slackClient.users.list();
/**
* Slack's docs doesn't say what their limit is for how
* many users you can fetch without causing an error. We'll
* see how long we can get away without using pagination.
*/
const slackUserForPrAuthor = slackUsers.members.find(
(user) => user.real_name === ghUserOfPrAuthor.data.name
);
const slackUserForPrMerger = slackUsers.members.find(
(user) => user.real_name === ghUserOfPrMerger.data.name
);
const blocks = createDeploymentNotificationMessage({
users: {
author: {
gh: ghUserOfPrAuthor,
slack: slackUserForPrAuthor,
},
merger: {
gh: ghUserOfPrMerger,
slack: slackUserForPrMerger,
},
},
pr,
repo_name,
sha1,
workflow_url,
});
const message = await slackClient.chat.postMessage({
channel: slack_channel,
blocks: blocks.initialMessage,
});
return slackClient.chat.postMessage({
channel: slack_channel,
thread_ts: message.ts,
blocks: blocks.thread,
});
}