Skip to main content
Example scripts
arrow icon
To homepage
Bitbucket
Data centre icon
Data Center

Selectively Withdraw Approvals When a Pull Request Changes

Features
Listeners
Tags
Created 1 year ago, Updated 6 month(s) ago
App in script
ScriptRunner For Bitbucket
ScriptRunner For Bitbucket
by Adaptavist
Compatibility
compatibility bullet
Bitbucket (6.0 - 7.17)
compatibility bullet
ScriptRunner For Bitbucket (7.10.0)
Language |
groovy
import com.atlassian.bitbucket.event.pull.PullRequestRescopedEvent
import com.atlassian.bitbucket.pull.PullRequest
import com.atlassian.bitbucket.pull.PullRequestParticipant
import com.atlassian.bitbucket.pull.PullRequestParticipantStatusRequest
import com.atlassian.bitbucket.pull.PullRequestService
import com.atlassian.bitbucket.user.ApplicationUser
import com.atlassian.bitbucket.user.SecurityService
import com.atlassian.sal.api.component.ComponentLocator
import com.onresolve.scriptrunner.parameters.annotation.UserPicker

import static com.atlassian.bitbucket.pull.PullRequestParticipantStatus.UNAPPROVED

@UserPicker( label = 'Users', description = 'Users whose approval to withdraw', multiple = true )
List<ApplicationUser> reviewersWithdrawingApprovals

def securityService = ComponentLocator.getComponent(SecurityService)
def pullRequestService = ComponentLocator.getComponent(PullRequestService)
def currentEvent = event as PullRequestRescopedEvent
def pullRequest = currentEvent.pullRequest as PullRequest

if (currentEvent.previousFromHash != pullRequest.fromRef.latestCommit) {
    pullRequest.reviewers.find {
        PullRequestParticipant reviewer -> reviewer.approved && reviewer.user in reviewersWithdrawingApprovals
    }.each {
        PullRequestParticipant reviewer ->
            securityService.impersonating(reviewer.user, "Unapproving pull-request on behalf of user").call {

                pullRequestService.setReviewerStatus(
                    new PullRequestParticipantStatusRequest.Builder(pullRequest)
                        .status(UNAPPROVED)
                        .build()
                )
            }
    }
}
Having an issue with this script?
Report it here