-
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.
- Loading branch information
1 parent
0198845
commit f8ca2ff
Showing
1 changed file
with
55 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,62 @@ | ||
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 { 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> { | ||
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]; | ||
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 | ||
}) | ||
] | ||
}) | ||
|
||
messageBuilder.setBlocks(block); | ||
|
||
return await modify.getCreator().finish(messageBuilder); | ||
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 | ||
}) | ||
] | ||
}) | ||
|
||
messageBuilder.setBlocks(block); | ||
|
||
return await modify.getCreator().finish(messageBuilder); | ||
} catch (error) { | ||
console.error("Error in handleGithubPRLink:", error); | ||
return "Error: Unable to process the GitHub PR link."; | ||
} | ||
} |