From e7b0c9bb9f0ba323df5ef5d47905bd9cbd86a20c Mon Sep 17 00:00:00 2001 From: Chetanya Kandhari Date: Thu, 8 Oct 2020 11:10:12 +0530 Subject: [PATCH 1/2] Add logic to disable webapp --- plugin.json | 7 +++++++ server/plugin/api.go | 11 +++++++++++ server/plugin/configuration.go | 1 + webapp/src/actions/index.js | 11 +++++++++++ webapp/src/client/client.js | 4 ++++ webapp/src/index.js | 35 ++++++++++++++++++---------------- 6 files changed, 53 insertions(+), 16 deletions(-) diff --git a/plugin.json b/plugin.json index 90576352c..3f0859adc 100644 --- a/plugin.json +++ b/plugin.json @@ -21,6 +21,13 @@ "settings_schema": { "header": "The GitHub plugin for Mattermost allows users to Subscribe to notifications, stay up-to-date with reviews, see the status of your pull requests at a glance, and other common GitHub actions - directly from Mattermost. \n \n Instructions for setup are [available here](https://www.mattermost.com/pl/default-github-plugin#configuration)", "settings": [ + { + "key": "EnableUI", + "display_name": "", + "type": "bool", + "help_text": "", + "default": true + }, { "key": "GitHubOAuthClientID", "display_name": "GitHub OAuth Client ID", diff --git a/server/plugin/api.go b/server/plugin/api.go index ebc300823..a2bf41670 100644 --- a/server/plugin/api.go +++ b/server/plugin/api.go @@ -108,6 +108,7 @@ func (p *Plugin) initializeAPI() { oauthRouter.HandleFunc("/connect", p.extractUserMiddleWare(p.connectUserToGitHub, ResponseTypePlain)).Methods(http.MethodGet) oauthRouter.HandleFunc("/complete", p.extractUserMiddleWare(p.completeConnectUserToGitHub, ResponseTypePlain)).Methods(http.MethodGet) + apiRouter.HandleFunc("/settingsinfo", p.getSettingsInfo).Methods(http.MethodPost) apiRouter.HandleFunc("/connected", p.getConnected).Methods(http.MethodGet) apiRouter.HandleFunc("/todo", p.extractUserMiddleWare(p.postToDo, ResponseTypeJSON)).Methods(http.MethodPost) apiRouter.HandleFunc("/reviews", p.extractUserMiddleWare(p.getReviews, ResponseTypePlain)).Methods(http.MethodGet) @@ -193,6 +194,16 @@ func (p *Plugin) ServeHTTP(c *plugin.Context, w http.ResponseWriter, r *http.Req p.router.ServeHTTP(w, r) } +func (p *Plugin) getSettingsInfo(w http.ResponseWriter, _ *http.Request) { + resp := struct { + UIEnabled bool `json:"ui_enabled"` + }{ + UIEnabled: p.getConfiguration().EnableUI, + } + + p.writeJSON(w, resp) +} + func (p *Plugin) connectUserToGitHub(w http.ResponseWriter, r *http.Request, userID string) { privateAllowed := false pValBool, _ := strconv.ParseBool(r.URL.Query().Get("private")) diff --git a/server/plugin/configuration.go b/server/plugin/configuration.go index 8384648ab..e7403ca67 100644 --- a/server/plugin/configuration.go +++ b/server/plugin/configuration.go @@ -18,6 +18,7 @@ import ( // If you add non-reference types to your configuration struct, be sure to rewrite Clone as a deep // copy appropriate for your types. type Configuration struct { + EnableUI bool GitHubOrg string GitHubOAuthClientID string GitHubOAuthClientSecret string diff --git a/webapp/src/actions/index.js b/webapp/src/actions/index.js index d32141fb7..861c86320 100644 --- a/webapp/src/actions/index.js +++ b/webapp/src/actions/index.js @@ -6,6 +6,17 @@ import ActionTypes from '../action_types'; import {id as pluginId} from '../manifest'; +export function getSettings() { + let data; + try { + data = Client.getSettings(); + } catch (error) { + return {error}; + } + + return {data}; +} + export function getConnected(reminder = false) { return async (dispatch) => { let data; diff --git a/webapp/src/client/client.js b/webapp/src/client/client.js index ed54e14b2..774554f4e 100644 --- a/webapp/src/client/client.js +++ b/webapp/src/client/client.js @@ -9,6 +9,10 @@ export default class Client { this.url = '/plugins/github/api/v1'; } + getSettings = async () => { + return this.doGet(`${this.url}/settingsinfo`); + } + getConnected = async (reminder = false) => { return this.doGet(`${this.url}/connected?reminder=${reminder}`); } diff --git a/webapp/src/index.js b/webapp/src/index.js index 05fdc7b3d..17b9f1c46 100644 --- a/webapp/src/index.js +++ b/webapp/src/index.js @@ -11,7 +11,7 @@ import UserAttribute from './components/user_attribute'; import SidebarRight from './components/sidebar_right'; import LinkTooltip from './components/link_tooltip'; import Reducer from './reducers'; -import {getConnected, setShowRHSAction} from './actions'; +import {getConnected, setShowRHSAction, getSettings} from './actions'; import {handleConnect, handleDisconnect, handleReconnect, handleRefresh} from './websocket'; let activityFunc; @@ -22,23 +22,26 @@ class PluginClass { async initialize(registry, store) { registry.registerReducer(Reducer); + const {data: settings} = await getSettings(store.getState); + await getConnected(true)(store.dispatch, store.getState); - registry.registerLeftSidebarHeaderComponent(SidebarHeader); - registry.registerBottomTeamSidebarComponent(TeamSidebar); - registry.registerPopoverUserAttributesComponent(UserAttribute); - registry.registerRootComponent(CreateIssueModal); - registry.registerPostDropdownMenuComponent(CreateIssuePostMenuAction); - registry.registerRootComponent(AttachCommentToIssueModal); - registry.registerPostDropdownMenuComponent(AttachCommentToIssuePostMenuAction); - registry.registerLinkTooltipComponent(LinkTooltip); - - const {showRHSPlugin} = registry.registerRightHandSidebarComponent(SidebarRight, 'GitHub'); - store.dispatch(setShowRHSAction(() => store.dispatch(showRHSPlugin))); - - registry.registerWebSocketEventHandler('custom_github_connect', handleConnect(store)); - registry.registerWebSocketEventHandler('custom_github_disconnect', handleDisconnect(store)); - registry.registerWebSocketEventHandler('custom_github_refresh', handleRefresh(store)); + if (settings && settings.ui_enabled) { + registry.registerLeftSidebarHeaderComponent(SidebarHeader); + registry.registerBottomTeamSidebarComponent(TeamSidebar); + registry.registerPopoverUserAttributesComponent(UserAttribute); + registry.registerRootComponent(CreateIssueModal); + registry.registerPostDropdownMenuComponent(CreateIssuePostMenuAction); + registry.registerRootComponent(AttachCommentToIssueModal); + registry.registerPostDropdownMenuComponent(AttachCommentToIssuePostMenuAction); + registry.registerLinkTooltipComponent(LinkTooltip); + const {showRHSPlugin} = registry.registerRightHandSidebarComponent(SidebarRight, 'GitHub'); + store.dispatch(setShowRHSAction(() => store.dispatch(showRHSPlugin))); + + registry.registerWebSocketEventHandler('custom_github_connect', handleConnect(store)); + registry.registerWebSocketEventHandler('custom_github_disconnect', handleDisconnect(store)); + registry.registerWebSocketEventHandler('custom_github_refresh', handleRefresh(store)); + } registry.registerReconnectHandler(handleReconnect(store)); activityFunc = () => { From 078250ea6f61051dc3c4489f8311d0141bdd42da Mon Sep 17 00:00:00 2001 From: Jatin KrishanKumar Sharma Date: Thu, 8 Oct 2020 14:00:10 +0530 Subject: [PATCH 2/2] fix getSettings function --- plugin.json | 2 +- server/plugin/api.go | 2 +- server/plugin/manifest.go | 8 ++++++++ webapp/package-lock.json | 6 +++--- webapp/src/actions/index.js | 6 ++---- webapp/src/manifest.ts | 8 ++++++++ 6 files changed, 23 insertions(+), 9 deletions(-) diff --git a/plugin.json b/plugin.json index 3f0859adc..2c35428cd 100644 --- a/plugin.json +++ b/plugin.json @@ -23,7 +23,7 @@ "settings": [ { "key": "EnableUI", - "display_name": "", + "display_name": "Enable UI", "type": "bool", "help_text": "", "default": true diff --git a/server/plugin/api.go b/server/plugin/api.go index a2bf41670..8cf7fd8ae 100644 --- a/server/plugin/api.go +++ b/server/plugin/api.go @@ -108,7 +108,7 @@ func (p *Plugin) initializeAPI() { oauthRouter.HandleFunc("/connect", p.extractUserMiddleWare(p.connectUserToGitHub, ResponseTypePlain)).Methods(http.MethodGet) oauthRouter.HandleFunc("/complete", p.extractUserMiddleWare(p.completeConnectUserToGitHub, ResponseTypePlain)).Methods(http.MethodGet) - apiRouter.HandleFunc("/settingsinfo", p.getSettingsInfo).Methods(http.MethodPost) + apiRouter.HandleFunc("/settingsinfo", p.getSettingsInfo).Methods(http.MethodGet) apiRouter.HandleFunc("/connected", p.getConnected).Methods(http.MethodGet) apiRouter.HandleFunc("/todo", p.extractUserMiddleWare(p.postToDo, ResponseTypeJSON)).Methods(http.MethodPost) apiRouter.HandleFunc("/reviews", p.extractUserMiddleWare(p.getReviews, ResponseTypePlain)).Methods(http.MethodGet) diff --git a/server/plugin/manifest.go b/server/plugin/manifest.go index f58ebafa4..8fe479645 100644 --- a/server/plugin/manifest.go +++ b/server/plugin/manifest.go @@ -36,6 +36,14 @@ const manifestStr = ` "header": "The GitHub plugin for Mattermost allows users to Subscribe to notifications, stay up-to-date with reviews, see the status of your pull requests at a glance, and other common GitHub actions - directly from Mattermost. \n \n Instructions for setup are [available here](https://www.mattermost.com/pl/default-github-plugin#configuration)", "footer": "* To report an issue, make a suggestion or a contribution, [check the repository](https://github.com/mattermost/mattermost-plugin-github).", "settings": [ + { + "key": "EnableUI", + "display_name": "Enable UI", + "type": "bool", + "help_text": "", + "placeholder": "", + "default": true + }, { "key": "GitHubOAuthClientID", "display_name": "GitHub OAuth Client ID", diff --git a/webapp/package-lock.json b/webapp/package-lock.json index b234203e4..4d8bbdcf3 100644 --- a/webapp/package-lock.json +++ b/webapp/package-lock.json @@ -15412,9 +15412,9 @@ }, "dependencies": { "safe-buffer": { - "version": "5.2.1", - "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", - "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.2.tgz", + "integrity": "sha512-Gd2UZBJDkXlY7GbJxfsE8/nvKkUEU1G38c1siN6QP6a9PT9MmHB8GnpscSmMJSoF8LOIrt8ud/wPtojys4G6+g==", "dev": true } } diff --git a/webapp/src/actions/index.js b/webapp/src/actions/index.js index 861c86320..170932545 100644 --- a/webapp/src/actions/index.js +++ b/webapp/src/actions/index.js @@ -6,17 +6,15 @@ import ActionTypes from '../action_types'; import {id as pluginId} from '../manifest'; -export function getSettings() { +export async function getSettings() { let data; try { - data = Client.getSettings(); + data = await Client.getSettings(); } catch (error) { return {error}; } - return {data}; } - export function getConnected(reminder = false) { return async (dispatch) => { let data; diff --git a/webapp/src/manifest.ts b/webapp/src/manifest.ts index 60ddd06a5..e0485954c 100644 --- a/webapp/src/manifest.ts +++ b/webapp/src/manifest.ts @@ -26,6 +26,14 @@ const manifest = JSON.parse(` "header": "The GitHub plugin for Mattermost allows users to Subscribe to notifications, stay up-to-date with reviews, see the status of your pull requests at a glance, and other common GitHub actions - directly from Mattermost. \\n \\n Instructions for setup are [available here](https://www.mattermost.com/pl/default-github-plugin#configuration)", "footer": "* To report an issue, make a suggestion or a contribution, [check the repository](https://github.com/mattermost/mattermost-plugin-github).", "settings": [ + { + "key": "EnableUI", + "display_name": "Enable UI", + "type": "bool", + "help_text": "", + "placeholder": "", + "default": true + }, { "key": "GitHubOAuthClientID", "display_name": "GitHub OAuth Client ID",