Skip to content

Commit b95c8c4

Browse files
authored
feat: improve robustness of subject user handling (#1517)
Signed-off-by: Adam Setch <[email protected]>
1 parent fa77c99 commit b95c8c4

File tree

2 files changed

+59
-24
lines changed

2 files changed

+59
-24
lines changed

src/utils/subject.test.ts

+31
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
getCheckSuiteAttributes,
1919
getGitifySubjectDetails,
2020
getLatestReviewForReviewers,
21+
getSubjectUser,
2122
getWorkflowRunAttributes,
2223
parseLinkedIssuesFromPr,
2324
} from './subject';
@@ -1332,6 +1333,36 @@ describe('utils/subject.ts', () => {
13321333
expect(result).toBeNull();
13331334
});
13341335
});
1336+
1337+
describe('getSubjectUser', () => {
1338+
it('returns null when all users are null', () => {
1339+
const result = getSubjectUser([null, null]);
1340+
1341+
expect(result).toBeNull();
1342+
});
1343+
1344+
it('returns first user', () => {
1345+
const result = getSubjectUser([mockAuthor, null]);
1346+
1347+
expect(result).toEqual({
1348+
login: mockAuthor.login,
1349+
html_url: mockAuthor.html_url,
1350+
avatar_url: mockAuthor.avatar_url,
1351+
type: mockAuthor.type,
1352+
});
1353+
});
1354+
1355+
it('returns second user if first is null', () => {
1356+
const result = getSubjectUser([null, mockAuthor]);
1357+
1358+
expect(result).toEqual({
1359+
login: mockAuthor.login,
1360+
html_url: mockAuthor.html_url,
1361+
avatar_url: mockAuthor.avatar_url,
1362+
type: mockAuthor.type,
1363+
});
1364+
});
1365+
});
13351366
});
13361367

13371368
function mockDiscussionNode(

src/utils/subject.ts

+28-24
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,7 @@ async function getGitifySubjectForCommit(
139139

140140
return {
141141
state: null,
142-
user: {
143-
login: user.login,
144-
html_url: user.html_url,
145-
avatar_url: user.avatar_url,
146-
type: user.type,
147-
},
142+
user: getSubjectUser([user]),
148143
};
149144
}
150145

@@ -230,12 +225,7 @@ async function getGitifySubjectForIssue(
230225
return {
231226
number: issue.number,
232227
state: issue.state_reason ?? issue.state,
233-
user: {
234-
login: issueCommentUser?.login ?? issue.user.login,
235-
html_url: issueCommentUser?.html_url ?? issue.user.html_url,
236-
avatar_url: issueCommentUser?.avatar_url ?? issue.user.avatar_url,
237-
type: issueCommentUser?.type ?? issue.user.type,
238-
},
228+
user: getSubjectUser([issueCommentUser, issue.user]),
239229
comments: issue.comments,
240230
labels: issue.labels?.map((label) => label.name) ?? [],
241231
milestone: issue.milestone,
@@ -277,12 +267,7 @@ async function getGitifySubjectForPullRequest(
277267
return {
278268
number: pr.number,
279269
state: prState,
280-
user: {
281-
login: prCommentUser?.login ?? pr.user.login,
282-
html_url: prCommentUser?.html_url ?? pr.user.html_url,
283-
avatar_url: prCommentUser?.avatar_url ?? pr.user.avatar_url,
284-
type: prCommentUser?.type ?? pr.user.type,
285-
},
270+
user: getSubjectUser([prCommentUser, pr.user]),
286271
reviews: reviews,
287272
comments: pr.comments,
288273
labels: pr.labels?.map((label) => label.name) ?? [],
@@ -371,12 +356,7 @@ async function getGitifySubjectForRelease(
371356

372357
return {
373358
state: null,
374-
user: {
375-
login: release.author.login,
376-
html_url: release.author.html_url,
377-
avatar_url: release.author.avatar_url,
378-
type: release.author.type,
379-
},
359+
user: getSubjectUser([release.author]),
380360
};
381361
}
382362

@@ -428,3 +408,27 @@ function getWorkflowRunStatus(statusDisplayName: string): CheckSuiteStatus {
428408
return null;
429409
}
430410
}
411+
412+
/**
413+
* Construct the notification subject user based on an order prioritized list of users
414+
* @param users array of users in order or priority
415+
* @returns the subject user
416+
*/
417+
export function getSubjectUser(users: User[]): SubjectUser {
418+
let subjectUser: SubjectUser = null;
419+
420+
for (const user of users) {
421+
if (user) {
422+
subjectUser = {
423+
login: user.login,
424+
html_url: user.html_url,
425+
avatar_url: user.avatar_url,
426+
type: user.type,
427+
};
428+
429+
return subjectUser;
430+
}
431+
}
432+
433+
return subjectUser;
434+
}

0 commit comments

Comments
 (0)