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

[FIX]: Fix PR expand Feature #142

Merged
merged 3 commits into from
Mar 31, 2024
Merged
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
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);
}
Comment on lines 84 to +88
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@VipinDevelops do you think adding more of these checks in checkPreMessageSentExtend will hinder performance ?


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);
}
Comment on lines +19 to +21
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This while loop is not making sense

Copy link
Contributor Author

@VipinDevelops VipinDevelops Mar 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @samad-yar-khan Actually We are using this loop to get all the possible PR links from the message.

This while loop iterates through the matches of the regular expression in the message text. It uses exec() to find the next match in the text. As long as there is a match (!== null), it adds the match to the matches array.

and now that we have an array of all possible PR links we can check if its more then 3 let's not show a button or else add the button for Each PR.


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 @@ -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}`,
}),
],
});

Expand Down