Skip to content

Commit 1f754ad

Browse files
Merge pull request #142 from VipinDevelops/fix-pr-expand
[FIX]: Fix PR expand Feature
2 parents a7267b4 + 5cad2f9 commit 1f754ad

File tree

5 files changed

+58
-76
lines changed

5 files changed

+58
-76
lines changed

github/GithubApp.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,31 +48,19 @@ import { IJobContext, StartupType } from "@rocket.chat/apps-engine/definition/sc
4848
import { IRoom } from "@rocket.chat/apps-engine/definition/rooms";
4949
import { clearInteractionRoomData, getInteractionRoomData } from "./persistance/roomInteraction";
5050
import { GHCommand } from "./commands/GhCommand";
51-
import { IPreMessageSentExtend, IMessage,IPreMessageSentModify, IPostMessageSent } from "@rocket.chat/apps-engine/definition/messages";
51+
import { IPreMessageSentExtend, IMessage } from "@rocket.chat/apps-engine/definition/messages";
5252
import { handleGitHubCodeSegmentLink } from "./handlers/GitHubCodeSegmentHandler";
5353
import { isGithubLink, hasGitHubCodeSegmentLink, hasGithubPRLink } from "./helpers/checkLinks";
5454
import { SendReminder } from "./handlers/SendReminder";
5555
import { AppSettings, settings } from "./settings/settings";
56-
import { ISetting } from "@rocket.chat/apps-engine/definition/settings";import { handleGithubPRLink } from "./handlers/GithubPRlinkHandler";
56+
import { ISetting } from "@rocket.chat/apps-engine/definition/settings";
57+
import { handleGithubPRLinks } from "./handlers/GithubPRlinkHandler";
5758

58-
export class GithubApp extends App implements IPreMessageSentExtend,IPostMessageSent{
59+
export class GithubApp extends App implements IPreMessageSentExtend {
5960
constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) {
6061
super(info, logger, accessors);
6162
}
6263

63-
async checkPostMessageSent?(message: IMessage, read: IRead, http: IHttp): Promise<boolean> {
64-
if (await hasGithubPRLink(message)){
65-
return true
66-
}
67-
return false;
68-
}
69-
70-
async executePostMessageSent(message: IMessage, read: IRead, http: IHttp, persistence: IPersistence, modify: IModify): Promise<void> {
71-
72-
await handleGithubPRLink(message,read,http,persistence,modify)
73-
74-
}
75-
7664
public async checkPreMessageSentExtend(
7765
message: IMessage,
7866
read: IRead,
@@ -95,7 +83,10 @@ export class GithubApp extends App implements IPreMessageSentExtend,IPostMessage
9583
if (await hasGitHubCodeSegmentLink(message)) {
9684
await handleGitHubCodeSegmentLink(message, read, http, message.sender, message.room, extend);
9785
}
98-
86+
if (await hasGithubPRLink(message)) {
87+
await handleGithubPRLinks(message, read, http, message.sender, message.room, extend);
88+
}
89+
9990
return extend.getMessage();
10091
}
10192

github/app.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"classFile": "GithubApp.ts",
1414
"description": "The ultimate app extending Rocket.Chat for all developers collaborating on Github",
1515
"implements": [
16-
"IPreMessageSentExtend",
17-
"IPostMessageSent"
16+
"IPreMessageSentExtend"
1817
]
1918
}

github/enum/Modals.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export enum ModalsEnum {
3535
MERGE_PULL_REQUEST_ACTION = 'merge-pull-request',
3636
MERGE_PULL_REQUEST_LABEL = 'Merge',
3737
APPROVE_PULL_REQUEST_ACTION = 'approve-pull-request',
38+
APPROVE_PULL_REQUEST_LABEL = 'Approve',
3839
COMMENT_PR_ACTION = 'comment-pull-request',
3940
COMMENT_PR_LABEL = 'Add Comment',
4041
COMMENT_ISSUE_ACTION = 'comment-issue',
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
}

github/modals/pullDetailsModal.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,14 @@ export async function pullDetailsModal({
165165
},
166166
value: `${data?.repository} ${data?.number}`,
167167
}),
168+
block.newButtonElement({
169+
actionId: ModalsEnum.APPROVE_PULL_REQUEST_ACTION,
170+
text: {
171+
text: ModalsEnum.APPROVE_PULL_REQUEST_LABEL,
172+
type: TextObjectType.PLAINTEXT,
173+
},
174+
value: `${data?.repository} ${data?.number}`,
175+
}),
168176
],
169177
});
170178

0 commit comments

Comments
 (0)