Skip to content

Commit c7776ab

Browse files
authored
refactor: make explicit the different user types (#924)
* refactor: make explicit the different user types * refactor: make explicit the different user types * refactor: make explicit the different user types
1 parent 25c1e1e commit c7776ab

File tree

7 files changed

+113
-38
lines changed

7 files changed

+113
-38
lines changed

src/__mocks__/mockedData.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import { AccountNotifications, EnterpriseAccount } from '../types';
1+
import { AccountNotifications, EnterpriseAccount, GitifyUser } from '../types';
22
import {
33
Notification,
44
Repository,
5-
User,
65
GraphQLSearch,
76
DiscussionSearchResultNode,
87
} from '../typesGithub';
@@ -14,7 +13,7 @@ export const mockedEnterpriseAccounts: EnterpriseAccount[] = [
1413
},
1514
];
1615

17-
export const mockedUser: User = {
16+
export const mockedUser: GitifyUser = {
1817
login: 'octocat',
1918
name: 'Mona Lisa Octocat',
2019
id: 123456789,

src/components/NotificationRow.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ export const NotificationRow: React.FC<IProps> = ({
6262
addSuffix: true,
6363
});
6464
const updatedBy = notification.subject.user
65-
? ` by ${notification.subject.user}`
65+
? ` by ${notification.subject.user.login}`
6666
: '';
6767
const updatedLabel = `Updated ${updatedAt}${updatedBy}`;
6868
const notificationTitle = formatForDisplay([

src/types.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { Notification, User } from './typesGithub';
1+
import { Notification } from './typesGithub';
22

33
export interface AuthState {
44
token?: string;
55
enterpriseAccounts: EnterpriseAccount[];
6-
user: User | null;
6+
user: GitifyUser | null;
77
}
88

99
export interface SettingsState {
@@ -56,3 +56,9 @@ export interface AuthTokenResponse {
5656
hostname: string;
5757
token: string;
5858
}
59+
60+
export interface GitifyUser {
61+
login: string;
62+
name: string;
63+
id: number;
64+
}

src/typesGithub.ts

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,66 @@ export interface Notification {
8181
subscription_url: string;
8282
}
8383

84+
export type UserDetails = User & UserProfile;
85+
86+
export interface UserProfile {
87+
name: string;
88+
company: string;
89+
blog: string;
90+
location: string;
91+
email: string;
92+
hireable: string;
93+
bio: string;
94+
twitter_username: string;
95+
public_repos: number;
96+
public_gists: number;
97+
followers: number;
98+
following: number;
99+
created_at: string;
100+
updated_at: string;
101+
private_gists: number;
102+
total_private_repos: number;
103+
owned_private_repos: number;
104+
disk_usage: number;
105+
collaborators: number;
106+
two_factor_authentication: boolean;
107+
plan: Plan;
108+
}
109+
export interface Plan {
110+
name: string;
111+
space: number;
112+
private_repos: number;
113+
collaborators: number;
114+
}
115+
84116
export interface User {
85117
login: string;
86118
name: string;
87119
id: number;
120+
node_id: string;
121+
avatar_url: string;
122+
gravatar_url: string;
123+
url: string;
124+
html_url: string;
125+
followers_url: string;
126+
following_url: string;
127+
gists_url: string;
128+
starred_url: string;
129+
subscriptions_url: string;
130+
organizations_url: string;
131+
repos_url: string;
132+
events_url: string;
133+
received_events_url: string;
134+
type: string;
135+
site_admin: boolean;
136+
}
137+
138+
export interface SubjectUser {
139+
login: string;
140+
}
141+
142+
export interface DiscussionAuthor {
143+
login: string;
88144
}
89145

90146
export interface Repository {
@@ -169,7 +225,7 @@ interface GitHubSubject {
169225
// This is not in the GitHub API, but we add it to the type to make it easier to work with
170226
export interface GitifySubject {
171227
state?: StateType;
172-
user?: string;
228+
user?: SubjectUser;
173229
}
174230

175231
export interface PullRequest {
@@ -285,7 +341,7 @@ export interface DiscussionSearchResultNode {
285341
export interface DiscussionCommentNode {
286342
databaseId: string | number;
287343
createdAt: string;
288-
author: { login: string };
344+
author: DiscussionAuthor;
289345
replies: {
290346
nodes: DiscussionSubcommentNode[];
291347
};
@@ -294,7 +350,7 @@ export interface DiscussionCommentNode {
294350
export interface DiscussionSubcommentNode {
295351
databaseId: string | number;
296352
createdAt: string;
297-
author: { login: string };
353+
author: DiscussionAuthor;
298354
}
299355

300356
export interface CheckSuiteAttributes {

src/utils/auth.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ import { BrowserWindow } from '@electron/remote';
22

33
import { generateGitHubAPIUrl, isEnterpriseHost } from './helpers';
44
import { apiRequest, apiRequestAuth } from '../utils/api-requests';
5-
import { AuthResponse, AuthState, AuthTokenResponse } from '../types';
5+
import {
6+
AuthResponse,
7+
AuthState,
8+
AuthTokenResponse,
9+
GitifyUser,
10+
} from '../types';
611
import { Constants } from '../utils/constants';
7-
import { User } from '../typesGithub';
12+
import { UserDetails } from '../typesGithub';
813

914
export const authGitHub = (
1015
authOptions = Constants.DEFAULT_AUTH_OPTIONS,
@@ -76,17 +81,15 @@ export const authGitHub = (
7681
export const getUserData = async (
7782
token: string,
7883
hostname: string,
79-
): Promise<User> => {
80-
const response = await apiRequestAuth(
81-
`${generateGitHubAPIUrl(hostname)}user`,
82-
'GET',
83-
token,
84-
);
84+
): Promise<GitifyUser> => {
85+
const response: UserDetails = (
86+
await apiRequestAuth(`${generateGitHubAPIUrl(hostname)}user`, 'GET', token)
87+
).data;
8588

8689
return {
87-
id: response.data.id,
88-
login: response.data.login,
89-
name: response.data.name,
90+
id: response.id,
91+
login: response.login,
92+
name: response.name,
9093
};
9194
};
9295

@@ -112,7 +115,7 @@ export const addAccount = (
112115
accounts: AuthState,
113116
token,
114117
hostname,
115-
user?: User,
118+
user?: GitifyUser,
116119
): AuthState => {
117120
if (!isEnterpriseHost(hostname)) {
118121
return {

src/utils/subject.test.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ describe('utils/subject.ts', () => {
435435
);
436436

437437
expect(result.state).toBe('open');
438-
expect(result.user).toBe('some-commenter');
438+
expect(result.user).toEqual({ login: 'some-commenter' });
439439
});
440440

441441
it('closed issue state', async () => {
@@ -453,7 +453,7 @@ describe('utils/subject.ts', () => {
453453
);
454454

455455
expect(result.state).toBe('closed');
456-
expect(result.user).toBe('some-commenter');
456+
expect(result.user).toEqual({ login: 'some-commenter' });
457457
});
458458

459459
it('completed issue state', async () => {
@@ -475,7 +475,7 @@ describe('utils/subject.ts', () => {
475475
);
476476

477477
expect(result.state).toBe('completed');
478-
expect(result.user).toBe('some-commenter');
478+
expect(result.user).toEqual({ login: 'some-commenter' });
479479
});
480480

481481
it('not_planned issue state', async () => {
@@ -497,7 +497,7 @@ describe('utils/subject.ts', () => {
497497
);
498498

499499
expect(result.state).toBe('not_planned');
500-
expect(result.user).toBe('some-commenter');
500+
expect(result.user).toEqual({ login: 'some-commenter' });
501501
});
502502

503503
it('reopened issue state', async () => {
@@ -519,7 +519,7 @@ describe('utils/subject.ts', () => {
519519
);
520520

521521
expect(result.state).toBe('reopened');
522-
expect(result.user).toBe('some-commenter');
522+
expect(result.user).toEqual({ login: 'some-commenter' });
523523
});
524524

525525
it('handle issues without latest_comment_url', async () => {
@@ -544,7 +544,7 @@ describe('utils/subject.ts', () => {
544544
);
545545

546546
expect(result.state).toBe('open');
547-
expect(result.user).toBe('some-user');
547+
expect(result.user).toEqual({ login: 'some-user' });
548548
});
549549
});
550550

@@ -577,7 +577,7 @@ describe('utils/subject.ts', () => {
577577
);
578578

579579
expect(result.state).toBe('closed');
580-
expect(result.user).toBe('some-commenter');
580+
expect(result.user).toEqual({ login: 'some-commenter' });
581581
});
582582

583583
it('draft pull request state', async () => {
@@ -600,7 +600,7 @@ describe('utils/subject.ts', () => {
600600
);
601601

602602
expect(result.state).toBe('draft');
603-
expect(result.user).toBe('some-commenter');
603+
expect(result.user).toEqual({ login: 'some-commenter' });
604604
});
605605

606606
it('merged pull request state', async () => {
@@ -623,7 +623,7 @@ describe('utils/subject.ts', () => {
623623
);
624624

625625
expect(result.state).toBe('merged');
626-
expect(result.user).toBe('some-commenter');
626+
expect(result.user).toEqual({ login: 'some-commenter' });
627627
});
628628

629629
it('open pull request state', async () => {
@@ -646,7 +646,7 @@ describe('utils/subject.ts', () => {
646646
);
647647

648648
expect(result.state).toBe('open');
649-
expect(result.user).toBe('some-commenter');
649+
expect(result.user).toEqual({ login: 'some-commenter' });
650650
});
651651

652652
it('handle pull request without latest_comment_url', async () => {
@@ -671,7 +671,7 @@ describe('utils/subject.ts', () => {
671671
);
672672

673673
expect(result.state).toBe('open');
674-
expect(result.user).toBe('some-user');
674+
expect(result.user).toEqual({ login: 'some-user' });
675675
});
676676
});
677677
});
@@ -698,7 +698,7 @@ describe('utils/subject.ts', () => {
698698
mockAccounts.token,
699699
);
700700

701-
expect(result.user).toBe('some-user');
701+
expect(result.user).toEqual({ login: 'some-user' });
702702
});
703703
});
704704

src/utils/subject.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,13 @@ async function getGitifySubjectForDiscussion(
107107
}
108108
}
109109

110-
const discussionUser = getLatestDiscussionComment(discussion.comments.nodes)
111-
?.author.login;
110+
const latestDiscussionComment = getLatestDiscussionComment(
111+
discussion.comments.nodes,
112+
);
113+
let discussionUser = null;
114+
if (latestDiscussionComment) {
115+
discussionUser = latestDiscussionComment.author;
116+
}
112117

113118
return {
114119
state: discussionState,
@@ -128,7 +133,9 @@ async function getGitifySubjectForIssue(
128133

129134
return {
130135
state: issue.state_reason ?? issue.state,
131-
user: issueCommentUser?.login ?? issue.user.login,
136+
user: {
137+
login: issueCommentUser?.login ?? issue.user.login,
138+
},
132139
};
133140
}
134141

@@ -151,7 +158,9 @@ async function getGitifySubjectForPullRequest(
151158

152159
return {
153160
state: prState,
154-
user: prCommentUser?.login ?? pr.user.login,
161+
user: {
162+
login: prCommentUser?.login ?? pr.user.login,
163+
},
155164
};
156165
}
157166

@@ -163,7 +172,9 @@ async function getGitifySubjectForRelease(
163172

164173
return {
165174
state: null,
166-
user: releaseCommentUser.login,
175+
user: {
176+
login: releaseCommentUser.login,
177+
},
167178
};
168179
}
169180

0 commit comments

Comments
 (0)