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"];
 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  const twoDaysAgo = new Date();
 50  twoDaysAgo.setDate(twoDaysAgo.getDate() - 2);
 51  const twoDaysAgoString = twoDaysAgo.toISOString().split("T")[0];
 52  const dateRangeFilter = `2025-02-01..${twoDaysAgoString}`;
 53
 54  const q = [
 55    `repo:${owner}/${repo}`,
 56    "is:issue",
 57    "state:open",
 58    `created:${dateRangeFilter}`,
 59    "sort:created-asc",
 60    ...commenterFilters,
 61    ...authorFilters,
 62  ];
 63
 64  const issues = await octokit.paginate(
 65    octokit.rest.search.issuesAndPullRequests,
 66    {
 67      q: q.join("+"),
 68      per_page: 100,
 69    },
 70  );
 71  const issueLines = issues.map((issue, index) => {
 72    const formattedDate = new Date(issue.created_at).toLocaleDateString(
 73      "en-US",
 74      {
 75        year: "numeric",
 76        month: "short",
 77        day: "numeric",
 78      },
 79    );
 80    const sanitizedTitle = issue.title
 81      .replaceAll("&", "&")
 82      .replaceAll("<", "&lt;")
 83      .replaceAll(">", "&gt;");
 84
 85    return `${index + 1}. ${formattedDate}: <${issue.html_url}|${sanitizedTitle}>\n`;
 86  });
 87
 88  const sections = [];
 89  /** @type {string[]} */
 90  let currentSection = [];
 91  let currentSectionLength = 0;
 92
 93  for (const issueLine of issueLines) {
 94    if (currentSectionLength + issueLine.length <= SECTION_BLOCK_TEXT_LIMIT) {
 95      currentSection.push(issueLine);
 96      currentSectionLength += issueLine.length;
 97    } else {
 98      sections.push(currentSection);
 99      currentSection = [];
100      currentSectionLength = 0;
101    }
102  }
103
104  if (currentSection.length > 0) {
105    sections.push(currentSection);
106  }
107
108  const blocks = sections.map((section) => ({
109    type: "section",
110    text: {
111      type: "mrkdwn",
112      text: section.join("").trimEnd(),
113    },
114  }));
115
116  const issuesUrl = `${GITHUB_ISSUES_URL}?q=${encodeURIComponent(q.join(" "))}`;
117
118  blocks.push({
119    type: "section",
120    text: {
121      type: "mrkdwn",
122      text: `<${issuesUrl}|View on GitHub>`,
123    },
124  });
125
126  await webhook.send({ blocks });
127}
128
129main().catch((error) => {
130  console.error("An error occurred:", error);
131  process.exit(1);
132});