Skip to content

Commit 6932eca

Browse files
committed
refactor(api): use graphql api for issue and pull request enrichment
Signed-off-by: Adam Setch <[email protected]>
1 parent 5d767f3 commit 6932eca

File tree

16 files changed

+93
-271
lines changed

16 files changed

+93
-271
lines changed

src/renderer/typesGitHub.ts

Lines changed: 1 addition & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -270,63 +270,14 @@ export interface GitifySubject {
270270
comments?: number;
271271
labels?: string[];
272272
milestone?: MilestoneFieldsFragment;
273-
}
274-
275-
export interface PullRequest {
276-
url: Link;
277-
id: number;
278-
node_id: string;
279-
html_url: Link;
280-
diff_url: Link;
281-
patch_url: Link;
282-
issue_url: Link;
283-
number: number;
284-
state: PullRequestStateType;
285-
locked: boolean;
286-
title: string;
287-
user: User;
288-
body: string;
289-
created_at: string;
290-
updated_at: string;
291-
closed_at: string | null;
292-
merged_at: string | null;
293-
merge_commit_sha: string | null;
294-
labels: Labels[];
295-
milestone: Milestone | null;
296-
draft: boolean;
297-
commits_url: Link;
298-
review_comments_url: Link;
299-
review_comment_url: Link;
300-
comments_url: Link;
301-
statuses_url: Link;
302-
author_association: string;
303-
merged: boolean;
304-
mergeable: boolean;
305-
rebaseable: boolean;
306-
comments: number;
307-
review_comments: number;
308-
maintainer_can_modify: boolean;
309-
commits: number;
310-
additions: number;
311-
deletions: number;
312-
changed_files: number;
273+
htmlUrl?: Link;
313274
}
314275

315276
export interface GitifyPullRequestReview {
316277
state: PullRequestReviewState;
317278
users: string[];
318279
}
319280

320-
export interface Labels {
321-
id: number;
322-
node_id: string;
323-
url: Link;
324-
name: string;
325-
color: string;
326-
default: boolean;
327-
description: string;
328-
}
329-
330281
export interface PullRequestReview {
331282
id: number;
332283
node_id: string;
@@ -411,64 +362,6 @@ export interface CommitComment {
411362
body: string;
412363
}
413364

414-
export interface Issue {
415-
url: Link;
416-
repository_url: Link;
417-
labels_url: Link;
418-
comments_url: Link;
419-
events_url: Link;
420-
html_url: Link;
421-
id: number;
422-
node_id: string;
423-
number: number;
424-
title: string;
425-
user: User;
426-
state: IssueStateType;
427-
locked: boolean;
428-
labels: Labels[];
429-
milestone: Milestone | null;
430-
comments: number;
431-
created_at: string;
432-
updated_at: string;
433-
closed_at: string | null;
434-
author_association: string;
435-
body: string;
436-
state_reason: IssueStateReasonType | null;
437-
}
438-
439-
export interface IssueOrPullRequestComment {
440-
url: Link;
441-
html_url: Link;
442-
issue_url: Link;
443-
id: number;
444-
node_id: string;
445-
user: User;
446-
created_at: string;
447-
updated_at: string;
448-
body: string;
449-
}
450-
451-
export interface Milestone {
452-
url: Link;
453-
html_url: Link;
454-
labels_url: Link;
455-
id: number;
456-
node_id: string;
457-
number: number;
458-
title: string;
459-
description: string;
460-
creator: User;
461-
open_issues: number;
462-
closed_issues: number;
463-
state: MilestoneStateType;
464-
created_at: string;
465-
updated_at: string;
466-
due_on: string | null;
467-
closed_at: string | null;
468-
}
469-
470-
type MilestoneStateType = 'open' | 'closed';
471-
472365
export interface Release {
473366
url: Link;
474367
assets_url: Link;

src/renderer/utils/api/__mocks__/response-mocks.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ export const mockDiscussionByNumberGraphQLResponse: FetchDiscussionByNumberQuery
398398
'https://avatars.githubusercontent.com/u/123456789?v=4' as Link,
399399
type: 'User',
400400
},
401+
url: 'https://github.com/gitify-app/notifications-test/discussions/612#discussioncomment-67890',
401402
replies: {
402403
nodes: [
403404
{
@@ -410,6 +411,7 @@ export const mockDiscussionByNumberGraphQLResponse: FetchDiscussionByNumberQuery
410411
'https://avatars.githubusercontent.com/u/123456789?v=4' as Link,
411412
type: 'User',
412413
},
414+
url: 'https://github.com/gitify-app/notifications-test/discussions/612#discussioncomment-12345',
413415
},
414416
],
415417
},

src/renderer/utils/api/graphql/discussion.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@ fragment CommentFields on DiscussionComment {
4444
author {
4545
...AuthorFields
4646
}
47+
url
4748
}

src/renderer/utils/api/graphql/generated/gql.ts

Lines changed: 9 additions & 9 deletions
Large diffs are not rendered by default.

src/renderer/utils/api/graphql/generated/graphql.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36203,7 +36203,7 @@ export type FetchDiscussionByNumberQuery = { __typename?: 'Query', repository?:
3620336203
| { __typename?: 'Mannequin', login: string, html_url: any, avatar_url: any, type: 'Mannequin' }
3620436204
| { __typename?: 'Organization', login: string, html_url: any, avatar_url: any, type: 'Organization' }
3620536205
| { __typename?: 'User', login: string, html_url: any, avatar_url: any, type: 'User' }
36206-
| null, comments: { __typename?: 'DiscussionCommentConnection', totalCount: number, nodes?: Array<{ __typename?: 'DiscussionComment', databaseId?: number | null, createdAt: any, replies: { __typename?: 'DiscussionCommentConnection', nodes?: Array<{ __typename?: 'DiscussionComment', databaseId?: number | null, createdAt: any, author?:
36206+
| null, comments: { __typename?: 'DiscussionCommentConnection', totalCount: number, nodes?: Array<{ __typename?: 'DiscussionComment', databaseId?: number | null, createdAt: any, url: any, replies: { __typename?: 'DiscussionCommentConnection', nodes?: Array<{ __typename?: 'DiscussionComment', databaseId?: number | null, createdAt: any, url: any, author?:
3620736207
| { __typename?: 'Bot', login: string, html_url: any, avatar_url: any, type: 'Bot' }
3620836208
| { __typename?: 'EnterpriseUserAccount', login: string, html_url: any, avatar_url: any, type: 'EnterpriseUserAccount' }
3620936209
| { __typename?: 'Mannequin', login: string, html_url: any, avatar_url: any, type: 'Mannequin' }
@@ -36217,7 +36217,7 @@ export type FetchDiscussionByNumberQuery = { __typename?: 'Query', repository?:
3621736217
| { __typename?: 'User', login: string, html_url: any, avatar_url: any, type: 'User' }
3621836218
| null } | null> | null }, labels?: { __typename?: 'LabelConnection', nodes?: Array<{ __typename?: 'Label', name: string } | null> | null } | null } | null } | null };
3621936219

36220-
export type CommentFieldsFragment = { __typename?: 'DiscussionComment', databaseId?: number | null, createdAt: any, author?:
36220+
export type CommentFieldsFragment = { __typename?: 'DiscussionComment', databaseId?: number | null, createdAt: any, url: any, author?:
3622136221
| { __typename?: 'Bot', login: string, html_url: any, avatar_url: any, type: 'Bot' }
3622236222
| { __typename?: 'EnterpriseUserAccount', login: string, html_url: any, avatar_url: any, type: 'EnterpriseUserAccount' }
3622336223
| { __typename?: 'Mannequin', login: string, html_url: any, avatar_url: any, type: 'Mannequin' }
@@ -36239,7 +36239,7 @@ export type FetchIssueByNumberQuery = { __typename?: 'Query', repository?: { __t
3623936239
| { __typename?: 'Mannequin', login: string, html_url: any, avatar_url: any, type: 'Mannequin' }
3624036240
| { __typename?: 'Organization', login: string, html_url: any, avatar_url: any, type: 'Organization' }
3624136241
| { __typename?: 'User', login: string, html_url: any, avatar_url: any, type: 'User' }
36242-
| null, comments: { __typename?: 'IssueCommentConnection', totalCount: number, nodes?: Array<{ __typename?: 'IssueComment', author?:
36242+
| null, comments: { __typename?: 'IssueCommentConnection', totalCount: number, nodes?: Array<{ __typename?: 'IssueComment', url: any, author?:
3624336243
| { __typename?: 'Bot', login: string, html_url: any, avatar_url: any, type: 'Bot' }
3624436244
| { __typename?: 'EnterpriseUserAccount', login: string, html_url: any, avatar_url: any, type: 'EnterpriseUserAccount' }
3624536245
| { __typename?: 'Mannequin', login: string, html_url: any, avatar_url: any, type: 'Mannequin' }
@@ -36263,13 +36263,13 @@ export type FetchPullByNumberQuery = { __typename?: 'Query', repository?: { __ty
3626336263
| { __typename?: 'Mannequin', login: string, html_url: any, avatar_url: any, type: 'Mannequin' }
3626436264
| { __typename?: 'Organization', login: string, html_url: any, avatar_url: any, type: 'Organization' }
3626536265
| { __typename?: 'User', login: string, html_url: any, avatar_url: any, type: 'User' }
36266-
| null, comments: { __typename?: 'IssueCommentConnection', totalCount: number, nodes?: Array<{ __typename?: 'IssueComment', author?:
36266+
| null, comments: { __typename?: 'IssueCommentConnection', totalCount: number, nodes?: Array<{ __typename?: 'IssueComment', url: any, author?:
3626736267
| { __typename?: 'Bot', login: string, html_url: any, avatar_url: any, type: 'Bot' }
3626836268
| { __typename?: 'EnterpriseUserAccount', login: string, html_url: any, avatar_url: any, type: 'EnterpriseUserAccount' }
3626936269
| { __typename?: 'Mannequin', login: string, html_url: any, avatar_url: any, type: 'Mannequin' }
3627036270
| { __typename?: 'Organization', login: string, html_url: any, avatar_url: any, type: 'Organization' }
3627136271
| { __typename?: 'User', login: string, html_url: any, avatar_url: any, type: 'User' }
36272-
| null } | null> | null }, reviews?: { __typename?: 'PullRequestReviewConnection', totalCount: number, nodes?: Array<{ __typename?: 'PullRequestReview', createdAt: any, author?:
36272+
| null } | null> | null }, reviews?: { __typename?: 'PullRequestReviewConnection', totalCount: number, nodes?: Array<{ __typename?: 'PullRequestReview', url: any, createdAt: any, author?:
3627336273
| { __typename?: 'Bot', login: string }
3627436274
| { __typename?: 'EnterpriseUserAccount', login: string }
3627536275
| { __typename?: 'Mannequin', login: string }
@@ -36310,6 +36310,7 @@ export const CommentFieldsFragmentDoc = new TypedDocumentString(`
3631036310
author {
3631136311
...AuthorFields
3631236312
}
36313+
url
3631336314
}
3631436315
fragment AuthorFields on Actor {
3631536316
login
@@ -36367,6 +36368,7 @@ fragment CommentFields on DiscussionComment {
3636736368
author {
3636836369
...AuthorFields
3636936370
}
36371+
url
3637036372
}`) as unknown as TypedDocumentString<FetchDiscussionByNumberQuery, FetchDiscussionByNumberQueryVariables>;
3637136373
export const FetchIssueByNumberDocument = new TypedDocumentString(`
3637236374
query FetchIssueByNumber($owner: String!, $name: String!, $number: Int!, $firstLabels: Int) {
@@ -36387,6 +36389,7 @@ export const FetchIssueByNumberDocument = new TypedDocumentString(`
3638736389
comments(last: 1) {
3638836390
totalCount
3638936391
nodes {
36392+
url
3639036393
author {
3639136394
...AuthorFields
3639236395
}
@@ -36431,6 +36434,7 @@ export const FetchPullByNumberDocument = new TypedDocumentString(`
3643136434
comments(last: 1) {
3643236435
totalCount
3643336436
nodes {
36437+
url
3643436438
author {
3643536439
...AuthorFields
3643636440
}
@@ -36439,6 +36443,7 @@ export const FetchPullByNumberDocument = new TypedDocumentString(`
3643936443
reviews(last: 1) {
3644036444
totalCount
3644136445
nodes {
36446+
url
3644236447
createdAt
3644336448
author {
3644436449
login

src/renderer/utils/api/graphql/issue.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ query FetchIssueByNumber(
2121
comments(last: 1) {
2222
totalCount
2323
nodes {
24+
url
2425
author {
2526
...AuthorFields
2627
}

src/renderer/utils/api/graphql/pull.graphql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ query FetchPullByNumber(
2323
comments(last: 1) {
2424
totalCount
2525
nodes {
26+
url
2627
author {
2728
...AuthorFields
2829
}
@@ -31,6 +32,7 @@ query FetchPullByNumber(
3132
reviews(last: 1) {
3233
totalCount
3334
nodes {
35+
url
3436
createdAt
3537
author {
3638
login

src/renderer/utils/helpers.ts

Lines changed: 11 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@ import {
77
import { Constants } from '../constants';
88
import type { Chevron, Hostname, Link } from '../types';
99
import type { Notification } from '../typesGitHub';
10-
import { fetchDiscussionByNumber, getHtmlUrl } from './api/client';
10+
import { getHtmlUrl } from './api/client';
1111
import type { PlatformType } from './auth/types';
1212
import { rendererLogError } from './logger';
13-
import { getCheckSuiteAttributes } from './notifications/handlers/checkSuite';
14-
import { getClosestDiscussionCommentOrReply } from './notifications/handlers/discussion';
15-
import { getWorkflowRunAttributes } from './notifications/handlers/workflowRun';
1613

1714
export function getPlatformFromHostname(hostname: string): PlatformType {
1815
return hostname.endsWith(Constants.DEFAULT_AUTH_OPTIONS.hostname)
@@ -31,40 +28,6 @@ export function generateNotificationReferrerId(
3128
return btoa(raw);
3229
}
3330

34-
export function getCheckSuiteUrl(notification: Notification): Link {
35-
const filters = [];
36-
37-
const checkSuiteAttributes = getCheckSuiteAttributes(notification);
38-
39-
if (checkSuiteAttributes?.workflowName) {
40-
filters.push(
41-
`workflow:"${checkSuiteAttributes.workflowName.replaceAll(' ', '+')}"`,
42-
);
43-
}
44-
45-
if (checkSuiteAttributes?.status) {
46-
filters.push(`is:${checkSuiteAttributes.status}`);
47-
}
48-
49-
if (checkSuiteAttributes?.branchName) {
50-
filters.push(`branch:${checkSuiteAttributes.branchName}`);
51-
}
52-
53-
return actionsURL(notification.repository.html_url, filters);
54-
}
55-
56-
export function getWorkflowRunUrl(notification: Notification): Link {
57-
const filters = [];
58-
59-
const workflowRunAttributes = getWorkflowRunAttributes(notification);
60-
61-
if (workflowRunAttributes?.status) {
62-
filters.push(`is:${workflowRunAttributes.status}`);
63-
}
64-
65-
return actionsURL(notification.repository.html_url, filters);
66-
}
67-
6831
/**
6932
* Construct a GitHub Actions URL for a repository with optional filters.
7033
*/
@@ -80,56 +43,22 @@ export function actionsURL(repositoryURL: string, filters: string[]): Link {
8043
return url.toString().replaceAll('%2B', '+') as Link;
8144
}
8245

83-
async function getDiscussionUrl(notification: Notification): Promise<Link> {
84-
const url = new URL(notification.repository.html_url);
85-
url.pathname += '/discussions';
86-
87-
const response = await fetchDiscussionByNumber(notification);
88-
const discussion = response.data.repository.discussion;
89-
90-
if (discussion) {
91-
url.href = discussion.url;
92-
93-
const closestComment = getClosestDiscussionCommentOrReply(
94-
notification,
95-
discussion.comments.nodes,
96-
);
97-
if (closestComment) {
98-
url.hash = `#discussioncomment-${closestComment.databaseId}`;
99-
}
100-
}
101-
102-
return url.toString() as Link;
103-
}
104-
10546
export async function generateGitHubWebUrl(
10647
notification: Notification,
10748
): Promise<Link> {
10849
const url = new URL(getDefaultURLForType(notification));
10950

11051
try {
111-
switch (notification.subject.type) {
112-
case 'CheckSuite':
113-
url.href = getCheckSuiteUrl(notification);
114-
break;
115-
case 'Discussion':
116-
url.href = await getDiscussionUrl(notification);
117-
break;
118-
case 'WorkflowRun':
119-
url.href = getWorkflowRunUrl(notification);
120-
break;
121-
default:
122-
if (notification.subject.latest_comment_url) {
123-
url.href = await getHtmlUrl(
124-
notification.subject.latest_comment_url,
125-
notification.account.token,
126-
);
127-
} else if (notification.subject.url) {
128-
url.href = await getHtmlUrl(
129-
notification.subject.url,
130-
notification.account.token,
131-
);
132-
}
52+
if (notification.subject.latest_comment_url) {
53+
url.href = await getHtmlUrl(
54+
notification.subject.latest_comment_url,
55+
notification.account.token,
56+
);
57+
} else if (notification.subject.url) {
58+
url.href = await getHtmlUrl(
59+
notification.subject.url,
60+
notification.account.token,
61+
);
13362
}
13463
} catch (err) {
13564
rendererLogError(

0 commit comments

Comments
 (0)