-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
177 lines (160 loc) · 4.43 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import axios from "axios";
import crypto from "crypto";
export async function handler(event) {
const MY_SECRET_TOKEN = process.env.MY_SECRET_TOKEN;
const signature = event.headers["X-Hub-Signature-256"];
const payload = JSON.stringify(event.body);
if (!verifySignature(MY_SECRET_TOKEN, signature, payload)) {
return {
statusCode: 401,
body: JSON.stringify({
message: "Invalid signature ",
}),
};
}
try {
const githubEvent = JSON.parse(event.body);
if (githubEvent.action === "created") {
const slackMessage = {
channel: process.env.SLACK_CHANNEL,
text: `New star for the repository! 🌟 Total stars: ${githubEvent.repository.stargazers_count}`,
blocks: [
{
type: "rich_text",
elements: [
{
type: "rich_text_section",
elements: [
{
type: "emoji",
name: "star",
},
{
type: "text",
text: " Woohoo, we have a new star! ",
style: {
bold: true,
},
},
{
type: "emoji",
name: "star",
},
],
},
],
},
{
type: "rich_text",
elements: [
{
type: "rich_text_list",
style: "bullet",
elements: [
{
type: "rich_text_section",
elements: [
{
type: "text",
text: `repo: ${githubEvent.repository.full_name}`,
},
],
},
{
type: "rich_text_section",
elements: [
{
type: "text",
text: `user: ${githubEvent.sender.login}`,
},
],
},
],
},
],
},
{
type: "rich_text",
elements: [
{
type: "rich_text_section",
elements: [
{
type: "text",
text: `Total stars: ${githubEvent.repository.stargazers_count}`,
},
],
},
],
},
],
};
const BEARER_TOKEN = process.env.BEARER_TOKEN;
// Await the axios call to ensure it completes before the function returns
const response = await axios.post(
"https://slack.com/api/chat.postMessage",
slackMessage,
{
headers: {
Authorization: `Bearer ${BEARER_TOKEN}`,
"Content-Type": "application/json",
},
}
);
console.log("Message posted successfully!", response.data);
return {
statusCode: 200,
body: JSON.stringify({
message: "Event processed for repo: " + githubEvent.repository.name,
}),
};
} else {
return {
statusCode: 200,
body: JSON.stringify({ message: "No new star event" }),
};
}
} catch (error) {
console.log("Error making Slack call:", error.message);
return {
statusCode: 500,
body: JSON.stringify({ message: "An error occurred: " + error.message }),
};
}
}
let encoder = new TextEncoder();
async function verifySignature(secret, header, payload) {
let parts = header.split("=");
let sigHex = parts[1];
let algorithm = { name: "HMAC", hash: { name: "SHA-256" } };
let keyBytes = encoder.encode(secret);
let extractable = false;
let key = await crypto.subtle.importKey(
"raw",
keyBytes,
algorithm,
extractable,
["sign", "verify"]
);
let sigBytes = hexToBytes(sigHex);
let dataBytes = encoder.encode(payload);
let equal = await crypto.subtle.verify(
algorithm.name,
key,
sigBytes,
dataBytes
);
return equal;
}
function hexToBytes(hex) {
let len = hex.length / 2;
let bytes = new Uint8Array(len);
let index = 0;
for (let i = 0; i < hex.length; i += 2) {
let c = hex.slice(i, i + 2);
let b = parseInt(c, 16);
bytes[index] = b;
index += 1;
}
return bytes;
}