Skip to content

Commit 2f7c522

Browse files
committed
update pr link handler
1 parent dceff19 commit 2f7c522

File tree

2 files changed

+41
-59
lines changed

2 files changed

+41
-59
lines changed

github/GithubApp.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ export class GithubApp extends App implements IPreMessageSentExtend {
8484
await handleGitHubCodeSegmentLink(message, read, http, message.sender, message.room, extend);
8585
}
8686
if (await hasGithubPRLink(message)) {
87-
console.log("test")
88-
await handleGithubPRLinks(message, read, http, message.sender, message.room, extend)
87+
await handleGithubPRLinks(message, read, http, message.sender, message.room, extend);
8988
}
9089

9190
return extend.getMessage();
Lines changed: 40 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,45 @@
11
import { IUser } from "@rocket.chat/apps-engine/definition/users";
2-
import { IHttp, IMessageBuilder, IModify, IPersistence, IRead } from "@rocket.chat/apps-engine/definition/accessors";
3-
import { IMessage } from "@rocket.chat/apps-engine/definition/messages";
4-
import { BlockBuilder, ButtonStyle, IBlock, TextObjectType } from "@rocket.chat/apps-engine/definition/uikit";
5-
import { ModalsEnum } from "../enum/Modals";
6-
7-
8-
export async function handleGithubPRLink(message: IMessage, read: IRead, http: IHttp, persistence: IPersistence, modify: IModify): Promise<String> {
9-
try {
10-
const githubPRLinkRegex = /\bhttps?:\/\/github\.com\/\S+\/pull\/\d+\b/;
11-
const text = message.text!;
12-
const prLinkMatch = text.match(githubPRLinkRegex);
13-
const prLink = prLinkMatch?.[0];
14-
const githubLinkPartsRegex = /(?:https?:\/\/github\.com\/)(\S+)\/(\S+)\/pull\/(\d+)/;
15-
const linkPartsMatch = prLink?.match(githubLinkPartsRegex);
16-
const username = linkPartsMatch?.[1];
17-
const repositoryName = linkPartsMatch?.[2];
18-
const pullNumber = linkPartsMatch?.[3];
19-
20-
if (!username || !repositoryName || !pullNumber) {
21-
throw new Error("Invalid GitHub PR link");
22-
}
23-
24-
const messageBuilder = await modify.getCreator().startMessage()
25-
.setRoom(message.room)
26-
.setSender(message.sender)
27-
.setGroupable(true);
28-
29-
const block = modify.getCreator().getBlockBuilder();
30-
31-
block.addActionsBlock({
32-
blockId: "githubdata",
33-
elements: [
34-
block.newButtonElement({
35-
actionId: ModalsEnum.MERGE_PULL_REQUEST_ACTION,
36-
text: block.newPlainTextObject("Merge"),
37-
value: `${username}/${repositoryName} ${pullNumber}`,
38-
style: ButtonStyle.PRIMARY
39-
}),
40-
block.newButtonElement({
41-
actionId: ModalsEnum.PR_COMMENT_LIST_ACTION,
42-
text: block.newPlainTextObject("Comment"),
43-
value: `${username}/${repositoryName} ${pullNumber}`,
44-
style: ButtonStyle.PRIMARY
45-
}),
46-
block.newButtonElement({
47-
actionId: ModalsEnum.APPROVE_PULL_REQUEST_ACTION,
48-
text: block.newPlainTextObject("Approve"),
49-
value: `${username}/${repositoryName} ${pullNumber}`,
50-
style: ButtonStyle.PRIMARY
51-
})
52-
]
53-
})
2+
import { IHttp, IMessageBuilder, IMessageExtender, IModify, IPersistence, IRead } from "@rocket.chat/apps-engine/definition/accessors";
3+
import { IMessage, IMessageAttachment, MessageActionButtonsAlignment, MessageActionType } from "@rocket.chat/apps-engine/definition/messages";
4+
import { IRoom } from "@rocket.chat/apps-engine/definition/rooms";
5+
6+
export async function handleGithubPRLinks(
7+
message: IMessage,
8+
read: IRead,
9+
http: IHttp,
10+
user: IUser,
11+
room: IRoom,
12+
extend: IMessageExtender
13+
) {
14+
const githubPRLinkRegex = /https?:\/\/github\.com\/(\S+)\/(\S+)\/pull\/(\d+)/g;
15+
const text = message.text!;
16+
let prLinkMatches: RegExpExecArray | null;
17+
const matches: RegExpExecArray[] = [];
18+
19+
while ((prLinkMatches = githubPRLinkRegex.exec(text)) !== null) {
20+
matches.push(prLinkMatches);
21+
}
5422

55-
messageBuilder.setBlocks(block);
23+
if (matches.length > 3) {
24+
return;
25+
}
5626

57-
return await modify.getCreator().finish(messageBuilder);
58-
} catch (error) {
59-
console.error("Error in handleGithubPRLink:", error);
60-
return "Error: Unable to process the GitHub PR link.";
27+
for (const match of matches) {
28+
const username = match[1];
29+
const repositoryName = match[2];
30+
const pullNumber = match[3];
31+
32+
const attachment: IMessageAttachment = {
33+
actionButtonsAlignment: MessageActionButtonsAlignment.VERTICAL,
34+
actions: [
35+
{
36+
type: MessageActionType.BUTTON,
37+
text: `PR Actions in ${repositoryName} #${pullNumber}`,
38+
msg: `/github ${username}/${repositoryName} pulls ${pullNumber}`,
39+
msg_in_chat_window: true,
40+
},
41+
],
42+
};
43+
extend.addAttachment(attachment);
6144
}
6245
}

0 commit comments

Comments
 (0)