Skip to content

refactor: is enterprise host fn #791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/hooks/useNotifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { apiRequestAuth } from '../utils/api-requests';
import {
getEnterpriseAccountToken,
generateGitHubAPIUrl,
isEnterpriseHost,
} from '../utils/helpers';
import { removeNotification } from '../utils/remove-notification';
import {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/utils/auth.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -114,7 +114,7 @@ export const addAccount = (
hostname,
user?: User,
): AuthState => {
if (hostname === Constants.DEFAULT_AUTH_OPTIONS.hostname) {
if (!isEnterpriseHost(hostname)) {
return {
...accounts,
token,
Expand Down
13 changes: 13 additions & 0 deletions src/utils/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
generateNotificationReferrerId,
getCommentId,
getLatestDiscussionCommentId,
isEnterpriseHost,
} from './helpers';
import {
mockedSingleNotification,
Expand All @@ -23,6 +24,18 @@ const URL = {
};

describe('utils/helpers.ts', () => {
describe('isEnterpriseHost', () => {
it('should return true for enterprise host', () => {
expect(isEnterpriseHost('github.manos.im')).toBe(true);
expect(isEnterpriseHost('api.github.manos.im')).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(
Expand Down
9 changes: 6 additions & 3 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ export function getEnterpriseAccountToken(
return accounts.find((obj) => obj.hostname === hostname).token;
}

export function isEnterpriseHost(hostname: string): boolean {
return !hostname.endsWith(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}/`;
Expand All @@ -39,8 +43,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)
Expand Down