Skip to content
This repository has been archived by the owner on Jun 3, 2024. It is now read-only.

Commit

Permalink
create auxiliary updateUserToAskForReview function
Browse files Browse the repository at this point in the history
fix bug regarding mutation of userInfo
  • Loading branch information
joao-paulo-parity committed Apr 26, 2022
1 parent 9a7e9b0 commit f2a529d
Showing 1 changed file with 31 additions and 39 deletions.
70 changes: 31 additions & 39 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,35 @@ const displayUserWithTeams = (
}`
}

const updateUserToAskForReview = (
usersToAskForReview: Map<string, RuleUserInfo>,
user: string,
userInfo: RuleUserInfo,
) => {
let userToAskForReview = usersToAskForReview.get(user)
if (userToAskForReview === undefined) {
/*
Shallow-copy the userInfo so that further updates don't affect the initial
RuleUserInfo
*/
userToAskForReview = { ...userInfo }
} else if (userInfo.teams === null) {
userToAskForReview.teams = null
} else if (
/*
Avoid registering a team for this user if their approval is supposed
to be requested individually
*/
userToAskForReview.teams !== null
) {
userToAskForReview.teams = new Set([
...(userToAskForReview.teams ?? []),
...(userInfo?.teams ?? []),
])
}
usersToAskForReview.set(user, userToAskForReview)
}

const processSubconditionMissingApprovers = (
approvedBy: Set<string>,
usersToAskForReview: Map<string, RuleUserInfo>,
Expand All @@ -47,27 +76,7 @@ const processSubconditionMissingApprovers = (
if (approvedBy.has(user)) {
continue
}

let userToAskForReview = usersToAskForReview.get(user)
if (userToAskForReview === undefined) {
userToAskForReview = userInfo
} else if (userInfo.teams === null) {
userToAskForReview.teams = null
} else if (
/*
Avoid registering a team for this user if their approval is supposed
to be requested individually
*/
userToAskForReview.teams !== null
) {
userToAskForReview.teams = new Set([
...(userToAskForReview.teams ?? []),
...(userInfo?.teams ?? []),
])
}

usersToAskForReview.set(user, userToAskForReview)

updateUserToAskForReview(usersToAskForReview, user, userInfo)
missingApprovers.set(user, userInfo)
}

Expand Down Expand Up @@ -909,24 +918,7 @@ export const runChecks = async ({ pr, ...ctx }: Context & { pr: PR }) => {
problems.push(outcome.problem)

for (const [user, userInfo] of outcome.usersToAskForReview) {
let userToAskForReview = usersToAskForReview.get(user)
if (userToAskForReview === undefined) {
userToAskForReview = userInfo
} else if (userInfo.teams === null) {
userToAskForReview.teams = null
} else if (
/*
Avoid registering a team for this user if their approval is supposed
to be requested individually
*/
userToAskForReview.teams !== null
) {
userToAskForReview.teams = new Set([
...(userToAskForReview.teams ?? []),
...(userInfo?.teams ?? []),
])
}
usersToAskForReview.set(user, userToAskForReview)
updateUserToAskForReview(usersToAskForReview, user, userInfo)
}
}

Expand Down

0 comments on commit f2a529d

Please sign in to comment.