This repository has been archived by the owner on Mar 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
141 lines (127 loc) · 3.42 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
135
136
137
138
139
140
141
"use strict";
const axios = require("axios");
const colourForResult = (result) => {
const blue = "#0000ff";
const red = "#ed0000";
const green = "#2ecc71";
if (result === "pass") {
return green;
} else if (result === "fail") {
return red;
}
return blue;
};
const getHeader = (test_name, result) => {
if (result === "pass") {
return {
title: test_name,
subtitle: "Runscope tests have passed successfully",
imageUrl:
"https://upload.wikimedia.org/wikipedia/commons/thumb/a/a1/Antu_vcs-normal.svg/200px-Antu_vcs-normal.svg.png",
imageStyle: "IMAGE",
};
} else if (result === "fail") {
return {
title: test_name,
subtitle: `Runscope tests failed for ${test_name}`,
imageUrl:
"https://upload.wikimedia.org/wikipedia/commons/thumb/5/5e/Antu_task-reject.svg/200px-Antu_task-reject.svg.png",
imageStyle: "IMAGE",
};
}
};
const getSectionFromRequest = (request) => {
const widgets = [
{
keyValue: {
topLabel: "Url",
content: request.url,
},
},
{
keyValue: {
topLabel: "Status",
content: `<font color=\"${colourForResult(request.result)}\">${
request.result
}</font>`,
},
},
request.result === "fail" ? {
keyValue: {
topLabel: "Response Status Code",
content: request.response_status_code,
},
} : null,
];
return {
widgets: widgets.filter(e => e !== null)
}
};
const getCards = (webhookData) => {
const { test_name, test_url, test_run_url, result, requests } = webhookData;
const requestSections = requests.map((request) =>
this.getSectionFromRequest(request)
);
return [
{
header: getHeader(test_name, result),
sections: [
...requestSections,
{
widgets: [
{
buttons: [
{
textButton: {
text: "View Result",
onClick: { openLink: { url: test_run_url } },
},
},
{
textButton: {
text: "Documentation",
onClick: { openLink: { url: "https://github.com/guardian/support-frontend/wiki/Runscope-Testing" } },
},
},
],
},
],
},
],
},
];
};
const postGchat = (endpointUrl, gchatBody) => {
axios.defaults.headers.common["Content-Type"] = "application/json";
return axios.post(endpointUrl, gchatBody);
};
const lambdaResponse = (statusCode, body) => {
return {
headers: { "Content-Type": "application/json" },
statusCode: statusCode,
body: JSON.stringify(body),
};
};
exports.handler = async (event) => {
console.log(event.body);
const webhookData = JSON.parse(event.body);
// the gchat webhook url is passed in as a header
const gchatUrl = event.headers.gchaturl;
if (!gchatUrl) {
console.log("no gchat url provided");
}
//post message to gchat
try {
let gchatPost = await postGchat(gchatUrl, {
cards: getCards(webhookData),
});
console.log(`Gchat Response status: ${gchatPost.status}`);
return lambdaResponse(200, "Success");
} catch (e) {
console.warn(e);
return lambdaResponse(500, `Error posting to Gchat: ${e}`);
}
};
exports.getSectionFromRequest = getSectionFromRequest;
exports.getCards = getCards;
exports.getHeader = getHeader;