From e1ff6303116d6504e475b20a36aee4b052675a29 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Sun, 18 Feb 2024 17:48:53 -0400 Subject: [PATCH 1/3] refactor: is enterprise host fn --- src/hooks/useNotifications.ts | 15 ++++++++------- src/utils/auth.ts | 4 ++-- src/utils/helpers.test.ts | 13 +++++++++++++ src/utils/helpers.ts | 12 +++++++++--- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/hooks/useNotifications.ts b/src/hooks/useNotifications.ts index 41875fb8b..532ef6e3b 100644 --- a/src/hooks/useNotifications.ts +++ b/src/hooks/useNotifications.ts @@ -8,6 +8,7 @@ import { apiRequestAuth } from '../utils/api-requests'; import { getEnterpriseAccountToken, generateGitHubAPIUrl, + isEnterpriseHost, } from '../utils/helpers'; import { removeNotification } from '../utils/remove-notification'; import { @@ -129,9 +130,9 @@ export const useNotifications = (colors: boolean): NotificationsState => { ) { return notification; } - const isEnterprise = - accountNotifications.hostname !== - Constants.DEFAULT_AUTH_OPTIONS.hostname; + const isEnterprise = isEnterpriseHost( + accountNotifications.hostname, + ); const token = isEnterprise ? getEnterpriseAccountToken( accountNotifications.hostname, @@ -191,7 +192,7 @@ export const useNotifications = (colors: boolean): NotificationsState => { async (accounts, id, hostname) => { setIsFetching(true); - const isEnterprise = hostname !== Constants.DEFAULT_AUTH_OPTIONS.hostname; + const isEnterprise = isEnterpriseHost(hostname); const token = isEnterprise ? getEnterpriseAccountToken(hostname, accounts.enterpriseAccounts) : accounts.token; @@ -224,7 +225,7 @@ export const useNotifications = (colors: boolean): NotificationsState => { async (accounts, id, hostname) => { setIsFetching(true); - const isEnterprise = hostname !== Constants.DEFAULT_AUTH_OPTIONS.hostname; + const isEnterprise = isEnterpriseHost(hostname); const token = isEnterprise ? getEnterpriseAccountToken(hostname, accounts.enterpriseAccounts) : accounts.token; @@ -257,7 +258,7 @@ export const useNotifications = (colors: boolean): NotificationsState => { async (accounts, id, hostname) => { setIsFetching(true); - const isEnterprise = hostname !== Constants.DEFAULT_AUTH_OPTIONS.hostname; + const isEnterprise = isEnterpriseHost(hostname); const token = isEnterprise ? getEnterpriseAccountToken(hostname, accounts.enterpriseAccounts) : accounts.token; @@ -283,7 +284,7 @@ export const useNotifications = (colors: boolean): NotificationsState => { async (accounts, repoSlug, hostname) => { setIsFetching(true); - const isEnterprise = hostname !== Constants.DEFAULT_AUTH_OPTIONS.hostname; + const isEnterprise = isEnterpriseHost(hostname); const token = isEnterprise ? getEnterpriseAccountToken(hostname, accounts.enterpriseAccounts) : accounts.token; diff --git a/src/utils/auth.ts b/src/utils/auth.ts index 96d83242f..f46ccc7ef 100644 --- a/src/utils/auth.ts +++ b/src/utils/auth.ts @@ -1,6 +1,6 @@ import { BrowserWindow } from '@electron/remote'; -import { generateGitHubAPIUrl } from './helpers'; +import { generateGitHubAPIUrl, isEnterpriseHost } from './helpers'; import { apiRequest, apiRequestAuth } from '../utils/api-requests'; import { AuthResponse, AuthState, AuthTokenResponse } from '../types'; import { Constants } from '../utils/constants'; @@ -114,7 +114,7 @@ export const addAccount = ( hostname, user?: User, ): AuthState => { - if (hostname === Constants.DEFAULT_AUTH_OPTIONS.hostname) { + if (!isEnterpriseHost(hostname)) { return { ...accounts, token, diff --git a/src/utils/helpers.test.ts b/src/utils/helpers.test.ts index f9a60b455..c7b2a1e1b 100644 --- a/src/utils/helpers.test.ts +++ b/src/utils/helpers.test.ts @@ -4,6 +4,7 @@ import { generateNotificationReferrerId, getCommentId, getLatestDiscussionCommentId, + isEnterpriseHost, } from './helpers'; import { mockedSingleNotification, @@ -23,6 +24,18 @@ const URL = { }; describe('utils/helpers.ts', () => { + describe('isEnterpriseHost', () => { + it('should return true for enterprise host', () => { + expect(isEnterpriseHost('enterprise.com')).toBe(true); + expect(isEnterpriseHost('api.enterprise.com')).toBe(true); + }); + + it('should return false for non-enterprise host', () => { + expect(isEnterpriseHost('github.com')).toBe(false); + expect(isEnterpriseHost('api.github.com')).toBe(false); + }); + }); + describe('generateNotificationReferrerId', () => { it('should generate the notification_referrer_id', () => { const referrerId = generateNotificationReferrerId( diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index 708aac3af..2f08c174d 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -15,8 +15,15 @@ export function getEnterpriseAccountToken( return accounts.find((obj) => obj.hostname === hostname).token; } +export function isEnterpriseHost(hostname: string): boolean { + return ( + hostname !== Constants.DEFAULT_AUTH_OPTIONS.hostname && + hostname !== `api.${Constants.DEFAULT_AUTH_OPTIONS.hostname}` + ); +} + export function generateGitHubAPIUrl(hostname) { - const isEnterprise = hostname !== Constants.DEFAULT_AUTH_OPTIONS.hostname; + const isEnterprise = isEnterpriseHost(hostname); return isEnterprise ? `https://${hostname}/api/v3/` : `https://api.${hostname}/`; @@ -39,8 +46,7 @@ export function generateGitHubWebUrl( comment: string = '', ) { const { hostname } = new URL(url); - const isEnterprise = - hostname !== `api.${Constants.DEFAULT_AUTH_OPTIONS.hostname}`; + const isEnterprise = isEnterpriseHost(hostname); let newUrl: string = isEnterprise ? url.replace(`${hostname}/api/v3/repos`, hostname) From 8b94ec92c97494d305a1d844caab50cb6eafe6a9 Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Sun, 18 Feb 2024 20:17:37 -0400 Subject: [PATCH 2/3] refactor: is enterprise host fn --- src/utils/helpers.ts | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index 2f08c174d..e84fd3a56 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -16,10 +16,7 @@ export function getEnterpriseAccountToken( } export function isEnterpriseHost(hostname: string): boolean { - return ( - hostname !== Constants.DEFAULT_AUTH_OPTIONS.hostname && - hostname !== `api.${Constants.DEFAULT_AUTH_OPTIONS.hostname}` - ); + return !hostname.endsWith(Constants.DEFAULT_AUTH_OPTIONS.hostname); } export function generateGitHubAPIUrl(hostname) { From 5119ec9ef54a11ef595301ed1328cbeae2cffe1b Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Sun, 18 Feb 2024 20:19:14 -0400 Subject: [PATCH 3/3] refactor: is enterprise host fn --- src/utils/helpers.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/helpers.test.ts b/src/utils/helpers.test.ts index c7b2a1e1b..ebaa908e7 100644 --- a/src/utils/helpers.test.ts +++ b/src/utils/helpers.test.ts @@ -26,8 +26,8 @@ const URL = { describe('utils/helpers.ts', () => { describe('isEnterpriseHost', () => { it('should return true for enterprise host', () => { - expect(isEnterpriseHost('enterprise.com')).toBe(true); - expect(isEnterpriseHost('api.enterprise.com')).toBe(true); + expect(isEnterpriseHost('github.manos.im')).toBe(true); + expect(isEnterpriseHost('api.github.manos.im')).toBe(true); }); it('should return false for non-enterprise host', () => {