Skip to content

Commit 85ae38d

Browse files
committed
refactor(api): use @octokit/types REST schema type
Signed-off-by: Adam Setch <[email protected]>
1 parent 1f4fd85 commit 85ae38d

File tree

2 files changed

+51
-254
lines changed

2 files changed

+51
-254
lines changed

src/renderer/typesGitHub.ts

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,7 @@ import type { GitifyNotification, GitifySubject, Link } from './types';
44

55
// TODO: #828 Add explicit types for GitHub API response vs Gitify Notifications object
66

7-
export interface User {
8-
login: string;
9-
id: number;
10-
node_id: string;
11-
avatar_url: Link;
12-
gravatar_url: Link;
13-
url: Link;
14-
html_url: Link;
15-
followers_url: Link;
16-
following_url: Link;
17-
gists_url: Link;
18-
starred_url: Link;
19-
subscriptions_url: Link;
20-
organizations_url: Link;
21-
repos_url: Link;
22-
events_url: Link;
23-
received_events_url: Link;
24-
type: UserType;
25-
site_admin: boolean;
26-
}
27-
28-
export type Notification = GitHubNotification &
29-
GitifyNotification & {
30-
reason: Reason;
31-
subject: Subject;
32-
repository: Repository;
33-
};
34-
export type Subject = GitHubSubject & {
35-
url: Link;
36-
latest_comment_url: Link;
37-
type: SubjectType;
38-
} & GitifySubject;
39-
7+
// Stronger typings for Reason string attribute
408
export type Reason =
419
| 'approval_requested'
4210
| 'assign'
@@ -54,6 +22,7 @@ export type Reason =
5422
| 'subscribed'
5523
| 'team_mention';
5624

25+
// Stronger typings for Subject Type string attribute
5726
export type SubjectType =
5827
| 'CheckSuite'
5928
| 'Commit'
@@ -66,13 +35,28 @@ export type SubjectType =
6635
| 'RepositoryVulnerabilityAlert'
6736
| 'WorkflowRun';
6837

38+
// Stronger typings for Reason User Type attribute
6939
export type UserType =
7040
| 'Bot'
7141
| 'EnterpriseUserAccount'
7242
| 'Mannequin'
7343
| 'Organization'
7444
| 'User';
7545

46+
export type Notification = GitHubNotification &
47+
GitifyNotification & {
48+
reason: Reason;
49+
subject: Subject;
50+
repository: Repository;
51+
};
52+
export type Subject = GitHubSubject & {
53+
url: Link;
54+
latest_comment_url: Link;
55+
type: SubjectType;
56+
} & GitifySubject;
57+
58+
export type User = components['schemas']['simple-user'] & { type: UserType };
59+
7660
export type GitHubNotification = components['schemas']['thread'];
7761
export type GitHubSubject = components['schemas']['thread']['subject'];
7862

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

Lines changed: 34 additions & 221 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,35 @@ import {
33
mockGitHubEnterpriseServerAccount,
44
} from '../../../__mocks__/account-mocks';
55
import type { Link } from '../../../types';
6-
import type { Notification, Repository, User } from '../../../typesGitHub';
6+
import type {
7+
Notification,
8+
Owner,
9+
Repository,
10+
User,
11+
} from '../../../typesGitHub';
712

8-
export const mockNotificationUser: User = {
9-
login: 'octocat',
13+
export const mockNotificationUser = {
1014
id: 123456789,
11-
node_id: 'MDQ6VXNlcjE=',
15+
login: 'octocat',
1216
avatar_url: 'https://avatars.githubusercontent.com/u/583231?v=4' as Link,
13-
gravatar_url: '' as Link,
1417
url: 'https://api.github.com/users/octocat' as Link,
1518
html_url: 'https://github.com/octocat' as Link,
16-
followers_url: 'https://api.github.com/users/octocat/followers' as Link,
17-
following_url:
18-
'https://api.github.com/users/octocat/following{/other_user}' as Link,
19-
gists_url: 'https://api.github.com/users/octocat/gists{/gist_id}' as Link,
20-
starred_url:
21-
'https://api.github.com/users/octocat/starred{/owner}{/repo}' as Link,
22-
subscriptions_url:
23-
'https://api.github.com/users/octocat/subscriptions' as Link,
24-
organizations_url: 'https://api.github.com/users/octocat/orgs' as Link,
25-
repos_url: 'https://api.github.com/users/octocat/repos' as Link,
26-
events_url: 'https://api.github.com/users/octocat/events{/privacy}' as Link,
27-
received_events_url:
28-
'https://api.github.com/users/octocat/received_events' as Link,
2919
type: 'User',
30-
site_admin: false,
31-
};
20+
} satisfies Partial<User>;
3221

3322
// 2 Notifications
3423
// Hostname : 'github.com'
3524
// Repository : 'gitify-app/notifications-test'
25+
const mockGitHubOwner: Owner = {
26+
id: 6333409,
27+
login: 'gitify-app',
28+
avatar_url:
29+
'https://avatars.githubusercontent.com/u/133795385?s=200&v=4' as Link,
30+
url: 'https://api.github.com/users/gitify-app' as Link,
31+
html_url: 'https://github.com/gitify-app' as Link,
32+
type: 'User',
33+
} as unknown as Owner;
34+
3635
export const mockGitHubNotifications: Notification[] = [
3736
{
3837
account: mockGitHubCloudAccount,
@@ -73,116 +72,12 @@ export const mockGitHubNotifications: Notification[] = [
7372
},
7473
repository: {
7574
id: 57216596,
76-
node_id: 'MDEwOlJlcG9zaXRvcnkzNjAyOTcwNg==',
7775
name: 'notifications-test',
7876
full_name: 'gitify-app/notifications-test',
7977
url: 'https://api.github.com/gitify-app/notifications-test' as Link,
80-
owner: {
81-
login: 'gitify-app',
82-
id: 6333409,
83-
node_id: 'MDQ6VXNlcjYzMzM0MDk=',
84-
avatar_url:
85-
'https://avatars.githubusercontent.com/u/133795385?s=200&v=4' as Link,
86-
gravatar_id: '',
87-
url: 'https://api.github.com/users/gitify-app' as Link,
88-
html_url: 'https://github.com/gitify-app' as Link,
89-
followers_url:
90-
'https://api.github.com/users/gitify-app/followers' as Link,
91-
following_url:
92-
'https://api.github.com/users/gitify-app/following{/other_user}' as Link,
93-
gists_url:
94-
'https://api.github.com/users/gitify-app/gists{/gist_id}' as Link,
95-
starred_url:
96-
'https://api.github.com/users/gitify-app/starred{/owner}{/repo}' as Link,
97-
subscriptions_url:
98-
'https://api.github.com/users/gitify-app/subscriptions' as Link,
99-
organizations_url:
100-
'https://api.github.com/users/gitify-app/orgs' as Link,
101-
repos_url: 'https://api.github.com/users/gitify-app/repos' as Link,
102-
events_url:
103-
'https://api.github.com/users/gitify-app/events{/privacy}' as Link,
104-
received_events_url:
105-
'https://api.github.com/users/gitify-app/received_events' as Link,
106-
type: 'User',
107-
site_admin: false,
108-
},
109-
private: true,
110-
description: 'Test Repository',
111-
fork: false,
112-
archive_url:
113-
'https://api.github.com/repos/gitify-app/notifications-test/{archive_format}{/ref}' as Link,
114-
assignees_url:
115-
'https://api.github.com/repos/gitify-app/notifications-test/assignees{/user}' as Link,
116-
blobs_url:
117-
'https://api.github.com/repos/gitify-app/notifications-test/git/blobs{/sha}' as Link,
118-
branches_url:
119-
'https://api.github.com/repos/gitify-app/notifications-test/branches{/branch}' as Link,
120-
collaborators_url:
121-
'https://api.github.com/repos/gitify-app/notifications-test/collaborators{/collaborator}' as Link,
122-
comments_url:
123-
'https://api.github.com/repos/gitify-app/notifications-test/comments{/number}' as Link,
124-
commits_url:
125-
'https://api.github.com/repos/gitify-app/notifications-test/commits{/sha}' as Link,
126-
compare_url:
127-
'https://api.github.com/repos/gitify-app/notifications-test/compare/{base}...{head}' as Link,
128-
contents_url:
129-
'https://api.github.com/repos/gitify-app/notifications-test/contents/{+path}' as Link,
130-
contributors_url:
131-
'https://api.github.com/repos/gitify-app/notifications-test/contributors' as Link,
132-
deployments_url:
133-
'https://api.github.com/repos/gitify-app/notifications-test/deployments' as Link,
134-
downloads_url:
135-
'https://api.github.com/repos/gitify-app/notifications-test/downloads' as Link,
136-
events_url:
137-
'https://api.github.com/repos/gitify-app/notifications-test/events' as Link,
138-
forks_url:
139-
'https://api.github.com/repos/gitify-app/notifications-test/forks' as Link,
140-
git_commits_url:
141-
'https://api.github.com/repos/gitify-app/notifications-test/git/commits{/sha}' as Link,
142-
git_refs_url:
143-
'https://api.github.com/repos/gitify-app/notifications-test/git/refs{/sha}' as Link,
144-
git_tags_url:
145-
'https://api.github.com/repos/gitify-app/notifications-test/git/tags{/sha}' as Link,
146-
hooks_url:
147-
'https://api.github.com/repos/gitify-app/notifications-test/hooks' as Link,
78+
owner: mockGitHubOwner,
14879
html_url: 'https://github.com/gitify-app/notifications-test' as Link,
149-
issue_comment_url:
150-
'https://api.github.com/repos/gitify-app/notifications-test/issues/comments{/number}' as Link,
151-
issue_events_url:
152-
'https://api.github.com/repos/gitify-app/notifications-test/issues/events{/number}' as Link,
153-
issues_url:
154-
'https://api.github.com/repos/gitify-app/notifications-test/issues{/number}' as Link,
155-
keys_url:
156-
'https://api.github.com/repos/gitify-app/notifications-test/keys{/key_id}' as Link,
157-
labels_url:
158-
'https://api.github.com/repos/gitify-app/notifications-test/labels{/name}' as Link,
159-
languages_url:
160-
'https://api.github.com/repos/gitify-app/notifications-test/languages' as Link,
161-
merges_url:
162-
'https://api.github.com/repos/gitify-app/notifications-test/merges' as Link,
163-
milestones_url:
164-
'https://api.github.com/repos/gitify-app/notifications-test/milestones{/number}' as Link,
165-
notifications_url:
166-
'https://api.github.com/repos/gitify-app/notifications-test/notifications{?since,all,participating}' as Link,
167-
pulls_url:
168-
'https://api.github.com/repos/gitify-app/notifications-test/pulls{/number}' as Link,
169-
releases_url:
170-
'https://api.github.com/repos/gitify-app/notifications-test/releases{/id}' as Link,
171-
stargazers_url:
172-
'https://api.github.com/repos/gitify-app/notifications-test/stargazers' as Link,
173-
statuses_url:
174-
'https://api.github.com/repos/gitify-app/notifications-test/statuses/{sha}' as Link,
175-
subscribers_url:
176-
'https://api.github.com/repos/gitify-app/notifications-test/subscribers' as Link,
177-
subscription_url:
178-
'https://api.github.com/repos/gitify-app/notifications-test/subscription' as Link,
179-
tags_url:
180-
'https://api.github.com/repos/gitify-app/notifications-test/tags' as Link,
181-
teams_url:
182-
'https://api.github.com/repos/gitify-app/notifications-test/teams' as Link,
183-
trees_url:
184-
'https://api.github.com/repos/gitify-app/notifications-test/git/trees{/sha}' as Link,
185-
},
80+
} as unknown as Repository,
18681
url: 'https://api.github.com/notifications/threads/138661096' as Link,
18782
subscription_url:
18883
'https://api.github.com/notifications/threads/138661096/subscription' as Link,
@@ -207,39 +102,8 @@ export const mockGitHubNotifications: Notification[] = [
207102
id: 57216596,
208103
name: 'notifications-test',
209104
full_name: 'gitify-app/notifications-test',
210-
owner: {
211-
login: 'gitify-app',
212-
id: 6333409,
213-
avatar_url:
214-
'https://avatars.githubusercontent.com/u/133795385?s=200&v=4' as Link,
215-
gravatar_id: '',
216-
url: 'https://api.github.com/users/gitify-app' as Link,
217-
html_url: 'https://github.com/gitify-app' as Link,
218-
followers_url:
219-
'https://api.github.com/users/gitify-app/followers' as Link,
220-
following_url:
221-
'https://api.github.com/users/gitify-app/following{/other_user}' as Link,
222-
gists_url:
223-
'https://api.github.com/users/gitify-app/gists{/gist_id}' as Link,
224-
starred_url:
225-
'https://api.github.com/users/gitify-app/starred{/owner}{/repo}' as Link,
226-
subscriptions_url:
227-
'https://api.github.com/users/gitify-app/subscriptions' as Link,
228-
organizations_url:
229-
'https://api.github.com/users/gitify-app/orgs' as Link,
230-
repos_url: 'https://api.github.com/users/gitify-app/repos' as Link,
231-
events_url:
232-
'https://api.github.com/users/gitify-app/events{/privacy}' as Link,
233-
received_events_url:
234-
'https://api.github.com/users/gitify-app/received_events' as Link,
235-
type: 'User',
236-
site_admin: false,
237-
},
238-
private: true,
239-
html_url: 'https://github.com/gitify-app/notifications-test',
240-
description: null,
241-
fork: false,
242-
// Removed the rest of the properties
105+
owner: mockGitHubOwner,
106+
html_url: 'https://github.com/gitify-app/notifications-test' as Link,
243107
} as unknown as Repository,
244108
url: 'https://api.github.com/notifications/threads/148827438' as Link,
245109
subscription_url:
@@ -250,6 +114,15 @@ export const mockGitHubNotifications: Notification[] = [
250114
// 2 Notifications
251115
// Hostname : 'github.gitify.io'
252116
// Repository : 'myorg/notifications-test'
117+
const mockEnterpriseOwner = {
118+
login: 'myorg',
119+
id: 4,
120+
avatar_url: 'https://github.gitify.io/avatars/u/4?' as Link,
121+
url: 'https://github.gitify.io/api/v3/users/myorg',
122+
html_url: 'https://github.gitify.io/myorg' as Link,
123+
type: 'Organization',
124+
} as unknown as Owner;
125+
253126
export const mockEnterpriseNotifications: Notification[] = [
254127
{
255128
account: mockGitHubEnterpriseServerAccount,
@@ -271,38 +144,8 @@ export const mockEnterpriseNotifications: Notification[] = [
271144
id: 1,
272145
name: 'notifications-test',
273146
full_name: 'myorg/notifications-test',
274-
owner: {
275-
login: 'myorg',
276-
id: 4,
277-
avatar_url: 'https://github.gitify.io/avatars/u/4?' as Link,
278-
gravatar_id: '',
279-
url: 'https://github.gitify.io/api/v3/users/myorg',
280-
html_url: 'https://github.gitify.io/myorg' as Link,
281-
followers_url:
282-
'https://github.gitify.io/api/v3/users/myorg/followers' as Link,
283-
following_url:
284-
'https://github.gitify.io/api/v3/users/myorg/following{/other_user}' as Link,
285-
gists_url:
286-
'https://github.gitify.io/api/v3/users/myorg/gists{/gist_id}' as Link,
287-
starred_url:
288-
'https://github.gitify.io/api/v3/users/myorg/starred{/owner}{/repo}' as Link,
289-
subscriptions_url:
290-
'https://github.gitify.io/api/v3/users/myorg/subscriptions' as Link,
291-
organizations_url:
292-
'https://github.gitify.io/api/v3/users/myorg/orgs' as Link,
293-
repos_url: 'https://github.gitify.io/api/v3/users/myorg/repos' as Link,
294-
events_url:
295-
'https://github.gitify.io/api/v3/users/myorg/events{/privacy}' as Link,
296-
received_events_url:
297-
'https://github.gitify.io/api/v3/users/myorg/received_events' as Link,
298-
type: 'Organization',
299-
site_admin: false,
300-
},
301-
private: true,
147+
owner: mockEnterpriseOwner,
302148
html_url: 'https://github.gitify.io/myorg/notifications-test' as Link,
303-
description: null,
304-
fork: false,
305-
// Removed the rest of the properties
306149
} as unknown as Repository,
307150
url: 'https://github.gitify.io/api/v3/notifications/threads/4' as Link,
308151
subscription_url:
@@ -328,38 +171,8 @@ export const mockEnterpriseNotifications: Notification[] = [
328171
id: 1,
329172
name: 'notifications-test',
330173
full_name: 'myorg/notifications-test',
331-
owner: {
332-
login: 'myorg',
333-
id: 4,
334-
avatar_url: 'https://github.gitify.io/avatars/u/4?' as Link,
335-
gravatar_id: '',
336-
url: 'https://github.gitify.io/api/v3/users/myorg' as Link,
337-
html_url: 'https://github.gitify.io/myorg' as Link,
338-
followers_url:
339-
'https://github.gitify.io/api/v3/users/myorg/followers' as Link,
340-
following_url:
341-
'https://github.gitify.io/api/v3/users/myorg/following{/other_user}' as Link,
342-
gists_url:
343-
'https://github.gitify.io/api/v3/users/myorg/gists{/gist_id}' as Link,
344-
starred_url:
345-
'https://github.gitify.io/api/v3/users/myorg/starred{/owner}{/repo}' as Link,
346-
subscriptions_url:
347-
'https://github.gitify.io/api/v3/users/myorg/subscriptions' as Link,
348-
organizations_url:
349-
'https://github.gitify.io/api/v3/users/myorg/orgs' as Link,
350-
repos_url: 'https://github.gitify.io/api/v3/users/myorg/repos' as Link,
351-
events_url:
352-
'https://github.gitify.io/api/v3/users/myorg/events{/privacy}' as Link,
353-
received_events_url:
354-
'https://github.gitify.io/api/v3/users/myorg/received_events' as Link,
355-
type: 'Organization',
356-
site_admin: false,
357-
},
358-
private: true,
174+
owner: mockEnterpriseOwner,
359175
html_url: 'https://github.gitify.io/myorg/notifications-test' as Link,
360-
description: null,
361-
fork: false,
362-
// Removed the rest of the properties
363176
} as unknown as Repository,
364177
url: 'https://github.gitify.io/api/v3/notifications/threads/3' as Link,
365178
subscription_url:

0 commit comments

Comments
 (0)