diff --git a/src/__mocks__/mockedData.ts b/src/__mocks__/mockedData.ts index cfa1ef9bc..3fedecffe 100644 --- a/src/__mocks__/mockedData.ts +++ b/src/__mocks__/mockedData.ts @@ -4,7 +4,7 @@ import { Repository, User, GraphQLSearch, - DiscussionSearchResultEdge, + DiscussionSearchResultNode, } from '../typesGithub'; export const mockedEnterpriseAccounts: EnterpriseAccount[] = [ @@ -282,214 +282,166 @@ export const mockedSingleAccountNotifications: AccountNotifications[] = [ }, ]; -export const mockedGraphQLResponse: GraphQLSearch = +export const mockedGraphQLResponse: GraphQLSearch = { data: { data: { search: { - edges: [ + nodes: [ { - node: { - viewerSubscription: 'SUBSCRIBED', - title: '1.16.0', - url: 'https://github.com/manosim/notifications-test/discussions/612', - comments: { - edges: [ - { - node: { - databaseId: 2215656, - createdAt: '2022-02-20T18:33:39Z', - replies: { - edges: [], - }, - }, + viewerSubscription: 'SUBSCRIBED', + title: '1.16.0', + url: 'https://github.com/manosim/notifications-test/discussions/612', + comments: { + nodes: [ + { + databaseId: 2215656, + createdAt: '2022-02-20T18:33:39Z', + replies: { + nodes: [], }, - { - node: { - databaseId: 2217789, - createdAt: '2022-02-21T03:30:42Z', - replies: { - edges: [], - }, - }, + }, + { + databaseId: 2217789, + createdAt: '2022-02-21T03:30:42Z', + replies: { + nodes: [], }, - { - node: { - databaseId: 2223243, - createdAt: '2022-02-21T18:26:27Z', - replies: { - edges: [ - { - node: { - databaseId: 2232922, - createdAt: '2022-02-23T00:57:58Z', - }, - }, - ], + }, + { + databaseId: 2223243, + createdAt: '2022-02-21T18:26:27Z', + replies: { + nodes: [ + { + databaseId: 2232922, + createdAt: '2022-02-23T00:57:58Z', }, - }, + ], }, - { - node: { - databaseId: 2232921, - createdAt: '2022-02-23T00:57:49Z', - replies: { - edges: [], - }, - }, + }, + { + databaseId: 2232921, + createdAt: '2022-02-23T00:57:49Z', + replies: { + nodes: [], }, - { - node: { - databaseId: 2258799, - createdAt: '2022-02-27T01:22:20Z', - replies: { - edges: [ - { - node: { - databaseId: 2300902, - createdAt: '2022-03-05T17:43:52Z', - }, - }, - ], + }, + { + databaseId: 2258799, + createdAt: '2022-02-27T01:22:20Z', + replies: { + nodes: [ + { + databaseId: 2300902, + createdAt: '2022-03-05T17:43:52Z', }, - }, + ], }, - { - node: { - databaseId: 2297637, - createdAt: '2022-03-04T20:39:44Z', - replies: { - edges: [ - { - node: { - databaseId: 2300893, - createdAt: '2022-03-05T17:41:04Z', - }, - }, - ], + }, + { + databaseId: 2297637, + createdAt: '2022-03-04T20:39:44Z', + replies: { + nodes: [ + { + databaseId: 2300893, + createdAt: '2022-03-05T17:41:04Z', }, - }, + ], }, - { - node: { - databaseId: 2299763, - createdAt: '2022-03-05T11:05:42Z', - replies: { - edges: [ - { - node: { - databaseId: 2300895, - createdAt: '2022-03-05T17:41:44Z', - }, - }, - ], + }, + { + databaseId: 2299763, + createdAt: '2022-03-05T11:05:42Z', + replies: { + nodes: [ + { + databaseId: 2300895, + createdAt: '2022-03-05T17:41:44Z', }, - }, + ], }, - ], - }, + }, + ], }, }, { - node: { - viewerSubscription: 'IGNORED', - title: '1.16.0', - url: 'https://github.com/manosim/notifications-test/discussions/612', - comments: { - edges: [ - { - node: { - databaseId: 2215656, - createdAt: '2022-02-20T18:33:39Z', - replies: { - edges: [], - }, - }, + viewerSubscription: 'IGNORED', + title: '1.16.0', + url: 'https://github.com/manosim/notifications-test/discussions/612', + comments: { + nodes: [ + { + databaseId: 2215656, + createdAt: '2022-02-20T18:33:39Z', + replies: { + nodes: [], }, - { - node: { - databaseId: 2217789, - createdAt: '2022-02-21T03:30:42Z', - replies: { - edges: [], - }, - }, + }, + { + databaseId: 2217789, + createdAt: '2022-02-21T03:30:42Z', + replies: { + nodes: [], }, - { - node: { - databaseId: 2223243, - createdAt: '2022-02-21T18:26:27Z', - replies: { - edges: [ - { - node: { - databaseId: 2232922, - createdAt: '2022-02-23T00:57:58Z', - }, - }, - ], + }, + { + databaseId: 2223243, + createdAt: '2022-02-21T18:26:27Z', + replies: { + nodes: [ + { + databaseId: 2232922, + createdAt: '2022-02-23T00:57:58Z', }, - }, + ], }, - { - node: { - databaseId: 2232921, - createdAt: '2022-02-23T00:57:49Z', - replies: { - edges: [], - }, - }, + }, + { + databaseId: 2232921, + createdAt: '2022-02-23T00:57:49Z', + replies: { + nodes: [], }, - { - node: { - databaseId: 2258799, - createdAt: '2022-02-27T01:22:20Z', - replies: { - edges: [ - { - node: { - databaseId: 2300902, - createdAt: '2022-03-05T17:43:52Z', - }, - }, - ], + }, + { + databaseId: 2258799, + createdAt: '2022-02-27T01:22:20Z', + replies: { + nodes: [ + { + databaseId: 2300902, + createdAt: '2022-03-05T17:43:52Z', }, - }, + ], }, - { - node: { - databaseId: 2297637, - createdAt: '2022-03-04T20:39:44Z', - replies: { - edges: [ - { - node: { - databaseId: 2300893, - createdAt: '2022-03-05T17:41:04Z', - }, - }, - ], + }, + { + databaseId: 2297637, + createdAt: '2022-03-04T20:39:44Z', + replies: { + nodes: [ + { + databaseId: 2300893, + createdAt: '2022-03-05T17:41:04Z', }, - }, + ], }, - { - node: { - databaseId: 2299763, - createdAt: '2022-03-05T11:05:42Z', - replies: { - edges: [ - { - node: { - databaseId: 2300895, - createdAt: '2022-03-05T17:41:44Z', - }, - }, - ], + }, + { + databaseId: 2299763, + createdAt: '2022-03-05T11:05:42Z', + replies: { + nodes: [ + { + databaseId: 2300895, + createdAt: '2022-03-05T17:41:44Z', }, - }, + ], }, - ], - }, + }, + ], }, }, ], diff --git a/src/typesGithub.ts b/src/typesGithub.ts index d051ab674..2f46d0153 100644 --- a/src/typesGithub.ts +++ b/src/typesGithub.ts @@ -169,47 +169,39 @@ export interface GraphQLSearch { data: { data: { search: { - edges: T[]; + nodes: T[]; }; }; }; } -export interface DiscussionStateSearchResultEdge { - node: { - viewerSubscription: ViewerSubscription; - title: string; - stateReason: DiscussionStateType; - isAnswered: boolean; - }; +export interface DiscussionStateSearchResultNode { + viewerSubscription: ViewerSubscription; + title: string; + stateReason: DiscussionStateType; + isAnswered: boolean; } -export interface DiscussionSearchResultEdge { - node: { - viewerSubscription: ViewerSubscription; - title: string; - url: string; - comments: { - edges: DiscussionCommentEdge[]; - }; +export interface DiscussionSearchResultNode { + viewerSubscription: ViewerSubscription; + title: string; + url: string; + comments: { + nodes: DiscussionCommentNode[]; }; } -export interface DiscussionCommentEdge { - node: { - databaseId: string | number; - createdAt: string; - replies: { - edges: DiscussionSubcommentEdge[]; - }; +export interface DiscussionCommentNode { + databaseId: string | number; + createdAt: string; + replies: { + nodes: DiscussionSubcommentNode[]; }; } -export interface DiscussionSubcommentEdge { - node: { - databaseId: string | number; - createdAt: string; - }; +export interface DiscussionSubcommentNode { + databaseId: string | number; + createdAt: string; } export interface CheckSuiteAttributes { diff --git a/src/utils/helpers.ts b/src/utils/helpers.ts index 2ccdbe9b4..15153cd46 100644 --- a/src/utils/helpers.ts +++ b/src/utils/helpers.ts @@ -2,8 +2,8 @@ import { EnterpriseAccount, AuthState } from '../types'; import { Notification, GraphQLSearch, - DiscussionCommentEdge, - DiscussionSearchResultEdge, + DiscussionCommentNode, + DiscussionSearchResultNode, } from '../typesGithub'; import { apiRequestAuth } from '../utils/api-requests'; import { openExternalLink } from '../utils/comms'; @@ -76,55 +76,52 @@ async function getDiscussionUrl( ): Promise { let url = `${notification.repository.html_url}/discussions`; - const response: GraphQLSearch = + const response: GraphQLSearch = await apiRequestAuth(`https://api.github.com/graphql`, 'POST', token, { query: `{ - search(query:"${formatSearchQueryString( - notification.repository.full_name, - notification.subject.title, - notification.updated_at, - )}", type: DISCUSSION, first: 10) { - edges { - node { - ... on Discussion { - viewerSubscription - title - url - comments(last: 100) { - edges { - node { - databaseId - createdAt - replies(last: 1) { - edges { - node { - databaseId - createdAt - } - } - } - } - } - } + search(query:"${formatSearchQueryString( + notification.repository.full_name, + notification.subject.title, + notification.updated_at, + )}", type: DISCUSSION, first: 10) { + nodes { + ... on Discussion { + viewerSubscription + title + url + comments(last: 100) { + nodes { + databaseId + createdAt + replies(last: 1) { + nodes { + databaseId + createdAt + } } + } } + } } - } - }`, + } + }`, }); - let edges = - response?.data?.data?.search?.edges?.filter( - (edge) => edge.node.title === notification.subject.title, + + let discussions = + response?.data?.data?.search?.nodes?.filter( + (discussion) => discussion.title === notification.subject.title, ) || []; - if (edges.length > 1) - edges = edges.filter( - (edge) => edge.node.viewerSubscription === 'SUBSCRIBED', + + if (discussions.length > 1) + discussions = discussions.filter( + (discussion) => discussion.viewerSubscription === 'SUBSCRIBED', ); - if (edges[0]) { - url = edges[0].node.url; + if (discussions[0]) { + const discussion = discussions[0]; + url = discussion.url; - let comments = edges[0]?.node.comments.edges; + let comments = discussion.comments.nodes; let latestCommentId: string | number; if (comments?.length) { @@ -137,13 +134,12 @@ async function getDiscussionUrl( } export const getLatestDiscussionCommentId = ( - comments: DiscussionCommentEdge[], + comments: DiscussionCommentNode[], ) => comments - .flatMap((comment) => comment.node.replies.edges) + .flatMap((comment) => comment.replies.nodes) .concat([comments.at(-1)]) - .reduce((a, b) => (a.node.createdAt > b.node.createdAt ? a : b))?.node - .databaseId; + .reduce((a, b) => (a.createdAt > b.createdAt ? a : b))?.databaseId; export async function generateGitHubWebUrl( notification: Notification, diff --git a/src/utils/state.test.ts b/src/utils/state.test.ts index 0335dde12..89a62318b 100644 --- a/src/utils/state.test.ts +++ b/src/utils/state.test.ts @@ -150,14 +150,12 @@ describe('utils/state.ts', () => { .reply(200, { data: { search: { - edges: [ + nodes: [ { - node: { - title: 'This is an answered discussion', - viewerSubscription: 'SUBSCRIBED', - stateReason: null, - isAnswered: true, - }, + title: 'This is an answered discussion', + viewerSubscription: 'SUBSCRIBED', + stateReason: null, + isAnswered: true, }, ], }, @@ -186,14 +184,12 @@ describe('utils/state.ts', () => { .reply(200, { data: { search: { - edges: [ + nodes: [ { - node: { - title: 'This is a duplicate discussion', - viewerSubscription: 'SUBSCRIBED', - stateReason: 'DUPLICATE', - isAnswered: false, - }, + title: 'This is a duplicate discussion', + viewerSubscription: 'SUBSCRIBED', + stateReason: 'DUPLICATE', + isAnswered: false, }, ], }, @@ -222,14 +218,12 @@ describe('utils/state.ts', () => { .reply(200, { data: { search: { - edges: [ + nodes: [ { - node: { - title: 'This is an open discussion', - viewerSubscription: 'SUBSCRIBED', - stateReason: null, - isAnswered: false, - }, + title: 'This is an open discussion', + viewerSubscription: 'SUBSCRIBED', + stateReason: null, + isAnswered: false, }, ], }, @@ -258,14 +252,12 @@ describe('utils/state.ts', () => { .reply(200, { data: { search: { - edges: [ + nodes: [ { - node: { - title: 'This is an outdated discussion', - viewerSubscription: 'SUBSCRIBED', - stateReason: 'OUTDATED', - isAnswered: false, - }, + title: 'This is an outdated discussion', + viewerSubscription: 'SUBSCRIBED', + stateReason: 'OUTDATED', + isAnswered: false, }, ], }, @@ -294,14 +286,12 @@ describe('utils/state.ts', () => { .reply(200, { data: { search: { - edges: [ + nodes: [ { - node: { - title: 'This is a reopened discussion', - viewerSubscription: 'SUBSCRIBED', - stateReason: 'REOPENED', - isAnswered: false, - }, + title: 'This is a reopened discussion', + viewerSubscription: 'SUBSCRIBED', + stateReason: 'REOPENED', + isAnswered: false, }, ], }, @@ -330,14 +320,12 @@ describe('utils/state.ts', () => { .reply(200, { data: { search: { - edges: [ + nodes: [ { - node: { - title: 'This is a resolved discussion', - viewerSubscription: 'SUBSCRIBED', - stateReason: 'RESOLVED', - isAnswered: false, - }, + title: 'This is a resolved discussion', + viewerSubscription: 'SUBSCRIBED', + stateReason: 'RESOLVED', + isAnswered: false, }, ], }, @@ -366,22 +354,18 @@ describe('utils/state.ts', () => { .reply(200, { data: { search: { - edges: [ + nodes: [ { - node: { - title: 'This is a discussion', - viewerSubscription: 'SUBSCRIBED', - stateReason: null, - isAnswered: false, - }, + title: 'This is a discussion', + viewerSubscription: 'SUBSCRIBED', + stateReason: null, + isAnswered: false, }, { - node: { - title: 'This is a discussion', - viewerSubscription: 'IGNORED', - stateReason: null, - isAnswered: true, - }, + title: 'This is a discussion', + viewerSubscription: 'IGNORED', + stateReason: null, + isAnswered: true, }, ], }, diff --git a/src/utils/state.ts b/src/utils/state.ts index 4d2b79c61..ad783e894 100644 --- a/src/utils/state.ts +++ b/src/utils/state.ts @@ -2,7 +2,7 @@ import { formatSearchQueryString } from './helpers'; import { CheckSuiteAttributes, CheckSuiteStatus, - DiscussionStateSearchResultEdge, + DiscussionStateSearchResultNode, DiscussionStateType, GraphQLSearch, IssueStateType, @@ -81,46 +81,45 @@ export async function getDiscussionState( notification: Notification, token: string, ): Promise { - const response: GraphQLSearch = + const response: GraphQLSearch = await apiRequestAuth(`https://api.github.com/graphql`, 'POST', token, { query: `{ - search(query:"${formatSearchQueryString( - notification.repository.full_name, - notification.subject.title, - notification.updated_at, - )}", type: DISCUSSION, first: 10) { - edges { - node { - ... on Discussion { - viewerSubscription - title - stateReason - isAnswered - } - } + search(query:"${formatSearchQueryString( + notification.repository.full_name, + notification.subject.title, + notification.updated_at, + )}", type: DISCUSSION, first: 10) { + nodes { + ... on Discussion { + viewerSubscription + title + stateReason + isAnswered } } - }`, + } + }`, }); - let edges = - response?.data?.data?.search?.edges?.filter( - (edge) => edge.node.title === notification.subject.title, + let discussions = + response?.data?.data?.search?.nodes?.filter( + (discussion) => discussion.title === notification.subject.title, ) || []; - if (edges.length > 1) { - edges = edges.filter( - (edge) => edge.node.viewerSubscription === 'SUBSCRIBED', + if (discussions.length > 1) { + discussions = discussions.filter( + (discussion) => discussion.viewerSubscription === 'SUBSCRIBED', ); } - if (edges[0]) { - if (edges[0].node.isAnswered) { + if (discussions[0]) { + const discussion = discussions[0]; + if (discussion.isAnswered) { return 'ANSWERED'; } - if (edges[0].node.stateReason) { - return edges[0].node.stateReason; + if (discussion.stateReason) { + return discussion.stateReason; } }