Skip to content

Commit 3019a49

Browse files
committed
refactor: use octokit client
Signed-off-by: Adam Setch <adam.setch@outlook.com>
1 parent cc915ac commit 3019a49

6 files changed

Lines changed: 71 additions & 107 deletions

File tree

src/renderer/hooks/useNotifications.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,9 @@ export const useNotifications = (): NotificationsState => {
143143
try {
144144
await Promise.all(
145145
readNotifications.map((notification) =>
146-
markNotificationThreadAsRead(
146+
markNotificationThreadAsRead(notification.account,
147147
notification.id,
148-
notification.account.hostname,
149-
notification.account.token,
148+
150149
),
151150
),
152151
);
@@ -183,10 +182,9 @@ export const useNotifications = (): NotificationsState => {
183182
try {
184183
await Promise.all(
185184
doneNotifications.map((notification) =>
186-
markNotificationThreadAsDone(
185+
markNotificationThreadAsDone(notification.account,
187186
notification.id,
188-
notification.account.hostname,
189-
notification.account.token,
187+
190188
),
191189
),
192190
);
@@ -218,9 +216,9 @@ export const useNotifications = (): NotificationsState => {
218216

219217
try {
220218
await ignoreNotificationThreadSubscription(
219+
notification.account,
221220
notification.id,
222-
notification.account.hostname,
223-
notification.account.token,
221+
224222
);
225223

226224
if (state.settings.markAsDoneOnUnsubscribe) {

src/renderer/utils/api/client.test.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { mockToken } from '../../__mocks__/state-mocks';
77

88
import { Constants } from '../../constants';
99

10-
import type { Hostname, Link, SettingsState, Token } from '../../types';
10+
import type { Hostname, Link, SettingsState } from '../../types';
1111
import type { GitHubGraphQLResponse } from './graphql/types';
1212

1313
import {
@@ -192,10 +192,9 @@ describe('renderer/utils/api/client.ts', () => {
192192
});
193193

194194
it('markNotificationThreadAsRead - should mark notification thread as read', async () => {
195-
await markNotificationThreadAsRead(
195+
await markNotificationThreadAsRead(mockGitHubCloudAccount,
196196
mockThreadId,
197-
mockGitHubHostname,
198-
mockToken,
197+
199198
);
200199

201200
expect(performAuthenticatedRESTRequestSpy).toHaveBeenCalledWith(
@@ -207,10 +206,9 @@ describe('renderer/utils/api/client.ts', () => {
207206
});
208207

209208
it('markNotificationThreadAsDone - should mark notification thread as done', async () => {
210-
await markNotificationThreadAsDone(
209+
await markNotificationThreadAsDone(mockGitHubCloudAccount,
211210
mockThreadId,
212-
mockGitHubHostname,
213-
mockToken,
211+
214212
);
215213

216214
expect(performAuthenticatedRESTRequestSpy).toHaveBeenCalledWith(
@@ -222,10 +220,9 @@ describe('renderer/utils/api/client.ts', () => {
222220
});
223221

224222
it('ignoreNotificationThreadSubscription - should ignore notification thread subscription', async () => {
225-
await ignoreNotificationThreadSubscription(
223+
await ignoreNotificationThreadSubscription(mockGitHubCloudAccount,
226224
mockThreadId,
227-
mockGitHubHostname,
228-
mockToken,
225+
229226
);
230227

231228
expect(performAuthenticatedRESTRequestSpy).toHaveBeenCalledWith(
@@ -238,8 +235,9 @@ describe('renderer/utils/api/client.ts', () => {
238235

239236
it('getHtmlUrl - should return the HTML URL', async () => {
240237
await getHtmlUrl(
238+
mockGitHubCloudAccount,
241239
'https://api.github.com/repos/gitify-app/notifications-test/issues/785' as Link,
242-
'123' as Token,
240+
243241
);
244242

245243
expect(performAuthenticatedRESTRequestSpy).toHaveBeenCalledWith(

src/renderer/utils/api/client.ts

Lines changed: 44 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,10 @@ export async function listNotificationsForAuthenticatedUser(
102102
* Endpoint documentation: https://docs.github.com/en/rest/activity/notifications#mark-a-thread-as-read
103103
*/
104104
export async function markNotificationThreadAsRead(
105+
account: Account,
105106
threadId: string,
106-
hostname: Hostname,
107-
token: Token,
108107
): Promise<MarkNotificationThreadAsReadResponse> {
109-
const octokit = await createOctokitClient(hostname, token);
108+
const octokit = await createOctokitClient(account.hostname, account.token);
110109

111110
const response = await octokit.rest.activity.markThreadAsRead({
112111
thread_id: Number(threadId),
@@ -124,11 +123,10 @@ export async function markNotificationThreadAsRead(
124123
* Endpoint documentation: https://docs.github.com/en/rest/activity/notifications#mark-a-thread-as-done
125124
*/
126125
export async function markNotificationThreadAsDone(
126+
account: Account,
127127
threadId: string,
128-
hostname: Hostname,
129-
token: Token,
130128
): Promise<MarkNotificationThreadAsDoneResponse> {
131-
const octokit = await createOctokitClient(hostname, token);
129+
const octokit = await createOctokitClient(account.hostname, account.token);
132130

133131
const response = await octokit.rest.activity.markThreadAsDone({
134132
thread_id: Number(threadId),
@@ -143,11 +141,10 @@ export async function markNotificationThreadAsDone(
143141
* Endpoint documentation: https://docs.github.com/en/rest/activity/notifications#delete-a-thread-subscription
144142
*/
145143
export async function ignoreNotificationThreadSubscription(
144+
account: Account,
146145
threadId: string,
147-
hostname: Hostname,
148-
token: Token,
149146
): Promise<IgnoreNotificationThreadSubscriptionResponse> {
150-
const octokit = await createOctokitClient(hostname, token);
147+
const octokit = await createOctokitClient(account.hostname, account.token);
151148

152149
const response = await octokit.rest.activity.setThreadSubscription({
153150
thread_id: Number(threadId),
@@ -163,31 +160,17 @@ export async function ignoreNotificationThreadSubscription(
163160
* Endpoint documentation: https://docs.github.com/en/rest/commits/commits#get-a-commit
164161
*/
165162
export async function getCommit(
163+
account: Account,
166164
url: Link,
167-
token: Token,
168165
): Promise<GetCommitResponse> {
169-
// Parse URL: /repos/{owner}/{repo}/commits/{ref}
170-
const urlObj = new URL(url);
171-
const match = urlObj.pathname.match(
172-
/^\/repos\/(?<owner>[^/]+)\/(?<repo>[^/]+)\/commits\/(?<ref>.+)$/,
173-
);
166+
const octokit = await createOctokitClient(account.hostname, account.token);
174167

175-
if (!match?.groups) {
176-
throw new Error(`Invalid commit URL format: ${url}`);
177-
}
178-
179-
const { owner, repo, ref } = match.groups;
180-
const hostname = urlObj.hostname as Hostname;
181-
182-
const octokit = await createOctokitClient(hostname, token);
183-
184-
const response = await octokit.rest.repos.getCommit({
185-
owner,
186-
repo,
187-
ref,
168+
// Perform a generic GET request using Octokit's request method
169+
const response = await octokit.request('GET {+url}', {
170+
url: url,
188171
});
189172

190-
return response.data;
173+
return response.data as GetCommitResponse;
191174
}
192175

193176
/**
@@ -196,31 +179,17 @@ export async function getCommit(
196179
* Endpoint documentation: https://docs.github.com/en/rest/commits/comments#get-a-commit-comment
197180
*/
198181
export async function getCommitComment(
182+
account: Account,
199183
url: Link,
200-
token: Token,
201184
): Promise<GetCommitCommentResponse> {
202-
// Parse URL: /repos/{owner}/{repo}/comments/{comment_id}
203-
const urlObj = new URL(url);
204-
const match = urlObj.pathname.match(
205-
/^\/repos\/(?<owner>[^/]+)\/(?<repo>[^/]+)\/comments\/(?<commentId>\d+)$/,
206-
);
207-
208-
if (!match?.groups) {
209-
throw new Error(`Invalid commit comment URL format: ${url}`);
210-
}
185+
const octokit = await createOctokitClient(account.hostname, account.token);
211186

212-
const { owner, repo, commentId } = match.groups;
213-
const hostname = urlObj.hostname as Hostname;
214-
215-
const octokit = await createOctokitClient(hostname, token);
216-
217-
const response = await octokit.rest.repos.getCommitComment({
218-
owner: owner,
219-
repo: repo,
220-
comment_id: Number(commentId),
187+
// Perform a generic GET request using Octokit's request method
188+
const response = await octokit.request('GET {+url}', {
189+
url: url,
221190
});
222191

223-
return response.data;
192+
return response.data as GetCommitCommentResponse;
224193
}
225194

226195
/**
@@ -229,45 +198,27 @@ export async function getCommitComment(
229198
* Endpoint documentation: https://docs.github.com/en/rest/releases/releases#get-a-release
230199
*/
231200
export async function getRelease(
201+
account: Account,
232202
url: Link,
233-
token: Token,
234203
): Promise<GetReleaseResponse> {
235-
// Parse URL: /repos/{owner}/{repo}/releases/{release_id}
236-
const urlObj = new URL(url);
237-
const match = urlObj.pathname.match(
238-
/^\/repos\/(?<owner>[^/]+)\/(?<repo>[^/]+)\/releases\/(?<releaseId>\d+)$/,
239-
);
240-
241-
if (!match?.groups) {
242-
throw new Error(`Invalid release URL format: ${url}`);
243-
}
244-
245-
const { owner, repo, releaseId } = match.groups;
246-
const hostname = urlObj.hostname as Hostname;
247-
248-
const octokit = await createOctokitClient(hostname, token);
204+
const octokit = await createOctokitClient(account.hostname, account.token);
249205

250-
const response = await octokit.rest.repos.getRelease({
251-
owner: owner,
252-
repo: repo,
253-
release_id: Number(releaseId),
206+
// Perform a generic GET request using Octokit's request method
207+
const response = await octokit.request('GET {+url}', {
208+
url: url,
254209
});
255210

256-
return response.data;
211+
return response.data as GetReleaseResponse;
257212
}
258213

259214
/**
260215
* Get the `html_url` from the GitHub response
261216
*/
262217
export async function getHtmlUrl(
218+
account: Account,
263219
url: Link,
264-
token: Token,
265220
): Promise<GitHubHtmlUrlResponse> {
266-
// Extract hostname from URL to determine which Octokit client to use
267-
const urlObj = new URL(url);
268-
const hostname = urlObj.hostname as Hostname;
269-
270-
const octokit = await createOctokitClient(hostname, token);
221+
const octokit = await createOctokitClient(account.hostname, account.token);
271222

272223
// Perform a generic GET request using Octokit's request method
273224
const response = await octokit.request('GET {+url}', {
@@ -277,6 +228,24 @@ export async function getHtmlUrl(
277228
return response.data as GitHubHtmlUrlResponse;
278229
}
279230

231+
232+
/**
233+
* Follow GitHub Response URL
234+
*/
235+
export async function followUrl<TResult>(
236+
account: Account,
237+
url: Link,
238+
): Promise<TResult> {
239+
const octokit = await createOctokitClient(account.hostname, account.token);
240+
241+
// Perform a generic GET request using Octokit's request method
242+
const response = await octokit.request('GET {+url}', {
243+
url: url,
244+
});
245+
246+
return response.data as TResult;
247+
}
248+
280249
/**
281250
* Fetch details of the currently authenticated GitHub user.
282251
*/

src/renderer/utils/helpers.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,15 @@ export async function generateGitHubWebUrl(
6262
} else {
6363
try {
6464
if (notification.subject.latestCommentUrl) {
65-
const response = await getHtmlUrl(
65+
const response = await getHtmlUrl(notification.account,
6666
notification.subject.latestCommentUrl,
67-
notification.account.token,
6867
);
6968

7069
url.href = response.html_url;
7170
} else if (notification.subject.url) {
72-
const response = await getHtmlUrl(
71+
const response = await getHtmlUrl(notification.account,
7372
notification.subject.url,
74-
notification.account.token,
73+
7574
);
7675

7776
url.href = response.html_url;

src/renderer/utils/notifications/handlers/commit.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ import type {
1212
SettingsState,
1313
} from '../../../types';
1414

15-
import { getCommit, getCommitComment } from '../../api/client';
15+
import { followUrl } from '../../api/client';
1616
import { isStateFilteredOut } from '../filters/filter';
1717
import { DefaultHandler } from './default';
1818
import { getNotificationAuthor } from './utils';
19+
import { GetCommitCommentResponse, GetCommitResponse } from '../../api/types';
1920

2021
class CommitHandler extends DefaultHandler {
2122
readonly type = 'Commit';
@@ -35,9 +36,8 @@ class CommitHandler extends DefaultHandler {
3536

3637
if (notification.subject.latestCommentUrl) {
3738
const commitComment = (
38-
await getCommitComment(
39+
await followUrl<GetCommitCommentResponse>(notification.account,
3940
notification.subject.latestCommentUrl,
40-
notification.account.token,
4141
)
4242
);
4343

@@ -48,10 +48,9 @@ class CommitHandler extends DefaultHandler {
4848
type: commitComment.user.type as GitifyNotificationUser['type'],
4949
};
5050
} else {
51-
const commit = await getCommit(
52-
notification.subject.url,
53-
notification.account.token,
54-
);
51+
52+
53+
const commit = await followUrl<GetCommitResponse>(notification.account, notification.subject.url)
5554

5655
user = {
5756
login: commit.author.login,

src/renderer/utils/notifications/handlers/release.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@ import type {
1313
UserType,
1414
} from '../../../types';
1515

16-
import { getRelease } from '../../api/client';
16+
import { followUrl } from '../../api/client';
1717
import { isStateFilteredOut } from '../filters/filter';
1818
import { DefaultHandler, defaultHandler } from './default';
1919
import { getNotificationAuthor } from './utils';
20+
import { GetReleaseResponse } from '../../api/types';
2021

2122
class ReleaseHandler extends DefaultHandler {
2223
readonly type = 'Release';
@@ -33,7 +34,7 @@ class ReleaseHandler extends DefaultHandler {
3334
}
3435

3536
const release = (
36-
await getRelease(notification.subject.url, notification.account.token)
37+
await followUrl<GetReleaseResponse>(notification.account, notification.subject.url)
3738
);
3839

3940
const user: GitifyNotificationUser = release.author

0 commit comments

Comments
 (0)