Skip to content

Commit 5bd4cd8

Browse files
committed
feat(api): merge query
Signed-off-by: Adam Setch <[email protected]>
1 parent 2ab9f68 commit 5bd4cd8

File tree

8 files changed

+103
-20
lines changed

8 files changed

+103
-20
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class CheckSuiteHandler extends DefaultHandler {
3434
async enrich(
3535
notification: Notification,
3636
_settings: SettingsState,
37+
_fetchedData?: unknown,
3738
): Promise<GitifySubject> {
3839
const state = getCheckSuiteAttributes(notification)?.status;
3940

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class CommitHandler extends DefaultHandler {
2121
async enrich(
2222
notification: Notification,
2323
settings: SettingsState,
24+
_fetchedData?: unknown,
2425
): Promise<GitifySubject> {
2526
const commitState: GitifyNotificationState = null; // Commit notifications are stateless
2627

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,17 @@ export class DefaultHandler implements NotificationTypeHandler {
1616
return null;
1717
}
1818

19-
async enrich(
20-
_notification: Notification,
21-
_settings: SettingsState,
22-
): Promise<GitifySubject> {
19+
async enrich(_fetchedData?: unknown): Promise<GitifySubject> {
2320
return null;
2421
}
2522

23+
async fetchAndEnrich(
24+
notification: Notification,
25+
settings: SettingsState,
26+
): Promise<GitifySubject> {
27+
return this.enrich(notification, settings, undefined);
28+
}
29+
2630
iconType(_subject: Subject): FC<OcticonProps> | null {
2731
return QuestionIcon;
2832
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { fetchDiscussionByNumber } from '../../api/client';
2222
import type {
2323
CommentFieldsFragment,
2424
DiscussionCommentFieldsFragment,
25+
DiscussionDetailsFragment,
2526
} from '../../api/graphql/generated/graphql';
2627
import { DefaultHandler, defaultHandler } from './default';
2728
import { getNotificationAuthor } from './utils';
@@ -32,9 +33,11 @@ class DiscussionHandler extends DefaultHandler {
3233
async enrich(
3334
notification: Notification,
3435
_settings: SettingsState,
36+
fetchedData?: DiscussionDetailsFragment,
3537
): Promise<GitifySubject> {
36-
const response = await fetchDiscussionByNumber(notification);
37-
const discussion = response.data.repository?.discussion;
38+
const discussion =
39+
fetchedData ??
40+
(await fetchDiscussionByNumber(notification)).data.repository?.discussion;
3841

3942
let discussionState: GitifyDiscussionState = 'OPEN';
4043

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import type {
1717
import { IconColor } from '../../../types';
1818
import type { Notification, Subject } from '../../../typesGitHub';
1919
import { fetchIssueByNumber } from '../../api/client';
20+
import type { IssueDetailsFragment } from '../../api/graphql/generated/graphql';
2021
import { DefaultHandler, defaultHandler } from './default';
2122
import { getNotificationAuthor } from './utils';
2223

@@ -26,9 +27,9 @@ class IssueHandler extends DefaultHandler {
2627
async enrich(
2728
notification: Notification,
2829
_settings: SettingsState,
30+
fetchedData?: IssueDetailsFragment,
2931
): Promise<GitifySubject> {
30-
const response = await fetchIssueByNumber(notification);
31-
const issue = response.data.repository?.issue;
32+
const issue = fetchedData;
3233

3334
const issueState = issue.stateReason ?? issue.state;
3435

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ import {
1919
} from '../../../types';
2020
import type { Notification, Subject } from '../../../typesGitHub';
2121
import { fetchPullByNumber } from '../../api/client';
22-
import type { PullRequestReviewFieldsFragment } from '../../api/graphql/generated/graphql';
22+
import type {
23+
PullRequestDetailsFragment,
24+
PullRequestReviewFieldsFragment,
25+
} from '../../api/graphql/generated/graphql';
2326
import { DefaultHandler, defaultHandler } from './default';
2427
import { getNotificationAuthor } from './utils';
2528

@@ -29,9 +32,11 @@ class PullRequestHandler extends DefaultHandler {
2932
async enrich(
3033
notification: Notification,
3134
_settings: SettingsState,
35+
fetchedData?: PullRequestDetailsFragment,
3236
): Promise<GitifySubject> {
33-
const response = await fetchPullByNumber(notification);
34-
const pr = response.data.repository.pullRequest;
37+
const pr =
38+
fetchedData ??
39+
(await fetchPullByNumber(notification)).data.repository.pullRequest;
3540

3641
let prState: GitifyPullRequestState = pr.state;
3742
if (pr.isDraft) {

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { OcticonProps } from '@primer/octicons-react';
55
import type { GitifySubject, Link, SettingsState } from '../../../types';
66
import type { Notification, Subject, SubjectType } from '../../../typesGitHub';
77

8-
export interface NotificationTypeHandler {
8+
export interface NotificationTypeHandler<TFragment = unknown> {
99
readonly type?: SubjectType;
1010

1111
query(notification: Notification): { query; variables } | null;
@@ -16,6 +16,15 @@ export interface NotificationTypeHandler {
1616
enrich(
1717
notification: Notification,
1818
settings: SettingsState,
19+
fetchedData?: TFragment,
20+
): Promise<GitifySubject>;
21+
22+
/**
23+
* Fetch remote data (if needed) and enrich a notification.
24+
*/
25+
fetchAndEnrich(
26+
notification: Notification,
27+
settings: SettingsState,
1928
): Promise<GitifySubject>;
2029

2130
/**

src/renderer/utils/notifications/notifications.ts

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ export async function enrichNotifications(
209209
const extraVariableDefinitions = new Map<string, string>();
210210
const extraVariableValues: Record<string, number | boolean> = {};
211211
const fragments = new Map<string, string>();
212+
const targets: Array<{
213+
alias: string;
214+
kind: NotificationKind;
215+
notification: Notification;
216+
}> = [];
212217

213218
const collectFragments = (doc: string) => {
214219
const fragmentRegex =
@@ -242,13 +247,16 @@ export async function enrichNotifications(
242247
const repo = notification.repository.name;
243248
const number = getNumberFromUrl(notification.subject.url);
244249

250+
const alias = `${config.aliasPrefix}${index}`;
251+
245252
selections.push(config.selection(index));
246253
variableDefinitions.push(
247254
`$owner${index}: String!, $name${index}: String!, $number${index}: Int!`,
248255
);
249256
variableValues[`owner${index}`] = org;
250257
variableValues[`name${index}`] = repo;
251258
variableValues[`number${index}`] = number;
259+
targets.push({ alias, kind, notification });
252260

253261
for (const extra of config.extras) {
254262
if (!extraVariableDefinitions.has(extra.name)) {
@@ -289,8 +297,7 @@ ${Array.from(fragments.values()).join('\n')}`;
289297
...extraVariableValues,
290298
};
291299

292-
console.log('MERGED QUERY ', JSON.stringify(mergedQuery, null, 2));
293-
console.log('MERGED ARGS ', JSON.stringify(queryVariables, null, 2));
300+
let mergedData: Record<string, unknown> | null = null;
294301

295302
try {
296303
const url = getGitHubGraphQLUrl(
@@ -300,24 +307,73 @@ ${Array.from(fragments.values()).join('\n')}`;
300307

301308
const headers = await getHeaders(url as Link, token);
302309

303-
axios({
310+
const response = await axios({
304311
method: 'POST',
305312
url,
306313
data: {
307314
query: mergedQuery,
308315
variables: queryVariables,
309316
},
310317
headers: headers,
311-
}).then((response) => {
312-
console.log('MERGED RESPONSE ', JSON.stringify(response, null, 2));
313318
});
319+
320+
mergedData =
321+
(response.data as { data?: Record<string, unknown> })?.data ?? null;
314322
} catch (err) {
315-
console.error('Failed to fetch merged notification details', err);
323+
rendererLogError(
324+
'enrichNotifications',
325+
'Failed to fetch merged notification details',
326+
err,
327+
);
316328
}
317329

318330
const enrichedNotifications = await Promise.all(
319331
notifications.map(async (notification: Notification) => {
320-
return enrichNotification(notification, settings);
332+
const handler = createNotificationHandler(notification);
333+
334+
const target = targets.find((item) => item.notification === notification);
335+
336+
if (mergedData && target) {
337+
const repoData = mergedData[target.alias] as
338+
| { pullRequest?: unknown; issue?: unknown; discussion?: unknown }
339+
| undefined;
340+
341+
let fragment: unknown;
342+
if (target.kind === 'PullRequest') {
343+
fragment = repoData?.pullRequest;
344+
} else if (target.kind === 'Issue') {
345+
fragment = repoData?.issue;
346+
} else if (target.kind === 'Discussion') {
347+
fragment = repoData?.discussion;
348+
}
349+
350+
if (fragment) {
351+
const details = await handler.enrich(
352+
notification,
353+
settings,
354+
fragment,
355+
);
356+
return {
357+
...notification,
358+
subject: {
359+
...notification.subject,
360+
...details,
361+
},
362+
};
363+
}
364+
}
365+
366+
const fetchedDetails = await handler.fetchAndEnrich(
367+
notification,
368+
settings,
369+
);
370+
return {
371+
...notification,
372+
subject: {
373+
...notification.subject,
374+
...fetchedDetails,
375+
},
376+
};
321377
}),
322378
);
323379

@@ -339,7 +395,10 @@ export async function enrichNotification(
339395

340396
try {
341397
const handler = createNotificationHandler(notification);
342-
additionalSubjectDetails = await handler.enrich(notification, settings);
398+
additionalSubjectDetails = await handler.fetchAndEnrich(
399+
notification,
400+
settings,
401+
);
343402
} catch (err) {
344403
rendererLogError(
345404
'enrichNotification',

0 commit comments

Comments
 (0)