Skip to content

Commit a53e65e

Browse files
committed
feat(api): codegen for discussions
Signed-off-by: Adam Setch <[email protected]>
1 parent 30403ad commit a53e65e

File tree

2 files changed

+85
-23
lines changed

2 files changed

+85
-23
lines changed

src/renderer/utils/helpers.test.ts

Lines changed: 62 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,8 @@ describe('renderer/utils/helpers.ts', () => {
488488
});
489489
});
490490

491-
describe('defaults to repository url', () => {
492-
it('defaults when no urls present in notification', async () => {
491+
describe('defaults web urls', () => {
492+
it('issues - defaults when no urls present in notification', async () => {
493493
const subject = {
494494
title: 'generate github web url unit tests',
495495
url: null,
@@ -504,7 +504,64 @@ describe('renderer/utils/helpers.ts', () => {
504504

505505
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
506506
expect(result).toBe(
507-
`${mockSingleNotification.repository.html_url}?${mockNotificationReferrer}`,
507+
`https://github.com/gitify-app/notifications-test/issues?${mockNotificationReferrer}`,
508+
);
509+
});
510+
511+
it('discussion - defaults when no urls present in notification', async () => {
512+
const subject = {
513+
title: 'generate github web url unit tests',
514+
url: null,
515+
latest_comment_url: null,
516+
type: 'Discussion' as SubjectType,
517+
};
518+
519+
const result = await generateGitHubWebUrl({
520+
...mockSingleNotification,
521+
subject: subject,
522+
});
523+
524+
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
525+
expect(result).toBe(
526+
`https://github.com/gitify-app/notifications-test/discussions?${mockNotificationReferrer}`,
527+
);
528+
});
529+
530+
it('issues - defaults when no urls present in notification', async () => {
531+
const subject = {
532+
title: 'generate github web url unit tests',
533+
url: null,
534+
latest_comment_url: null,
535+
type: 'PullRequest' as SubjectType,
536+
};
537+
538+
const result = await generateGitHubWebUrl({
539+
...mockSingleNotification,
540+
subject: subject,
541+
});
542+
543+
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
544+
expect(result).toBe(
545+
`https://github.com/gitify-app/notifications-test/pulls?${mockNotificationReferrer}`,
546+
);
547+
});
548+
549+
it('other - defaults when no urls present in notification', async () => {
550+
const subject = {
551+
title: 'generate github web url unit tests',
552+
url: null,
553+
latest_comment_url: null,
554+
type: 'Commit' as SubjectType,
555+
};
556+
557+
const result = await generateGitHubWebUrl({
558+
...mockSingleNotification,
559+
subject: subject,
560+
});
561+
562+
expect(apiRequestAuthSpy).toHaveBeenCalledTimes(0);
563+
expect(result).toBe(
564+
`https://github.com/gitify-app/notifications-test?${mockNotificationReferrer}`,
508565
);
509566
});
510567

@@ -521,9 +578,7 @@ describe('renderer/utils/helpers.ts', () => {
521578
type: 'Issue' as SubjectType,
522579
};
523580

524-
const mockError = new Error('Test error');
525-
526-
apiRequestAuthSpy.mockRejectedValue(mockError);
581+
apiRequestAuthSpy.mockRejectedValue(new Error('Test error'));
527582

528583
const result = await generateGitHubWebUrl({
529584
...mockSingleNotification,
@@ -537,7 +592,7 @@ describe('renderer/utils/helpers.ts', () => {
537592
mockPersonalAccessTokenAccount.token,
538593
);
539594
expect(result).toBe(
540-
`https://github.com/gitify-app/notifications-test?${mockNotificationReferrer}`,
595+
`https://github.com/gitify-app/notifications-test/issues?${mockNotificationReferrer}`,
541596
);
542597
expect(rendererLogErrorSpy).toHaveBeenCalledTimes(2);
543598
});

src/renderer/utils/helpers.ts

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

77
import { Constants } from '../constants';
88
import type { Chevron, Hostname, Link } from '../types';
9-
import type { Notification } from '../typesGitHub';
9+
import type { Notification, SubjectType } from '../typesGitHub';
1010
import { fetchDiscussionByNumber, getHtmlUrl } from './api/client';
1111
import type { PlatformType } from './auth/types';
1212
import { rendererLogError } from './logger';
@@ -138,8 +138,9 @@ export async function generateGitHubWebUrl(
138138
notification.subject.url,
139139
notification.account.token,
140140
);
141+
} else {
142+
url.href = defaultGitHubWebUrl(url, notification.subject.type);
141143
}
142-
break;
143144
}
144145
} catch (err) {
145146
rendererLogError(
@@ -149,20 +150,7 @@ export async function generateGitHubWebUrl(
149150
notification,
150151
);
151152

152-
// Error state fallback urls
153-
switch (notification.subject.type) {
154-
case 'Issue':
155-
url.pathname += '/issues';
156-
break;
157-
case 'Discussion':
158-
url.pathname += '/discussions';
159-
break;
160-
case 'PullRequest':
161-
url.pathname += '/pulls';
162-
break;
163-
default:
164-
break;
165-
}
153+
url.href = defaultGitHubWebUrl(url, notification.subject.type);
166154
}
167155

168156
url.searchParams.set(
@@ -173,6 +161,25 @@ export async function generateGitHubWebUrl(
173161
return url.toString() as Link;
174162
}
175163

164+
export function defaultGitHubWebUrl(url: URL, type: SubjectType) {
165+
// Error state fallback urls
166+
switch (type) {
167+
case 'Issue':
168+
url.pathname += '/issues';
169+
break;
170+
case 'Discussion':
171+
url.pathname += '/discussions';
172+
break;
173+
case 'PullRequest':
174+
url.pathname += '/pulls';
175+
break;
176+
default:
177+
break;
178+
}
179+
180+
return url.href;
181+
}
182+
176183
export function getChevronDetails(
177184
hasNotifications: boolean,
178185
isVisible: boolean,

0 commit comments

Comments
 (0)