github-triage.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /// <reference path="../env.d.ts" />
  2. // import { Octokit } from "@octokit/rest"
  3. import { tool } from "@opencode-ai/plugin"
  4. import DESCRIPTION from "./github-triage.txt"
  5. function getIssueNumber(): number {
  6. const issue = parseInt(process.env.ISSUE_NUMBER ?? "", 10)
  7. if (!issue) throw new Error("ISSUE_NUMBER env var not set")
  8. return issue
  9. }
  10. async function githubFetch(endpoint: string, options: RequestInit = {}) {
  11. const response = await fetch(`https://api.github.com${endpoint}`, {
  12. ...options,
  13. headers: {
  14. Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
  15. Accept: "application/vnd.github+json",
  16. "Content-Type": "application/json",
  17. ...options.headers,
  18. },
  19. })
  20. if (!response.ok) {
  21. throw new Error(`GitHub API error: ${response.status} ${response.statusText}`)
  22. }
  23. return response.json()
  24. }
  25. export default tool({
  26. description: DESCRIPTION,
  27. args: {
  28. assignee: tool.schema
  29. .enum(["thdxr", "adamdotdevin", "rekram1-node", "fwang", "jayair", "kommander"])
  30. .describe("The username of the assignee")
  31. .default("rekram1-node"),
  32. labels: tool.schema
  33. .array(tool.schema.enum(["nix", "opentui", "perf", "desktop", "zen", "docs", "windows"]))
  34. .describe("The labels(s) to add to the issue")
  35. .default([]),
  36. },
  37. async execute(args) {
  38. const issue = getIssueNumber()
  39. // const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN })
  40. const owner = "anomalyco"
  41. const repo = "opencode"
  42. const results: string[] = []
  43. if (args.assignee === "adamdotdevin" && !args.labels.includes("desktop")) {
  44. throw new Error("Only desktop issues should be assigned to adamdotdevin")
  45. }
  46. if (args.assignee === "fwang" && !args.labels.includes("zen")) {
  47. throw new Error("Only zen issues should be assigned to fwang")
  48. }
  49. if (args.assignee === "kommander" && !args.labels.includes("opentui")) {
  50. throw new Error("Only opentui issues should be assigned to kommander")
  51. }
  52. // await octokit.rest.issues.addAssignees({
  53. // owner,
  54. // repo,
  55. // issue_number: issue,
  56. // assignees: [args.assignee],
  57. // })
  58. await githubFetch(`/repos/${owner}/${repo}/issues/${issue}/assignees`, {
  59. method: "POST",
  60. body: JSON.stringify({ assignees: [args.assignee] }),
  61. })
  62. results.push(`Assigned @${args.assignee} to issue #${issue}`)
  63. const labels: string[] = args.labels.map((label) => (label === "desktop" ? "web" : label))
  64. if (labels.length > 0) {
  65. // await octokit.rest.issues.addLabels({
  66. // owner,
  67. // repo,
  68. // issue_number: issue,
  69. // labels,
  70. // })
  71. await githubFetch(`/repos/${owner}/${repo}/issues/${issue}/labels`, {
  72. method: "POST",
  73. body: JSON.stringify({ labels }),
  74. })
  75. results.push(`Added labels: ${args.labels.join(", ")}`)
  76. }
  77. return results.join("\n")
  78. },
  79. })