Map Edits - Hotel and Lobster Station #41
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |