fix: [code-1519]: fix issue on approval btn (#1074)

This commit is contained in:
Calvin Lee 2024-02-27 22:46:05 +00:00 committed by Harness
parent 0f888bf20f
commit 7cd975c370

View File

@ -23,6 +23,7 @@ import { Menu, PopoverPosition } from '@blueprintjs/core'
import cx from 'classnames'
import { Render } from 'react-jsx-match'
import { isEmpty } from 'lodash-es'
import { useAppContext } from 'AppContext'
import { useStrings } from 'framework/strings'
import type { EnumPullReqReviewDecision, TypesPullReq } from 'services/code'
import type { GitInfoProps } from 'utils/GitUtils'
@ -55,6 +56,7 @@ const ReviewSplitButton = (props: ReviewSplitButtonProps) => {
const { refetchReviewers, pullRequestMetadata, repoMetadata, shouldHide, refreshPr, disabled } = props
const { getString } = useStrings()
const { showError, showSuccess } = useToaster()
const { currentUser } = useAppContext()
const {
data: reviewers,
@ -65,33 +67,33 @@ const ReviewSplitButton = (props: ReviewSplitButtonProps) => {
})
const determineOverallDecision = (data: any[] | null) => {
let hasChangeReq = false
let allApproved = true
if (data === null || isEmpty(data)) {
return ApproveState.APPROVE // Default case
}
for (const item of data) {
if (item.review_decision === ApproveState.CHANGEREQ) {
hasChangeReq = true
break
} else if (item.review_decision !== ApproveState.APPROVED) {
allApproved = false
}
// Check if the current user is among the reviewers
const currentUserReviews = data.filter(val => val.reviewer.uid === currentUser.uid)
if (currentUserReviews.length === 0) {
// Current user not found among reviewers, return default approval state
return ApproveState.APPROVE
}
if (hasChangeReq) {
// Directly return based on the review decision of the current user
const decision = currentUserReviews[0].review_decision
if (decision === ApproveState.CHANGEREQ) {
return ApproveState.CHANGEREQ
} else if (allApproved) {
} else if (decision === ApproveState.APPROVED) {
return ApproveState.APPROVED
} else {
return ApproveState.APPROVE // Default case
return ApproveState.APPROVE // Default case or any other state not explicitly handled
}
}
const [commitSha, setCommitSha] = useState('')
useEffect(() => {
if (reviewers) {
if (reviewers[0] && reviewers[0].sha) {
setCommitSha(reviewers[0].sha)
const currentUserData = reviewers.filter(val => val.reviewer.uid === currentUser.uid)
if (currentUserData[0] && currentUserData[0].sha) {
setCommitSha(currentUserData[0].sha)
}
setApproveState(determineOverallDecision(reviewers))
}
@ -141,7 +143,6 @@ const ReviewSplitButton = (props: ReviewSplitButtonProps) => {
const getApprovalState = (state: string) => {
const checkOutdated = processReviewDecision(approveState, commitSha, pullRequestMetadata?.source_sha)
if (
(state === ApproveState.APPROVED && checkOutdated === ApproveState.OUTDATED) ||
(state === ApproveState.CHANGEREQ && checkOutdated === ApproveState.OUTDATED)
@ -177,7 +178,6 @@ const ReviewSplitButton = (props: ReviewSplitButtonProps) => {
}
return false
}
return (
<Container
className={cx(css.reviewButton, {