#!/usr/bin/env node --redirect-warnings=/dev/null

const { execFileSync } = require("child_process");
let { GITHUB_ACCESS_TOKEN } = process.env;
const GITHUB_URL = "https://github.com";
const SKIPPABLE_NOTE_REGEX = /^\s*-?\s*n\/?a\s*/ims;
const PULL_REQUEST_WEB_URL = "https://github.com/zed-industries/zed/pull";
const PULL_REQUEST_API_URL =
  "https://api.github.com/repos/zed-industries/zed/pulls";
const DIVIDER = "-".repeat(80);

main();

async function main() {
  if (!GITHUB_ACCESS_TOKEN) {
    try {
      GITHUB_ACCESS_TOKEN = execFileSync("gh", ["auth", "token"]).toString();
    } catch (error) {
      console.log(error);
      console.log("No GITHUB_ACCESS_TOKEN, and no `gh auth token`");
      process.exit(1);
    }
  }

  const STAFF_MEMBERS = new Set(
    (
      await (
        await fetch(
          "https://api.github.com/orgs/zed-industries/teams/staff/members",
          {
            headers: {
              Authorization: `token ${GITHUB_ACCESS_TOKEN}`,
              Accept: "application/vnd.github+json",
            },
          },
        )
      ).json()
    ).map(({ login }) => login.toLowerCase()),
  );

  const isStaffMember = (githubHandle) => {
    githubHandle = githubHandle.toLowerCase();
    return STAFF_MEMBERS.has(githubHandle);
  };

  // Get the last two preview tags
  const [newTag, oldTag] = execFileSync(
    "git",
    ["tag", "--sort", "-committerdate"],
    { encoding: "utf8" },
  )
    .split("\n")
    .filter((t) => t.startsWith("v") && t.endsWith("-pre"));

  // Print the previous release
  console.log(`Changes from ${oldTag} to ${newTag}\n`);

  // Get the PRs merged between those two tags.
  const pullRequestNumbers = getPullRequestNumbers(oldTag, newTag);

  // Get the PRs that were cherry-picked between main and the old tag.
  const existingPullRequestNumbers = new Set(
    getPullRequestNumbers("main", oldTag),
  );

  // Filter out those existing PRs from the set of new PRs.
  const newPullRequestNumbers = pullRequestNumbers.filter(
    (number) => !existingPullRequestNumbers.has(number),
  );

  // Fetch the pull requests from the GitHub API.
  console.log("Merged Pull requests:");
  console.log(DIVIDER);
  for (const pullRequestNumber of newPullRequestNumbers) {
    const pullRequestApiURL = `${PULL_REQUEST_API_URL}/${pullRequestNumber}`;

    const response = await fetch(pullRequestApiURL, {
      headers: {
        Authorization: `token ${GITHUB_ACCESS_TOKEN}`,
      },
    });

    const pullRequest = await response.json();
    const releaseNotesHeader = /^\s*Release Notes:(.+)/ims;

    const releaseNotes = pullRequest.body || "";
    let contributor =
      pullRequest.user?.login ?? "Unable to identify contributor";
    const captures = releaseNotesHeader.exec(releaseNotes);
    let notes = captures ? captures[1] : "MISSING";
    notes = notes.trim();
    const isStaff = isStaffMember(contributor);

    if (SKIPPABLE_NOTE_REGEX.exec(notes) != null) {
      continue;
    }

    const credit = getCreditString(pullRequestNumber, contributor, isStaff);
    contributor = isStaff ? `${contributor} (staff)` : contributor;

    console.log(`PR Title: ${pullRequest.title}`);
    console.log(`Contributor: ${contributor}`);
    console.log(`Credit: (${credit})`);

    console.log("Release Notes:");
    console.log();
    console.log(notes);

    console.log(DIVIDER);
  }
}

function getCreditString(pullRequestNumber, contributor, isStaff) {
  let credit = "";

  if (pullRequestNumber) {
    const pullRequestMarkdownLink = `[#${pullRequestNumber}](${PULL_REQUEST_WEB_URL}/${pullRequestNumber})`;
    credit += pullRequestMarkdownLink;
  }

  if (contributor && !isStaff) {
    const contributorMarkdownLink = `[${contributor}](${GITHUB_URL}/${contributor})`;
    credit += `; thanks ${contributorMarkdownLink}`;
  }

  return credit;
}

function getPullRequestNumbers(oldTag, newTag) {
  const pullRequestNumbers = execFileSync(
    "git",
    ["log", `${oldTag}..${newTag}`, "--oneline"],
    { encoding: "utf8" },
  )
    .split("\n")
    .filter((line) => line.length > 0)
    .map((line) => {
      const match = line.match(/#(\d+)/);
      return match ? match[1] : null;
    })
    .filter((line) => line);

  return pullRequestNumbers;
}
