main.js

  1import { Octokit } from "@octokit/rest";
  2import { IncomingWebhook } from "@slack/webhook";
  3
  4/**
  5 * The maximum length of the `text` in a section block.
  6 *
  7 * [Slack Docs](https://api.slack.com/reference/block-kit/blocks#section)
  8 */
  9const SECTION_BLOCK_TEXT_LIMIT = 3000;
 10const GITHUB_ISSUES_URL = "https://github.com/zed-industries/zed/issues";
 11
 12async function main() {
 13  const octokit = new Octokit({
 14    auth: process.env["ISSUE_RESPONSE_GITHUB_TOKEN"],
 15  });
 16
 17  if (!process.env["SLACK_ISSUE_RESPONSE_WEBHOOK_URL"]) {
 18    throw new Error("SLACK_ISSUE_RESPONSE_WEBHOOK_URL is not set");
 19  }
 20
 21  const webhook = new IncomingWebhook(
 22    process.env["SLACK_ISSUE_RESPONSE_WEBHOOK_URL"],
 23  );
 24
 25  const owner = "zed-industries";
 26  const repo = "zed";
 27  const teams = ["staff", "triagers"];
 28  const githubHandleSet = new Set();
 29
 30  for (const team of teams) {
 31    const teamMembers = await octokit.paginate(
 32      octokit.rest.teams.listMembersInOrg,
 33      {
 34        org: owner,
 35        team_slug: team,
 36        per_page: 100,
 37      },
 38    );
 39
 40    for (const teamMember of teamMembers) {
 41      githubHandleSet.add(teamMember.login);
 42    }
 43  }
 44
 45  const githubHandles = Array.from(githubHandleSet);
 46  githubHandles.sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()));
 47  const commenterFilters = githubHandles.map((name) => `-commenter:${name}`);
 48  const authorFilters = githubHandles.map((name) => `-author:${name}`);
 49
 50  const q = [
 51    `repo:${owner}/${repo}`,
 52    "is:issue",
 53    "state:open",
 54    "created:>=2025-02-01",
 55    "sort:created-asc",
 56    ...commenterFilters,
 57    ...authorFilters,
 58  ];
 59
 60  const response = await octokit.rest.search.issuesAndPullRequests({
 61    q: q.join("+"),
 62    per_page: 100,
 63  });
 64
 65  const issues = response.data.items;
 66  const issueLines = issues.map((issue, index) => {
 67    const formattedDate = new Date(issue.created_at).toLocaleDateString(
 68      "en-US",
 69      {
 70        year: "numeric",
 71        month: "short",
 72        day: "numeric",
 73      },
 74    );
 75    const sanitizedTitle = issue.title
 76      .replaceAll("&", "&")
 77      .replaceAll("<", "&lt;")
 78      .replaceAll(">", "&gt;");
 79
 80    return `${index + 1}. ${formattedDate}: <${issue.html_url}|${sanitizedTitle}>\n`;
 81  });
 82
 83  const sections = [];
 84  /** @type {string[]} */
 85  let currentSection = [];
 86  let currentSectionLength = 0;
 87
 88  for (const issueLine of issueLines) {
 89    if (currentSectionLength + issueLine.length <= SECTION_BLOCK_TEXT_LIMIT) {
 90      currentSection.push(issueLine);
 91      currentSectionLength += issueLine.length;
 92    } else {
 93      sections.push(currentSection);
 94      currentSection = [];
 95      currentSectionLength = 0;
 96    }
 97  }
 98
 99  if (currentSection.length > 0) {
100    sections.push(currentSection);
101  }
102
103  const blocks = sections.map((section) => ({
104    type: "section",
105    text: {
106      type: "mrkdwn",
107      text: section.join("").trimEnd(),
108    },
109  }));
110
111  const issuesUrl = `${GITHUB_ISSUES_URL}?q=${encodeURIComponent(q.join(" "))}`;
112
113  blocks.push({
114    type: "section",
115    text: {
116      type: "mrkdwn",
117      text: `<${issuesUrl}|View on GitHub>`,
118    },
119  });
120
121  await webhook.send({ blocks });
122}
123
124main().catch((error) => {
125  console.error("An error occurred:", error);
126  process.exit(1);
127});