Skip to content

Commit e8dd750

Browse files
committed
Merge branch 'main' into feat/enterprise-server-support
2 parents 4937e24 + 1f754ad commit e8dd750

File tree

5 files changed

+83
-76
lines changed

5 files changed

+83
-76
lines changed

github/GithubApp.ts

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,33 +49,21 @@ import { IJobContext, StartupType } from "@rocket.chat/apps-engine/definition/sc
4949
import { IRoom } from "@rocket.chat/apps-engine/definition/rooms";
5050
import { clearInteractionRoomData, getInteractionRoomData } from "./persistance/roomInteraction";
5151
import { GHCommand } from "./commands/GhCommand";
52-
import { IPreMessageSentExtend, IMessage,IPreMessageSentModify, IPostMessageSent } from "@rocket.chat/apps-engine/definition/messages";
52+
import { IPreMessageSentExtend, IMessage } from "@rocket.chat/apps-engine/definition/messages";
5353
import { handleGitHubCodeSegmentLink } from "./handlers/GitHubCodeSegmentHandler";
5454
import { isGithubLink, hasGitHubCodeSegmentLink, hasGithubPRLink } from "./helpers/checkLinks";
5555
import { SendReminder } from "./handlers/SendReminder";
5656
import { AppSettingsEnum, settings } from "./settings/settings";
5757
import { ISetting } from "@rocket.chat/apps-engine/definition/settings";
58-
import { handleGithubPRLink } from "./handlers/GithubPRlinkHandler";
58+
59+
import { handleGithubPRLinks } from "./handlers/GithubPRlinkHandler";
5960
import { UpdateSetting } from "./persistance/setting";
6061

61-
export class GithubApp extends App implements IPreMessageSentExtend, IPostMessageSent {
62+
export class GithubApp extends App implements IPreMessageSentExtend {
6263
constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) {
6364
super(info, logger, accessors);
6465
}
6566

66-
async checkPostMessageSent?(message: IMessage, read: IRead, http: IHttp): Promise<boolean> {
67-
if (await hasGithubPRLink(message)){
68-
return true
69-
}
70-
return false;
71-
}
72-
73-
async executePostMessageSent(message: IMessage, read: IRead, http: IHttp, persistence: IPersistence, modify: IModify): Promise<void> {
74-
75-
await handleGithubPRLink(message,read,http,persistence,modify)
76-
77-
}
78-
7967
public async checkPreMessageSentExtend(
8068
message: IMessage,
8169
read: IRead,
@@ -98,7 +86,10 @@ export class GithubApp extends App implements IPreMessageSentExtend, IPostMessag
9886
if (await hasGitHubCodeSegmentLink(message)) {
9987
await handleGitHubCodeSegmentLink(message, read, http, message.sender, message.room, extend);
10088
}
101-
89+
if (await hasGithubPRLink(message)) {
90+
await handleGithubPRLinks(message, read, http, message.sender, message.room, extend);
91+
}
92+
10293
return extend.getMessage();
10394
}
10495

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: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,31 @@ export async function pullDetailsModal({
5656
const pullRawData = await http.get(
5757
`https://api.github.com/repos/${data?.repository}/pulls/${data?.number}`
5858
);
59+
60+
// If pullsNumber doesn't exist, notify the user
61+
if (pullRawData.statusCode === 404) {
62+
block.addSectionBlock({
63+
text: {
64+
text: `Pull request #${data?.number} doesn't exist.`,
65+
type: TextObjectType.PLAINTEXT,
66+
},
67+
});
68+
69+
return {
70+
title: {
71+
type: TextObjectType.PLAINTEXT,
72+
text: AppEnum.DEFAULT_TITLE,
73+
},
74+
close: block.newButtonElement({
75+
text: {
76+
type: TextObjectType.PLAINTEXT,
77+
text: "Close",
78+
},
79+
}),
80+
blocks: block.getBlocks(),
81+
};
82+
}
83+
5984
const pullData = pullRawData.data;
6085

6186
const pullRequestFilesRaw = await http.get(
@@ -140,6 +165,14 @@ export async function pullDetailsModal({
140165
},
141166
value: `${data?.repository} ${data?.number}`,
142167
}),
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+
}),
143176
],
144177
});
145178

0 commit comments

Comments
 (0)