-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
122 lines (108 loc) · 2.95 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
const axios = require("axios");
const GREY = 9545382;
// const BLUE = 39393
const RED = 16333359;
const GREEN = 53606;
const YELLOW = 16302848;
const INFO_IMG = "https://img.icons8.com/color/2x/info.png";
const ERROR_IMG = "https://img.icons8.com/flat_round/2x/stop.png";
const WARNING_IMG = "https://img.icons8.com/flat_round/2x/pause.png";
const SUCCESS_IMG = "https://img.icons8.com/flat_round/2x/checkmark.png";
const addFeild = (inline = false) =>
(name) => (value) => ({ inline, name, value });
module.exports.subscribeDiscord = function (event) {
const build = eventToBuild(event.data);
const status = [
"WORKING",
"SUCCESS",
"FAILURE",
"INTERNAL_ERROR",
"TIMEOUT",
"CANCELLED",
];
if (!status.includes(build.status)) return;
const buildStatus = build.status;
const substitutions = build.substitutions;
const logUrl = build.logUrl;
const branch = substitutions.BRANCH_NAME;
const repo = substitutions.REPO_NAME;
const commit = substitutions.COMMIT_SHA;
const owner = process.env.OWNER_NAME;
const DISCORD_WEBHOOK_URL = process.env.DISCORD_WEBHOOK_URL;
const inlineField = addFeild(true);
fetchGitMessage({ commit, repo, owner }).then((result) => {
const thumbnail = getThumbnail(buildStatus);
const color = getColor(buildStatus);
const title = `${repo}(${branch}) is ${capitalized(buildStatus)}`
axios.post(DISCORD_WEBHOOK_URL, {
embeds: [
{
title,
color,
thumbnail,
fields: [
inlineField("Repo")(repo),
inlineField("Branch")(branch),
inlineField("Status")(buildStatus),
addFeild()("See log")(logUrl),
addFeild()("Commit")(getMessage(result)),
],
},
],
})
.catch((e) => {
console.error(e.response.message);
});
});
};
const eventToBuild = (data) => {
return JSON.parse(Buffer.from(data, "base64").toString());
};
const getColor = (
statusIndex,
) => {
const colorSet = { SUCCESS: GREEN, WORKING: GREY, CANCELLED: YELLOW };
return colorSet[statusIndex] || RED;
};
const getThumbnail = (status) => {
const imgSet = {
SUCCESS: SUCCESS_IMG,
WORKING: INFO_IMG,
CANCELLED: WARNING_IMG,
};
return {
url: imgSet[status] || ERROR_IMG,
width: 64,
height: 64
};
};
const getMessage = (result) => {
try {
return result.data.data.repository.object.message;
} catch (error) {
return error;
}
};
const fetchGitMessage = ({ commit, repo, owner }) =>
axios.post(
"https://api.github.com/graphql",
{
query: `{
repository(owner:"${owner}",name:"${repo}") {
object(oid: "${commit}") {
... on Commit {
message
}
}
}
}`,
},
{
headers: {
Authorization: `bearer ${process.env.GITHUB_API_TOKEN}`,
},
},
);
const capitalized = (textString) => {
return `${textString[0].toUpperCase()}${textString.toLowerCase().slice(1)}`
}