-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
85 lines (67 loc) · 2.59 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
const Discord = require("discord.js");
const core = require("@actions/core");
const AAU_ICON =
"https://media-exp1.licdn.com/dms/image/C510BAQHgfIxjqWSSsQ/company-logo_200_200/0/1519855922416?e=2159024400&v=beta&t=_L2XLMCXeEzSX_c8oBMM_uuEK1kgfno7ViFJXD5BP6U";
const GITHUB_ICON = "https://avatars.githubusercontent.com/u/86346038?s=200&v=4";
try {
const team = JSON.parse(core.getInput("team"));
const scrumMasters = determineScrumMasters(team);
const embed = buildEmbed(scrumMasters);
postScrumMastersDiscord(embed);
} catch (error) {
core.setFailed(error.message);
}
function determineScrumMasters(team) {
if (team.length < 2) throw new Error("You need a larger team");
const first = process.env.GITHUB_RUN_NUMBER % team.length;
const second = Math.random() * team.length - 1;
const scrumMasterOne = team.splice(first, 1);
if (team.length < 4) return scrumMasterOne;
const scrumMasterTwo = team.splice(second, 1);
return scrumMasterOne.concat(scrumMasterTwo);
}
function buildEmbed(scrumMasters) {
const week = getWeekStartAndEnd();
let result = new Discord.MessageEmbed()
.setTitle("New Scrum Master(s)")
.setDescription(`The scrum masters for ${week.start} to ${week.end}`)
.setColor("#211a52")
.setAuthor("AAU-Dat", AAU_ICON, "http://github.com/AAU-Dat")
.setFooter("Generated by the scrummasters github action.")
.addField("Scrum master", scrumMasters[0], true);
if (scrumMasters.length > 1) {
result.addField("Second Scrum Master", scrumMasters[1], true);
}
return result;
}
function postScrumMastersDiscord(embed) {
const webhook = process.env.DISCORD_WEBHOOK;
const { id, token } = breakWebhook(webhook);
const webhookClient = new Discord.WebhookClient(id, token);
webhookClient
.send({
username: "Github Action",
avatarURL: GITHUB_ICON,
embeds: [embed],
})
.catch(console.error);
webhookClient.destroy();
}
function breakWebhook(webhook) {
const array = webhook.split("/");
const length = array.length;
if (length < 3) throw new Error("Webhook probably misrepresented");
if (array[array.length - 1] == "github") {
return { id: array[length - 3], token: array[length - 2] };
}
return { id: array[length - 2], token: array[length - 1] };
}
function getWeekStartAndEnd(){
const today = new Date();
const weekend = new Date();
weekend.setDate(weekend.getDate() + 6);
const options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
const todayString = today.toDateString();
const weekendString = weekend.toDateString();
return {start: todayString, end: weekendString}
}