Skip to content

Commit cb2b01b

Browse files
committed
refactor(api): use graphql api for issue and pull request enrichment
Signed-off-by: Adam Setch <[email protected]>
1 parent 356b06c commit cb2b01b

File tree

13 files changed

+80
-119
lines changed

13 files changed

+80
-119
lines changed

src/renderer/utils/helpers.test.ts

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66

77
import type { ExecutionResult } from 'graphql';
88

9-
import { createPartialMockNotification } from '../__mocks__/notifications-mocks';
109
import type { Hostname, Link } from '../types';
1110
import type { SubjectType } from '../typesGitHub';
1211
import * as logger from '../utils/logger';
@@ -20,7 +19,6 @@ import {
2019
generateGitHubWebUrl,
2120
generateNotificationReferrerId,
2221
getChevronDetails,
23-
getDefaultURLForType,
2422
getPlatformFromHostname,
2523
isEnterpriseServerHost,
2624
} from './helpers';
@@ -556,76 +554,6 @@ describe('renderer/utils/helpers.ts', () => {
556554
});
557555
});
558556

559-
describe('getDefaultURLForType', () => {
560-
const mockUrl = 'https://github.com/gitify-app/notifications-test' as Link;
561-
562-
it('discussions', () => {
563-
const mockNotification = createPartialMockNotification(
564-
{ type: 'Discussion' },
565-
{ html_url: mockUrl },
566-
);
567-
568-
expect(getDefaultURLForType(mockNotification)).toEqual(
569-
'https://github.com/gitify-app/notifications-test/discussions',
570-
);
571-
});
572-
573-
it('issues', () => {
574-
const mockNotification = createPartialMockNotification(
575-
{ type: 'Issue' },
576-
{ html_url: mockUrl },
577-
);
578-
579-
expect(getDefaultURLForType(mockNotification)).toEqual(
580-
'https://github.com/gitify-app/notifications-test/issues',
581-
);
582-
});
583-
584-
it('pull requests', () => {
585-
const mockNotification = createPartialMockNotification(
586-
{ type: 'PullRequest' },
587-
{ html_url: mockUrl },
588-
);
589-
590-
expect(getDefaultURLForType(mockNotification)).toEqual(
591-
'https://github.com/gitify-app/notifications-test/pulls',
592-
);
593-
});
594-
595-
it('repository invitation', () => {
596-
const mockNotification = createPartialMockNotification(
597-
{ type: 'RepositoryInvitation' },
598-
{ html_url: mockUrl },
599-
);
600-
601-
expect(getDefaultURLForType(mockNotification)).toEqual(
602-
'https://github.com/gitify-app/notifications-test/invitations',
603-
);
604-
});
605-
606-
it('repository dependabot alert thread', () => {
607-
const mockNotification = createPartialMockNotification(
608-
{ type: 'RepositoryDependabotAlertsThread' },
609-
{ html_url: mockUrl },
610-
);
611-
612-
expect(getDefaultURLForType(mockNotification)).toEqual(
613-
'https://github.com/gitify-app/notifications-test/security/dependabot',
614-
);
615-
});
616-
617-
it('default web urls', () => {
618-
const mockNotification = createPartialMockNotification(
619-
{ type: 'Commit' },
620-
{ html_url: mockUrl },
621-
);
622-
623-
expect(getDefaultURLForType(mockNotification)).toEqual(
624-
'https://github.com/gitify-app/notifications-test',
625-
);
626-
});
627-
});
628-
629557
describe('getChevronDetails', () => {
630558
it('should return correct chevron details', () => {
631559
expect(getChevronDetails(true, true, 'account')).toEqual({

src/renderer/utils/helpers.ts

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { Notification } from '../typesGitHub';
1010
import { getHtmlUrl } from './api/client';
1111
import type { PlatformType } from './auth/types';
1212
import { rendererLogError } from './logger';
13+
import { createNotificationHandler } from './notifications/handlers';
1314

1415
export function getPlatformFromHostname(hostname: string): PlatformType {
1516
return hostname.endsWith(Constants.DEFAULT_AUTH_OPTIONS.hostname)
@@ -46,10 +47,12 @@ export function actionsURL(repositoryURL: string, filters: string[]): Link {
4647
export async function generateGitHubWebUrl(
4748
notification: Notification,
4849
): Promise<Link> {
49-
const url = new URL(getDefaultURLForType(notification));
50+
const url = new URL(notification.repository.html_url);
5051

5152
try {
52-
if (notification.subject.latest_comment_url) {
53+
if (notification.subject.htmlUrl) {
54+
url.href = notification.subject.htmlUrl;
55+
} else if (notification.subject.latest_comment_url) {
5356
url.href = await getHtmlUrl(
5457
notification.subject.latest_comment_url,
5558
notification.account.token,
@@ -59,6 +62,9 @@ export async function generateGitHubWebUrl(
5962
notification.subject.url,
6063
notification.account.token,
6164
);
65+
} else {
66+
const handler = createNotificationHandler(notification);
67+
handler.defaultUrl(notification);
6268
}
6369
} catch (err) {
6470
rendererLogError(
@@ -77,41 +83,6 @@ export async function generateGitHubWebUrl(
7783
return url.toString() as Link;
7884
}
7985

80-
export function getDefaultURLForType(notification: Notification) {
81-
const url = new URL(notification.repository.html_url);
82-
83-
switch (notification.subject.type) {
84-
case 'CheckSuite':
85-
url.pathname += '/actions';
86-
break;
87-
case 'Discussion':
88-
url.pathname += '/discussions';
89-
break;
90-
case 'Issue':
91-
url.pathname += '/issues';
92-
break;
93-
case 'PullRequest':
94-
url.pathname += '/pulls';
95-
break;
96-
case 'Release':
97-
url.pathname += '/releases';
98-
break;
99-
case 'RepositoryInvitation':
100-
url.pathname += '/invitations';
101-
break;
102-
case 'RepositoryDependabotAlertsThread':
103-
url.pathname += '/security/dependabot';
104-
break;
105-
case 'WorkflowRun':
106-
url.pathname += '/actions';
107-
break;
108-
default:
109-
break;
110-
}
111-
112-
return url.href;
113-
}
114-
11586
export function getChevronDetails(
11687
hasNotifications: boolean,
11788
isVisible: boolean,

src/renderer/utils/links.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import type { Account, Hostname, Link } from '../types';
55
import type { Notification, Repository, SubjectUser } from '../typesGitHub';
66
import { getDeveloperSettingsURL } from './auth/utils';
77
import { openExternalLink } from './comms';
8-
import { generateNotificationReferrerId } from './helpers';
8+
import {
9+
generateGitHubWebUrl,
10+
generateNotificationReferrerId,
11+
} from './helpers';
912

1013
export function openGitifyReleaseNotes(version: string) {
1114
openExternalLink(
@@ -55,7 +58,7 @@ export function openRepository(repository: Repository) {
5558
}
5659

5760
export async function openNotification(notification: Notification) {
58-
const url = new URL(notification.subject.htmlUrl);
61+
const url = new URL(await generateGitHubWebUrl(notification));
5962

6063
url.searchParams.set(
6164
'notification_referrer_id',

src/renderer/utils/notifications/handlers/checkSuite.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ class CheckSuiteHandler extends DefaultHandler {
5454
return RocketIcon;
5555
}
5656
}
57+
58+
defaultUrl(notification: Notification): Link {
59+
const url = new URL(notification.repository.html_url);
60+
url.pathname += '/actions';
61+
return url.href as Link;
62+
}
5763
}
5864

5965
export const checkSuiteHandler = new CheckSuiteHandler();

src/renderer/utils/notifications/handlers/default.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { FC } from 'react';
33
import type { OcticonProps } from '@primer/octicons-react';
44
import { QuestionIcon } from '@primer/octicons-react';
55

6-
import type { SettingsState } from '../../../types';
6+
import type { Link, SettingsState } from '../../../types';
77
import { IconColor } from '../../../types';
88
import type {
99
GitifySubject,
@@ -80,6 +80,10 @@ export class DefaultHandler implements NotificationTypeHandler {
8080
}
8181
return title;
8282
}
83+
84+
defaultUrl(notification: Notification): Link {
85+
return notification.repository.html_url as Link;
86+
}
8387
}
8488

8589
export const defaultHandler = new DefaultHandler();

src/renderer/utils/notifications/handlers/discussion.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010

1111
import { differenceInMilliseconds } from 'date-fns';
1212

13-
import type { SettingsState } from '../../../types';
13+
import type { Link, SettingsState } from '../../../types';
1414
import type {
1515
DiscussionStateType,
1616
GitifySubject,
@@ -111,6 +111,12 @@ class DiscussionHandler extends DefaultHandler {
111111
return CommentDiscussionIcon;
112112
}
113113
}
114+
115+
defaultUrl(notification: Notification): Link {
116+
const url = new URL(notification.repository.html_url);
117+
url.pathname += '/discussions';
118+
return url.href as Link;
119+
}
114120
}
115121

116122
export const discussionHandler = new DiscussionHandler();

src/renderer/utils/notifications/handlers/issue.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
SkipIcon,
1010
} from '@primer/octicons-react';
1111

12-
import type { SettingsState } from '../../../types';
12+
import type { Link, SettingsState } from '../../../types';
1313
import type {
1414
GitifySubject,
1515
Notification,
@@ -64,6 +64,12 @@ class IssueHandler extends DefaultHandler {
6464
return IssueOpenedIcon;
6565
}
6666
}
67+
68+
defaultUrl(notification: Notification): Link {
69+
const url = new URL(notification.repository.html_url);
70+
url.pathname += '/issues';
71+
return url.href as Link;
72+
}
6773
}
6874

6975
export const issueHandler = new IssueHandler();

src/renderer/utils/notifications/handlers/pullRequest.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
GitPullRequestIcon,
99
} from '@primer/octicons-react';
1010

11-
import type { SettingsState } from '../../../types';
11+
import type { Link, SettingsState } from '../../../types';
1212
import type {
1313
GitifyPullRequestReview,
1414
GitifySubject,
@@ -81,6 +81,12 @@ class PullRequestHandler extends DefaultHandler {
8181
return GitPullRequestIcon;
8282
}
8383
}
84+
85+
defaultUrl(notification: Notification): Link {
86+
const url = new URL(notification.repository.html_url);
87+
url.pathname += '/pulls';
88+
return url.href as Link;
89+
}
8490
}
8591

8692
export const pullRequestHandler = new PullRequestHandler();

src/renderer/utils/notifications/handlers/release.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { FC } from 'react';
33
import type { OcticonProps } from '@primer/octicons-react';
44
import { TagIcon } from '@primer/octicons-react';
55

6-
import type { SettingsState } from '../../../types';
6+
import type { Link, SettingsState } from '../../../types';
77
import type {
88
GitifySubject,
99
Notification,
@@ -42,6 +42,12 @@ class ReleaseHandler extends DefaultHandler {
4242
iconType(_subject: Subject): FC<OcticonProps> | null {
4343
return TagIcon;
4444
}
45+
46+
defaultUrl(notification: Notification): Link {
47+
const url = new URL(notification.repository.html_url);
48+
url.pathname += '/releases';
49+
return url.href as Link;
50+
}
4551
}
4652

4753
export const releaseHandler = new ReleaseHandler();

src/renderer/utils/notifications/handlers/repositoryDependabotAlertsThread.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import type { FC } from 'react';
33
import type { OcticonProps } from '@primer/octicons-react';
44
import { AlertIcon } from '@primer/octicons-react';
55

6-
import type { Subject } from '../../../typesGitHub';
6+
import type { Link } from '../../../types';
7+
import type { Notification, Subject } from '../../../typesGitHub';
78
import { DefaultHandler } from './default';
89

910
class RepositoryDependabotAlertsThreadHandler extends DefaultHandler {
@@ -12,6 +13,12 @@ class RepositoryDependabotAlertsThreadHandler extends DefaultHandler {
1213
iconType(_subject: Subject): FC<OcticonProps> | null {
1314
return AlertIcon;
1415
}
16+
17+
defaultUrl(notification: Notification): Link {
18+
const url = new URL(notification.repository.html_url);
19+
url.pathname += '/security/dependabot';
20+
return url.href as Link;
21+
}
1522
}
1623

1724
export const repositoryDependabotAlertsThreadHandler =

0 commit comments

Comments
 (0)