Skip to content

Commit 5d767f3

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

File tree

17 files changed

+476
-196
lines changed

17 files changed

+476
-196
lines changed

src/renderer/components/metrics/MetricGroup.test.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import { renderWithAppContext } from '../../__helpers__/test-utils';
22
import { mockSettings } from '../../__mocks__/state-mocks';
3-
import type { Milestone } from '../../typesGitHub';
43
import { mockSingleNotification } from '../../utils/api/__mocks__/response-mocks';
4+
import {
5+
type MilestoneFieldsFragment,
6+
MilestoneState,
7+
} from '../../utils/api/graphql/generated/graphql';
58
import { MetricGroup } from './MetricGroup';
69

710
describe('renderer/components/metrics/MetricGroup.tsx', () => {
@@ -103,8 +106,8 @@ describe('renderer/components/metrics/MetricGroup.tsx', () => {
103106
const mockNotification = mockSingleNotification;
104107
mockNotification.subject.milestone = {
105108
title: 'Milestone 1',
106-
state: 'open',
107-
} as Milestone;
109+
state: MilestoneState.Open,
110+
} as MilestoneFieldsFragment;
108111

109112
const props = {
110113
notification: mockNotification,
@@ -118,8 +121,8 @@ describe('renderer/components/metrics/MetricGroup.tsx', () => {
118121
const mockNotification = mockSingleNotification;
119122
mockNotification.subject.milestone = {
120123
title: 'Milestone 1',
121-
state: 'closed',
122-
} as Milestone;
124+
state: MilestoneState.Closed,
125+
} as MilestoneFieldsFragment;
123126

124127
const props = {
125128
notification: mockNotification,

src/renderer/components/metrics/MetricGroup.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import { AppContext } from '../../context/App';
1111
import { IconColor } from '../../types';
1212
import type { Notification } from '../../typesGitHub';
13+
import { MilestoneState } from '../../utils/api/graphql/generated/graphql';
1314
import { getPullRequestReviewIcon } from '../../utils/icons';
1415
import { MetricPill } from './MetricPill';
1516

@@ -84,7 +85,7 @@ export const MetricGroup: FC<MetricGroupProps> = ({
8485
{notification.subject.milestone && (
8586
<MetricPill
8687
color={
87-
notification.subject.milestone.state === 'open'
88+
notification.subject.milestone.state === MilestoneState.Open
8889
? IconColor.GREEN
8990
: IconColor.RED
9091
}

src/renderer/typesGitHub.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
import type { Account, Link } from './types';
2+
import type {
3+
AuthorFieldsFragment,
4+
IssueState,
5+
IssueStateReason,
6+
MilestoneFieldsFragment,
7+
PullRequestState,
8+
} from './utils/api/graphql/generated/graphql';
29

310
export type Reason =
411
| 'approval_requested'
@@ -60,7 +67,10 @@ export type StateType =
6067
| DiscussionStateType
6168
| IssueStateType
6269
| IssueStateReasonType
63-
| PullRequestStateType;
70+
| PullRequestStateType
71+
| IssueState
72+
| IssueStateReason
73+
| PullRequestState;
6474

6575
export type CheckSuiteStatus =
6676
| 'action_required'
@@ -169,12 +179,7 @@ export interface User {
169179
site_admin: boolean;
170180
}
171181

172-
export interface SubjectUser {
173-
login: string;
174-
html_url: Link;
175-
avatar_url: Link;
176-
type: UserType;
177-
}
182+
export type SubjectUser = AuthorFieldsFragment;
178183

179184
export interface Repository {
180185
id: number;
@@ -264,7 +269,7 @@ export interface GitifySubject {
264269
linkedIssues?: string[];
265270
comments?: number;
266271
labels?: string[];
267-
milestone?: Milestone;
272+
milestone?: MilestoneFieldsFragment;
268273
}
269274

270275
export interface PullRequest {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ export const mockDiscussionByNumberGraphQLResponse: FetchDiscussionByNumberQuery
380380
url: 'https://github.com/gitify-app/notifications-test/discussions/612' as Link,
381381
author: {
382382
login: 'comment-user',
383-
url: 'https://github.com/comment-user' as Link,
383+
html_url: 'https://github.com/comment-user' as Link,
384384
avatar_url:
385385
'https://avatars.githubusercontent.com/u/123456789?v=4' as Link,
386386
type: 'User',
@@ -393,7 +393,7 @@ export const mockDiscussionByNumberGraphQLResponse: FetchDiscussionByNumberQuery
393393
createdAt: '2017-02-20T17:51:57Z',
394394
author: {
395395
login: 'comment-user',
396-
url: 'https://github.com/comment-user' as Link,
396+
html_url: 'https://github.com/comment-user' as Link,
397397
avatar_url:
398398
'https://avatars.githubusercontent.com/u/123456789?v=4' as Link,
399399
type: 'User',
@@ -405,7 +405,7 @@ export const mockDiscussionByNumberGraphQLResponse: FetchDiscussionByNumberQuery
405405
createdAt: '2017-05-20T17:51:57Z',
406406
author: {
407407
login: 'reply-user',
408-
url: 'https://github.com/reply-user' as Link,
408+
html_url: 'https://github.com/reply-user' as Link,
409409
avatar_url:
410410
'https://avatars.githubusercontent.com/u/123456789?v=4' as Link,
411411
type: 'User',

src/renderer/utils/api/client.ts

Lines changed: 49 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,8 @@ import type {
1111
import type {
1212
Commit,
1313
CommitComment,
14-
Issue,
15-
IssueOrPullRequestComment,
1614
Notification,
1715
NotificationThreadSubscription,
18-
PullRequest,
19-
PullRequestReview,
2016
Release,
2117
UserDetails,
2218
} from '../../typesGitHub';
@@ -25,6 +21,10 @@ import { rendererLogError } from '../logger';
2521
import {
2622
FetchDiscussionByNumberDocument,
2723
type FetchDiscussionByNumberQuery,
24+
FetchIssueByNumberDocument,
25+
type FetchIssueByNumberQuery,
26+
FetchPullByNumberDocument,
27+
type FetchPullByNumberQuery,
2828
} from './graphql/generated/graphql';
2929
import { apiRequestAuth, performGraphQLRequest } from './request';
3030
import {
@@ -161,52 +161,6 @@ export function getCommitComment(
161161
return apiRequestAuth(url, 'GET', token);
162162
}
163163

164-
/**
165-
* Get details of an issue.
166-
*
167-
* Endpoint documentation: https://docs.github.com/en/rest/issues/issues#get-an-issue
168-
*/
169-
export function getIssue(url: Link, token: Token): AxiosPromise<Issue> {
170-
return apiRequestAuth(url, 'GET', token);
171-
}
172-
173-
/**
174-
* Get comments on issues and pull requests.
175-
* Every pull request is an issue, but not every issue is a pull request.
176-
*
177-
* Endpoint documentation: https://docs.github.com/en/rest/issues/comments#get-an-issue-comment
178-
*/
179-
export function getIssueOrPullRequestComment(
180-
url: Link,
181-
token: Token,
182-
): AxiosPromise<IssueOrPullRequestComment> {
183-
return apiRequestAuth(url, 'GET', token);
184-
}
185-
186-
/**
187-
* Get details of a pull request.
188-
*
189-
* Endpoint documentation: https://docs.github.com/en/rest/pulls/pulls#get-a-pull-request
190-
*/
191-
export function getPullRequest(
192-
url: Link,
193-
token: Token,
194-
): AxiosPromise<PullRequest> {
195-
return apiRequestAuth(url, 'GET', token);
196-
}
197-
198-
/**
199-
* Lists all reviews for a specified pull request. The list of reviews returns in chronological order.
200-
*
201-
* Endpoint documentation: https://docs.github.com/en/rest/pulls/reviews#list-reviews-for-a-pull-request
202-
*/
203-
export function getPullRequestReviews(
204-
url: Link,
205-
token: Token,
206-
): AxiosPromise<PullRequestReview[]> {
207-
return apiRequestAuth(url, 'GET', token);
208-
}
209-
210164
/**
211165
* Gets a public release with the specified release ID.
212166
*
@@ -234,10 +188,51 @@ export async function getHtmlUrl(url: Link, token: Token): Promise<string> {
234188
}
235189

236190
/**
237-
* Search for Discussions that match notification title and repository.
238-
*
239-
* Returns the latest discussion and their latest comments / replies
240-
*
191+
* Fetch GitHub Issue by Issue Number.
192+
*/
193+
export async function fetchIssueByNumber(
194+
notification: Notification,
195+
): Promise<ExecutionResult<FetchIssueByNumberQuery>> {
196+
const url = getGitHubGraphQLUrl(notification.account.hostname);
197+
const number = getNumberFromUrl(notification.subject.url);
198+
199+
return performGraphQLRequest(
200+
url.toString() as Link,
201+
notification.account.token,
202+
FetchIssueByNumberDocument,
203+
{
204+
owner: notification.repository.owner.login,
205+
name: notification.repository.name,
206+
number: number,
207+
firstLabels: 100,
208+
},
209+
);
210+
}
211+
212+
/**
213+
* Fetch GitHub Pull Request by PR Number.
214+
*/
215+
export async function fetchPullByNumber(
216+
notification: Notification,
217+
): Promise<ExecutionResult<FetchPullByNumberQuery>> {
218+
const url = getGitHubGraphQLUrl(notification.account.hostname);
219+
const number = getNumberFromUrl(notification.subject.url);
220+
221+
return performGraphQLRequest(
222+
url.toString() as Link,
223+
notification.account.token,
224+
FetchPullByNumberDocument,
225+
{
226+
owner: notification.repository.owner.login,
227+
name: notification.repository.name,
228+
number: number,
229+
firstLabels: 100,
230+
},
231+
);
232+
}
233+
234+
/**
235+
* Fetch GitHub Discussion by Discussion Number.
241236
*/
242237
export async function fetchDiscussionByNumber(
243238
notification: Notification,
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fragment AuthorFields on Actor {
2+
login
3+
html_url: url
4+
avatar_url: avatarUrl
5+
type: __typename
6+
}

src/renderer/utils/api/graphql/discussions.graphql renamed to src/renderer/utils/api/graphql/discussion.graphql

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,6 @@ query FetchDiscussionByNumber(
3838
}
3939
}
4040

41-
fragment AuthorFields on Actor {
42-
login
43-
url
44-
avatar_url: avatarUrl
45-
type: __typename
46-
}
47-
4841
fragment CommentFields on DiscussionComment {
4942
databaseId
5043
createdAt

0 commit comments

Comments
 (0)