From d2d5e3ac7b31fce244e26c67255ebaa78c3e72cf Mon Sep 17 00:00:00 2001 From: Adam Setch Date: Mon, 25 Mar 2024 17:24:39 -0400 Subject: [PATCH] refactor(discussions): align types for github graphql schema naming. use fragment to ensure comments and replies fields are always in-sync --- src/__mocks__/mockedData.ts | 421 ++++++++++++++++++------------------ src/typesGithub.ts | 16 +- src/utils/helpers.ts | 44 ++-- 3 files changed, 239 insertions(+), 242 deletions(-) diff --git a/src/__mocks__/mockedData.ts b/src/__mocks__/mockedData.ts index 27ec5fa65..987b26c71 100644 --- a/src/__mocks__/mockedData.ts +++ b/src/__mocks__/mockedData.ts @@ -3,7 +3,7 @@ import { Notification, Repository, GraphQLSearch, - DiscussionSearchResultNode, + Discussion, } from '../typesGithub'; export const mockedEnterpriseAccounts: EnterpriseAccount[] = [ @@ -281,240 +281,239 @@ export const mockedSingleAccountNotifications: AccountNotifications[] = [ }, ]; -export const mockedGraphQLResponse: GraphQLSearch = - { +export const mockedGraphQLResponse: GraphQLSearch = { + data: { data: { - data: { - search: { - nodes: [ - { - viewerSubscription: 'SUBSCRIBED', - title: '1.16.0', - isAnswered: false, - stateReason: null, - url: 'https://github.com/manosim/notifications-test/discussions/612', - comments: { - nodes: [ - { - databaseId: 2215656, - createdAt: '2022-02-20T18:33:39Z', - author: { - login: 'comment-user', - }, - replies: { - nodes: [], - }, + search: { + nodes: [ + { + viewerSubscription: 'SUBSCRIBED', + title: '1.16.0', + isAnswered: false, + stateReason: null, + url: 'https://github.com/manosim/notifications-test/discussions/612', + comments: { + nodes: [ + { + databaseId: 2215656, + createdAt: '2022-02-20T18:33:39Z', + author: { + login: 'comment-user', + }, + replies: { + nodes: [], + }, + }, + { + databaseId: 2217789, + createdAt: '2022-02-21T03:30:42Z', + author: { + login: 'comment-user', }, - { - databaseId: 2217789, - createdAt: '2022-02-21T03:30:42Z', - author: { - login: 'comment-user', - }, - replies: { - nodes: [], - }, + replies: { + nodes: [], }, - { - databaseId: 2223243, - createdAt: '2022-02-21T18:26:27Z', - author: { - login: 'comment-user', - }, - replies: { - nodes: [ - { - databaseId: 2232922, - createdAt: '2022-02-23T00:57:58Z', - author: { - login: 'reply-user', - }, + }, + { + databaseId: 2223243, + createdAt: '2022-02-21T18:26:27Z', + author: { + login: 'comment-user', + }, + replies: { + nodes: [ + { + databaseId: 2232922, + createdAt: '2022-02-23T00:57:58Z', + author: { + login: 'reply-user', }, - ], - }, + }, + ], + }, + }, + { + databaseId: 2232921, + createdAt: '2022-02-23T00:57:49Z', + author: { + login: 'comment-user', }, - { - databaseId: 2232921, - createdAt: '2022-02-23T00:57:49Z', - author: { - login: 'comment-user', - }, - replies: { - nodes: [], - }, + replies: { + nodes: [], }, - { - databaseId: 2258799, - createdAt: '2022-02-27T01:22:20Z', - author: { - login: 'comment-user', - }, - replies: { - nodes: [ - { - databaseId: 2300902, - createdAt: '2022-03-05T17:43:52Z', - author: { - login: 'reply-user', - }, + }, + { + databaseId: 2258799, + createdAt: '2022-02-27T01:22:20Z', + author: { + login: 'comment-user', + }, + replies: { + nodes: [ + { + databaseId: 2300902, + createdAt: '2022-03-05T17:43:52Z', + author: { + login: 'reply-user', }, - ], - }, + }, + ], + }, + }, + { + databaseId: 2297637, + createdAt: '2022-03-04T20:39:44Z', + author: { + login: 'comment-user', }, - { - databaseId: 2297637, - createdAt: '2022-03-04T20:39:44Z', - author: { - login: 'comment-user', - }, - replies: { - nodes: [ - { - databaseId: 2300893, - createdAt: '2022-03-05T17:41:04Z', - author: { - login: 'reply-user', - }, + replies: { + nodes: [ + { + databaseId: 2300893, + createdAt: '2022-03-05T17:41:04Z', + author: { + login: 'reply-user', }, - ], - }, + }, + ], }, - { - databaseId: 2299763, - createdAt: '2022-03-05T11:05:42Z', - author: { - login: 'comment-user', - }, - replies: { - nodes: [ - { - databaseId: 2300895, - createdAt: '2022-03-05T17:41:44Z', - author: { - login: 'reply-user', - }, + }, + { + databaseId: 2299763, + createdAt: '2022-03-05T11:05:42Z', + author: { + login: 'comment-user', + }, + replies: { + nodes: [ + { + databaseId: 2300895, + createdAt: '2022-03-05T17:41:44Z', + author: { + login: 'reply-user', }, - ], - }, + }, + ], }, - ], - }, + }, + ], }, - { - viewerSubscription: 'IGNORED', - title: '1.16.0', - isAnswered: false, - stateReason: null, - url: 'https://github.com/manosim/notifications-test/discussions/612', - comments: { - nodes: [ - { - databaseId: 2215656, - createdAt: '2022-02-20T18:33:39Z', - author: { - login: 'comment-user', - }, - replies: { - nodes: [], - }, + }, + { + viewerSubscription: 'IGNORED', + title: '1.16.0', + isAnswered: false, + stateReason: null, + url: 'https://github.com/manosim/notifications-test/discussions/612', + comments: { + nodes: [ + { + databaseId: 2215656, + createdAt: '2022-02-20T18:33:39Z', + author: { + login: 'comment-user', + }, + replies: { + nodes: [], }, - { - databaseId: 2217789, - createdAt: '2022-02-21T03:30:42Z', - author: { - login: 'comment-user', - }, - replies: { - nodes: [], - }, + }, + { + databaseId: 2217789, + createdAt: '2022-02-21T03:30:42Z', + author: { + login: 'comment-user', }, - { - databaseId: 2223243, - createdAt: '2022-02-21T18:26:27Z', - author: { - login: 'comment-user', - }, - replies: { - nodes: [ - { - databaseId: 2232922, - createdAt: '2022-02-23T00:57:58Z', - author: { - login: 'reply-user', - }, + replies: { + nodes: [], + }, + }, + { + databaseId: 2223243, + createdAt: '2022-02-21T18:26:27Z', + author: { + login: 'comment-user', + }, + replies: { + nodes: [ + { + databaseId: 2232922, + createdAt: '2022-02-23T00:57:58Z', + author: { + login: 'reply-user', }, - ], - }, + }, + ], + }, + }, + { + databaseId: 2232921, + createdAt: '2022-02-23T00:57:49Z', + author: { + login: 'comment-user', + }, + replies: { + nodes: [], }, - { - databaseId: 2232921, - createdAt: '2022-02-23T00:57:49Z', - author: { - login: 'comment-user', - }, - replies: { - nodes: [], - }, + }, + { + databaseId: 2258799, + createdAt: '2022-02-27T01:22:20Z', + author: { + login: 'comment-user', }, - { - databaseId: 2258799, - createdAt: '2022-02-27T01:22:20Z', - author: { - login: 'comment-user', - }, - replies: { - nodes: [ - { - databaseId: 2300902, - createdAt: '2022-03-05T17:43:52Z', - author: { - login: 'reply-user', - }, + replies: { + nodes: [ + { + databaseId: 2300902, + createdAt: '2022-03-05T17:43:52Z', + author: { + login: 'reply-user', }, - ], - }, + }, + ], }, - { - databaseId: 2297637, - createdAt: '2022-03-04T20:39:44Z', - author: { - login: 'comment-user', - }, - replies: { - nodes: [ - { - databaseId: 2300893, - createdAt: '2022-03-05T17:41:04Z', - author: { - login: 'reply-user', - }, + }, + { + databaseId: 2297637, + createdAt: '2022-03-04T20:39:44Z', + author: { + login: 'comment-user', + }, + replies: { + nodes: [ + { + databaseId: 2300893, + createdAt: '2022-03-05T17:41:04Z', + author: { + login: 'reply-user', }, - ], - }, + }, + ], + }, + }, + { + databaseId: 2299763, + createdAt: '2022-03-05T11:05:42Z', + author: { + login: 'comment-user', }, - { - databaseId: 2299763, - createdAt: '2022-03-05T11:05:42Z', - author: { - login: 'comment-user', - }, - replies: { - nodes: [ - { - databaseId: 2300895, - createdAt: '2022-03-05T17:41:44Z', - author: { - login: 'reply-user', - }, + replies: { + nodes: [ + { + databaseId: 2300895, + createdAt: '2022-03-05T17:41:44Z', + author: { + login: 'reply-user', }, - ], - }, + }, + ], }, - ], - }, + }, + ], }, - ], - }, + }, + ], }, }, - }; + }, +}; diff --git a/src/typesGithub.ts b/src/typesGithub.ts index 863933257..2c640ef86 100644 --- a/src/typesGithub.ts +++ b/src/typesGithub.ts @@ -327,32 +327,26 @@ export interface GraphQLSearch { }; } -export interface DiscussionSearchResultNode { +export interface Discussion { viewerSubscription: ViewerSubscription; title: string; stateReason: DiscussionStateType; isAnswered: boolean; url: string; comments: { - nodes: DiscussionCommentNode[]; + nodes: DiscussionComment[]; }; } -export interface DiscussionCommentNode { +export interface DiscussionComment { databaseId: string | number; createdAt: string; author: DiscussionAuthor; - replies: { - nodes: DiscussionSubcommentNode[]; + replies?: { + nodes: DiscussionComment[]; }; } -export interface DiscussionSubcommentNode { - databaseId: string | number; - createdAt: string; - author: DiscussionAuthor; -} - export interface CheckSuiteAttributes { workflowName: string; attemptNumber?: number; diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index 016605e88..91d1dd9f3 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -2,12 +2,11 @@ import { EnterpriseAccount, AuthState } from '../types'; import { Notification, GraphQLSearch, - DiscussionCommentNode, - DiscussionSearchResultNode, + Discussion, PullRequest, Issue, IssueComments, - DiscussionSubcommentNode, + DiscussionComment, } from '../typesGithub'; import { apiRequestAuth } from '../utils/api-requests'; import { openExternalLink } from '../utils/comms'; @@ -149,10 +148,22 @@ async function getDiscussionUrl( export async function fetchDiscussion( notification: Notification, token: string, -): Promise { - const response: GraphQLSearch = - await apiRequestAuth(`https://api.github.com/graphql`, 'POST', token, { - query: `query fetchDiscussions( +): Promise { + const response: GraphQLSearch = await apiRequestAuth( + `https://api.github.com/graphql`, + 'POST', + token, + { + query: ` + fragment CommentFields on DiscussionComment { + databaseId + createdAt + author { + login + } + } + + query fetchDiscussions( $queryStatement: String!, $type: SearchType!, $firstDiscussions: Int, @@ -169,18 +180,10 @@ export async function fetchDiscussion( url comments(last: $lastComments){ nodes { - databaseId - createdAt - author { - login - } + ...CommentFields replies(last: $firstReplies) { nodes { - databaseId - createdAt - author { - login - } + ...CommentFields } } } @@ -201,7 +204,8 @@ export async function fetchDiscussion( lastComments: 100, firstReplies: 1, }, - }); + }, + ); let discussions = response?.data?.data.search.nodes.filter( @@ -217,8 +221,8 @@ export async function fetchDiscussion( } export function getLatestDiscussionComment( - comments: DiscussionCommentNode[], -): DiscussionSubcommentNode | null { + comments: DiscussionComment[], +): DiscussionComment | null { if (!comments || comments.length == 0) { return null; }