Skip to content

Commit 4d97973

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

File tree

4 files changed

+47
-49
lines changed

4 files changed

+47
-49
lines changed

src/renderer/utils/api/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { AxiosPromise } from 'axios';
2+
import type { ExecutionResult } from 'graphql';
23

34
import type {
45
Account,
@@ -26,7 +27,6 @@ import {
2627
type FetchDiscussionByNumberQuery,
2728
} from './graphql/generated/graphql';
2829
import { apiRequestAuth, performGraphQLRequest } from './request';
29-
import type { GitHubGraphQLResponse } from './types';
3030
import {
3131
getGitHubAPIBaseUrl,
3232
getGitHubGraphQLUrl,
@@ -241,7 +241,7 @@ export async function getHtmlUrl(url: Link, token: Token): Promise<string> {
241241
*/
242242
export async function fetchDiscussionByNumber(
243243
notification: Notification,
244-
): Promise<GitHubGraphQLResponse<FetchDiscussionByNumberQuery>> {
244+
): Promise<ExecutionResult<FetchDiscussionByNumberQuery>> {
245245
const url = getGitHubGraphQLUrl(notification.account.hostname);
246246
const number = getNumberFromUrl(notification.subject.url);
247247

src/renderer/utils/api/request.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import type { Link, Token } from '../../types';
99
import { decryptValue } from '../comms';
1010
import { rendererLogError } from '../logger';
1111
import type { TypedDocumentString } from './graphql/generated/graphql';
12-
import type { GitHubGraphQLResponse } from './types';
1312
import { getNextURLFromLinkHeader } from './utils';
1413

1514
/**

src/renderer/utils/helpers.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
} from '@primer/octicons-react';
66

77
import type { AxiosResponse } from 'axios';
8+
import type { ExecutionResult } from 'graphql';
89

910
import { mockPersonalAccessTokenAccount } from '../__mocks__/account-mocks';
1011
import type { Hostname, Link } from '../types';
@@ -17,7 +18,6 @@ import {
1718
import * as apiClient from './api/client';
1819
import type { FetchDiscussionByNumberQuery } from './api/graphql/generated/graphql';
1920
import * as apiRequests from './api/request';
20-
import type { GitHubGraphQLResponse } from './api/types';
2121
import {
2222
generateGitHubWebUrl,
2323
generateNotificationReferrerId,
@@ -323,7 +323,7 @@ describe('renderer/utils/helpers.ts', () => {
323323
message: 'Something failed',
324324
},
325325
],
326-
} as GitHubGraphQLResponse<FetchDiscussionByNumberQuery>);
326+
} as unknown as ExecutionResult<FetchDiscussionByNumberQuery>);
327327

328328
const result = await generateGitHubWebUrl({
329329
...mockSingleNotification,
@@ -347,7 +347,7 @@ describe('renderer/utils/helpers.ts', () => {
347347

348348
fetchDiscussionByNumberSpy.mockResolvedValue({
349349
data: mockDiscussionByNumberGraphQLResponse,
350-
} as GitHubGraphQLResponse<FetchDiscussionByNumberQuery>);
350+
} as ExecutionResult<FetchDiscussionByNumberQuery>);
351351

352352
const result = await generateGitHubWebUrl({
353353
...mockSingleNotification,

src/renderer/utils/helpers.ts

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -110,48 +110,36 @@ export async function generateGitHubWebUrl(
110110
): Promise<Link> {
111111
const url = new URL(notification.repository.html_url);
112112

113-
/**
114-
* Discussions - override generic response values for subject and comment urls,
115-
* as we will fetch more specific html urls ourselves below
116-
* See issue #1583:
117-
*/
118-
if (notification.subject.type === 'Discussion') {
119-
notification.subject.url = null;
120-
notification.subject.latest_comment_url = null;
121-
}
122-
123113
try {
124-
if (notification.subject.latest_comment_url) {
125-
url.href = await getHtmlUrl(
126-
notification.subject.latest_comment_url,
127-
notification.account.token,
128-
);
129-
} else if (notification.subject.url) {
130-
url.href = await getHtmlUrl(
131-
notification.subject.url,
132-
notification.account.token,
133-
);
134-
} else {
135-
// Perform any specific notification type handling (only required for a few special notification scenarios)
136-
switch (notification.subject.type) {
137-
case 'CheckSuite':
138-
url.href = getCheckSuiteUrl(notification);
139-
break;
140-
case 'Discussion':
141-
url.href = await getDiscussionUrl(notification);
142-
break;
143-
case 'RepositoryInvitation':
144-
url.pathname += '/invitations';
145-
break;
146-
case 'RepositoryDependabotAlertsThread':
147-
url.pathname += '/security/dependabot';
148-
break;
149-
case 'WorkflowRun':
150-
url.href = getWorkflowRunUrl(notification);
151-
break;
152-
default:
153-
break;
154-
}
114+
switch (notification.subject.type) {
115+
case 'CheckSuite':
116+
url.href = getCheckSuiteUrl(notification);
117+
break;
118+
case 'Discussion':
119+
url.href = await getDiscussionUrl(notification);
120+
break;
121+
case 'RepositoryInvitation':
122+
url.pathname += '/invitations';
123+
break;
124+
case 'RepositoryDependabotAlertsThread':
125+
url.pathname += '/security/dependabot';
126+
break;
127+
case 'WorkflowRun':
128+
url.href = getWorkflowRunUrl(notification);
129+
break;
130+
default:
131+
if (notification.subject.latest_comment_url) {
132+
url.href = await getHtmlUrl(
133+
notification.subject.latest_comment_url,
134+
notification.account.token,
135+
);
136+
} else if (notification.subject.url) {
137+
url.href = await getHtmlUrl(
138+
notification.subject.url,
139+
notification.account.token,
140+
);
141+
}
142+
break;
155143
}
156144
} catch (err) {
157145
rendererLogError(
@@ -161,8 +149,19 @@ export async function generateGitHubWebUrl(
161149
notification,
162150
);
163151

164-
if (notification.subject.type === 'Discussion') {
165-
url.pathname += '/discussions';
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;
166165
}
167166
}
168167

0 commit comments

Comments
 (0)