github-triage.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /// <reference path="../env.d.ts" />
  2. import { tool } from "@opencode-ai/plugin"
  3. const TEAM = {
  4. tui: ["kommander", "simonklee"],
  5. desktop_web: ["Hona", "Brendonovich"],
  6. core: ["jlongster", "rekram1-node", "nexxeln", "kitlangton"],
  7. inference: ["fwang", "MrMushrooooom", "starptech"],
  8. windows: ["Hona"],
  9. } as const
  10. function pick<T>(items: readonly T[]) {
  11. return items[Math.floor(Math.random() * items.length)]!
  12. }
  13. function getIssueNumber(): number {
  14. const issue = parseInt(process.env.ISSUE_NUMBER ?? "", 10)
  15. if (!issue) throw new Error("ISSUE_NUMBER env var not set")
  16. return issue
  17. }
  18. async function githubFetch(endpoint: string, options: RequestInit = {}) {
  19. const response = await fetch(`https://api.github.com${endpoint}`, {
  20. ...options,
  21. headers: {
  22. Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
  23. Accept: "application/vnd.github+json",
  24. "Content-Type": "application/json",
  25. ...(options.headers instanceof Headers ? Object.fromEntries(options.headers.entries()) : options.headers),
  26. },
  27. })
  28. if (!response.ok) {
  29. throw new Error(`GitHub API error: ${response.status} ${response.statusText}`)
  30. }
  31. return response.json()
  32. }
  33. export default tool({
  34. description: `Use this tool to assign a GitHub issue.
  35. Provide the team that should own the issue. This tool picks a random assignee from that team and does not apply labels.`,
  36. args: {
  37. team: tool.schema
  38. .enum(Object.keys(TEAM) as [keyof typeof TEAM, ...(keyof typeof TEAM)[]])
  39. .describe("The owning team"),
  40. },
  41. async execute(args) {
  42. const issue = getIssueNumber()
  43. const owner = "anomalyco"
  44. const repo = "opencode"
  45. const assignee = pick(TEAM[args.team])
  46. await githubFetch(`/repos/${owner}/${repo}/issues/${issue}/assignees`, {
  47. method: "POST",
  48. body: JSON.stringify({ assignees: [assignee] }),
  49. })
  50. return `Assigned @${assignee} from ${args.team} to issue #${issue}`
  51. },
  52. })