Skip to content

Commit

Permalink
refactor: add try catch
Browse files Browse the repository at this point in the history
  • Loading branch information
VipinDevelops committed Oct 25, 2023
1 parent 0198845 commit f8ca2ff
Showing 1 changed file with 55 additions and 45 deletions.
100 changes: 55 additions & 45 deletions github/handlers/GithubPRlinkHandler.ts
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.";
}
}

0 comments on commit f8ca2ff

Please sign in to comment.