Skip to content

Commit

Permalink
Added support to create issue from discord using /issue command
Browse files Browse the repository at this point in the history
  • Loading branch information
devanshkansagra committed Jul 29, 2024
1 parent c5564d5 commit 453b5a4
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 24 deletions.
9 changes: 1 addition & 8 deletions commands/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,5 @@ const { SlashCommandBuilder } = require("discord.js");

const auth = new SlashCommandBuilder()
.setName("authorize")
.setDescription("Authorize your GitHub Account")
.addStringOption((option) =>
option
.setName("access-token")
.setDescription("Github Access Token")
.setRequired(true),
);

.setDescription("Authorize your GitHub Account");
module.exports = auth.toJSON();
7 changes: 7 additions & 0 deletions commands/issue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { SlashCommandBuilder } = require("discord.js");

const issue = new SlashCommandBuilder()
.setName("issue")
.setDescription("Create an issue");

module.exports = issue.toJSON();
30 changes: 30 additions & 0 deletions controllers/createIssue.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const { Octokit } = require("@octokit/rest");

const createIssue = async (
title,
description,
label,
owner,
repoName,
authToken,
) => {
try {
const octokit = new Octokit({ auth: authToken });
const response = await octokit.request(
"POST /repos/{owner}/{repo}/issues",
{
owner: owner,
repo: repoName,
title: title,
body: description,
labels: label,
},
);

return response;
} catch (error) {
console.log("Error: ", error);
}
};

module.exports = createIssue;
123 changes: 107 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,22 @@ const issues = require("./commands/issues");
const auth = require("./commands/auth");
const track = require("./commands/track");
const untrack = require("./commands/untrack");
const issue = require("./commands/issue");

const createIssue = require("./controllers/createIssue");

const {
REST,
Routes,
Client,
GatewayIntentBits,
IntegrationApplication,
ActionRowBuilder,
Events,
ModalBuilder,
TextInputBuilder,
TextInputStyle,
} = require("discord.js");

const TokenDoc = require("./models/tokenSchema");
const client = new Client({
intents: [
Expand Down Expand Up @@ -74,21 +82,20 @@ app.get("/", (req, res) => {
client.on("interactionCreate", async (interaction) => {
if (interaction.isChatInputCommand()) {
if (interaction.commandName === "authorize") {
const token = interaction.options.data[0].value.toString();
const guildId = interaction.guildId;
const modal = new ModalBuilder()
.setCustomId("authorize")
.setTitle("Authorize your github account");

const validateToken = await TokenDoc.findOne({ accessToken: token });
if (validateToken) {
interaction.reply("You are already authorized");
} else {
const newToken = new TokenDoc({ guildId: guildId, accessToken: token });
const saveToken = await newToken.save();
if (saveToken) {
interaction.reply("You are authorized successfully");
} else {
interaction.reply("Unable to authorize");
}
}
const accessToken = new TextInputBuilder()
.setCustomId("accessToken")
.setLabel("Github Access Token")
.setStyle(TextInputStyle.Short);

const tokenRow = new ActionRowBuilder().addComponents(accessToken);

modal.addComponents(tokenRow);

await interaction.showModal(modal);
}
if (interaction.commandName === "track") {
const repoUrl = interaction.options.data[0].value;
Expand Down Expand Up @@ -251,6 +258,40 @@ client.on("interactionCreate", async (interaction) => {
console.log("Error", error);
}
}
if (interaction.commandName === "issue") {
const modal = new ModalBuilder()
.setCustomId("issue")
.setTitle("Create a new issue");

const repository = new TextInputBuilder()
.setCustomId("repository")
.setLabel("Repository name (Username/RepoName)")
.setStyle(TextInputStyle.Short);

const issueTitle = new TextInputBuilder()
.setCustomId("title")
.setLabel("Issue title")
.setStyle(TextInputStyle.Short);

const issueDescription = new TextInputBuilder()
.setCustomId("description")
.setLabel("Issue Description")
.setStyle(TextInputStyle.Paragraph);

const issueLabel = new TextInputBuilder()
.setCustomId("label")
.setLabel("Issue Label")
.setStyle(TextInputStyle.Short);

const repoAction = new ActionRowBuilder().addComponents(repository);
const titleAction = new ActionRowBuilder().addComponents(issueTitle);
const descAction = new ActionRowBuilder().addComponents(issueDescription);
const labelAction = new ActionRowBuilder().addComponents(issueLabel);

modal.addComponents(repoAction, titleAction, descAction, labelAction);

await interaction.showModal(modal);
}
if (interaction.commandName === "untrack") {
const repoName = interaction.options.data[0].value.split("/")[1];
const guildId = interaction.guildId;
Expand Down Expand Up @@ -287,6 +328,56 @@ client.on("interactionCreate", async (interaction) => {
}
});

client.on(Events.InteractionCreate, async (interaction) => {
if (!interaction.isModalSubmit()) return;
else if (interaction.customId === "issue") {
const repository = interaction.fields.getTextInputValue("repository");
const title = interaction.fields.getTextInputValue("title");
const description = interaction.fields.getTextInputValue("description");
const label = interaction.fields.getTextInputValue("label").split(",");

try {
const repoName = repository.split("/")[1];
const owner = repository.split("/")[0];
const token = await TokenDoc.find({ guildId: interaction.guildId });
if (token) {
const authToken = token[0].accessToken;
const res = await createIssue(
title,
description,
label,
owner,
repoName,
authToken,
);
if (res) {
interaction.reply("Issue Created");
}
} else {
console.log("Token not fetched");
}
} catch (error) {
console.log(error);
}
} else if (interaction.customId === "authorize") {
const token = interaction.fields.getTextInputValue("accessToken");
const guildId = interaction.guildId;

const validateToken = await TokenDoc.findOne({ accessToken: token });
if (validateToken) {
interaction.reply("You are already authorized");
} else {
const newToken = new TokenDoc({ guildId: guildId, accessToken: token });
const saveToken = await newToken.save();
if (saveToken) {
interaction.reply("You are authorized successfully");
} else {
interaction.reply("Unable to authorize");
}
}
}
});

client.on("messageCreate", async (message) => {
if (message.author.bot) return;
else if (message.content === "Hi") {
Expand All @@ -298,7 +389,7 @@ client.on("messageCreate", async (message) => {

const rest = new REST({ version: "10" }).setToken(process.env.RESET_TOKEN);
(async () => {
const commands = [commits, pulls, issues, auth, track, untrack];
const commands = [commits, pulls, issues, auth, track, untrack, issue];
try {
console.log("Started refreshing application (/) commands.");
const guildId = process.env.GUILD_ID;
Expand Down

0 comments on commit 453b5a4

Please sign in to comment.