|
1 | | -import DiscordJS from "discord.js" |
| 1 | +import * as DiscordJS from "discord.js" |
2 | 2 | import dotenv from "dotenv" |
3 | | -import { Octokit } from "@octokit/rest" |
4 | | -import { getModal } from "./utils" |
| 3 | +import { App } from "octokit" |
| 4 | +import { getModal } from "./utils.js" |
| 5 | +import arkenv from "arkenv" |
5 | 6 | import express from "express" |
| 7 | +import { regex } from "arktype" |
6 | 8 | dotenv.config() |
7 | 9 |
|
8 | | -const app = express() |
9 | | -const PORT = process.env.PORT || 3000 |
| 10 | +const env = arkenv({ |
| 11 | + "PORT?": "number.port", |
| 12 | + GITHUB_APP_ID: "string > 0", |
| 13 | + GITHUB_APP_INSTALLATION_ID: "string.integer.parse", |
| 14 | + GITHUB_USERNAME: "string > 0", |
| 15 | + BOT_TOKEN: "string > 0", |
| 16 | + GUILD_ID: "string.integer > 0", |
| 17 | + GITHUB_REPOSITORY: [ |
| 18 | + regex("^(?<owner>[^/]+)/(?<repo>[^/]+)$"), |
| 19 | + "=>", |
| 20 | + (repoAndOwner) => { |
| 21 | + const [owner, repo] = repoAndOwner.split("/", 2) |
| 22 | + return { owner, repo } |
| 23 | + }, |
| 24 | + ], |
| 25 | +}) |
10 | 26 |
|
11 | | -app.use(express.json()) |
| 27 | +// const app = express() |
12 | 28 |
|
13 | | -app.get("/", (req, res) => { |
14 | | - res.send("Github issues bot!") |
15 | | -}) |
| 29 | +// app.use(express.json()) |
| 30 | + |
| 31 | +// app.get("/", (_, res) => { |
| 32 | +// res.send("Github issues bot!") |
| 33 | +// }) |
16 | 34 |
|
17 | | -app.listen(PORT, () => { |
18 | | - console.log(`Server is listening on port ${PORT}`) |
| 35 | +// app.listen(env.PORT ?? 3000, () => { |
| 36 | +// console.log(`Server is listening on port ${env.PORT ?? 3000}`) |
| 37 | +// }) |
| 38 | + |
| 39 | +const reporter = new App({ |
| 40 | + appId: env.GITHUB_APP_ID, |
| 41 | + privateKey: await Bun.file("./github-app-key.pem").text(), |
| 42 | + log: console, |
19 | 43 | }) |
20 | 44 |
|
| 45 | +const gh = await reporter.getInstallationOctokit(env.GITHUB_APP_INSTALLATION_ID) |
| 46 | + |
| 47 | +const { owner, repo } = env.GITHUB_REPOSITORY |
| 48 | + |
| 49 | +const { data: repository } = await gh.rest.repos.get({ owner, repo }) |
| 50 | +console.info(`Acting on ${repository.full_name}`) |
| 51 | + |
21 | 52 | const client = new DiscordJS.Client({ |
22 | 53 | intents: ["Guilds", "GuildMessages"], |
23 | 54 | }) |
24 | 55 |
|
25 | | -client.on("ready", () => { |
26 | | - console.log("issue bot ready") |
| 56 | +client.on("clientReady", async () => { |
| 57 | + console.log("Bot is ready.") |
27 | 58 | const guildId = process.env.GUILD_ID || "" |
28 | 59 |
|
29 | 60 | const guild = client.guilds.cache.get(guildId) |
| 61 | + if (!guild) { |
| 62 | + throw new Error("Guild not found") |
| 63 | + } |
30 | 64 |
|
31 | | - let commands: |
32 | | - | DiscordJS.GuildApplicationCommandManager |
33 | | - | DiscordJS.ApplicationCommandManager |
34 | | - | undefined |
| 65 | + const commands = guild.commands |
35 | 66 |
|
36 | | - if (guild) { |
37 | | - commands = guild.commands |
38 | | - } else { |
39 | | - commands = client.application?.commands |
| 67 | + for (const [, cmd] of await commands.fetch()) { |
| 68 | + await commands.delete(cmd.id) |
40 | 69 | } |
41 | 70 |
|
42 | | - commands?.create({ |
43 | | - name: "Open github issue", |
| 71 | + await commands.create({ |
| 72 | + name: "To Github Bug", |
| 73 | + type: 3, |
| 74 | + }) |
| 75 | + |
| 76 | + await commands.create({ |
| 77 | + name: "To Github Feature Request", |
| 78 | + type: 3, |
| 79 | + }) |
| 80 | + |
| 81 | + await commands.create({ |
| 82 | + name: "To Github Task", |
44 | 83 | type: 3, |
45 | 84 | }) |
46 | 85 | }) |
47 | 86 |
|
48 | 87 | client.on("interactionCreate", async (interaction) => { |
49 | 88 | if (interaction.isMessageContextMenuCommand()) { |
50 | | - const { commandName, targetMessage } = interaction |
51 | | - if (commandName === "Open github issue") { |
52 | | - const modal = getModal(targetMessage) |
| 89 | + const { commandName, targetMessage, user } = interaction |
| 90 | + console.log(`Received command ${commandName} from ${user.tag}`) |
| 91 | + const githubIssueCommand = |
| 92 | + /^To Github (?<type>Bug|Feature Request|Task)$/.exec(commandName) |
| 93 | + if (githubIssueCommand) { |
| 94 | + const { data: labels } = await gh.rest.issues |
| 95 | + .listLabelsForRepo({ owner, repo }) |
| 96 | + .catch(() => ({ data: [] })) |
| 97 | + |
| 98 | + const { data: issueTypes } = await gh |
| 99 | + .request("GET /orgs/{org}/issue-types", { org: owner }) |
| 100 | + .catch(() => ({ data: [] })) |
| 101 | + |
| 102 | + const { data: milestones } = await gh.rest.issues |
| 103 | + .listMilestones({ owner, repo }) |
| 104 | + .catch(() => ({ data: [] })) |
| 105 | + |
| 106 | + const { data: collaborators } = await gh.rest.repos |
| 107 | + .listCollaborators({ owner, repo }) |
| 108 | + .catch(() => ({ data: [] })) |
| 109 | + |
| 110 | + const modal = getModal({ |
| 111 | + id: `create github issue ${githubIssueCommand.groups?.type}`, |
| 112 | + user, |
| 113 | + message: targetMessage, |
| 114 | + labels, |
| 115 | + issueTypes, |
| 116 | + milestones, |
| 117 | + collaborators, |
| 118 | + }) |
53 | 119 | interaction.showModal(modal) |
54 | 120 | } |
55 | 121 | } else if (interaction.isModalSubmit()) { |
56 | | - const { fields } = interaction |
57 | | - const title = fields.getTextInputValue("issueTitle") |
58 | | - const body = fields.getTextInputValue("issueDescription") |
59 | | - const octokit = new Octokit({ |
60 | | - auth: process.env.GITHUB_ACCESS_TOKEN, |
61 | | - baseUrl: "https://api.github.com", |
62 | | - }) |
| 122 | + const { fields, customId } = interaction |
| 123 | + console.log( |
| 124 | + `Received modal submit ${interaction.customId} from ${interaction.user.tag}` |
| 125 | + ) |
| 126 | + const title = fields.getTextInputValue("title") |
| 127 | + const body = fields.getTextInputValue("desc") |
| 128 | + const labels = fields.fields.has("labels") |
| 129 | + ? fields.getStringSelectValues("labels") |
| 130 | + : [] |
| 131 | + const [milestone] = fields.fields.has("milestone") |
| 132 | + ? fields.getStringSelectValues("milestone") |
| 133 | + : [] |
| 134 | + const [assignee] = fields.fields.has("assignee") |
| 135 | + ? fields.getStringSelectValues("assignee") |
| 136 | + : [] |
63 | 137 |
|
64 | | - let [owner, repo] = (process.env.GITHUB_REPOSITORY || "").split("/", 2) |
65 | | - if (!repo) { |
66 | | - repo = owner |
67 | | - owner = process.env.GITHUB_USERNAME || "" |
68 | | - } |
| 138 | + const type = { |
| 139 | + Task: "Task", |
| 140 | + Bug: "Bug", |
| 141 | + "Feature Request": "Feature", |
| 142 | + // "Dependencies", |
| 143 | + }[customId.replace("create github issue ", "")] |
69 | 144 |
|
70 | | - octokit.rest.issues.create({ owner, repo, title, body }).then((res) => { |
71 | | - interaction.reply(`Issue created: ${res.data.html_url}`) |
| 145 | + console.log(`Creating issue with`, { |
| 146 | + title, |
| 147 | + body, |
| 148 | + labels, |
| 149 | + milestone, |
| 150 | + assignee, |
| 151 | + type, |
72 | 152 | }) |
| 153 | + |
| 154 | + const { data: issue } = await gh.rest.issues.create({ |
| 155 | + owner, |
| 156 | + repo, |
| 157 | + title, |
| 158 | + body, |
| 159 | + milestone, |
| 160 | + assignee, |
| 161 | + labels: [...labels], |
| 162 | + type, |
| 163 | + }) |
| 164 | + |
| 165 | + await interaction.reply(`[Created #${issue.number}](${issue.html_url})`) |
73 | 166 | } |
74 | 167 | }) |
75 | 168 |
|
|
0 commit comments