Skip to content

Commit

Permalink
Merge pull request #142 from VipinDevelops/fix-pr-expand
Browse files Browse the repository at this point in the history
[FIX]: Fix PR expand Feature
  • Loading branch information
samad-yar-khan authored Mar 31, 2024
2 parents a7267b4 + 5cad2f9 commit 1f754ad
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 76 deletions.
25 changes: 8 additions & 17 deletions github/GithubApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean> {
if (await hasGithubPRLink(message)){
return true
}
return false;
}

async executePostMessageSent(message: IMessage, read: IRead, http: IHttp, persistence: IPersistence, modify: IModify): Promise<void> {

await handleGithubPRLink(message,read,http,persistence,modify)

}

public async checkPreMessageSentExtend(
message: IMessage,
read: IRead,
Expand All @@ -95,7 +83,10 @@ 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)) {
await handleGithubPRLinks(message, read, http, message.sender, message.room, extend);
}

return extend.getMessage();
}

Expand Down
3 changes: 1 addition & 2 deletions github/app.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
"classFile": "GithubApp.ts",
"description": "The ultimate app extending Rocket.Chat for all developers collaborating on Github",
"implements": [
"IPreMessageSentExtend",
"IPostMessageSent"
"IPreMessageSentExtend"
]
}
1 change: 1 addition & 0 deletions github/enum/Modals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
97 changes: 40 additions & 57 deletions github/handlers/GithubPRlinkHandler.ts
Original file line number Diff line number Diff line change
@@ -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<String> {
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);
}
}
8 changes: 8 additions & 0 deletions github/modals/pullDetailsModal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,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}`,
}),
],
});

Expand Down

0 comments on commit 1f754ad

Please sign in to comment.