Skip to content

Commit 289f353

Browse files
committed
feat(api): codegen for discussions
Signed-off-by: Adam Setch <[email protected]>
1 parent 82085f7 commit 289f353

File tree

2 files changed

+19
-31
lines changed

2 files changed

+19
-31
lines changed

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

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from '../../../__mocks__/notifications-mocks';
88
import { mockSettings } from '../../../__mocks__/state-mocks';
99
import type { Link } from '../../../types';
10-
import type { Repository } from '../../../typesGitHub';
10+
import type { Owner, Repository } from '../../../typesGitHub';
1111
import {
1212
type AuthorFieldsFragment,
1313
type Discussion,
@@ -24,11 +24,13 @@ const mockDiscussionAuthor: AuthorFieldsFragment = {
2424

2525
describe('renderer/utils/notifications/handlers/discussion.ts', () => {
2626
describe('enrich', () => {
27+
const partialOwner: Partial<Owner> = {
28+
login: 'gitify-app',
29+
};
30+
2731
const partialRepository: Partial<Repository> = {
2832
full_name: 'gitify-app/notifications-test',
29-
owner: {
30-
login: 'gitify-app',
31-
} as any,
33+
owner: partialOwner as Owner,
3234
};
3335

3436
const mockNotification = createPartialMockNotification({
@@ -244,17 +246,18 @@ describe('renderer/utils/notifications/handlers/discussion.ts', () => {
244246
mockDiscussion.labels = {
245247
nodes: [
246248
{
247-
id: 'MDU6TGFiZWwxMQ==',
248249
name: 'enhancement',
249250
},
250251
],
251-
} as unknown as typeof mockDiscussion.labels;
252+
} as Partial<Discussion>['labels'];
252253
nock('https://api.github.com')
253254
.post('/graphql')
254255
.reply(200, {
255256
data: {
256257
repository: {
257-
discussion: mockDiscussion,
258+
discussion: {
259+
...mockDiscussion,
260+
},
258261
},
259262
},
260263
});
@@ -325,26 +328,20 @@ describe('renderer/utils/notifications/handlers/discussion.ts', () => {
325328
});
326329

327330
function mockDiscussionNode(
328-
state: DiscussionStateReason | null,
331+
state: DiscussionStateReason,
329332
isAnswered: boolean,
330333
): Partial<Discussion> {
331334
return {
332335
number: 123,
333336
title: 'This is a mock discussion',
334337
url: 'https://github.com/gitify-app/notifications-test/discussions/1' as Link,
335-
stateReason: state || undefined,
338+
stateReason: state,
336339
isAnswered: isAnswered,
337340
author: mockDiscussionAuthor,
338341
comments: {
339342
nodes: [],
340343
totalCount: 0,
341-
pageInfo: {
342-
hasNextPage: false,
343-
hasPreviousPage: false,
344-
startCursor: null,
345-
endCursor: null,
346-
},
347-
} as unknown as Partial<Discussion>['comments'],
348-
labels: null as Partial<Discussion>['labels'],
344+
},
345+
labels: null,
349346
} as unknown as Partial<Discussion>;
350347
}

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

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,32 +124,23 @@ export function getClosestDiscussionCommentOrReply(
124124
return null;
125125
}
126126

127-
const targetDate = notification.updated_at;
127+
const targetTimestamp = notification.updated_at;
128128

129129
const allCommentsAndReplies = comments.flatMap((comment) => [
130130
comment,
131131
...comment.replies.nodes,
132132
]);
133133

134-
// Consider only comments with a databaseId so we can anchor the URL
135-
const commentsWithIds = allCommentsAndReplies.filter(
136-
(item) => item?.databaseId != null,
137-
);
138-
139-
if (commentsWithIds.length === 0) {
140-
return null;
141-
}
142-
143134
// Find the closest match using the target timestamp
144-
const closestComment = commentsWithIds.reduce((prev, curr) => {
135+
const closestComment = allCommentsAndReplies.reduce((prev, curr) => {
145136
const prevDiff = Math.abs(
146-
differenceInMilliseconds(prev.createdAt, targetDate),
137+
differenceInMilliseconds(prev.createdAt, targetTimestamp),
147138
);
148139
const currDiff = Math.abs(
149-
differenceInMilliseconds(curr.createdAt, targetDate),
140+
differenceInMilliseconds(curr.createdAt, targetTimestamp),
150141
);
151142
return currDiff < prevDiff ? curr : prev;
152-
}, commentsWithIds[0]);
143+
}, allCommentsAndReplies[0]);
153144

154145
return closestComment;
155146
}

0 commit comments

Comments
 (0)