Skip to content

feat: improve robustness of subject user handling #1517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/utils/subject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
getCheckSuiteAttributes,
getGitifySubjectDetails,
getLatestReviewForReviewers,
getSubjectUser,
getWorkflowRunAttributes,
parseLinkedIssuesFromPr,
} from './subject';
Expand Down Expand Up @@ -1332,6 +1333,36 @@ describe('utils/subject.ts', () => {
expect(result).toBeNull();
});
});

describe('getSubjectUser', () => {
it('returns null when all users are null', () => {
const result = getSubjectUser([null, null]);

expect(result).toBeNull();
});

it('returns first user', () => {
const result = getSubjectUser([mockAuthor, null]);

expect(result).toEqual({
login: mockAuthor.login,
html_url: mockAuthor.html_url,
avatar_url: mockAuthor.avatar_url,
type: mockAuthor.type,
});
});

it('returns second user if first is null', () => {
const result = getSubjectUser([null, mockAuthor]);

expect(result).toEqual({
login: mockAuthor.login,
html_url: mockAuthor.html_url,
avatar_url: mockAuthor.avatar_url,
type: mockAuthor.type,
});
});
});
});

function mockDiscussionNode(
Expand Down
52 changes: 28 additions & 24 deletions src/utils/subject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,7 @@ async function getGitifySubjectForCommit(

return {
state: null,
user: {
login: user.login,
html_url: user.html_url,
avatar_url: user.avatar_url,
type: user.type,
},
user: getSubjectUser([user]),
};
}

Expand Down Expand Up @@ -230,12 +225,7 @@ async function getGitifySubjectForIssue(
return {
number: issue.number,
state: issue.state_reason ?? issue.state,
user: {
login: issueCommentUser?.login ?? issue.user.login,
html_url: issueCommentUser?.html_url ?? issue.user.html_url,
avatar_url: issueCommentUser?.avatar_url ?? issue.user.avatar_url,
type: issueCommentUser?.type ?? issue.user.type,
},
user: getSubjectUser([issueCommentUser, issue.user]),
comments: issue.comments,
labels: issue.labels?.map((label) => label.name) ?? [],
milestone: issue.milestone,
Expand Down Expand Up @@ -277,12 +267,7 @@ async function getGitifySubjectForPullRequest(
return {
number: pr.number,
state: prState,
user: {
login: prCommentUser?.login ?? pr.user.login,
html_url: prCommentUser?.html_url ?? pr.user.html_url,
avatar_url: prCommentUser?.avatar_url ?? pr.user.avatar_url,
type: prCommentUser?.type ?? pr.user.type,
},
user: getSubjectUser([prCommentUser, pr.user]),
reviews: reviews,
comments: pr.comments,
labels: pr.labels?.map((label) => label.name) ?? [],
Expand Down Expand Up @@ -371,12 +356,7 @@ async function getGitifySubjectForRelease(

return {
state: null,
user: {
login: release.author.login,
html_url: release.author.html_url,
avatar_url: release.author.avatar_url,
type: release.author.type,
},
user: getSubjectUser([release.author]),
};
}

Expand Down Expand Up @@ -428,3 +408,27 @@ function getWorkflowRunStatus(statusDisplayName: string): CheckSuiteStatus {
return null;
}
}

/**
* Construct the notification subject user based on an order prioritized list of users
* @param users array of users in order or priority
* @returns the subject user
*/
export function getSubjectUser(users: User[]): SubjectUser {
let subjectUser: SubjectUser = null;

for (const user of users) {
if (user) {
subjectUser = {
login: user.login,
html_url: user.html_url,
avatar_url: user.avatar_url,
type: user.type,
};

return subjectUser;
}
}

return subjectUser;
}