-
Notifications
You must be signed in to change notification settings - Fork 39
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This while loop is not making sense There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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); | ||
} | ||
} |
There was a problem hiding this comment.
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 ?