ZIm/script/issue_response/main.js
Marshall Bowers fc85ca0101
ci: Fix issue response script (#24891)
This PR fixes the issue response script.

There were a number of things preventing it from working:

- The directory name used in the GitHub Action did not match the one on
disk.
  - The script has been moved accordingly
- `ts-node` does not support ESM.
- `ts-node` seems unmaintained, so I changed the script to be plain JS
that is type-checked with TypeScript.
- The data being sent to the Slack API was invalid:
- Each section block can only have a maximum of 3000 characters in the
`text` field, so we need to break up the issue list across multiple
sections.
- We needed to escape `&`, `<`, and `>` characters in the issue titles.

Release Notes:

- N/A
2025-02-14 19:37:33 +00:00

99 lines
2.5 KiB
JavaScript

import { Octokit } from "@octokit/rest";
import { IncomingWebhook } from "@slack/webhook";
/**
* The maximum length of the `text` in a section block.
*
* [Slack Docs](https://api.slack.com/reference/block-kit/blocks#section)
*/
const SECTION_BLOCK_TEXT_LIMIT = 3000;
async function main() {
const octokit = new Octokit({ auth: process.env["GITHUB_TOKEN"] });
if (!process.env["SLACK_ISSUE_RESPONSE_WEBHOOK_URL"]) {
throw new Error("SLACK_ISSUE_RESPONSE_WEBHOOK_URL is not set");
}
const webhook = new IncomingWebhook(
process.env["SLACK_ISSUE_RESPONSE_WEBHOOK_URL"],
);
const owner = "zed-industries";
const repo = "zed";
const staff = await octokit.paginate(octokit.rest.orgs.listMembers, {
org: owner,
per_page: 100,
});
let staffHandles = staff.map((member) => member.login);
let commenterFilters = staffHandles.map((name) => `-commenter:${name}`);
let authorFilters = staffHandles.map((name) => `-author:${name}`);
const q = [
`repo:${owner}/${repo}`,
"is:issue",
"state:open",
"created:>=2025-02-01",
"sort:created-asc",
...commenterFilters,
...authorFilters,
];
const response = await octokit.rest.search.issuesAndPullRequests({
q: q.join("+"),
per_page: 100,
});
let issues = response.data.items;
let issueLines = issues.map((issue, index) => {
const formattedDate = new Date(issue.created_at).toLocaleDateString(
"en-US",
{
year: "numeric",
month: "short",
day: "numeric",
},
);
const sanitizedTitle = issue.title
.replaceAll("&", "&amp;")
.replaceAll("<", "&lt;")
.replaceAll(">", "&gt;");
return `${index + 1}. ${formattedDate}: <${issue.html_url}|${sanitizedTitle}>\n`;
});
const sections = [];
/** @type {string[]} */
let currentSection = [];
let currentSectionLength = 0;
for (const issueLine of issueLines) {
if (currentSectionLength + issueLine.length <= SECTION_BLOCK_TEXT_LIMIT) {
currentSection.push(issueLine);
currentSectionLength += issueLine.length;
} else {
sections.push(currentSection);
currentSection = [];
currentSectionLength = 0;
}
}
if (currentSection.length > 0) {
sections.push(currentSection);
}
const blocks = sections.map((section) => ({
type: "section",
text: {
type: "mrkdwn",
text: section.join("").trimEnd(),
},
}));
await webhook.send({ blocks });
}
main().catch((error) => {
console.error("An error occurred:", error);
process.exit(1);
});