Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor PTAL embed creation and Add buttons to PTAL response to make it easier to access #3

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
"drizzle-kit": "^0.28.1",
"tsx": "^4.19.2",
"typescript": "^5.6.3"
}
},
"packageManager": "[email protected]+sha512.0a203ffaed5a3f63242cd064c8fb5892366c103e328079318f78062f24ea8c9d50bc6a47aa3567cabefd824d170e78fa2745ed1f16b132e16436146b7688f19b"
}
13 changes: 3 additions & 10 deletions src/commands/ptal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,9 @@ const handler = async (interaction: ChatInputCommandInteraction) => {
return;
}

const { embed, message, roleId } = await makePtalEmbed(pr, reviewList, description, pullRequestUrl, interaction.user, interaction.guild!.id);

const reply = await interaction.reply({
content: message,
embeds: [embed],
fetchReply: true,
allowedMentions: {
roles: [roleId]
}
});
const { newInteraction } = await makePtalEmbed(pr, reviewList, description, pullRequestUrl, interaction.user, interaction.guild!.id);

const reply = await interaction.reply(newInteraction);

if (!reply) return;

Expand Down
16 changes: 6 additions & 10 deletions src/utils/ptal/editPtalMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,18 @@ const editPtalMessage = async (data: typeof ptalTable.$inferSelect, client: Clie

const originalMessage = await channel.messages.fetch(data.message);

const { embed, message, roleId } = await makePtalEmbed(
const pullRequestUrl = new URL(`https://github.com/${data.owner}/${data.repository}/pull/${data.pr}`)

const { edit } = await makePtalEmbed(
pullReq.data,
reviewList.data,
data.description,
new URL(`https://github.com/${data.owner}/${data.repository}/pull/${data.pr}`),
pullRequestUrl,
originalMessage.author,
originalMessage.guild.id
);

await originalMessage.edit({
content: message,
embeds: [embed],
allowedMentions: {
roles: [roleId]
}
});

await originalMessage.edit(edit);

if (pullReq.data.merged) {
await db.delete(ptalTable).where(eq(ptalTable.id, data.id));
Expand Down
67 changes: 58 additions & 9 deletions src/utils/ptal/makePtalEmbed.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { PullRequest, PullRequestReplies, ReviewStatus } from "@/commands/ptal";
import { ChatInputCommandInteraction, EmbedBuilder } from "discord.js";
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, ChatInputCommandInteraction, EmbedBuilder, InteractionReplyOptions, MessageEditOptions } from "discord.js";
import { useDB } from "../global/useDB";
import { guildsTable } from "@/db/schema";
import { eq } from "drizzle-orm";
import { parsePullRequest } from "./parsePullRequest";
import { BRAND_COLOR } from "@/consts";
import { client } from "@/index";

/**
* Returns the relevant emoji for a given status enum
Expand All @@ -19,20 +20,32 @@ const getReviewEmoji = (status: ReviewStatus) => {
return ":question:";
}

type MakePtalEmbed = (pr: PullRequest,
reviewList: PullRequestReplies,
description: string,
pullRequestUrl: URL,
user: ChatInputCommandInteraction['user'],
guildId: string) => Promise<{
newInteraction: InteractionReplyOptions & {
fetchReply: true;
};
edit: MessageEditOptions;
}>;

/**
* Creates an embed and message to be used in the PTAL notifications.
* @param pr The PR to base the embed on
* @param reviewList The reviews for the PR
* @param interaction The discord.js interaction to reply to
* @returns The embed and message for the PTAL notification
*/
const makePtalEmbed = async (
pr: PullRequest,
reviewList: PullRequestReplies,
description: string,
pullRequestUrl: URL,
user: ChatInputCommandInteraction['user'],
guildId: string
const makePtalEmbed: MakePtalEmbed = async (
pr,
reviewList,
description,
pullRequestUrl,
user,
guildId
) => {
const db = useDB();
const data = await db.select().from(guildsTable).where(eq(guildsTable.id, guildId));
Expand Down Expand Up @@ -84,8 +97,44 @@ const makePtalEmbed = async (
timestamp: Date.now(),
title,
});

const viewOnGithub = new ButtonBuilder()
.setStyle(ButtonStyle.Link)
.setLabel('See on GitHub')
.setURL(pullRequestUrl.href);

if (client.emojis.resolve('1329780197385441340')) {
viewOnGithub.setEmoji('<:github:1329780197385441340>');
}

return { embed, message, roleId: role! };
const viewFiles = new ButtonBuilder()
.setStyle(ButtonStyle.Link)
.setLabel('View Files')
.setURL(new URL('files', pullRequestUrl).href);

const row = new ActionRowBuilder<ButtonBuilder>({
components: [viewOnGithub, viewFiles]
});

return {
newInteraction: {
content: message,
embeds: [embed],
fetchReply: true,
components: [row],
allowedMentions: {
roles: [role!],
}
},
edit: {
content: message,
embeds: [embed],
components: [row],
allowedMentions: {
roles: [role!],
}
}
}
}

export { makePtalEmbed };
Loading