Skip to content

Commit 2f92596

Browse files
authored
refactor: is enterprise host fn (#791)
1 parent fd4d383 commit 2f92596

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

src/hooks/useNotifications.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { apiRequestAuth } from '../utils/api-requests';
88
import {
99
getEnterpriseAccountToken,
1010
generateGitHubAPIUrl,
11+
isEnterpriseHost,
1112
} from '../utils/helpers';
1213
import { removeNotification } from '../utils/remove-notification';
1314
import {
@@ -130,9 +131,9 @@ export const useNotifications = (colors: boolean): NotificationsState => {
130131
) {
131132
return notification;
132133
}
133-
const isEnterprise =
134-
accountNotifications.hostname !==
135-
Constants.DEFAULT_AUTH_OPTIONS.hostname;
134+
const isEnterprise = isEnterpriseHost(
135+
accountNotifications.hostname,
136+
);
136137
const token = isEnterprise
137138
? getEnterpriseAccountToken(
138139
accountNotifications.hostname,
@@ -192,7 +193,7 @@ export const useNotifications = (colors: boolean): NotificationsState => {
192193
async (accounts, id, hostname) => {
193194
setIsFetching(true);
194195

195-
const isEnterprise = hostname !== Constants.DEFAULT_AUTH_OPTIONS.hostname;
196+
const isEnterprise = isEnterpriseHost(hostname);
196197
const token = isEnterprise
197198
? getEnterpriseAccountToken(hostname, accounts.enterpriseAccounts)
198199
: accounts.token;
@@ -225,7 +226,7 @@ export const useNotifications = (colors: boolean): NotificationsState => {
225226
async (accounts, id, hostname) => {
226227
setIsFetching(true);
227228

228-
const isEnterprise = hostname !== Constants.DEFAULT_AUTH_OPTIONS.hostname;
229+
const isEnterprise = isEnterpriseHost(hostname);
229230
const token = isEnterprise
230231
? getEnterpriseAccountToken(hostname, accounts.enterpriseAccounts)
231232
: accounts.token;
@@ -258,7 +259,7 @@ export const useNotifications = (colors: boolean): NotificationsState => {
258259
async (accounts, id, hostname) => {
259260
setIsFetching(true);
260261

261-
const isEnterprise = hostname !== Constants.DEFAULT_AUTH_OPTIONS.hostname;
262+
const isEnterprise = isEnterpriseHost(hostname);
262263
const token = isEnterprise
263264
? getEnterpriseAccountToken(hostname, accounts.enterpriseAccounts)
264265
: accounts.token;
@@ -284,7 +285,7 @@ export const useNotifications = (colors: boolean): NotificationsState => {
284285
async (accounts, repoSlug, hostname) => {
285286
setIsFetching(true);
286287

287-
const isEnterprise = hostname !== Constants.DEFAULT_AUTH_OPTIONS.hostname;
288+
const isEnterprise = isEnterpriseHost(hostname);
288289
const token = isEnterprise
289290
? getEnterpriseAccountToken(hostname, accounts.enterpriseAccounts)
290291
: accounts.token;

src/utils/auth.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { BrowserWindow } from '@electron/remote';
22

3-
import { generateGitHubAPIUrl } from './helpers';
3+
import { generateGitHubAPIUrl, isEnterpriseHost } from './helpers';
44
import { apiRequest, apiRequestAuth } from '../utils/api-requests';
55
import { AuthResponse, AuthState, AuthTokenResponse } from '../types';
66
import { Constants } from '../utils/constants';
@@ -114,7 +114,7 @@ export const addAccount = (
114114
hostname,
115115
user?: User,
116116
): AuthState => {
117-
if (hostname === Constants.DEFAULT_AUTH_OPTIONS.hostname) {
117+
if (!isEnterpriseHost(hostname)) {
118118
return {
119119
...accounts,
120120
token,

src/utils/helpers.test.ts

+13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
generateNotificationReferrerId,
55
getCommentId,
66
getLatestDiscussionCommentId,
7+
isEnterpriseHost,
78
} from './helpers';
89
import {
910
mockedSingleNotification,
@@ -23,6 +24,18 @@ const URL = {
2324
};
2425

2526
describe('utils/helpers.ts', () => {
27+
describe('isEnterpriseHost', () => {
28+
it('should return true for enterprise host', () => {
29+
expect(isEnterpriseHost('github.manos.im')).toBe(true);
30+
expect(isEnterpriseHost('api.github.manos.im')).toBe(true);
31+
});
32+
33+
it('should return false for non-enterprise host', () => {
34+
expect(isEnterpriseHost('github.com')).toBe(false);
35+
expect(isEnterpriseHost('api.github.com')).toBe(false);
36+
});
37+
});
38+
2639
describe('generateNotificationReferrerId', () => {
2740
it('should generate the notification_referrer_id', () => {
2841
const referrerId = generateNotificationReferrerId(

src/utils/helpers.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ export function getEnterpriseAccountToken(
1515
return accounts.find((obj) => obj.hostname === hostname).token;
1616
}
1717

18+
export function isEnterpriseHost(hostname: string): boolean {
19+
return !hostname.endsWith(Constants.DEFAULT_AUTH_OPTIONS.hostname);
20+
}
21+
1822
export function generateGitHubAPIUrl(hostname) {
19-
const isEnterprise = hostname !== Constants.DEFAULT_AUTH_OPTIONS.hostname;
23+
const isEnterprise = isEnterpriseHost(hostname);
2024
return isEnterprise
2125
? `https://${hostname}/api/v3/`
2226
: `https://api.${hostname}/`;
@@ -39,8 +43,7 @@ export function generateGitHubWebUrl(
3943
comment: string = '',
4044
) {
4145
const { hostname } = new URL(url);
42-
const isEnterprise =
43-
hostname !== `api.${Constants.DEFAULT_AUTH_OPTIONS.hostname}`;
46+
const isEnterprise = isEnterpriseHost(hostname);
4447

4548
let newUrl: string = isEnterprise
4649
? url.replace(`${hostname}/api/v3/repos`, hostname)

0 commit comments

Comments
 (0)