Skip to content

fix(discussions): find closest discussion comment or reply by timestamp #2135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/renderer/utils/api/__mocks__/response-mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,13 +390,13 @@ export const mockDiscussionComments: DiscussionComments = {
nodes: [
{
databaseId: 2258799,
createdAt: '2022-02-27T01:22:20Z',
createdAt: '2017-02-20T17:51:57Z',
author: mockDiscussionAuthor,
replies: {
nodes: [
{
databaseId: 2300902,
createdAt: '2022-03-05T17:43:52Z',
createdAt: '2017-05-20T17:51:57Z',
author: mockDiscussionReplier,
},
],
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/utils/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ export async function searchDiscussions(
notification.subject.title,
),
firstDiscussions: 1,
lastComments: 1,
lastReplies: 1,
lastComments: 100,
lastReplies: 100,
Copy link
Preview

Copilot AI Aug 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Increasing from 1 to 100 comments significantly increases the API response size and processing time. Consider using a smaller number like 10-20 initially, or make this configurable based on the actual need.

Suggested change
lastReplies: 100,
lastComments: options?.lastComments ?? 20,
lastReplies: options?.lastReplies ?? 20,

Copilot uses AI. Check for mistakes.

Copy link
Preview

Copilot AI Aug 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Increasing from 1 to 100 replies significantly increases the API response size and processing time. Consider using a smaller number like 10-20 initially, or make this configurable based on the actual need.

Suggested change
lastReplies: 100,
lastReplies,

Copilot uses AI. Check for mistakes.

firstLabels: 100,
includeIsAnswered: isAnsweredDiscussionFeatureSupported(
notification.account,
Expand Down
12 changes: 7 additions & 5 deletions src/renderer/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import type { PlatformType } from './auth/types';
import { Constants } from './constants';
import {
getCheckSuiteAttributes,
getLatestDiscussionComment,
getClosestDiscussionCommentOrReply,
getWorkflowRunAttributes,
} from './subject';

Expand Down Expand Up @@ -93,10 +93,12 @@ async function getDiscussionUrl(notification: Notification): Promise<Link> {
if (discussion) {
url.href = discussion.url;

const latestComment = getLatestDiscussionComment(discussion.comments.nodes);

if (latestComment) {
url.hash = `#discussioncomment-${latestComment.databaseId}`;
const closestComment = getClosestDiscussionCommentOrReply(
notification,
discussion.comments.nodes,
);
if (closestComment) {
url.hash = `#discussioncomment-${closestComment.databaseId}`;
}
}

Expand Down
32 changes: 24 additions & 8 deletions src/renderer/utils/subject.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { differenceInMilliseconds } from 'date-fns';

import { logError } from '../../shared/logger';
import type { Link } from '../types';
import type {
Expand Down Expand Up @@ -162,7 +164,8 @@ async function getGitifySubjectForDiscussion(
}
}

const latestDiscussionComment = getLatestDiscussionComment(
const latestDiscussionComment = getClosestDiscussionCommentOrReply(
notification,
discussion.comments.nodes,
);

Expand Down Expand Up @@ -190,20 +193,33 @@ async function getGitifySubjectForDiscussion(
};
}

export function getLatestDiscussionComment(
export function getClosestDiscussionCommentOrReply(
notification: Notification,
comments: DiscussionComment[],
): DiscussionComment | null {
if (!comments || comments.length === 0) {
return null;
}

// Return latest reply if available
if (comments[0].replies.nodes.length === 1) {
return comments[0].replies.nodes[0];
}
const targetTimestamp = notification.updated_at;

const allCommentsAndReplies = comments.flatMap((comment) => [
comment,
...comment.replies.nodes,
]);

// Find the closest match using the target timestamp
const closestComment = allCommentsAndReplies.reduce((prev, curr) => {
const prevDiff = Math.abs(
differenceInMilliseconds(prev.createdAt, targetTimestamp),
);
const currDiff = Math.abs(
differenceInMilliseconds(curr.createdAt, targetTimestamp),
);
return currDiff < prevDiff ? curr : prev;
}, allCommentsAndReplies[0]);

// Return latest comment if no replies
return comments[0];
return closestComment;
}

async function getGitifySubjectForIssue(
Expand Down
Loading