Skip to content

Commit ffc1a6b

Browse files
committed
feat(api): merge query
Signed-off-by: Adam Setch <[email protected]>
1 parent 412da46 commit ffc1a6b

File tree

6 files changed

+29
-67
lines changed

6 files changed

+29
-67
lines changed

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

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

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ import { getNotificationAuthor } from './utils';
1818
class CommitHandler extends DefaultHandler {
1919
readonly type = 'Commit';
2020

21-
async fetchAndEnrich(
21+
async enrich(
2222
notification: Notification,
2323
settings: SettingsState,
24-
_fetchedData?: unknown,
2524
): Promise<GitifySubject> {
2625
const commitState: GitifyNotificationState = null; // Commit notifications are stateless
2726

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

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,9 @@ export class DefaultHandler implements NotificationTypeHandler {
1616
return undefined;
1717
}
1818

19-
query(_notification: Notification) {
20-
return null;
21-
}
22-
23-
async fetchAndEnrich(
24-
_notification: Notification,
25-
_settings: SettingsState,
26-
): Promise<GitifySubject> {
27-
return null;
28-
}
29-
3019
async enrich(
3120
_notification: Notification,
3221
_settings: SettingsState,
33-
_fetchedData?: unknown,
3422
): Promise<GitifySubject> {
3523
return null;
3624
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { getNotificationAuthor } from './utils';
1818
class ReleaseHandler extends DefaultHandler {
1919
readonly type = 'Release';
2020

21-
async fetchAndEnrich(
21+
async enrich(
2222
notification: Notification,
2323
settings: SettingsState,
2424
): Promise<GitifySubject> {

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

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,16 @@ export interface NotificationTypeHandler<TFragment = unknown> {
2020

2121
mergeQueryConfig(): GraphQLMergedQueryConfig;
2222

23-
query(notification: Notification): { query; variables } | null;
24-
25-
/**
26-
* Fetch remote data (if needed) and enrich a notification.
27-
*/
28-
fetchAndEnrich(
29-
notification: Notification,
30-
settings: SettingsState,
31-
): Promise<GitifySubject>;
32-
3323
/**
34-
* Enrich a notification. Settings may be unused for some handlers.
24+
* Enriches a base notification with additional information (state, author, metrics, etc).
25+
*
26+
* @param notification The base notification being enriched
27+
* @param settings The app settings, which for some handlers may not be used during enrichment.
28+
* @param fetchedData Previously fetched enrichment data (upstream). If present, then enrich will skip fetching detailed data inline.
3529
*/
3630
enrich(
3731
notification: Notification,
38-
settings?: SettingsState,
32+
settings: SettingsState,
3933
fetchedData?: TFragment,
4034
): Promise<GitifySubject>;
4135

src/renderer/utils/notifications/notifications.ts

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ export async function enrichNotifications(
133133
if (!settings.detailedNotifications) {
134134
return notifications;
135135
}
136-
type NotificationKind = 'PullRequest' | 'Issue' | 'Discussion';
137136

138137
const selections: string[] = [];
139138
const variableDefinitions: string[] = [];
@@ -143,8 +142,8 @@ export async function enrichNotifications(
143142
const fragments = new Map<string, string>();
144143
const targets: Array<{
145144
alias: string;
146-
kind: NotificationKind;
147145
notification: Notification;
146+
handler: ReturnType<typeof createNotificationHandler>;
148147
}> = [];
149148

150149
const collectFragments = (doc: string) => {
@@ -166,54 +165,46 @@ export async function enrichNotifications(
166165
};
167166

168167
let index = 0;
169-
170168
for (const notification of notifications) {
171169
const handler = createNotificationHandler(notification);
172-
const kind = notification.subject.type as NotificationKind;
173170
const config = handler.mergeQueryConfig();
174-
175171
if (!config) {
176172
continue;
177173
}
178174

179175
const org = notification.repository.owner.login;
180176
const repo = notification.repository.name;
181177
const number = getNumberFromUrl(notification.subject.url);
182-
183178
const alias = `node${index}`;
184179
const queryFragment = config.queryFragment.replaceAll(
185180
'INDEX',
186181
index.toString(),
187182
);
188-
189183
selections.push(queryFragment);
190184
variableDefinitions.push(
191185
`$owner${index}: String!, $name${index}: String!, $number${index}: Int!`,
192186
);
193187
variableValues[`owner${index}`] = org;
194188
variableValues[`name${index}`] = repo;
195189
variableValues[`number${index}`] = number;
196-
targets.push({ alias, kind, notification });
197-
190+
targets.push({ alias, notification, handler });
198191
for (const extra of config.extras) {
199192
if (!extraVariableDefinitions.has(extra.name)) {
200193
extraVariableDefinitions.set(extra.name, extra.type);
201194
extraVariableValues[extra.name] = extra.defaultValue;
202195
}
203196
}
204-
205197
collectFragments(config.responseFragment);
206-
207198
index += 1;
208199
}
209200

210201
if (selections.length === 0) {
211-
const enrichedNotifications = await Promise.all(
212-
notifications.map(async (notification: Notification) => {
213-
return enrichNotification(notification, settings);
214-
}),
202+
// No handlers with mergeQueryConfig, just enrich individually
203+
return Promise.all(
204+
notifications.map((notification) =>
205+
enrichNotification(notification, settings),
206+
),
215207
);
216-
return enrichedNotifications;
217208
}
218209

219210
const combinedVariableDefinitions = [
@@ -267,26 +258,23 @@ export async function enrichNotifications(
267258

268259
const enrichedNotifications = await Promise.all(
269260
notifications.map(async (notification: Notification) => {
270-
const handler = createNotificationHandler(notification);
271-
272261
const target = targets.find((item) => item.notification === notification);
273-
274262
if (mergedData && target) {
263+
// Try to find the first defined property in the repoData (pullRequest, issue, discussion, etc.)
275264
const repoData = mergedData[target.alias] as
276-
| { pullRequest?: unknown; issue?: unknown; discussion?: unknown }
265+
| Record<string, unknown>
277266
| undefined;
278-
279267
let fragment: unknown;
280-
if (target.kind === 'PullRequest') {
281-
fragment = repoData?.pullRequest;
282-
} else if (target.kind === 'Issue') {
283-
fragment = repoData?.issue;
284-
} else if (target.kind === 'Discussion') {
285-
fragment = repoData?.discussion;
268+
if (repoData) {
269+
for (const value of Object.values(repoData)) {
270+
if (value !== undefined) {
271+
fragment = value;
272+
break;
273+
}
274+
}
286275
}
287-
288276
if (fragment) {
289-
const details = await handler.enrich(
277+
const details = await target.handler.enrich(
290278
notification,
291279
settings,
292280
fragment,
@@ -300,11 +288,9 @@ export async function enrichNotifications(
300288
};
301289
}
302290
}
303-
304-
const fetchedDetails = await handler.fetchAndEnrich(
305-
notification,
306-
settings,
307-
);
291+
// fallback
292+
const handler = createNotificationHandler(notification);
293+
const fetchedDetails = await handler.enrich(notification, settings);
308294
return {
309295
...notification,
310296
subject: {
@@ -314,7 +300,6 @@ export async function enrichNotifications(
314300
};
315301
}),
316302
);
317-
318303
return enrichedNotifications;
319304
}
320305

@@ -333,10 +318,7 @@ export async function enrichNotification(
333318

334319
try {
335320
const handler = createNotificationHandler(notification);
336-
additionalSubjectDetails = await handler.fetchAndEnrich(
337-
notification,
338-
settings,
339-
);
321+
additionalSubjectDetails = await handler.enrich(notification, settings);
340322
} catch (err) {
341323
rendererLogError(
342324
'enrichNotification',

0 commit comments

Comments
 (0)