From c65dc7e02c89b91bcefd71b36086e37ccdf20696 Mon Sep 17 00:00:00 2001 From: Nabhag Motivaras Date: Sun, 25 Dec 2022 15:15:00 +0530 Subject: [PATCH 1/6] feat: handler for invalid reponame with helper message --- github/handlers/HandleInvalidRepoName.ts | 52 ++++++++++++++++++++++++ github/helpers/githubSDK.ts | 21 +++++++++- github/lib/commandUtility.ts | 29 +++++++++---- 3 files changed, 94 insertions(+), 8 deletions(-) create mode 100644 github/handlers/HandleInvalidRepoName.ts diff --git a/github/handlers/HandleInvalidRepoName.ts b/github/handlers/HandleInvalidRepoName.ts new file mode 100644 index 0000000..89b7b77 --- /dev/null +++ b/github/handlers/HandleInvalidRepoName.ts @@ -0,0 +1,52 @@ +import { + IHttp, + IModify, + IRead, +} from "@rocket.chat/apps-engine/definition/accessors"; +import { GithubApp } from "../GithubApp"; +import { isRepositoryExist } from "../helpers/githubSDK"; +import { getAccessTokenForUser } from "../persistance/auth"; +import { IUser } from "@rocket.chat/apps-engine/definition/users"; +import { IRoom } from "@rocket.chat/apps-engine/definition/rooms"; +import { IAuthData } from "@rocket.chat/apps-engine/definition/oauth2/IOAuth2"; + +export async function HandleInvalidRepoName( + repoName: string, + http: IHttp, + app: GithubApp, + modify: IModify, + sender: IUser, + read: IRead, + room: IRoom +): Promise { + const { token } = await getAccessTokenForUser( + read, + sender, + app.oauth2Config + ) as IAuthData; + const isValidRepository: boolean = await isRepositoryExist( + http, + repoName, + token + ); + + if (!isValidRepository) { + const warningBuilder = await modify + .getCreator() + .startMessage() + .setRoom(room) + .setText( + `Hey ${sender.username} ! Provided username and/or repository doesn't exist` + ); + + if (room.type !== "l") { + await modify + .getNotifier() + .notifyUser(sender, warningBuilder.getMessage()); + } else { + await modify.getCreator().finish(warningBuilder); + } + } + + return isValidRepository; +} diff --git a/github/helpers/githubSDK.ts b/github/helpers/githubSDK.ts index 8aab870..1d6b62b 100644 --- a/github/helpers/githubSDK.ts +++ b/github/helpers/githubSDK.ts @@ -1,4 +1,4 @@ -import { IHttp } from "@rocket.chat/apps-engine/definition/accessors"; +import { IHttp, HttpStatusCode } from "@rocket.chat/apps-engine/definition/accessors"; import { IGitHubIssue } from "../definitions/githubIssue"; import { ModalsEnum } from "../enum/Modals"; @@ -726,3 +726,22 @@ export async function updateGithubIssues( } return repsonseJSON; } + +export async function isRepositoryExist( + http: IHttp, + repoName: string, + access_token: string +): Promise { + const response = await http.get(BaseRepoApiHost + repoName, { + headers: { + Authorization: `token ${access_token}`, + "Content-Type": "application/json", + }, + }); + + if (response.statusCode == HttpStatusCode.OK) { + return true; + } + + return false; +} \ No newline at end of file diff --git a/github/lib/commandUtility.ts b/github/lib/commandUtility.ts index dcf4607..7ec7a56 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 { HandleInvalidRepoName } from "../handlers/HandleInvalidRepoName"; export class CommandUtility implements ExecutorProps { sender: IUser; @@ -54,13 +55,27 @@ export class CommandUtility implements ExecutorProps { arguments: this.command, }; if (this.command[0].includes("/")) { - await initiatorMessage({ - data, - read: this.read, - persistence: this.persistence, - modify: this.modify, - http: this.http, - }); + const repoName = this.command[0]; + const isValidRepoName = await HandleInvalidRepoName( + repoName, + this.http, + this.app, + this.modify, + this.sender, + this.read, + this.room + ) + + if(isValidRepoName){ + await initiatorMessage({ + data, + read: this.read, + persistence: this.persistence, + modify: this.modify, + http: this.http, + }); + } + } else { switch (this.command[0]) { case SubcommandEnum.LOGIN: { From e2a577b3f958056e8e280e868f096abf0127358e Mon Sep 17 00:00:00 2001 From: Nabhag Motivaras Date: Sun, 25 Dec 2022 16:34:39 +0530 Subject: [PATCH 2/6] feat: validate repo-name provided in commands --- github/lib/commandUtility.ts | 37 ++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/github/lib/commandUtility.ts b/github/lib/commandUtility.ts index 7ec7a56..bddc0df 100644 --- a/github/lib/commandUtility.ts +++ b/github/lib/commandUtility.ts @@ -57,16 +57,16 @@ export class CommandUtility implements ExecutorProps { if (this.command[0].includes("/")) { const repoName = this.command[0]; const isValidRepoName = await HandleInvalidRepoName( - repoName, + repoName, this.http, this.app, this.modify, this.sender, this.read, this.room - ) + ); - if(isValidRepoName){ + if (isValidRepoName) { await initiatorMessage({ data, read: this.read, @@ -75,7 +75,6 @@ export class CommandUtility implements ExecutorProps { http: this.http, }); } - } else { switch (this.command[0]) { case SubcommandEnum.LOGIN: { @@ -182,6 +181,21 @@ export class CommandUtility implements ExecutorProps { private async handleDualParamCommands() { const query = this.command[1]; const repository = this.command[0]; + + const isValidRepo = await HandleInvalidRepoName( + repository, + this.http, + this.app, + this.modify, + this.sender, + this.read, + this.room + ); + + if (!isValidRepo) { + return; + } + switch (query) { case SubcommandEnum.SUBSCRIBE: { SubscribeAllEvents( @@ -230,6 +244,21 @@ export class CommandUtility implements ExecutorProps { query: this.command[1], number: this.command[2], }; + + const isValidRepo = await HandleInvalidRepoName( + data.repository, + this.http, + this.app, + this.modify, + this.sender, + this.read, + this.room + ); + + if (!isValidRepo) { + return; + } + const triggerId = this.context.getTriggerId(); if (triggerId) { const modal = await pullDetailsModal({ From e8b4d7e0392a2f40beb04fcc9177e587bf1899a4 Mon Sep 17 00:00:00 2001 From: Nabhag Motivaras Date: Tue, 21 Mar 2023 13:36:50 +0530 Subject: [PATCH 3/6] fix: breaking changes of unauth user unable to access --- github/handlers/HandleInvalidRepoName.ts | 9 ++------- github/helpers/githubSDK.ts | 8 +++----- 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/github/handlers/HandleInvalidRepoName.ts b/github/handlers/HandleInvalidRepoName.ts index 89b7b77..5c7c5d6 100644 --- a/github/handlers/HandleInvalidRepoName.ts +++ b/github/handlers/HandleInvalidRepoName.ts @@ -19,15 +19,10 @@ export async function HandleInvalidRepoName( read: IRead, room: IRoom ): Promise { - const { token } = await getAccessTokenForUser( - read, - sender, - app.oauth2Config - ) as IAuthData; + const isValidRepository: boolean = await isRepositoryExist( http, repoName, - token ); if (!isValidRepository) { @@ -36,7 +31,7 @@ export async function HandleInvalidRepoName( .startMessage() .setRoom(room) .setText( - `Hey ${sender.username} ! Provided username and/or repository doesn't exist` + `Hey ${sender.username}! Provided username and/or repository doesn't exist` ); if (room.type !== "l") { diff --git a/github/helpers/githubSDK.ts b/github/helpers/githubSDK.ts index 1d6b62b..cf15f62 100644 --- a/github/helpers/githubSDK.ts +++ b/github/helpers/githubSDK.ts @@ -729,17 +729,15 @@ export async function updateGithubIssues( export async function isRepositoryExist( http: IHttp, - repoName: string, - access_token: string + repoName: string ): Promise { - const response = await http.get(BaseRepoApiHost + repoName, { + const response = await http.get(`${BaseRepoApiHost}${repoName}`, { headers: { - Authorization: `token ${access_token}`, "Content-Type": "application/json", }, }); - if (response.statusCode == HttpStatusCode.OK) { + if (response.statusCode.toString().startsWith("2")) { return true; } From 3dc5ebd8682d273548e2c57ca0f05bdf1c9a9d95 Mon Sep 17 00:00:00 2001 From: Nabhag Motivaras Date: Tue, 21 Mar 2023 13:41:42 +0530 Subject: [PATCH 4/6] fix: subscribe giving invalid message-first --- github/handlers/EventHandler.ts | 30 ++++++++++++++++++++++++++++++ github/lib/commandUtility.ts | 14 -------------- 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/github/handlers/EventHandler.ts b/github/handlers/EventHandler.ts index 9df243c..6860d1b 100644 --- a/github/handlers/EventHandler.ts +++ b/github/handlers/EventHandler.ts @@ -17,6 +17,7 @@ import { sendNotification } from "../lib/message"; import { subsciptionsModal } from "../modals/subscriptionsModal"; import { getAccessTokenForUser } from "../persistance/auth"; import { Subscription } from "../persistance/subscriptions"; +import { HandleInvalidRepoName } from "./HandleInvalidRepoName"; export async function SubscribeAllEvents( read: IRead, @@ -36,6 +37,21 @@ export async function SubscribeAllEvents( const repository = command[0]; if (accessToken && accessToken?.token) { try { + const sender = context.getSender(); + const isValidRepo = await HandleInvalidRepoName( + repository, + http, + app, + modify, + sender, + read, + room + ); + + if (!isValidRepo) { + return; + } + let events: Array = [ "pull_request", "push", @@ -148,6 +164,20 @@ export async function UnsubscribeAllEvents( if (accessToken && accessToken?.token) { try { let user = await context.getSender(); + const isValidRepo = await HandleInvalidRepoName( + repository, + http, + app, + modify, + user, + read, + room + ); + + if (!isValidRepo) { + return; + } + let events: Array = [ "pull_request", "push", diff --git a/github/lib/commandUtility.ts b/github/lib/commandUtility.ts index bddc0df..265c865 100644 --- a/github/lib/commandUtility.ts +++ b/github/lib/commandUtility.ts @@ -182,20 +182,6 @@ export class CommandUtility implements ExecutorProps { const query = this.command[1]; const repository = this.command[0]; - const isValidRepo = await HandleInvalidRepoName( - repository, - this.http, - this.app, - this.modify, - this.sender, - this.read, - this.room - ); - - if (!isValidRepo) { - return; - } - switch (query) { case SubcommandEnum.SUBSCRIBE: { SubscribeAllEvents( From 190e9f37a7e9ad0da7f296095a89c6de1fa98abd Mon Sep 17 00:00:00 2001 From: Nabhag Motivaras Date: Wed, 22 Mar 2023 05:04:21 +0530 Subject: [PATCH 5/6] fix: private repo access & confusing message --- github/handlers/HandleInvalidRepoName.ts | 11 +++++++++- github/helpers/githubSDK.ts | 26 +++++++++++++++++------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/github/handlers/HandleInvalidRepoName.ts b/github/handlers/HandleInvalidRepoName.ts index 5c7c5d6..36d226b 100644 --- a/github/handlers/HandleInvalidRepoName.ts +++ b/github/handlers/HandleInvalidRepoName.ts @@ -19,10 +19,18 @@ export async function HandleInvalidRepoName( read: IRead, room: IRoom ): Promise { + const accessTokenInfo = (await getAccessTokenForUser( + read, + sender, + app.oauth2Config + )) as IAuthData; + const access_token = (accessTokenInfo == undefined) ? undefined : accessTokenInfo.token; + const isValidRepository: boolean = await isRepositoryExist( http, repoName, + access_token ); if (!isValidRepository) { @@ -31,7 +39,7 @@ export async function HandleInvalidRepoName( .startMessage() .setRoom(room) .setText( - `Hey ${sender.username}! Provided username and/or repository doesn't exist` + `Hey ${sender.username}! Provided repository doesn't exist or you need to login to access private repo.` ); if (room.type !== "l") { @@ -45,3 +53,4 @@ export async function HandleInvalidRepoName( return isValidRepository; } + diff --git a/github/helpers/githubSDK.ts b/github/helpers/githubSDK.ts index cf15f62..274b8d8 100644 --- a/github/helpers/githubSDK.ts +++ b/github/helpers/githubSDK.ts @@ -729,17 +729,29 @@ export async function updateGithubIssues( export async function isRepositoryExist( http: IHttp, - repoName: string + repoName: string, + access_token?: string ): Promise { - const response = await http.get(`${BaseRepoApiHost}${repoName}`, { - headers: { - "Content-Type": "application/json", - }, - }); + let response; + + if (access_token) { + response = await http.get(`${BaseRepoApiHost}${repoName}`, { + headers: { + Authorization: `token ${access_token}`, + "Content-Type": "application/json", + }, + }); + } else { + response = await http.get(`${BaseRepoApiHost}${repoName}`, { + headers: { + "Content-Type": "application/json", + }, + }); + } if (response.statusCode.toString().startsWith("2")) { return true; } return false; -} \ No newline at end of file +} From 77d46dd4420e53f2d24ad277b0d9156e0d446010 Mon Sep 17 00:00:00 2001 From: Nabhag Motivaras Date: Thu, 23 Mar 2023 00:23:02 +0530 Subject: [PATCH 6/6] fix: naming convention, removable condition & warning text --- github/handlers/HandleInvalidRepoName.ts | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/github/handlers/HandleInvalidRepoName.ts b/github/handlers/HandleInvalidRepoName.ts index 36d226b..ac53e68 100644 --- a/github/handlers/HandleInvalidRepoName.ts +++ b/github/handlers/HandleInvalidRepoName.ts @@ -19,28 +19,33 @@ export async function HandleInvalidRepoName( read: IRead, room: IRoom ): Promise { - const accessTokenInfo = (await getAccessTokenForUser( + const accessToken = (await getAccessTokenForUser( read, sender, app.oauth2Config )) as IAuthData; - const access_token = (accessTokenInfo == undefined) ? undefined : accessTokenInfo.token; - const isValidRepository: boolean = await isRepositoryExist( http, repoName, - access_token + accessToken?.token ); if (!isValidRepository) { const warningBuilder = await modify .getCreator() .startMessage() - .setRoom(room) - .setText( + .setRoom(room); + + if (accessToken) { + warningBuilder.setText( + `Hey ${sender.username}! Provided repository doesn't exist.` + ); + } else { + warningBuilder.setText( `Hey ${sender.username}! Provided repository doesn't exist or you need to login to access private repo.` ); + } if (room.type !== "l") { await modify @@ -53,4 +58,3 @@ export async function HandleInvalidRepoName( return isValidRepository; } -