-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #142 from VipinDevelops/fix-pr-expand
[FIX]: Fix PR expand Feature
- Loading branch information
Showing
5 changed files
with
58 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters