From 18ca8cbfb0ecce5dddc2e692402efa2d6f27dc9a Mon Sep 17 00:00:00 2001 From: Prince Mendiratta Date: Mon, 27 Mar 2023 02:50:01 +0530 Subject: [PATCH 1/6] feat: show github login button directly when auth req Signed-off-by: Prince Mendiratta --- github/handlers/HandleIssues.ts | 10 ++++++++-- github/handlers/SearchHandler.ts | 9 +++++++-- github/handlers/UserProfileHandler.ts | 10 ++++++++-- github/oath2/authentication.ts | 14 ++++++++++---- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/github/handlers/HandleIssues.ts b/github/handlers/HandleIssues.ts index 52f38cc..af447f7 100644 --- a/github/handlers/HandleIssues.ts +++ b/github/handlers/HandleIssues.ts @@ -6,6 +6,8 @@ import { sendNotification } from "../lib/message"; import { NewIssueStarterModal } from "../modals/newIssueStarterModal"; import { getAccessTokenForUser } from "../persistance/auth"; import { GitHubIssuesStarterModal } from "../modals/getIssuesStarterModal"; +import { getGithubOauthBlock } from "../oath2/authentication"; +import { createSectionBlock } from "../lib/blocks"; export async function handleNewIssue( read: IRead, @@ -42,12 +44,16 @@ export async function handleNewIssue( console.log("Inavlid Trigger ID !"); } } else { + const user = context.getSender(); + const message = `"Login to subscribe to repository events!`; + const block = await getGithubOauthBlock(app, user, modify, message); await sendNotification( read, modify, - context.getSender(), + user, room, - "Login to subscribe to repository events ! `/github login`" + message, + block ); } } diff --git a/github/handlers/SearchHandler.ts b/github/handlers/SearchHandler.ts index a3348b5..5f20697 100644 --- a/github/handlers/SearchHandler.ts +++ b/github/handlers/SearchHandler.ts @@ -4,6 +4,7 @@ import { SlashCommandContext } from "@rocket.chat/apps-engine/definition/slashco import { GithubApp } from "../GithubApp"; import { sendNotification } from "../lib/message"; import { githubSearchModal } from "../modals/githubSearchModal"; +import { getGithubOauthBlock } from "../oath2/authentication"; import { getAccessTokenForUser } from "../persistance/auth"; export async function handleSearch( @@ -41,12 +42,16 @@ export async function handleSearch( console.log("Inavlid Trigger ID !"); } } else { + const user = context.getSender(); + const message = `"Login to subscribe to repository events!`; + const block = await getGithubOauthBlock(app, user, modify, message); await sendNotification( read, modify, - context.getSender(), + user, room, - "Login to subscribe to repository events ! `/github login`" + message, + block ); } } diff --git a/github/handlers/UserProfileHandler.ts b/github/handlers/UserProfileHandler.ts index 0c94295..aabfb8a 100644 --- a/github/handlers/UserProfileHandler.ts +++ b/github/handlers/UserProfileHandler.ts @@ -3,8 +3,10 @@ import { IRoom } from "@rocket.chat/apps-engine/definition/rooms"; import { SlashCommandContext } from "@rocket.chat/apps-engine/definition/slashcommands"; import { UIKitInteractionContext } from "@rocket.chat/apps-engine/definition/uikit"; import { GithubApp } from "../GithubApp"; +import { createSectionBlock } from "../lib/blocks"; import { sendNotification } from "../lib/message"; import { userProfileModal } from "../modals/UserProfileModal"; +import { getGithubOauthBlock } from "../oath2/authentication"; import { getAccessTokenForUser } from "../persistance/auth"; export async function handleUserProfileRequest( @@ -40,12 +42,16 @@ export async function handleUserProfileRequest( ); } }else { + const user = context.getSender(); + const message = `"Login to get your user info!`; + const block = await getGithubOauthBlock(app, user, modify, message); await sendNotification( read, modify, - context.getSender(), + user, room, - "Login is Mandatory for getting User Info ! `/github login`" + message, + block ) } diff --git a/github/oath2/authentication.ts b/github/oath2/authentication.ts index 4108fd2..c6fec42 100644 --- a/github/oath2/authentication.ts +++ b/github/oath2/authentication.ts @@ -18,6 +18,13 @@ export async function authorize( room: IRoom, persistence: IPersistence ): Promise { + const message = `Login to GitHub`; + const block = await getGithubOauthBlock(app, user, modify, message); + await storeInteractionRoomData(persistence,user.id,room.id); + await sendNotification(read, modify, user, room, message, block); +} + +export async function getGithubOauthBlock(app: GithubApp, user: IUser, modify: IModify, message: string) { const url = await app .getOauth2ClientInstance() .getUserAuthorizationUrl(user); @@ -26,8 +33,7 @@ export async function authorize( text: "GitHub Login", url: url.toString(), }; - const message = `Login to GitHub`; + const block = await createSectionBlock(modify, message, button); - await storeInteractionRoomData(persistence,user.id,room.id); - await sendNotification(read, modify, user, room, message, block); -} + return block; +} \ No newline at end of file From e00a66942a9ef2b844884327435c1f68408d5729 Mon Sep 17 00:00:00 2001 From: Prince Mendiratta Date: Mon, 27 Mar 2023 03:17:27 +0530 Subject: [PATCH 2/6] fix: remove unnecessary quotations Signed-off-by: Prince Mendiratta --- github/handlers/HandleIssues.ts | 2 +- github/handlers/SearchHandler.ts | 2 +- github/handlers/UserProfileHandler.ts | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/github/handlers/HandleIssues.ts b/github/handlers/HandleIssues.ts index af447f7..16aba86 100644 --- a/github/handlers/HandleIssues.ts +++ b/github/handlers/HandleIssues.ts @@ -45,7 +45,7 @@ export async function handleNewIssue( } } else { const user = context.getSender(); - const message = `"Login to subscribe to repository events!`; + const message = `Login to subscribe to repository events!`; const block = await getGithubOauthBlock(app, user, modify, message); await sendNotification( read, diff --git a/github/handlers/SearchHandler.ts b/github/handlers/SearchHandler.ts index 5f20697..846a8e3 100644 --- a/github/handlers/SearchHandler.ts +++ b/github/handlers/SearchHandler.ts @@ -43,7 +43,7 @@ export async function handleSearch( } } else { const user = context.getSender(); - const message = `"Login to subscribe to repository events!`; + const message = `Login to subscribe to repository events!`; const block = await getGithubOauthBlock(app, user, modify, message); await sendNotification( read, diff --git a/github/handlers/UserProfileHandler.ts b/github/handlers/UserProfileHandler.ts index aabfb8a..2a4902b 100644 --- a/github/handlers/UserProfileHandler.ts +++ b/github/handlers/UserProfileHandler.ts @@ -43,7 +43,7 @@ export async function handleUserProfileRequest( } }else { const user = context.getSender(); - const message = `"Login to get your user info!`; + const message = `Login to get your user info!`; const block = await getGithubOauthBlock(app, user, modify, message); await sendNotification( read, From f04803a424f7c252551ed12b761a9e4215c8f76b Mon Sep 17 00:00:00 2001 From: Prince Mendiratta Date: Mon, 27 Mar 2023 03:22:12 +0530 Subject: [PATCH 3/6] feat: add inline oauth to EventHandler Signed-off-by: Prince Mendiratta --- github/handlers/EventHandler.ts | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/github/handlers/EventHandler.ts b/github/handlers/EventHandler.ts index 6860d1b..c626988 100644 --- a/github/handlers/EventHandler.ts +++ b/github/handlers/EventHandler.ts @@ -15,6 +15,7 @@ import { } from "../helpers/githubSDK"; import { sendNotification } from "../lib/message"; import { subsciptionsModal } from "../modals/subscriptionsModal"; +import { getGithubOauthBlock } from "../oath2/authentication"; import { getAccessTokenForUser } from "../persistance/auth"; import { Subscription } from "../persistance/subscriptions"; import { HandleInvalidRepoName } from "./HandleInvalidRepoName"; @@ -135,12 +136,16 @@ export async function SubscribeAllEvents( console.log("SubcommandError", error); } } else { + const user = context.getSender(); + const message = `Login to subscribe to repository events!`; + const block = await getGithubOauthBlock(app, user, modify, message); await sendNotification( read, modify, - context.getSender(), + user, room, - "Login to subscribe to repository events ! `/github login`" + message, + block ); } } @@ -254,12 +259,16 @@ export async function UnsubscribeAllEvents( console.log("SubcommandError", error); } } else { + const user = context.getSender(); + const message = `Login to subscribe to repository events!`; + const block = await getGithubOauthBlock(app, user, modify, message); await sendNotification( read, modify, - context.getSender(), + user, room, - "Login to subscribe to repository events ! `/github login`" + message, + block ); } } @@ -295,12 +304,16 @@ export async function ManageSubscriptions( console.log("Invalid Trigger ID !"); } } else { + const user = context.getSender(); + const message = `Login to subscribe to repository events!`; + const block = await getGithubOauthBlock(app, user, modify, message); await sendNotification( read, modify, - context.getSender(), + user, room, - "Login to subscribe to repository events ! `/github login`" + message, + block ); } } From 9e74841339231d1bc6362dca9d518abb9bba3b92 Mon Sep 17 00:00:00 2001 From: Prince Mendiratta Date: Mon, 27 Mar 2023 03:55:26 +0530 Subject: [PATCH 4/6] feat: add authorization check to issues search Signed-off-by: Prince Mendiratta --- github/handlers/HandleIssues.ts | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/github/handlers/HandleIssues.ts b/github/handlers/HandleIssues.ts index 16aba86..fdded8e 100644 --- a/github/handlers/HandleIssues.ts +++ b/github/handlers/HandleIssues.ts @@ -67,12 +67,31 @@ export async function handleIssues( room: IRoom, modify: IModify ){ - const triggerId= context.getTriggerId(); - if(triggerId){ - const modal = await GitHubIssuesStarterModal({modify,read,persistence,http,slashcommandcontext:context}); - await modify.getUiController().openModalView(modal,{triggerId},context.getSender()); - }else{ - console.log("Inavlid Trigger ID !"); + let accessToken = await getAccessTokenForUser( + read, + context.getSender(), + app.oauth2Config + ); + if (accessToken && accessToken.token) { + const triggerId= context.getTriggerId(); + if(triggerId){ + const modal = await GitHubIssuesStarterModal({modify,read,persistence,http,slashcommandcontext:context}); + await modify.getUiController().openModalView(modal,{triggerId},context.getSender()); + }else{ + console.log("Inavlid Trigger ID !"); + } + } else { + const user = context.getSender(); + const message = `Login to assign!`; + const block = await getGithubOauthBlock(app, user, modify, message); + await sendNotification( + read, + modify, + user, + room, + message, + block + ); } } From 73d8e6af7f6c9a1c2f54148b5d288a322497c44b Mon Sep 17 00:00:00 2001 From: Prince Mendiratta Date: Mon, 27 Mar 2023 03:57:00 +0530 Subject: [PATCH 5/6] feat: improve login required disclaimers Signed-off-by: Prince Mendiratta --- github/handlers/EventHandler.ts | 4 ++-- github/handlers/HandleIssues.ts | 4 ++-- github/handlers/SearchHandler.ts | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/github/handlers/EventHandler.ts b/github/handlers/EventHandler.ts index c626988..f2b8b78 100644 --- a/github/handlers/EventHandler.ts +++ b/github/handlers/EventHandler.ts @@ -260,7 +260,7 @@ export async function UnsubscribeAllEvents( } } else { const user = context.getSender(); - const message = `Login to subscribe to repository events!`; + const message = `Login to unsubscribe from repository events!`; const block = await getGithubOauthBlock(app, user, modify, message); await sendNotification( read, @@ -305,7 +305,7 @@ export async function ManageSubscriptions( } } else { const user = context.getSender(); - const message = `Login to subscribe to repository events!`; + const message = `Login to manage repository events!`; const block = await getGithubOauthBlock(app, user, modify, message); await sendNotification( read, diff --git a/github/handlers/HandleIssues.ts b/github/handlers/HandleIssues.ts index fdded8e..2a5b777 100644 --- a/github/handlers/HandleIssues.ts +++ b/github/handlers/HandleIssues.ts @@ -45,7 +45,7 @@ export async function handleNewIssue( } } else { const user = context.getSender(); - const message = `Login to subscribe to repository events!`; + const message = `Login to add a new issue!`; const block = await getGithubOauthBlock(app, user, modify, message); await sendNotification( read, @@ -82,7 +82,7 @@ export async function handleIssues( } } else { const user = context.getSender(); - const message = `Login to assign!`; + const message = `Login to assign issues!`; const block = await getGithubOauthBlock(app, user, modify, message); await sendNotification( read, diff --git a/github/handlers/SearchHandler.ts b/github/handlers/SearchHandler.ts index 846a8e3..96235ed 100644 --- a/github/handlers/SearchHandler.ts +++ b/github/handlers/SearchHandler.ts @@ -43,7 +43,7 @@ export async function handleSearch( } } else { const user = context.getSender(); - const message = `Login to subscribe to repository events!`; + const message = `Login to search issues!`; const block = await getGithubOauthBlock(app, user, modify, message); await sendNotification( read, From 0dc457a32e7b230b53f3eeb10c7e71476bfa3e10 Mon Sep 17 00:00:00 2001 From: Prince Mendiratta Date: Mon, 3 Apr 2023 15:55:39 +0530 Subject: [PATCH 6/6] Revert "feat: add authorization check to issues search" This reverts commit 9e74841339231d1bc6362dca9d518abb9bba3b92. --- github/handlers/HandleIssues.ts | 31 ++++++------------------------- 1 file changed, 6 insertions(+), 25 deletions(-) diff --git a/github/handlers/HandleIssues.ts b/github/handlers/HandleIssues.ts index 2a5b777..69f5f2f 100644 --- a/github/handlers/HandleIssues.ts +++ b/github/handlers/HandleIssues.ts @@ -67,31 +67,12 @@ export async function handleIssues( room: IRoom, modify: IModify ){ - let accessToken = await getAccessTokenForUser( - read, - context.getSender(), - app.oauth2Config - ); - if (accessToken && accessToken.token) { - const triggerId= context.getTriggerId(); - if(triggerId){ - const modal = await GitHubIssuesStarterModal({modify,read,persistence,http,slashcommandcontext:context}); - await modify.getUiController().openModalView(modal,{triggerId},context.getSender()); - }else{ - console.log("Inavlid Trigger ID !"); - } - } else { - const user = context.getSender(); - const message = `Login to assign issues!`; - const block = await getGithubOauthBlock(app, user, modify, message); - await sendNotification( - read, - modify, - user, - room, - message, - block - ); + const triggerId= context.getTriggerId(); + if(triggerId){ + const modal = await GitHubIssuesStarterModal({modify,read,persistence,http,slashcommandcontext:context}); + await modify.getUiController().openModalView(modal,{triggerId},context.getSender()); + }else{ + console.log("Inavlid Trigger ID !"); } }