From eb6e2090b4b1f3b24670179fc5828cd170b234f2 Mon Sep 17 00:00:00 2001 From: imvipindev Date: Mon, 19 Jun 2023 22:02:04 +0530 Subject: [PATCH 1/9] feat(Helper): :sparkles: add helper message for code expand --- github/helpers/checkLinks.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 github/helpers/checkLinks.ts diff --git a/github/helpers/checkLinks.ts b/github/helpers/checkLinks.ts new file mode 100644 index 0000000..0f2cd4a --- /dev/null +++ b/github/helpers/checkLinks.ts @@ -0,0 +1,19 @@ +import { IMessage } from "@rocket.chat/apps-engine/definition/messages"; + +export async function hasCodeLink(message: IMessage): Promise { + let lineNo: RegExp = + /https?:\/\/github\.com\/[A-Za-z0-9_-]+\/[A-Za-z0-9_.-]+\/blob\/[A-Za-z0-9_-]+\/.+/; + + if (lineNo.test(message.text!)) { + return true; + } + return false; +} + +export async function isGithubLink(message: IMessage) { + let githubLink: RegExp = /(?:https?:\/\/)?(?:www\.)?github\.com\//; + if (githubLink.test(message.text!)) { + return true; + } + return false; +} \ No newline at end of file From 223ceb473a7f1aa569b8ecc628d1c19b51ae333f Mon Sep 17 00:00:00 2001 From: imvipindev Date: Mon, 19 Jun 2023 22:02:30 +0530 Subject: [PATCH 2/9] feat(Handle): :sparkles: add handle for links --- github/handlers/HandleLinks.ts | 57 ++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 github/handlers/HandleLinks.ts diff --git a/github/handlers/HandleLinks.ts b/github/handlers/HandleLinks.ts new file mode 100644 index 0000000..fed2248 --- /dev/null +++ b/github/handlers/HandleLinks.ts @@ -0,0 +1,57 @@ +import { IUser } from "@rocket.chat/apps-engine/definition/users"; +import { IHttp, IRead } from "@rocket.chat/apps-engine/definition/accessors"; +import { + IMessage, + IMessageAttachment, +} from "@rocket.chat/apps-engine/definition/messages"; +import { IRoom } from "@rocket.chat/apps-engine/definition/rooms"; +import { IMessageExtender } from "@rocket.chat/apps-engine/definition/accessors"; +import { TextObjectType } from "@rocket.chat/apps-engine/definition/uikit"; + +async function checkLines(content, url) { + const regex: RegExp = /(?:L(\d+)+-L(\d+)|L(\d+))/; + const match: RegExpMatchArray = url.match(regex); + if (match[2]) { + const endLine = parseInt(match[2]) - parseInt(match[1]); + const lines = new RegExp( + `(?:.*\n){${parseInt(match[1]) - 1}}(.*(?:\n.*){${endLine}})` + ); + const text = await content.match(lines); + return text[1]; + } else if (match[3]) { + const lines = new RegExp( + `(?:.*\n){${parseInt(match[3]) - 1}}(.*(?:\n.*){5})` + ); + const text = await content.match(lines); + return text[1]; + } else { + const lines = new RegExp(`(?:.*\n){0}(.*(?:\n.*){5})`); + const text = await content.match(lines); + return text[1]; + } +} + +export async function handleCodeLink( + message: IMessage, + read: IRead, + http: IHttp, + user: IUser, + room: IRoom, + extend: IMessageExtender +) { + const regex: RegExp = /\bhttps?:\/\/github\.com\/\S+\b/; + let text = message.text!; + const match: RegExpMatchArray | null = text.match(regex); + const result: string | undefined = match?.[0]; + let url: string = result?.replace("blob/", "")!; + url = url.replace("github.com", "raw.githubusercontent.com"); + let response: any = await http.get(url); + const { content } = response; + let code = await checkLines(content, url); + + let attachment: IMessageAttachment = { + text: `\`\`\`\n${code}\n\`\`\` \n[Show more...](${result})`, + type: TextObjectType.MARKDOWN, + }; + extend.addAttachment(attachment); +} \ No newline at end of file From cf1b0bb81c10ff4e6c408754520f3d3e497181da Mon Sep 17 00:00:00 2001 From: imvipindev Date: Mon, 19 Jun 2023 22:03:30 +0530 Subject: [PATCH 3/9] feat(Premessage): :sparkles: Added PreMessageSentExtend --- github/GithubApp.ts | 42 ++++++++++++++++++++++++++++++++++++------ github/app.json | 4 +++- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/github/GithubApp.ts b/github/GithubApp.ts index 8659bde..050c97b 100644 --- a/github/GithubApp.ts +++ b/github/GithubApp.ts @@ -3,6 +3,7 @@ import { IConfigurationExtend, IHttp, ILogger, + IMessageExtender, IModify, IPersistence, IRead, @@ -40,12 +41,41 @@ 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 { IPreMessageSentExtend, IMessage } from "@rocket.chat/apps-engine/definition/messages"; +import { handleCodeLink } from "./handlers/HandleLinks"; +import { isGithubLink, hasCodeLink } from "./helpers/checkLinks"; -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 hasCodeLink(message)) { + await handleCodeLink(message, read, http, message.sender, message.room, extend); + } + return extend.getMessage(); + } + + public async authorizationCallback( token: IAuthData, user: IUser, @@ -63,7 +93,7 @@ export class GithubApp extends App { }, }; let text = `GitHub Authentication Succesfull 🚀`; - let interactionData = await getInteractionRoomData(read.getPersistenceReader(),user.id) ; + let interactionData = await getInteractionRoomData(read.getPersistenceReader(), user.id); if (token) { // await registerAuthorizedUser(read, persistence, user); @@ -71,12 +101,12 @@ export class GithubApp extends App { } else { text = `Authentication Failure 😔`; } - if(interactionData && interactionData.roomId){ + if (interactionData && interactionData.roomId) { let roomId = interactionData.roomId as string; let room = await read.getRoomReader().getById(roomId) as IRoom; - await clearInteractionRoomData(persistence,user.id); - await sendNotification(read,modify,user,room,text); - }else{ + await clearInteractionRoomData(persistence, user.id); + await sendNotification(read, modify, user, room, text); + } else { await sendDirectMessage(read, modify, user, text, persistence); } 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 From d08bf3abc3e1768285178c80af7a7358f6f9b4d4 Mon Sep 17 00:00:00 2001 From: imvipindev Date: Wed, 28 Jun 2023 15:02:55 +0530 Subject: [PATCH 4/9] refactor(Pr expand feature): :recycle: rename some function's --- github/GithubApp.ts | 14 ++++++++------ github/handlers/HandleLinks.ts | 2 +- github/helpers/checkLinks.ts | 2 +- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/github/GithubApp.ts b/github/GithubApp.ts index 050c97b..ccc5aa8 100644 --- a/github/GithubApp.ts +++ b/github/GithubApp.ts @@ -42,8 +42,8 @@ import { IRoom } from "@rocket.chat/apps-engine/definition/rooms"; import { clearInteractionRoomData, getInteractionRoomData } from "./persistance/roomInteraction"; import { GHCommand } from "./commands/GhCommand"; import { IPreMessageSentExtend, IMessage } from "@rocket.chat/apps-engine/definition/messages"; -import { handleCodeLink } from "./handlers/HandleLinks"; -import { isGithubLink, hasCodeLink } from "./helpers/checkLinks"; +import { handleGitHubCodeSegmentLink } from "./handlers/HandleLinks"; +import { isGithubLink, hasGitHubCodeSegmentLink } from "./helpers/checkLinks"; export class GithubApp extends App implements IPreMessageSentExtend { constructor(info: IAppInfo, logger: ILogger, accessors: IAppAccessors) { @@ -69,13 +69,12 @@ export class GithubApp extends App implements IPreMessageSentExtend { persistence: IPersistence ): Promise { - if (await hasCodeLink(message)) { - await handleCodeLink(message, read, http, message.sender, message.room, extend); + if (await hasGitHubCodeSegmentLink(message)) { + await handleGitHubCodeSegmentLink(message, read, http, message.sender, message.room, extend); } return extend.getMessage(); } - public async authorizationCallback( token: IAuthData, user: IUser, @@ -93,7 +92,10 @@ export class GithubApp extends App implements IPreMessageSentExtend { }, }; let text = `GitHub Authentication Succesfull 🚀`; - let interactionData = await getInteractionRoomData(read.getPersistenceReader(), user.id); + let interactionData = await getInteractionRoomData( + read.getPersistenceReader(), + user.id + ); if (token) { // await registerAuthorizedUser(read, persistence, user); diff --git a/github/handlers/HandleLinks.ts b/github/handlers/HandleLinks.ts index fed2248..2cb3024 100644 --- a/github/handlers/HandleLinks.ts +++ b/github/handlers/HandleLinks.ts @@ -31,7 +31,7 @@ async function checkLines(content, url) { } } -export async function handleCodeLink( +export async function handleGitHubCodeSegmentLink( message: IMessage, read: IRead, http: IHttp, diff --git a/github/helpers/checkLinks.ts b/github/helpers/checkLinks.ts index 0f2cd4a..dd1fb69 100644 --- a/github/helpers/checkLinks.ts +++ b/github/helpers/checkLinks.ts @@ -1,6 +1,6 @@ import { IMessage } from "@rocket.chat/apps-engine/definition/messages"; -export async function hasCodeLink(message: IMessage): Promise { +export async function hasGitHubCodeSegmentLink(message: IMessage): Promise { let lineNo: RegExp = /https?:\/\/github\.com\/[A-Za-z0-9_-]+\/[A-Za-z0-9_.-]+\/blob\/[A-Za-z0-9_-]+\/.+/; From ebe8be1429088d19aba7acd02302876dbeefe922 Mon Sep 17 00:00:00 2001 From: imvipindev Date: Wed, 28 Jun 2023 16:53:40 +0530 Subject: [PATCH 5/9] refactor(Main-modal): :recycle: rewrite the function for more readability --- github/GithubApp.ts | 3 +- github/handlers/HandleLinks.ts | 92 +++++++++++++++++----------------- 2 files changed, 47 insertions(+), 48 deletions(-) diff --git a/github/GithubApp.ts b/github/GithubApp.ts index ccc5aa8..441f30a 100644 --- a/github/GithubApp.ts +++ b/github/GithubApp.ts @@ -105,13 +105,12 @@ export class GithubApp extends App implements IPreMessageSentExtend { } if (interactionData && interactionData.roomId) { let roomId = interactionData.roomId as string; - let room = await read.getRoomReader().getById(roomId) as IRoom; + let room = (await read.getRoomReader().getById(roomId)) as IRoom; await clearInteractionRoomData(persistence, user.id); await sendNotification(read, modify, user, room, text); } else { await sendDirectMessage(read, modify, user, text, persistence); } - } public oauth2ClientInstance: IOAuth2Client; public oauth2Config: IOAuth2ClientOptions = { diff --git a/github/handlers/HandleLinks.ts b/github/handlers/HandleLinks.ts index 2cb3024..adb865b 100644 --- a/github/handlers/HandleLinks.ts +++ b/github/handlers/HandleLinks.ts @@ -1,57 +1,57 @@ import { IUser } from "@rocket.chat/apps-engine/definition/users"; import { IHttp, IRead } from "@rocket.chat/apps-engine/definition/accessors"; -import { - IMessage, - IMessageAttachment, -} from "@rocket.chat/apps-engine/definition/messages"; +import { IMessage, IMessageAttachment } from "@rocket.chat/apps-engine/definition/messages"; import { IRoom } from "@rocket.chat/apps-engine/definition/rooms"; import { IMessageExtender } from "@rocket.chat/apps-engine/definition/accessors"; import { TextObjectType } from "@rocket.chat/apps-engine/definition/uikit"; -async function checkLines(content, url) { - const regex: RegExp = /(?:L(\d+)+-L(\d+)|L(\d+))/; - const match: RegExpMatchArray = url.match(regex); - if (match[2]) { - const endLine = parseInt(match[2]) - parseInt(match[1]); - const lines = new RegExp( - `(?:.*\n){${parseInt(match[1]) - 1}}(.*(?:\n.*){${endLine}})` - ); - const text = await content.match(lines); - return text[1]; - } else if (match[3]) { - const lines = new RegExp( - `(?:.*\n){${parseInt(match[3]) - 1}}(.*(?:\n.*){5})` - ); - const text = await content.match(lines); - return text[1]; - } else { - const lines = new RegExp(`(?:.*\n){0}(.*(?:\n.*){5})`); - const text = await content.match(lines); - return text[1]; - } +async function extractCodeSnippet(content: string, url: string): Promise { + const lineRangeRegex: RegExp = /(?:L(\d+)+-L(\d+)|L(\d+))/; + const lineRangeMatch: RegExpMatchArray | null = url.match(lineRangeRegex); + + if (lineRangeMatch?.[2]) { + const startLine = parseInt(lineRangeMatch[1]); + const endLine = parseInt(lineRangeMatch[2]); + const linesRegex = `(?:.*\n){${startLine - 1}}(.*(?:\n.*){${endLine - startLine + 1}})`; + const lines = new RegExp(linesRegex); + const match = await content.match(lines); + return match?.[1] ?? ""; + } else if (lineRangeMatch?.[3]) { + const line = parseInt(lineRangeMatch[3]); + const linesRegex = `(?:.*\n){${line - 1}}(.*(?:\n.*){5})`; + const lines = new RegExp(linesRegex); + const match = await content.match(lines); + return match?.[1] ?? ""; + } else { + const linesRegex = `(?:.*\n){0}(.*(?:\n.*){5})`; + const lines = new RegExp(linesRegex); + const match = await content.match(lines); + return match?.[1] ?? ""; + } } export async function handleGitHubCodeSegmentLink( - message: IMessage, - read: IRead, - http: IHttp, - user: IUser, - room: IRoom, - extend: IMessageExtender + message: IMessage, + read: IRead, + http: IHttp, + user: IUser, + room: IRoom, + extend: IMessageExtender ) { - const regex: RegExp = /\bhttps?:\/\/github\.com\/\S+\b/; - let text = message.text!; - const match: RegExpMatchArray | null = text.match(regex); - const result: string | undefined = match?.[0]; - let url: string = result?.replace("blob/", "")!; - url = url.replace("github.com", "raw.githubusercontent.com"); - let response: any = await http.get(url); - const { content } = response; - let code = await checkLines(content, url); + const urlRegex: RegExp = /\bhttps?:\/\/github\.com\/\S+\b/; + const messageText: string = message.text!; + const urlMatch: RegExpMatchArray | null = messageText.match(urlRegex); + const url: string | undefined = urlMatch?.[0]; + let modifiedUrl: string = url?.replace("blob/", "")!; + modifiedUrl = modifiedUrl.replace("github.com", "raw.githubusercontent.com"); + + const response: any = await http.get(modifiedUrl); + const { content } = response; + const codeSnippet = await extractCodeSnippet(content, modifiedUrl); - let attachment: IMessageAttachment = { - text: `\`\`\`\n${code}\n\`\`\` \n[Show more...](${result})`, - type: TextObjectType.MARKDOWN, - }; - extend.addAttachment(attachment); -} \ No newline at end of file + const attachment: IMessageAttachment = { + text: `\`\`\`\n${codeSnippet}\n\`\`\` \n[Show more...](${url})`, + type: TextObjectType.MARKDOWN, + }; + extend.addAttachment(attachment); +} From 0516d02d316ca8de012f8472b5b43d7f1f7b1531 Mon Sep 17 00:00:00 2001 From: imvipindev Date: Wed, 28 Jun 2023 17:12:41 +0530 Subject: [PATCH 6/9] feat(Pr expand feature): :sparkles: add enums --- github/GithubApp.ts | 18 ++++++++---------- github/enum/URLmodifications.ts | 5 +++++ github/handlers/HandleLinks.ts | 11 ++++++----- 3 files changed, 19 insertions(+), 15 deletions(-) create mode 100644 github/enum/URLmodifications.ts diff --git a/github/GithubApp.ts b/github/GithubApp.ts index 441f30a..f841b3d 100644 --- a/github/GithubApp.ts +++ b/github/GithubApp.ts @@ -92,10 +92,7 @@ export class GithubApp extends App implements IPreMessageSentExtend { }, }; let text = `GitHub Authentication Succesfull 🚀`; - let interactionData = await getInteractionRoomData( - read.getPersistenceReader(), - user.id - ); + let interactionData = await getInteractionRoomData(read.getPersistenceReader(),user.id) ; if (token) { // await registerAuthorizedUser(read, persistence, user); @@ -103,14 +100,15 @@ export class GithubApp extends App implements IPreMessageSentExtend { } else { text = `Authentication Failure 😔`; } - if (interactionData && interactionData.roomId) { + if(interactionData && interactionData.roomId){ let roomId = interactionData.roomId as string; - let room = (await read.getRoomReader().getById(roomId)) as IRoom; - await clearInteractionRoomData(persistence, user.id); - await sendNotification(read, modify, user, room, text); - } else { + let room = await read.getRoomReader().getById(roomId) as IRoom; + await clearInteractionRoomData(persistence,user.id); + await sendNotification(read,modify,user,room,text); + }else{ await sendDirectMessage(read, modify, user, text, persistence); } + } public oauth2ClientInstance: IOAuth2Client; public oauth2Config: IOAuth2ClientOptions = { @@ -226,4 +224,4 @@ export class GithubApp extends App implements IPreMessageSentExtend { endpoints: [new githubWebHooks(this)], }); } -} +} \ No newline at end of file diff --git a/github/enum/URLmodifications.ts b/github/enum/URLmodifications.ts new file mode 100644 index 0000000..5e3d801 --- /dev/null +++ b/github/enum/URLmodifications.ts @@ -0,0 +1,5 @@ +export enum URLEnums { + REPLACE_PREFIX = "blob/", + REPLACE_HOST = "github.com", + NEW_HOST = "raw.githubusercontent.com", + } diff --git a/github/handlers/HandleLinks.ts b/github/handlers/HandleLinks.ts index adb865b..5bed76c 100644 --- a/github/handlers/HandleLinks.ts +++ b/github/handlers/HandleLinks.ts @@ -4,6 +4,7 @@ import { IMessage, IMessageAttachment } from "@rocket.chat/apps-engine/definitio import { IRoom } from "@rocket.chat/apps-engine/definition/rooms"; import { IMessageExtender } from "@rocket.chat/apps-engine/definition/accessors"; import { TextObjectType } from "@rocket.chat/apps-engine/definition/uikit"; +import { URLEnums } from "../enum/URLmodifications"; async function extractCodeSnippet(content: string, url: string): Promise { const lineRangeRegex: RegExp = /(?:L(\d+)+-L(\d+)|L(\d+))/; @@ -14,18 +15,18 @@ async function extractCodeSnippet(content: string, url: string): Promise const endLine = parseInt(lineRangeMatch[2]); const linesRegex = `(?:.*\n){${startLine - 1}}(.*(?:\n.*){${endLine - startLine + 1}})`; const lines = new RegExp(linesRegex); - const match = await content.match(lines); + const match = content.match(lines); return match?.[1] ?? ""; } else if (lineRangeMatch?.[3]) { const line = parseInt(lineRangeMatch[3]); const linesRegex = `(?:.*\n){${line - 1}}(.*(?:\n.*){5})`; const lines = new RegExp(linesRegex); - const match = await content.match(lines); + const match = content.match(lines); return match?.[1] ?? ""; } else { const linesRegex = `(?:.*\n){0}(.*(?:\n.*){5})`; const lines = new RegExp(linesRegex); - const match = await content.match(lines); + const match = content.match(lines); return match?.[1] ?? ""; } } @@ -42,8 +43,8 @@ export async function handleGitHubCodeSegmentLink( const messageText: string = message.text!; const urlMatch: RegExpMatchArray | null = messageText.match(urlRegex); const url: string | undefined = urlMatch?.[0]; - let modifiedUrl: string = url?.replace("blob/", "")!; - modifiedUrl = modifiedUrl.replace("github.com", "raw.githubusercontent.com"); + let modifiedUrl: string = url?.replace(URLEnums.REPLACE_PREFIX, "")!; + modifiedUrl = modifiedUrl.replace(URLEnums.REPLACE_HOST, URLEnums.NEW_HOST); const response: any = await http.get(modifiedUrl); const { content } = response; From dfe540b8e17f38639914ae598eefa6790dd06c8f Mon Sep 17 00:00:00 2001 From: imvipindev Date: Thu, 6 Jul 2023 08:57:24 +0530 Subject: [PATCH 7/9] refactor(expand-code): :recycle: refactor whole code to increase readibilty --- github/GithubApp.ts | 2 +- github/enum/GitHubURL.ts | 5 ++ github/enum/URLmodifications.ts | 5 -- github/handlers/EventHandler.ts | 2 +- github/handlers/GitHubCodeSegmentHandler.ts | 69 +++++++++++++++++++++ github/handlers/HandleLinks.ts | 58 ----------------- 6 files changed, 76 insertions(+), 65 deletions(-) create mode 100644 github/enum/GitHubURL.ts delete mode 100644 github/enum/URLmodifications.ts create mode 100644 github/handlers/GitHubCodeSegmentHandler.ts delete mode 100644 github/handlers/HandleLinks.ts diff --git a/github/GithubApp.ts b/github/GithubApp.ts index ba8977f..8e27a6e 100644 --- a/github/GithubApp.ts +++ b/github/GithubApp.ts @@ -47,7 +47,7 @@ import { IRoom } from "@rocket.chat/apps-engine/definition/rooms"; import { clearInteractionRoomData, getInteractionRoomData } from "./persistance/roomInteraction"; import { GHCommand } from "./commands/GhCommand"; import { IPreMessageSentExtend, IMessage } from "@rocket.chat/apps-engine/definition/messages"; -import { handleGitHubCodeSegmentLink } from "./handlers/HandleLinks"; +import { handleGitHubCodeSegmentLink } from "./handlers/GitHubCodeSegmentHandler"; import { isGithubLink, hasGitHubCodeSegmentLink } from "./helpers/checkLinks"; export class GithubApp extends App implements IPreMessageSentExtend { diff --git a/github/enum/GitHubURL.ts b/github/enum/GitHubURL.ts new file mode 100644 index 0000000..d71f95d --- /dev/null +++ b/github/enum/GitHubURL.ts @@ -0,0 +1,5 @@ +export enum GitHubURLEnum { + PREFIX = "blob/", + HOST = "github.com", + RAW_HOST = "raw.githubusercontent.com", +} diff --git a/github/enum/URLmodifications.ts b/github/enum/URLmodifications.ts deleted file mode 100644 index 5e3d801..0000000 --- a/github/enum/URLmodifications.ts +++ /dev/null @@ -1,5 +0,0 @@ -export enum URLEnums { - REPLACE_PREFIX = "blob/", - REPLACE_HOST = "github.com", - NEW_HOST = "raw.githubusercontent.com", - } diff --git a/github/handlers/EventHandler.ts b/github/handlers/EventHandler.ts index 2d74cca..fbeb796 100644 --- a/github/handlers/EventHandler.ts +++ b/github/handlers/EventHandler.ts @@ -21,7 +21,7 @@ import { HandleInvalidRepoName } from "./HandleInvalidRepoName"; export async function SubscribeAllEvents( read: IRead, - context: SlashCommandContext, + context: SlashCommandContext, app: GithubApp, command: string[], persistence: IPersistence, diff --git a/github/handlers/GitHubCodeSegmentHandler.ts b/github/handlers/GitHubCodeSegmentHandler.ts new file mode 100644 index 0000000..3e029da --- /dev/null +++ b/github/handlers/GitHubCodeSegmentHandler.ts @@ -0,0 +1,69 @@ +import { IUser } from "@rocket.chat/apps-engine/definition/users"; +import { IHttp, IRead } from "@rocket.chat/apps-engine/definition/accessors"; +import { IMessage, IMessageAttachment } from "@rocket.chat/apps-engine/definition/messages"; +import { IRoom } from "@rocket.chat/apps-engine/definition/rooms"; +import { IMessageExtender } from "@rocket.chat/apps-engine/definition/accessors"; +import { TextObjectType } from "@rocket.chat/apps-engine/definition/uikit"; +import { GitHubURLEnum } from "../enum/GitHubURL"; + +async function extractCodeSnippetFromURL(content: string, url: string): Promise { + const lineRangeRegex: RegExp = /(?:L(\d+)+-L(\d+)|L(\d+))/; + const lineRangeMatch: RegExpMatchArray | null = url.match(lineRangeRegex); + + if (lineRangeMatch) { + return extractCodeSnippetByLineRange(content, lineRangeMatch); + } + + return ""; +} + +function extractCodeSnippetByLineRange(content: string, lineRangeMatch: RegExpMatchArray): string { + const [_, startLine, endLine, singleLine] = lineRangeMatch; + + const lineOffset = singleLine ? parseInt(singleLine) : parseInt(startLine) - 1; + const lineCount = singleLine ? 1 : parseInt(endLine) - parseInt(startLine) + 1; + + const linesRegex = `(?:.*\n){${lineOffset}}(.*(?:\n.*){${lineCount}})`; + const lines = new RegExp(linesRegex); + const match = content.match(lines); + + return match?.[1] ?? ""; +} + +async function fetchGitHubContent(http: IHttp, modifiedUrl: string): Promise { + const response: any = await http.get(modifiedUrl); + const { content } = response; + return content; +} + +function buildCodeSnippetAttachment(codeSnippet: string, url: string): IMessageAttachment { + const attachment: IMessageAttachment = { + text: `\`\`\`\n${codeSnippet}\n\`\`\` \n[Show more...](${url})`, + type: TextObjectType.MARKDOWN, + }; + return attachment; +} + +export async function handleGitHubCodeSegmentLink( + message: IMessage, + read: IRead, + http: IHttp, + user: IUser, + room: IRoom, + extend: IMessageExtender +) { + const urlRegex: RegExp = /\bhttps?:\/\/github\.com\/\S+\b/; + const messageText: string = message.text!; + const urlMatch: RegExpMatchArray | null = messageText.match(urlRegex); + const url: string | undefined = urlMatch?.[0]; + let modifiedUrl: string = url?.replace(GitHubURLEnum.PREFIX, "")!; + modifiedUrl = modifiedUrl.replace(GitHubURLEnum.HOST, GitHubURLEnum.RAW_HOST); + + const content: string = await fetchGitHubContent(http, modifiedUrl); + const codeSnippet = await extractCodeSnippetFromURL(content, modifiedUrl); + + if (codeSnippet) { + const attachment: IMessageAttachment = buildCodeSnippetAttachment(codeSnippet, url!); + extend.addAttachment(attachment); + } +} diff --git a/github/handlers/HandleLinks.ts b/github/handlers/HandleLinks.ts deleted file mode 100644 index 5bed76c..0000000 --- a/github/handlers/HandleLinks.ts +++ /dev/null @@ -1,58 +0,0 @@ -import { IUser } from "@rocket.chat/apps-engine/definition/users"; -import { IHttp, IRead } from "@rocket.chat/apps-engine/definition/accessors"; -import { IMessage, IMessageAttachment } from "@rocket.chat/apps-engine/definition/messages"; -import { IRoom } from "@rocket.chat/apps-engine/definition/rooms"; -import { IMessageExtender } from "@rocket.chat/apps-engine/definition/accessors"; -import { TextObjectType } from "@rocket.chat/apps-engine/definition/uikit"; -import { URLEnums } from "../enum/URLmodifications"; - -async function extractCodeSnippet(content: string, url: string): Promise { - const lineRangeRegex: RegExp = /(?:L(\d+)+-L(\d+)|L(\d+))/; - const lineRangeMatch: RegExpMatchArray | null = url.match(lineRangeRegex); - - if (lineRangeMatch?.[2]) { - const startLine = parseInt(lineRangeMatch[1]); - const endLine = parseInt(lineRangeMatch[2]); - const linesRegex = `(?:.*\n){${startLine - 1}}(.*(?:\n.*){${endLine - startLine + 1}})`; - const lines = new RegExp(linesRegex); - const match = content.match(lines); - return match?.[1] ?? ""; - } else if (lineRangeMatch?.[3]) { - const line = parseInt(lineRangeMatch[3]); - const linesRegex = `(?:.*\n){${line - 1}}(.*(?:\n.*){5})`; - const lines = new RegExp(linesRegex); - const match = content.match(lines); - return match?.[1] ?? ""; - } else { - const linesRegex = `(?:.*\n){0}(.*(?:\n.*){5})`; - const lines = new RegExp(linesRegex); - const match = content.match(lines); - return match?.[1] ?? ""; - } -} - -export async function handleGitHubCodeSegmentLink( - message: IMessage, - read: IRead, - http: IHttp, - user: IUser, - room: IRoom, - extend: IMessageExtender -) { - const urlRegex: RegExp = /\bhttps?:\/\/github\.com\/\S+\b/; - const messageText: string = message.text!; - const urlMatch: RegExpMatchArray | null = messageText.match(urlRegex); - const url: string | undefined = urlMatch?.[0]; - let modifiedUrl: string = url?.replace(URLEnums.REPLACE_PREFIX, "")!; - modifiedUrl = modifiedUrl.replace(URLEnums.REPLACE_HOST, URLEnums.NEW_HOST); - - const response: any = await http.get(modifiedUrl); - const { content } = response; - const codeSnippet = await extractCodeSnippet(content, modifiedUrl); - - const attachment: IMessageAttachment = { - text: `\`\`\`\n${codeSnippet}\n\`\`\` \n[Show more...](${url})`, - type: TextObjectType.MARKDOWN, - }; - extend.addAttachment(attachment); -} From 14494e3896c3ef54a46f0220e936f345639cc99a Mon Sep 17 00:00:00 2001 From: imvipindev Date: Sun, 9 Jul 2023 21:47:33 +0530 Subject: [PATCH 8/9] refactor(expand-code): :art: improve formatting of code --- github/handlers/GitHubCodeSegmentHandler.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/github/handlers/GitHubCodeSegmentHandler.ts b/github/handlers/GitHubCodeSegmentHandler.ts index 3e029da..7f8bddc 100644 --- a/github/handlers/GitHubCodeSegmentHandler.ts +++ b/github/handlers/GitHubCodeSegmentHandler.ts @@ -11,7 +11,7 @@ async function extractCodeSnippetFromURL(content: string, url: string): Promise< const lineRangeMatch: RegExpMatchArray | null = url.match(lineRangeRegex); if (lineRangeMatch) { - return extractCodeSnippetByLineRange(content, lineRangeMatch); + return extractCodeSnippetByLineRange(content, lineRangeMatch); } return ""; @@ -38,8 +38,8 @@ async function fetchGitHubContent(http: IHttp, modifiedUrl: string): Promise Date: Sun, 9 Jul 2023 21:48:56 +0530 Subject: [PATCH 9/9] Update EventHandler.ts --- github/handlers/EventHandler.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/handlers/EventHandler.ts b/github/handlers/EventHandler.ts index fbeb796..2d74cca 100644 --- a/github/handlers/EventHandler.ts +++ b/github/handlers/EventHandler.ts @@ -21,7 +21,7 @@ import { HandleInvalidRepoName } from "./HandleInvalidRepoName"; export async function SubscribeAllEvents( read: IRead, - context: SlashCommandContext, + context: SlashCommandContext, app: GithubApp, command: string[], persistence: IPersistence,