Skip to content

Commit

Permalink
refactor(discussions): move helper code to client
Browse files Browse the repository at this point in the history
  • Loading branch information
setchy committed May 12, 2024
1 parent 7bd9ccc commit 24ab730
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 41 deletions.
19 changes: 18 additions & 1 deletion src/utils/api/client.ts
Expand Up @@ -240,7 +240,7 @@ export async function getHtmlUrl(url: string, token: string): Promise<string> {
/**
* Search for Discussions that match notification title and repository.
*
* Returns first 10 matching discussions and their latest comments / replies
* Returns the latest discussion and their latest comments / replies
*
*/
export async function searchDiscussions(
Expand All @@ -260,3 +260,20 @@ export async function searchDiscussions(
},
});
}

/**
* Return the latest discussion that matches the notification title and repository.
*/
export async function getLatestDiscussion(
notification: Notification,
token: string,
): Promise<Discussion | null> {
try {
const response = await searchDiscussions(notification, token);
return (
response.data?.data.search.nodes.filter(
(discussion) => discussion.title === notification.subject.title,
)[0] ?? null
);
} catch (err) {}
}
46 changes: 8 additions & 38 deletions src/utils/helpers.ts
@@ -1,13 +1,13 @@
import type { AuthState } from '../types';
import type {
Discussion,
DiscussionComment,
Notification,
} from '../typesGitHub';
import type { Notification } from '../typesGitHub';
import { openExternalLink } from '../utils/comms';
import { getHtmlUrl, searchDiscussions } from './api/client';
import { getHtmlUrl, getLatestDiscussion } from './api/client';
import { Constants } from './constants';
import { getCheckSuiteAttributes, getWorkflowRunAttributes } from './subject';
import {
getCheckSuiteAttributes,
getLatestDiscussionComment,
getWorkflowRunAttributes,
} from './subject';

export function isGitHubLoggedIn(accounts: AuthState): boolean {
return accounts.token != null;
Expand Down Expand Up @@ -94,7 +94,7 @@ async function getDiscussionUrl(
const url = new URL(notification.repository.html_url);
url.pathname += '/discussions';

const discussion = await fetchDiscussion(notification, token);
const discussion = await getLatestDiscussion(notification, token);

if (discussion) {
url.href = discussion.url;
Expand All @@ -109,36 +109,6 @@ async function getDiscussionUrl(
return url.toString();
}

export async function fetchDiscussion(
notification: Notification,
token: string,
): Promise<Discussion | null> {
try {
const response = await searchDiscussions(notification, token);
return (
response.data?.data.search.nodes.filter(
(discussion) => discussion.title === notification.subject.title,
)[0] ?? null
);
} catch (err) {}
}

export function getLatestDiscussionComment(
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];
}

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

export async function generateGitHubWebUrl(
notification: Notification,
accounts: AuthState,
Expand Down
21 changes: 19 additions & 2 deletions src/utils/subject.ts
@@ -1,6 +1,7 @@
import type {
CheckSuiteAttributes,
CheckSuiteStatus,
DiscussionComment,
DiscussionStateType,
GitifyPullRequestReview,
GitifySubject,
Expand All @@ -16,11 +17,11 @@ import {
getCommitComment,
getIssue,
getIssueOrPullRequestComment,
getLatestDiscussion,
getPullRequest,
getPullRequestReviews,
getRelease,
} from './api/client';
import { fetchDiscussion, getLatestDiscussionComment } from './helpers';

export async function getGitifySubjectDetails(
notification: Notification,
Expand Down Expand Up @@ -145,7 +146,7 @@ async function getGitifySubjectForDiscussion(
notification: Notification,
token: string,
): Promise<GitifySubject> {
const discussion = await fetchDiscussion(notification, token);
const discussion = await getLatestDiscussion(notification, token);
let discussionState: DiscussionStateType = 'OPEN';

if (discussion) {
Expand Down Expand Up @@ -183,6 +184,22 @@ async function getGitifySubjectForDiscussion(
};
}

export function getLatestDiscussionComment(
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];
}

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

async function getGitifySubjectForIssue(
notification: Notification,
token: string,
Expand Down

0 comments on commit 24ab730

Please sign in to comment.