Skip to content

Commit

Permalink
Merge c62f9ce into a013411
Browse files Browse the repository at this point in the history
  • Loading branch information
setchy committed May 12, 2024
2 parents a013411 + c62f9ce commit b77fa74
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 121 deletions.
15 changes: 0 additions & 15 deletions src/__mocks__/mockedData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,6 @@ export const mockedGraphQLResponse: GraphQLSearch<Discussion> = {
search: {
nodes: [
{
viewerSubscription: 'SUBSCRIBED',
title: '1.16.0',
isAnswered: false,
stateReason: 'OPEN',
Expand All @@ -439,20 +438,6 @@ export const mockedGraphQLResponse: GraphQLSearch<Discussion> = {
},
comments: mockDiscussionComments,
},
{
viewerSubscription: 'IGNORED',
title: '1.16.0',
isAnswered: false,
stateReason: 'ANSWERED',
url: 'https://github.com/gitify-app/notifications-test/discussions/123',
author: {
login: 'discussion-creator',
url: 'https://github.com/discussion-creator',
avatar_url: 'https://avatars.githubusercontent.com/u/123456789?v=4',
type: 'User',
},
comments: mockDiscussionComments,
},
],
},
},
Expand Down
1 change: 0 additions & 1 deletion src/hooks/useNotifications.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,6 @@ describe('hooks/useNotifications.ts', () => {
nodes: [
{
title: 'This is a Discussion.',
viewerSubscription: 'SUBSCRIBED',
stateReason: null,
isAnswered: true,
url: 'https://github.com/gitify-app/notifications-test/discussions/612',
Expand Down
3 changes: 0 additions & 3 deletions src/typesGitHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ export type StateType =
| IssueStateReasonType
| PullRequestStateType;

export type ViewerSubscription = 'IGNORED' | 'SUBSCRIBED' | 'UNSUBSCRIBED';

export type CheckSuiteStatus =
| 'action_required'
| 'cancelled'
Expand Down Expand Up @@ -450,7 +448,6 @@ export interface GraphQLSearch<T> {
}

export interface Discussion {
viewerSubscription: ViewerSubscription;
title: string;
stateReason: DiscussionStateType;
isAnswered: boolean;
Expand Down
8 changes: 4 additions & 4 deletions src/utils/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ import { apiRequestAuth } from './request';
import { print } from 'graphql/language/printer';
import Constants from '../constants';
import { QUERY_SEARCH_DISCUSSIONS } from './graphql/discussions';
import { formatSearchQueryString, getGitHubAPIBaseUrl } from './utils';
import { formatAsGitHubSearchSyntax } from './graphql/utils';
import { getGitHubAPIBaseUrl } from './utils';

/**
* Get Hypermedia links to resources accessible in GitHub's REST API
Expand Down Expand Up @@ -249,12 +250,11 @@ export async function searchDiscussions(
return apiRequestAuth(Constants.GITHUB_API_GRAPHQL_URL, 'POST', token, {
query: print(QUERY_SEARCH_DISCUSSIONS),
variables: {
queryStatement: formatSearchQueryString(
queryStatement: formatAsGitHubSearchSyntax(
notification.repository.full_name,
notification.subject.title,
notification.updated_at,
),
firstDiscussions: 10,
firstDiscussions: 1,
lastComments: 1,
lastReplies: 1,
},
Expand Down
1 change: 0 additions & 1 deletion src/utils/api/graphql/discussions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export const QUERY_SEARCH_DISCUSSIONS = gql`
search(query:$queryStatement, type: DISCUSSION, first: $firstDiscussions) {
nodes {
... on Discussion {
viewerSubscription
title
stateReason
isAnswered
Expand Down
11 changes: 11 additions & 0 deletions src/utils/api/graphql/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { formatAsGitHubSearchSyntax } from './utils';

describe('utils/api/graphql/utils.ts', () => {
describe('formatAsGitHubCodeSearchSyntax', () => {
test('formats search query string correctly', () => {
const result = formatAsGitHubSearchSyntax('exampleRepo', 'exampleTitle');

expect(result).toBe('exampleTitle in:title repo:exampleRepo');
});
});
});
6 changes: 6 additions & 0 deletions src/utils/api/graphql/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export function formatAsGitHubSearchSyntax(
repo: string,
title: string,
): string {
return `${title} in:title repo:${repo}`;
}
32 changes: 1 addition & 31 deletions src/utils/api/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import {
addHours,
formatSearchQueryString,
getGitHubAPIBaseUrl,
} from './utils';
import { getGitHubAPIBaseUrl } from './utils';

describe('utils/api/utils.ts', () => {
describe('generateGitHubAPIUrl', () => {
Expand All @@ -16,30 +12,4 @@ describe('utils/api/utils.ts', () => {
expect(result.toString()).toBe('https://github.manos.im/api/v3/');
});
});

describe('formatSearchQueryString', () => {
test('formats search query string correctly', () => {
const result = formatSearchQueryString(
'exampleRepo',
'exampleTitle',
'2024-02-20T12:00:00.000Z',
);

expect(result).toBe(
'exampleTitle in:title repo:exampleRepo updated:>2024-02-20T10:00:00.000Z',
);
});
});

describe('addHours', () => {
test('adds hours correctly for positive values', () => {
const result = addHours('2024-02-20T12:00:00.000Z', 3);
expect(result).toBe('2024-02-20T15:00:00.000Z');
});

test('adds hours correctly for negative values', () => {
const result = addHours('2024-02-20T12:00:00.000Z', -2);
expect(result).toBe('2024-02-20T10:00:00.000Z');
});
});
});
12 changes: 0 additions & 12 deletions src/utils/api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,3 @@ export function getGitHubAPIBaseUrl(hostname: string): URL {
}
return url;
}

export function formatSearchQueryString(
repo: string,
title: string,
lastUpdated: string,
): string {
return `${title} in:title repo:${repo} updated:>${addHours(lastUpdated, -2)}`;
}

export function addHours(date: string, hours: number): string {
return new Date(new Date(date).getTime() + hours * 36e5).toISOString();
}
17 changes: 2 additions & 15 deletions src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@ async function getDiscussionUrl(
if (discussion) {
url.href = discussion.url;

const comments = discussion.comments.nodes;

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

if (latestComment) {
url.hash = `#discussioncomment-${latestComment.databaseId}`;
Expand All @@ -118,18 +116,7 @@ export async function fetchDiscussion(
try {
const response = await searchDiscussions(notification, token);

let discussions =
response.data?.data.search.nodes.filter(
(discussion) => discussion.title === notification.subject.title,
) || [];

if (discussions.length > 1) {
discussions = discussions.filter(
(discussion) => discussion.viewerSubscription === 'SUBSCRIBED',
);
}

return discussions[0];
return response.data?.data.search.nodes[0] ?? null;
} catch (err) {}
}

Expand Down
45 changes: 6 additions & 39 deletions src/utils/subject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import type {
DiscussionStateType,
Notification,
Repository,
ViewerSubscription,
} from '../typesGitHub';
import {
getCheckSuiteAttributes,
Expand Down Expand Up @@ -239,7 +238,7 @@ describe('utils/subject.ts', () => {
.reply(200, {
data: {
search: {
nodes: [mockDiscussionNode('SUBSCRIBED', null, true)],
nodes: [mockDiscussionNode(null, true)],
},
},
});
Expand All @@ -266,7 +265,7 @@ describe('utils/subject.ts', () => {
.reply(200, {
data: {
search: {
nodes: [mockDiscussionNode('SUBSCRIBED', 'DUPLICATE', false)],
nodes: [mockDiscussionNode('DUPLICATE', false)],
},
},
});
Expand All @@ -293,7 +292,7 @@ describe('utils/subject.ts', () => {
.reply(200, {
data: {
search: {
nodes: [mockDiscussionNode('SUBSCRIBED', null, false)],
nodes: [mockDiscussionNode(null, false)],
},
},
});
Expand All @@ -320,7 +319,7 @@ describe('utils/subject.ts', () => {
.reply(200, {
data: {
search: {
nodes: [mockDiscussionNode('SUBSCRIBED', 'OUTDATED', false)],
nodes: [mockDiscussionNode('OUTDATED', false)],
},
},
});
Expand All @@ -347,7 +346,7 @@ describe('utils/subject.ts', () => {
.reply(200, {
data: {
search: {
nodes: [mockDiscussionNode('SUBSCRIBED', 'REOPENED', false)],
nodes: [mockDiscussionNode('REOPENED', false)],
},
},
});
Expand All @@ -374,7 +373,7 @@ describe('utils/subject.ts', () => {
.reply(200, {
data: {
search: {
nodes: [mockDiscussionNode('SUBSCRIBED', 'RESOLVED', true)],
nodes: [mockDiscussionNode('RESOLVED', true)],
},
},
});
Expand All @@ -394,36 +393,6 @@ describe('utils/subject.ts', () => {
},
});
});

it('filtered response by subscribed', async () => {
nock('https://api.github.com')
.post('/graphql')
.reply(200, {
data: {
search: {
nodes: [
mockDiscussionNode('SUBSCRIBED', null, false),
mockDiscussionNode('IGNORED', null, true),
],
},
},
});

const result = await getGitifySubjectDetails(
mockNotification,
mockAccounts.token,
);

expect(result).toEqual({
state: 'OPEN',
user: {
login: mockDiscussionAuthor.login,
html_url: mockDiscussionAuthor.url,
avatar_url: mockDiscussionAuthor.avatar_url,
type: mockDiscussionAuthor.type,
},
});
});
});

describe('Issues', () => {
Expand Down Expand Up @@ -1176,14 +1145,12 @@ describe('utils/subject.ts', () => {
});

function mockDiscussionNode(
subscription: ViewerSubscription,
state: DiscussionStateType,
isAnswered: boolean,
): Discussion {
return {
title: 'This is a mocked discussion',
url: 'https://github.com/gitify-app/notifications-test/discussions/1',
viewerSubscription: subscription,
stateReason: state,
isAnswered: isAnswered,
author: mockDiscussionAuthor,
Expand Down

0 comments on commit b77fa74

Please sign in to comment.