-
Notifications
You must be signed in to change notification settings - Fork 592
152 lines (136 loc) · 4.76 KB
/
Copy pathmapping-notify.yml
File metadata and controls
152 lines (136 loc) · 4.76 KB
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
name: Notify Discord on Map PR
on:
pull_request:
types: [opened, labeled]
jobs:
notify_discord:
runs-on: ubuntu-latest
steps:
- name: Check for label
id: check-label
uses: actions/github-script@v7
with:
script: |
const pr = context.payload.pull_request;
if (!pr) {
core.setFailed("No PR data found!");
return;
}
const labels = pr.labels.map(l => l.name);
if (labels.includes("Changes: Map")) {
core.setOutput("notify", "true");
} else {
core.setOutput("notify", "false");
}
- name: Send to Discord
if: steps.check-label.outputs.notify == 'true'
uses: actions/github-script@v7
env:
DISCORD_WEBHOOK: ${{ secrets.DISCORD_MAPPING_WEBHOOK_URL }}
with:
script: |
const pr = context.payload.pull_request;
if (!pr) {
core.setFailed("No PR data found!");
return;
}
const https = require("https");
const { URL } = require("url");
const url = process.env.DISCORD_WEBHOOK;
if (!url) {
core.setFailed("No Discord webhook provided.");
return;
}
// Prepare a single embed object
const embed = {
title: "New PR with map changes!",
description: pr.title || "Untitled PR",
url: pr.html_url,
color: 16776960,
fields: [],
footer: {
text: "GitHub Actions",
icon_url: "https://github.githubassets.com/images/modules/logos_page/GitHub-Mark.png"
},
timestamp: new Date().toISOString(),
};
// Function to split a string into chunks of given size
function chunkString(str, size) {
const chunks = [];
let i = 0;
while (i < str.length) {
chunks.push(str.slice(i, i + size));
i += size;
}
return chunks;
}
// Split the PR body to avoid Discord's 1024-char field limit
const prBody = pr.body || "No description provided";
const bodyChunks = chunkString(prBody, 1024);
// Add each chunk as a separate field (up to 25 fields total)
if (bodyChunks.length === 1) {
embed.fields.push({
name: "Description",
value: bodyChunks[0],
inline: false
});
} else {
for (let i = 0; i < bodyChunks.length && i < 25; i++) {
embed.fields.push({
name: `Description (part ${i+1}/${bodyChunks.length})`,
value: bodyChunks[i],
inline: false
});
}
}
// Add other PR info as separate fields
embed.fields.push(
{
name: "Author",
value: pr.user.login,
inline: true
},
{
name: "Repository",
value: process.env.GITHUB_REPOSITORY,
inline: true
},
{
name: "PR Number",
value: `#${pr.number}`,
inline: true
}
);
// Final payload
const payload = { embeds: [embed] };
// Log it for debugging
core.info("Discord payload: " + JSON.stringify(payload));
// Prepare https request
const urlObj = new URL(url);
const data = JSON.stringify(payload);
const options = {
hostname: urlObj.hostname,
path: urlObj.pathname + (urlObj.search || ""),
protocol: urlObj.protocol,
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Length": Buffer.byteLength(data)
}
};
const req = https.request(options, res => {
let body = "";
res.on("data", chunk => body += chunk);
res.on("end", () => {
if (res.statusCode < 200 || res.statusCode >= 300) {
core.setFailed(`Discord webhook returned ${res.statusCode}: ${body}`);
} else {
core.info("Successfully sent notification to Discord.");
}
});
});
req.on("error", err => {
core.setFailed(`Failed to send message: ${err.message}`);
});
req.write(data);
req.end();