Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: Fix issue response script #24891

Merged
merged 15 commits into from
Feb 14, 2025
2 changes: 1 addition & 1 deletion .github/workflows/issue_response.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
- run: pnpm install --dir script/issue_response

- name: Run Issue Response
run: pnpm exec ts-node script/issue_response/main.ts
run: pnpm run --dir script/issue_response start
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SLACK_ISSUE_RESPONSE_WEBHOOK_URL: ${{ secrets.SLACK_ISSUE_RESPONSE_WEBHOOK_URL }}
File renamed without changes.
99 changes: 99 additions & 0 deletions script/issue_response/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
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("&", "&")
.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);
});
24 changes: 24 additions & 0 deletions script/issue_response/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "issue_response",
"version": "1.0.0",
"private": true,
"main": "main.js",
"type": "module",
"scripts": {
"build": "tsc -p .",
"start": "node main.js"
},
"dependencies": {
"@octokit/rest": "^21.1.0",
"@slack/webhook": "^7.0.4",
"date-fns": "^4.1.0",
"octokit": "^4.1.1"
},
"devDependencies": {
"@octokit/types": "^13.8.0",
"@slack/types": "^2.14.0",
"@tsconfig/node20": "20.1.4",
"@tsconfig/strictest": "2.0.5",
"typescript": "5.7.3"
}
}
Loading
Loading