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 response = await octokit.rest.search.issuesAndPullRequests({
 65    q: q.join("+"),
 66    per_page: 100,
 67  });
 68
 69  const issues = response.data.items;
 70  const issueLines = issues.map((issue, index) => {
 71    const formattedDate = new Date(issue.created_at).toLocaleDateString(
 72      "en-US",
 73      {
 74        year: "numeric",
 75        month: "short",
 76        day: "numeric",
 77      },
 78    );
 79    const sanitizedTitle = issue.title
 80      .replaceAll("&", "&")
 81      .replaceAll("<", "&lt;")
 82      .replaceAll(">", "&gt;");
 83
 84    return `${index + 1}. ${formattedDate}: <${issue.html_url}|${sanitizedTitle}>\n`;
 85  });
 86
 87  const sections = [];
 88  /** @type {string[]} */
 89  let currentSection = [];
 90  let currentSectionLength = 0;
 91
 92  for (const issueLine of issueLines) {
 93    if (currentSectionLength + issueLine.length <= SECTION_BLOCK_TEXT_LIMIT) {
 94      currentSection.push(issueLine);
 95      currentSectionLength += issueLine.length;
 96    } else {
 97      sections.push(currentSection);
 98      currentSection = [];
 99      currentSectionLength = 0;
100    }
101  }
102
103  if (currentSection.length > 0) {
104    sections.push(currentSection);
105  }
106
107  const blocks = sections.map((section) => ({
108    type: "section",
109    text: {
110      type: "mrkdwn",
111      text: section.join("").trimEnd(),
112    },
113  }));
114
115  const issuesUrl = `${GITHUB_ISSUES_URL}?q=${encodeURIComponent(q.join(" "))}`;
116
117  blocks.push({
118    type: "section",
119    text: {
120      type: "mrkdwn",
121      text: `<${issuesUrl}|View on GitHub>`,
122    },
123  });
124
125  await webhook.send({ blocks });
126}
127
128main().catch((error) => {
129  console.error("An error occurred:", error);
130  process.exit(1);
131});