From cc612610d19cc809cc067ec2eea6b59c46beccb0 Mon Sep 17 00:00:00 2001 From: chinmayshewale Date: Thu, 23 Mar 2023 22:42:43 +0530 Subject: [PATCH 1/3] Handle link --- github/GithubApp.ts | 21 ++++++++++++++++++- github/app.json | 4 +++- github/handlers/handleLinks.ts | 38 ++++++++++++++++++++++++++++++++++ github/helpers/checkLinks.ts | 19 +++++++++++++++++ 4 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 github/handlers/handleLinks.ts create mode 100644 github/helpers/checkLinks.ts diff --git a/github/GithubApp.ts b/github/GithubApp.ts index 8659bde..9196302 100644 --- a/github/GithubApp.ts +++ b/github/GithubApp.ts @@ -3,6 +3,7 @@ import { IConfigurationExtend, IHttp, ILogger, + IMessageExtender, IModify, IPersistence, IRead, @@ -40,12 +41,30 @@ import { IJobContext } from "@rocket.chat/apps-engine/definition/scheduler"; import { IRoom } from "@rocket.chat/apps-engine/definition/rooms"; import { clearInteractionRoomData, getInteractionRoomData } from "./persistance/roomInteraction"; import { GHCommand } from "./commands/GhCommand"; +import { IMessage, IPreMessageSentExtend } from "@rocket.chat/apps-engine/definition/messages"; +import { hasRepoLink, isGithubLink } from "./helpers/checkLinks"; +import { handleRepoLink } from "./handlers/handleLinks"; -export class GithubApp extends App { +export class GithubApp extends App implements IPreMessageSentExtend{ constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) { super(info, logger, accessors); } + public async checkPreMessageSentExtend(message: IMessage, read: IRead, http: IHttp): Promise { + if(await isGithubLink(message)){ + console.log("github link") + return true; + } + return false; + } + public async executePreMessageSentExtend(message: IMessage, extend: IMessageExtender, read: IRead, http: IHttp, persistence: IPersistence): Promise { + if(await hasRepoLink(message)){ + console.log("is repo link"); + await handleRepoLink(message,read,http,message.sender,message.room,extend); + } + console.log(extend.getMessage()); + return extend.getMessage(); + } public async authorizationCallback( token: IAuthData, user: IUser, diff --git a/github/app.json b/github/app.json index 58af157..0d2452e 100644 --- a/github/app.json +++ b/github/app.json @@ -12,5 +12,7 @@ "nameSlug": "github", "classFile": "GithubApp.ts", "description": "The ultimate app extending Rocket.Chat for all developers collaborating on Github", - "implements": [] + "implements": [ + "IPreMessageSentExtend" + ] } \ No newline at end of file diff --git a/github/handlers/handleLinks.ts b/github/handlers/handleLinks.ts new file mode 100644 index 0000000..3e3d075 --- /dev/null +++ b/github/handlers/handleLinks.ts @@ -0,0 +1,38 @@ +import { IMessage, IMessageAttachment, MessageActionButtonsAlignment, MessageActionType } from "@rocket.chat/apps-engine/definition/messages"; +import { IRead, IHttp } from "@rocket.chat/apps-engine/definition/accessors"; +import { IUser } from "@rocket.chat/apps-engine/definition/users"; +import { IRoom } from "@rocket.chat/apps-engine/definition/rooms"; +import { IMessageExtender } from "@rocket.chat/apps-engine/definition/accessors"; + +export async function handleRepoLink( + message: IMessage, + read: IRead, + http: IHttp, + user: IUser, + room: IRoom, + extend: IMessageExtender +) { + console.log("Here"); + const regex = + /github\.com\/([A-Za-z0-9-]+)\/([A-Za-z0-9-_]+)/; + const url = message.text!; + console.log("Here"); + + const matches = url.match(regex)!; + console.log("Here"); + console.log(matches); + const owner = matches[0]; + const repo = matches[1]; + console.log("here"); + console.log(owner+repo); + const attachment:IMessageAttachment={ + actionButtonsAlignment:MessageActionButtonsAlignment.HORIZONTAL, + actions:[{ + type:MessageActionType.BUTTON, + text:"Overview", + msg:`/github ${owner}/${repo}`, + msg_in_chat_window:false + }] + } + extend.addAttachment(attachment); +} diff --git a/github/helpers/checkLinks.ts b/github/helpers/checkLinks.ts new file mode 100644 index 0000000..1c79785 --- /dev/null +++ b/github/helpers/checkLinks.ts @@ -0,0 +1,19 @@ +import { IMessage } from "@rocket.chat/apps-engine/definition/messages"; + +export async function isGithubLink(message: IMessage) { + let githubLink: RegExp = /(?:https?:\/\/)?(?:www\.)?github\.com\//; + if (githubLink.test(message.text!)) { + return true; + } + return false; +} + +export async function hasRepoLink(message: IMessage) { + let repoLink1: RegExp = /https:\/\/github\.com\/\S+\/\S+\/?\s/; + let repoLink2: RegExp = /https:\/\/github\.com\/\S+\/\S+\/?$/; + + if (repoLink1.test(message.text!) || repoLink2.test(message.text!)) { + return true; + } + return false; +} From 9e3cdc24d54088e47a8d3f5874a147486937832b Mon Sep 17 00:00:00 2001 From: chinmayshewale Date: Fri, 24 Mar 2023 00:07:50 +0530 Subject: [PATCH 2/3] Finished --- github/GithubApp.ts | 3 -- github/handlers/handleLinks.ts | 55 +++++++++++++++++++++------------- github/helpers/checkLinks.ts | 6 ++-- 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/github/GithubApp.ts b/github/GithubApp.ts index 9196302..6870e6e 100644 --- a/github/GithubApp.ts +++ b/github/GithubApp.ts @@ -51,18 +51,15 @@ export class GithubApp extends App implements IPreMessageSentExtend{ } public async checkPreMessageSentExtend(message: IMessage, read: IRead, http: IHttp): Promise { if(await isGithubLink(message)){ - console.log("github link") return true; } return false; } public async executePreMessageSentExtend(message: IMessage, extend: IMessageExtender, read: IRead, http: IHttp, persistence: IPersistence): Promise { if(await hasRepoLink(message)){ - console.log("is repo link"); await handleRepoLink(message,read,http,message.sender,message.room,extend); } - console.log(extend.getMessage()); return extend.getMessage(); } public async authorizationCallback( diff --git a/github/handlers/handleLinks.ts b/github/handlers/handleLinks.ts index 3e3d075..9a38478 100644 --- a/github/handlers/handleLinks.ts +++ b/github/handlers/handleLinks.ts @@ -1,4 +1,9 @@ -import { IMessage, IMessageAttachment, MessageActionButtonsAlignment, MessageActionType } from "@rocket.chat/apps-engine/definition/messages"; +import { + IMessage, + IMessageAttachment, + MessageActionButtonsAlignment, + MessageActionType, +} from "@rocket.chat/apps-engine/definition/messages"; import { IRead, IHttp } from "@rocket.chat/apps-engine/definition/accessors"; import { IUser } from "@rocket.chat/apps-engine/definition/users"; import { IRoom } from "@rocket.chat/apps-engine/definition/rooms"; @@ -12,27 +17,35 @@ export async function handleRepoLink( room: IRoom, extend: IMessageExtender ) { - console.log("Here"); - const regex = - /github\.com\/([A-Za-z0-9-]+)\/([A-Za-z0-9-_]+)/; - const url = message.text!; - console.log("Here"); + const regex = /github\.com\/([A-Za-z0-9-]+)\/([A-Za-z0-9-_.]+)/; + const url = message.text!; const matches = url.match(regex)!; - console.log("Here"); - console.log(matches); - const owner = matches[0]; - const repo = matches[1]; - console.log("here"); - console.log(owner+repo); - const attachment:IMessageAttachment={ - actionButtonsAlignment:MessageActionButtonsAlignment.HORIZONTAL, - actions:[{ - type:MessageActionType.BUTTON, - text:"Overview", - msg:`/github ${owner}/${repo}`, - msg_in_chat_window:false - }] - } + const owner = matches[1]; + const repo = matches[2]; + const attachment: IMessageAttachment = { + actionButtonsAlignment: MessageActionButtonsAlignment.HORIZONTAL, + actions: [ + { + type: MessageActionType.BUTTON, + text: "Issues", + msg: `/github ${owner}/${repo} issues`, + msg_in_chat_window: true, + }, + { + type: MessageActionType.BUTTON, + text: "Contributors", + msg: `/github ${owner}/${repo} contributors`, + msg_in_chat_window: true, + }, + { + type: MessageActionType.BUTTON, + text: "Pull Requests", + msg: `/github ${owner}/${repo} pulls`, + msg_in_chat_window: true, + }, + + ], + }; extend.addAttachment(attachment); } diff --git a/github/helpers/checkLinks.ts b/github/helpers/checkLinks.ts index 1c79785..b29d57f 100644 --- a/github/helpers/checkLinks.ts +++ b/github/helpers/checkLinks.ts @@ -9,10 +9,12 @@ export async function isGithubLink(message: IMessage) { } export async function hasRepoLink(message: IMessage) { - let repoLink1: RegExp = /https:\/\/github\.com\/\S+\/\S+\/?\s/; - let repoLink2: RegExp = /https:\/\/github\.com\/\S+\/\S+\/?$/; + // const repoLink1: RegExp = /https:\/\/github\.com\/\S+\/\S+\/?\s/; + const repoLink1 = /https?:\/\/github\.com\/[A-Za-z0-9-]+\/[A-Za-z0-9-_.]+\s(?!\/)/; + const repoLink2 = /https?:\/\/github\.com\/[A-Za-z0-9-]+\/[A-Za-z0-9-_.]+\/?$/; if (repoLink1.test(message.text!) || repoLink2.test(message.text!)) { + return true; } return false; From eed611a55deb0ec0eab2c58f5c6375e6e09d6c49 Mon Sep 17 00:00:00 2001 From: chinmayshewale Date: Sat, 25 Mar 2023 00:59:15 +0530 Subject: [PATCH 3/3] Create issue --- github/handlers/handleLinks.ts | 12 +++++------- github/lib/commandUtility.ts | 25 +++++++++++++++++++++++-- github/lib/helperMessage.ts | 5 +++-- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/github/handlers/handleLinks.ts b/github/handlers/handleLinks.ts index 9a38478..bf0c128 100644 --- a/github/handlers/handleLinks.ts +++ b/github/handlers/handleLinks.ts @@ -28,21 +28,19 @@ export async function handleRepoLink( actions: [ { type: MessageActionType.BUTTON, - text: "Issues", - msg: `/github ${owner}/${repo} issues`, + text: "Create issue", + msg: `/github ${owner}/${repo} issue`, msg_in_chat_window: true, }, { type: MessageActionType.BUTTON, - text: "Contributors", - msg: `/github ${owner}/${repo} contributors`, - msg_in_chat_window: true, + text: "Issues", + url:`https://www.github.com/${owner}/${repo}/issues` }, { type: MessageActionType.BUTTON, text: "Pull Requests", - msg: `/github ${owner}/${repo} pulls`, - msg_in_chat_window: true, + url:`https://www.github.com/${owner}/${repo}/pulls` }, ], diff --git a/github/lib/commandUtility.ts b/github/lib/commandUtility.ts index dcf4607..b0a6a13 100644 --- a/github/lib/commandUtility.ts +++ b/github/lib/commandUtility.ts @@ -23,6 +23,7 @@ import { import { handleSearch } from "../handlers/SearchHandler"; import { handleIssues, handleNewIssue } from "../handlers/HandleIssues"; import { handleUserProfileRequest } from "../handlers/UserProfileHandler"; +import { NewIssueModal } from "../modals/newIssueModal"; export class CommandUtility implements ExecutorProps { sender: IUser; @@ -138,7 +139,7 @@ export class CommandUtility implements ExecutorProps { ); break; } - case SubcommandEnum.ISSUES :{ + case SubcommandEnum.ISSUES: { handleIssues( this.read, this.context, @@ -147,7 +148,7 @@ export class CommandUtility implements ExecutorProps { this.http, this.room, this.modify - ) + ); break; } default: { @@ -194,6 +195,26 @@ export class CommandUtility implements ExecutorProps { ); break; } + case SubcommandEnum.NEW_ISSUE: { + const data = { repository: repository }; + const modal = await NewIssueModal({ + data: data, + modify: this.modify, + read: this.read, + persistence: this.persistence, + http: this.http, + slashcommandcontext: this.context, + }); + const triggerId = this.context.getTriggerId()!; + await this.modify + .getUiController() + .openModalView( + modal, + { triggerId }, + this.context.getSender() + ); + break; + } default: { await basicQueryMessage({ query, diff --git a/github/lib/helperMessage.ts b/github/lib/helperMessage.ts index 82a433b..c1d210f 100644 --- a/github/lib/helperMessage.ts +++ b/github/lib/helperMessage.ts @@ -34,8 +34,9 @@ export async function helperMessage({ 11. Subscribe to all repository events -> \`/github Username/RepositoryName subscribe\` 12. Unsubscribe to all repository events -> \`/github Username/RepositoryName unsubscribe\` 13. Add New Issues to GitHub Repository -> \`/github issue\` - 14. Search Issues and Pull Requests -> \`/github search\` - 15. Assign and Share Issues -> \`/github issues\` + 14. Add New Issue to a GitHub Repository -> \`/github Username/RepositoryName issue\` + 15. Search Issues and Pull Requests -> \`/github search\` + 16. Assign and Share Issues -> \`/github issues\` `; const textSender = await modify