Skip to content

Commit f5b88ae

Browse files
authored
feat: improve version handling robustness (#1674)
Signed-off-by: Adam Setch <[email protected]>
1 parent 48d481a commit f5b88ae

File tree

5 files changed

+41
-24
lines changed

5 files changed

+41
-24
lines changed

src/renderer/__mocks__/state-mocks.ts

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export const mockGitHubCloudAccount: Account = {
5050
token: 'token-123-456' as Token,
5151
hostname: Constants.DEFAULT_AUTH_OPTIONS.hostname,
5252
user: mockGitifyUser,
53+
version: 'latest',
5354
};
5455

5556
export const mockGitHubEnterpriseServerAccount: Account = {

src/renderer/utils/auth/utils.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,30 @@ describe('renderer/utils/auth/utils.ts', () => {
253253
});
254254
});
255255

256+
it('extractHostVersion', () => {
257+
expect(auth.extractHostVersion(null)).toBe('latest');
258+
259+
expect(auth.extractHostVersion('foo')).toBe(null);
260+
261+
expect(auth.extractHostVersion('3')).toBe('3.0.0');
262+
263+
expect(auth.extractHostVersion('3.15')).toBe('3.15.0');
264+
265+
expect(auth.extractHostVersion('3.15.0')).toBe('3.15.0');
266+
267+
expect(auth.extractHostVersion('3.15.0-beta1')).toBe('3.15.0');
268+
269+
expect(auth.extractHostVersion('enterprise-server@3')).toBe('3.0.0');
270+
271+
expect(auth.extractHostVersion('[email protected]')).toBe('3.15.0');
272+
273+
expect(auth.extractHostVersion('[email protected]')).toBe('3.15.0');
274+
275+
expect(auth.extractHostVersion('[email protected]')).toBe(
276+
'3.15.0',
277+
);
278+
});
279+
256280
it('getDeveloperSettingsURL', () => {
257281
expect(
258282
auth.getDeveloperSettingsURL({

src/renderer/utils/auth/utils.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { BrowserWindow } from '@electron/remote';
22
import { format } from 'date-fns';
33
import log from 'electron-log';
4+
import semver from 'semver';
45
import type {
56
Account,
67
AuthCode,
@@ -165,15 +166,24 @@ export async function refreshAccount(account: Account): Promise<Account> {
165166
avatar: res.data.avatar_url,
166167
};
167168

168-
// Refresh platform version
169-
account.version = res.headers['x-github-enterprise-version'] ?? 'latest';
169+
account.version = extractHostVersion(
170+
res.headers['x-github-enterprise-version'],
171+
);
170172
} catch (error) {
171173
log.error('Failed to refresh account', error);
172174
}
173175

174176
return account;
175177
}
176178

179+
export function extractHostVersion(version: string | null): string {
180+
if (version) {
181+
return semver.valid(semver.coerce(version));
182+
}
183+
184+
return 'latest';
185+
}
186+
177187
export function getDeveloperSettingsURL(account: Account): Link {
178188
const settingsURL = new URL(`https://${account.hostname}`);
179189

src/renderer/utils/features.test.ts

+2-20
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,12 @@ describe('renderer/utils/features.ts', () => {
2626
it('should return true for GitHub Enterprise Server >= v3.13', () => {
2727
const account = {
2828
...mockGitHubEnterpriseServerAccount,
29-
version: '3.13.3',
29+
version: '3.13.0',
3030
};
3131

3232
expect(isMarkAsDoneFeatureSupported(account)).toBe(true);
3333
});
3434

35-
it('should return false for GitHub Enterprise Server when partial version available', () => {
36-
const account = {
37-
...mockGitHubEnterpriseServerAccount,
38-
version: '3',
39-
};
40-
41-
expect(isMarkAsDoneFeatureSupported(account)).toBe(false);
42-
});
43-
4435
it('should return false for GitHub Enterprise Server when no version available', () => {
4536
const account = {
4637
...mockGitHubEnterpriseServerAccount,
@@ -70,21 +61,12 @@ describe('renderer/utils/features.ts', () => {
7061
it('should return true for GitHub Enterprise Server >= v3.12', () => {
7162
const account = {
7263
...mockGitHubEnterpriseServerAccount,
73-
version: '3.12.3',
64+
version: '3.12.0',
7465
};
7566

7667
expect(isAnsweredDiscussionFeatureSupported(account)).toBe(true);
7768
});
7869

79-
it('should return false for GitHub Enterprise Server when partial version available', () => {
80-
const account = {
81-
...mockGitHubEnterpriseServerAccount,
82-
version: '3',
83-
};
84-
85-
expect(isAnsweredDiscussionFeatureSupported(account)).toBe(false);
86-
});
87-
8870
it('should return false for GitHub Enterprise Server when no version available', () => {
8971
const account = {
9072
...mockGitHubEnterpriseServerAccount,

src/renderer/utils/features.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { isEnterpriseServerHost } from './helpers';
1111
export function isMarkAsDoneFeatureSupported(account: Account): boolean {
1212
if (isEnterpriseServerHost(account.hostname)) {
1313
if (account.version) {
14-
return semver.gte(semver.coerce(account.version), '3.13.0');
14+
return semver.gte(account.version, '3.13.0');
1515
}
1616

1717
return false;
@@ -30,7 +30,7 @@ export function isAnsweredDiscussionFeatureSupported(
3030
): boolean {
3131
if (isEnterpriseServerHost(account.hostname)) {
3232
if (account.version) {
33-
return semver.gte(semver.coerce(account.version), '3.12.0');
33+
return semver.gte(account.version, '3.12.0');
3434
}
3535

3636
return false;

0 commit comments

Comments
 (0)