Skip to content

Commit 3de77b0

Browse files
setchyadufr
authored andcommitted
refactor: is enterprise host fn (#791)
1 parent 44fe621 commit 3de77b0

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 {
@@ -135,9 +136,9 @@ export const useNotifications = (colors: boolean): NotificationsState => {
135136
) {
136137
return notification;
137138
}
138-
const isEnterprise =
139-
accountNotifications.hostname !==
140-
Constants.DEFAULT_AUTH_OPTIONS.hostname;
139+
const isEnterprise = isEnterpriseHost(
140+
accountNotifications.hostname,
141+
);
141142
const token = isEnterprise
142143
? getEnterpriseAccountToken(
143144
accountNotifications.hostname,
@@ -197,7 +198,7 @@ export const useNotifications = (colors: boolean): NotificationsState => {
197198
async (accounts, id, hostname) => {
198199
setIsFetching(true);
199200

200-
const isEnterprise = hostname !== Constants.DEFAULT_AUTH_OPTIONS.hostname;
201+
const isEnterprise = isEnterpriseHost(hostname);
201202
const token = isEnterprise
202203
? getEnterpriseAccountToken(hostname, accounts.enterpriseAccounts)
203204
: accounts.token;
@@ -230,7 +231,7 @@ export const useNotifications = (colors: boolean): NotificationsState => {
230231
async (accounts, id, hostname) => {
231232
setIsFetching(true);
232233

233-
const isEnterprise = hostname !== Constants.DEFAULT_AUTH_OPTIONS.hostname;
234+
const isEnterprise = isEnterpriseHost(hostname);
234235
const token = isEnterprise
235236
? getEnterpriseAccountToken(hostname, accounts.enterpriseAccounts)
236237
: accounts.token;
@@ -263,7 +264,7 @@ export const useNotifications = (colors: boolean): NotificationsState => {
263264
async (accounts, id, hostname) => {
264265
setIsFetching(true);
265266

266-
const isEnterprise = hostname !== Constants.DEFAULT_AUTH_OPTIONS.hostname;
267+
const isEnterprise = isEnterpriseHost(hostname);
267268
const token = isEnterprise
268269
? getEnterpriseAccountToken(hostname, accounts.enterpriseAccounts)
269270
: accounts.token;
@@ -289,7 +290,7 @@ export const useNotifications = (colors: boolean): NotificationsState => {
289290
async (accounts, repoSlug, hostname) => {
290291
setIsFetching(true);
291292

292-
const isEnterprise = hostname !== Constants.DEFAULT_AUTH_OPTIONS.hostname;
293+
const isEnterprise = isEnterpriseHost(hostname);
293294
const token = isEnterprise
294295
? getEnterpriseAccountToken(hostname, accounts.enterpriseAccounts)
295296
: 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)