From dceff198b071c5b8b038f40bae31026d464093bc Mon Sep 17 00:00:00 2001 From: VipinDevelops Date: Wed, 6 Mar 2024 22:51:39 +0530 Subject: [PATCH 1/3] remove post message handlers --- github/GithubApp.ts | 26 +++++++++----------------- github/app.json | 3 +-- 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/github/GithubApp.ts b/github/GithubApp.ts index 866303d..92732e8 100644 --- a/github/GithubApp.ts +++ b/github/GithubApp.ts @@ -48,31 +48,19 @@ import { IJobContext, StartupType } from "@rocket.chat/apps-engine/definition/sc import { IRoom } from "@rocket.chat/apps-engine/definition/rooms"; import { clearInteractionRoomData, getInteractionRoomData } from "./persistance/roomInteraction"; import { GHCommand } from "./commands/GhCommand"; -import { IPreMessageSentExtend, IMessage,IPreMessageSentModify, IPostMessageSent } from "@rocket.chat/apps-engine/definition/messages"; +import { IPreMessageSentExtend, IMessage } from "@rocket.chat/apps-engine/definition/messages"; import { handleGitHubCodeSegmentLink } from "./handlers/GitHubCodeSegmentHandler"; import { isGithubLink, hasGitHubCodeSegmentLink, hasGithubPRLink } from "./helpers/checkLinks"; import { SendReminder } from "./handlers/SendReminder"; import { AppSettings, settings } from "./settings/settings"; -import { ISetting } from "@rocket.chat/apps-engine/definition/settings";import { handleGithubPRLink } from "./handlers/GithubPRlinkHandler"; +import { ISetting } from "@rocket.chat/apps-engine/definition/settings"; +import { handleGithubPRLinks } from "./handlers/GithubPRlinkHandler"; -export class GithubApp extends App implements IPreMessageSentExtend,IPostMessageSent{ +export class GithubApp extends App implements IPreMessageSentExtend { constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) { super(info, logger, accessors); } - async checkPostMessageSent?(message: IMessage, read: IRead, http: IHttp): Promise { - if (await hasGithubPRLink(message)){ - return true - } - return false; - } - - async executePostMessageSent(message: IMessage, read: IRead, http: IHttp, persistence: IPersistence, modify: IModify): Promise { - - await handleGithubPRLink(message,read,http,persistence,modify) - - } - public async checkPreMessageSentExtend( message: IMessage, read: IRead, @@ -95,7 +83,11 @@ export class GithubApp extends App implements IPreMessageSentExtend,IPostMessage if (await hasGitHubCodeSegmentLink(message)) { await handleGitHubCodeSegmentLink(message, read, http, message.sender, message.room, extend); } - + if (await hasGithubPRLink(message)) { + console.log("test") + await handleGithubPRLinks(message, read, http, message.sender, message.room, extend) + } + return extend.getMessage(); } diff --git a/github/app.json b/github/app.json index baeab48..0d2452e 100644 --- a/github/app.json +++ b/github/app.json @@ -13,7 +13,6 @@ "classFile": "GithubApp.ts", "description": "The ultimate app extending Rocket.Chat for all developers collaborating on Github", "implements": [ - "IPreMessageSentExtend", - "IPostMessageSent" + "IPreMessageSentExtend" ] } \ No newline at end of file From 2f7c522094411dda92ee22be3e4c8258a83837d4 Mon Sep 17 00:00:00 2001 From: VipinDevelops Date: Wed, 6 Mar 2024 22:53:12 +0530 Subject: [PATCH 2/3] update pr link handler --- github/GithubApp.ts | 3 +- github/handlers/GithubPRlinkHandler.ts | 97 +++++++++++--------------- 2 files changed, 41 insertions(+), 59 deletions(-) diff --git a/github/GithubApp.ts b/github/GithubApp.ts index 92732e8..b625eb8 100644 --- a/github/GithubApp.ts +++ b/github/GithubApp.ts @@ -84,8 +84,7 @@ export class GithubApp extends App implements IPreMessageSentExtend { await handleGitHubCodeSegmentLink(message, read, http, message.sender, message.room, extend); } if (await hasGithubPRLink(message)) { - console.log("test") - await handleGithubPRLinks(message, read, http, message.sender, message.room, extend) + await handleGithubPRLinks(message, read, http, message.sender, message.room, extend); } return extend.getMessage(); diff --git a/github/handlers/GithubPRlinkHandler.ts b/github/handlers/GithubPRlinkHandler.ts index 3c5a21d..a74ea8c 100644 --- a/github/handlers/GithubPRlinkHandler.ts +++ b/github/handlers/GithubPRlinkHandler.ts @@ -1,62 +1,45 @@ import { IUser } from "@rocket.chat/apps-engine/definition/users"; -import { IHttp, IMessageBuilder, IModify, IPersistence, IRead } from "@rocket.chat/apps-engine/definition/accessors"; -import { IMessage } from "@rocket.chat/apps-engine/definition/messages"; -import { BlockBuilder, ButtonStyle, IBlock, TextObjectType } from "@rocket.chat/apps-engine/definition/uikit"; -import { ModalsEnum } from "../enum/Modals"; - - -export async function handleGithubPRLink(message: IMessage, read: IRead, http: IHttp, persistence: IPersistence, modify: IModify): Promise { - try { - const githubPRLinkRegex = /\bhttps?:\/\/github\.com\/\S+\/pull\/\d+\b/; - const text = message.text!; - const prLinkMatch = text.match(githubPRLinkRegex); - const prLink = prLinkMatch?.[0]; - const githubLinkPartsRegex = /(?:https?:\/\/github\.com\/)(\S+)\/(\S+)\/pull\/(\d+)/; - const linkPartsMatch = prLink?.match(githubLinkPartsRegex); - const username = linkPartsMatch?.[1]; - const repositoryName = linkPartsMatch?.[2]; - const pullNumber = linkPartsMatch?.[3]; - - if (!username || !repositoryName || !pullNumber) { - throw new Error("Invalid GitHub PR link"); - } - - const messageBuilder = await modify.getCreator().startMessage() - .setRoom(message.room) - .setSender(message.sender) - .setGroupable(true); - - const block = modify.getCreator().getBlockBuilder(); - - block.addActionsBlock({ - blockId: "githubdata", - elements: [ - block.newButtonElement({ - actionId: ModalsEnum.MERGE_PULL_REQUEST_ACTION, - text: block.newPlainTextObject("Merge"), - value: `${username}/${repositoryName} ${pullNumber}`, - style: ButtonStyle.PRIMARY - }), - block.newButtonElement({ - actionId: ModalsEnum.PR_COMMENT_LIST_ACTION, - text: block.newPlainTextObject("Comment"), - value: `${username}/${repositoryName} ${pullNumber}`, - style: ButtonStyle.PRIMARY - }), - block.newButtonElement({ - actionId: ModalsEnum.APPROVE_PULL_REQUEST_ACTION, - text: block.newPlainTextObject("Approve"), - value: `${username}/${repositoryName} ${pullNumber}`, - style: ButtonStyle.PRIMARY - }) - ] - }) +import { IHttp, IMessageBuilder, IMessageExtender, IModify, IPersistence, IRead } from "@rocket.chat/apps-engine/definition/accessors"; +import { IMessage, IMessageAttachment, MessageActionButtonsAlignment, MessageActionType } from "@rocket.chat/apps-engine/definition/messages"; +import { IRoom } from "@rocket.chat/apps-engine/definition/rooms"; + +export async function handleGithubPRLinks( + message: IMessage, + read: IRead, + http: IHttp, + user: IUser, + room: IRoom, + extend: IMessageExtender +) { + const githubPRLinkRegex = /https?:\/\/github\.com\/(\S+)\/(\S+)\/pull\/(\d+)/g; + const text = message.text!; + let prLinkMatches: RegExpExecArray | null; + const matches: RegExpExecArray[] = []; + + while ((prLinkMatches = githubPRLinkRegex.exec(text)) !== null) { + matches.push(prLinkMatches); + } - messageBuilder.setBlocks(block); + if (matches.length > 3) { + return; + } - return await modify.getCreator().finish(messageBuilder); - } catch (error) { - console.error("Error in handleGithubPRLink:", error); - return "Error: Unable to process the GitHub PR link."; + for (const match of matches) { + const username = match[1]; + const repositoryName = match[2]; + const pullNumber = match[3]; + + const attachment: IMessageAttachment = { + actionButtonsAlignment: MessageActionButtonsAlignment.VERTICAL, + actions: [ + { + type: MessageActionType.BUTTON, + text: `PR Actions in ${repositoryName} #${pullNumber}`, + msg: `/github ${username}/${repositoryName} pulls ${pullNumber}`, + msg_in_chat_window: true, + }, + ], + }; + extend.addAttachment(attachment); } } From 5cad2f9a4573380e6dd787ce55d5a26cea500cfa Mon Sep 17 00:00:00 2001 From: VipinDevelops Date: Wed, 6 Mar 2024 22:53:29 +0530 Subject: [PATCH 3/3] add approve button --- github/enum/Modals.ts | 1 + github/modals/pullDetailsModal.ts | 8 ++++++++ 2 files changed, 9 insertions(+) diff --git a/github/enum/Modals.ts b/github/enum/Modals.ts index 684eb52..489320a 100644 --- a/github/enum/Modals.ts +++ b/github/enum/Modals.ts @@ -35,6 +35,7 @@ export enum ModalsEnum { MERGE_PULL_REQUEST_ACTION = 'merge-pull-request', MERGE_PULL_REQUEST_LABEL = 'Merge', APPROVE_PULL_REQUEST_ACTION = 'approve-pull-request', + APPROVE_PULL_REQUEST_LABEL = 'Approve', COMMENT_PR_ACTION = 'comment-pull-request', COMMENT_PR_LABEL = 'Add Comment', COMMENT_ISSUE_ACTION = 'comment-issue', diff --git a/github/modals/pullDetailsModal.ts b/github/modals/pullDetailsModal.ts index 13d9154..0cf9cb6 100644 --- a/github/modals/pullDetailsModal.ts +++ b/github/modals/pullDetailsModal.ts @@ -140,6 +140,14 @@ export async function pullDetailsModal({ }, value: `${data?.repository} ${data?.number}`, }), + block.newButtonElement({ + actionId: ModalsEnum.APPROVE_PULL_REQUEST_ACTION, + text: { + text: ModalsEnum.APPROVE_PULL_REQUEST_LABEL, + type: TextObjectType.PLAINTEXT, + }, + value: `${data?.repository} ${data?.number}`, + }), ], });