diff --git a/github/GithubApp.ts b/github/GithubApp.ts index 8659bde..6870e6e 100644 --- a/github/GithubApp.ts +++ b/github/GithubApp.ts @@ -3,6 +3,7 @@ import { IConfigurationExtend, IHttp, ILogger, + IMessageExtender, IModify, IPersistence, IRead, @@ -40,12 +41,27 @@ 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)){ + return true; + } + return false; + } + public async executePreMessageSentExtend(message: IMessage, extend: IMessageExtender, read: IRead, http: IHttp, persistence: IPersistence): Promise { + if(await hasRepoLink(message)){ + await handleRepoLink(message,read,http,message.sender,message.room,extend); + } + 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..bf0c128 --- /dev/null +++ b/github/handlers/handleLinks.ts @@ -0,0 +1,49 @@ +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 +) { + + const regex = /github\.com\/([A-Za-z0-9-]+)\/([A-Za-z0-9-_.]+)/; + const url = message.text!; + const matches = url.match(regex)!; + const owner = matches[1]; + const repo = matches[2]; + const attachment: IMessageAttachment = { + actionButtonsAlignment: MessageActionButtonsAlignment.HORIZONTAL, + actions: [ + { + type: MessageActionType.BUTTON, + text: "Create issue", + msg: `/github ${owner}/${repo} issue`, + msg_in_chat_window: true, + }, + { + type: MessageActionType.BUTTON, + text: "Issues", + url:`https://www.github.com/${owner}/${repo}/issues` + }, + { + type: MessageActionType.BUTTON, + text: "Pull Requests", + url:`https://www.github.com/${owner}/${repo}/pulls` + }, + + ], + }; + extend.addAttachment(attachment); +} diff --git a/github/helpers/checkLinks.ts b/github/helpers/checkLinks.ts new file mode 100644 index 0000000..b29d57f --- /dev/null +++ b/github/helpers/checkLinks.ts @@ -0,0 +1,21 @@ +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) { + // 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; +} 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