Skip to content

Commit 1cc2e89

Browse files
authored
feat(subject): support enriched commit notification details (#1017)
1 parent 5c1971d commit 1cc2e89

File tree

3 files changed

+157
-0
lines changed

3 files changed

+157
-0
lines changed

src/typesGithub.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,76 @@ export interface PullRequest {
272272
changed_files: number;
273273
}
274274

275+
export interface Commit {
276+
sha: string;
277+
node_id: string;
278+
commit: {
279+
author: CommitUser;
280+
committer: CommitUser;
281+
message: string;
282+
tree: {
283+
sha: string;
284+
url: string;
285+
};
286+
url: string;
287+
comment_count: number;
288+
verification: {
289+
verified: boolean;
290+
reason: string;
291+
signature: string | null;
292+
payload: string | null;
293+
};
294+
};
295+
url: string;
296+
html_url: string;
297+
comments_url: string;
298+
author: User;
299+
committer: User;
300+
parents: CommitParent[];
301+
stats: {
302+
total: number;
303+
additions: number;
304+
deletions: number;
305+
};
306+
files: CommitFiles[];
307+
}
308+
309+
interface CommitUser {
310+
name: string;
311+
email: string;
312+
date: string;
313+
}
314+
315+
interface CommitParent {
316+
sha: string;
317+
url: string;
318+
html_url: string;
319+
}
320+
321+
interface CommitFiles {
322+
sha: string;
323+
filename: string;
324+
status: string;
325+
additions: number;
326+
deletions: number;
327+
changes: number;
328+
blob_url: string;
329+
raw_url: string;
330+
contents_url: string;
331+
patch: string;
332+
}
333+
export interface CommitComments {
334+
url: string;
335+
html_url: string;
336+
issue_url: string;
337+
id: number;
338+
node_id: string;
339+
user: User;
340+
created_at: string;
341+
updated_at: string;
342+
body: string;
343+
}
344+
275345
export interface Issue {
276346
url: string;
277347
repository_url: string;

src/utils/subject.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,65 @@ describe('utils/subject.ts', () => {
138138
});
139139

140140
describe('getGitifySubjectDetails', () => {
141+
describe('Commits', () => {
142+
it('get commit commenter', async () => {
143+
const mockNotification = {
144+
...mockedSingleNotification,
145+
subject: {
146+
title: 'This is a commit with comments',
147+
url: 'https://api.github.com/repos/manosim/notifications-test/commits/d2a86d80e3d24ea9510d5de6c147e53c30f313a8',
148+
latest_comment_url:
149+
'https://api.github.com/repos/manosim/notifications-test/comments/141012658',
150+
type: 'Commit' as SubjectType,
151+
},
152+
};
153+
154+
nock('https://api.github.com')
155+
.get(
156+
'/repos/manosim/notifications-test/commits/d2a86d80e3d24ea9510d5de6c147e53c30f313a8',
157+
)
158+
.reply(200, { author: { login: 'some-author' } });
159+
160+
nock('https://api.github.com')
161+
.get('/repos/manosim/notifications-test/comments/141012658')
162+
.reply(200, { user: { login: 'some-commenter' } });
163+
164+
const result = await getGitifySubjectDetails(
165+
mockNotification,
166+
mockAccounts.token,
167+
);
168+
169+
expect(result.state).toBeNull();
170+
expect(result.user).toEqual({ login: 'some-commenter' });
171+
});
172+
173+
it('get commit without commenter', async () => {
174+
const mockNotification = {
175+
...mockedSingleNotification,
176+
subject: {
177+
title: 'This is a commit with comments',
178+
url: 'https://api.github.com/repos/manosim/notifications-test/commits/d2a86d80e3d24ea9510d5de6c147e53c30f313a8',
179+
latest_comment_url: null,
180+
type: 'Commit' as SubjectType,
181+
},
182+
};
183+
184+
nock('https://api.github.com')
185+
.get(
186+
'/repos/manosim/notifications-test/commits/d2a86d80e3d24ea9510d5de6c147e53c30f313a8',
187+
)
188+
.reply(200, { author: { login: 'some-author' } });
189+
190+
const result = await getGitifySubjectDetails(
191+
mockNotification,
192+
mockAccounts.token,
193+
);
194+
195+
expect(result.state).toBeNull();
196+
expect(result.user).toEqual({ login: 'some-author' });
197+
});
198+
});
199+
141200
describe('Discussions', () => {
142201
it('answered discussion state', async () => {
143202
const mockNotification = {

src/utils/subject.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type {
22
CheckSuiteAttributes,
33
CheckSuiteStatus,
4+
Commit,
45
DiscussionStateType,
56
GitifySubject,
67
Issue,
@@ -23,6 +24,8 @@ export async function getGitifySubjectDetails(
2324
switch (notification.subject.type) {
2425
case 'CheckSuite':
2526
return getGitifySubjectForCheckSuite(notification);
27+
case 'Commit':
28+
return getGitifySubjectForCommit(notification, token);
2629
case 'Discussion':
2730
return await getGitifySubjectForDiscussion(notification, token);
2831
case 'Issue':
@@ -91,6 +94,31 @@ function getGitifySubjectForCheckSuite(
9194
};
9295
}
9396

97+
async function getGitifySubjectForCommit(
98+
notification: Notification,
99+
token: string,
100+
): Promise<GitifySubject> {
101+
try {
102+
const commit: Commit = (
103+
await apiRequestAuth(notification.subject.url, 'GET', token)
104+
).data;
105+
106+
const commitCommentUser = await getLatestCommentUser(notification, token);
107+
108+
return {
109+
state: null,
110+
user: {
111+
login: commitCommentUser?.login ?? commit.author.login,
112+
html_url: commitCommentUser?.html_url ?? commit.author.html_url,
113+
avatar_url: commitCommentUser?.avatar_url ?? commit.author.avatar_url,
114+
type: commitCommentUser?.type ?? commit.author.type,
115+
},
116+
};
117+
} catch (err) {
118+
console.error('Issue subject retrieval failed');
119+
}
120+
}
121+
94122
async function getGitifySubjectForDiscussion(
95123
notification: Notification,
96124
token: string,

0 commit comments

Comments
 (0)