You can now vote for scripts to help people know if they're useful or not. Login or create an account to vote!

1

Selectively Withdraw Approvals When a Pull Request Changes

Overview

Selectively remove approvals from a pull request when changes have been made.

Example

I have a pull request open for a new feature with members of the development team and members of the documentation team set as reviewers. Several of my reviewers from the development team have approved already, but I need to address some comments resulting in code changes. I want to automatically withdraw approvals from members of the development team when there are changes pushed to a pull request. This is to ensure the code is thoroughly checked. Approvals will remain for any reviewers not in the development team.

Good to Know

  • This script must be configured as a Custom listener for the PullRequestRescopedEvent event by someone with at least global administrator permission.
  • Use the REVIEWERS_WHICH_ALWAYS_WITHDRAW_APPROVAL string collection to add the users name for which approval will be withdrawn after each change

Requirements

  • Bitbucket Bitbucket (6.3 - 7.12)
  • ScriptRunner for Bitbucket ScriptRunner for Bitbucket (6.11.0)
    
import com.atlassian.bitbucket.event.pull.PullRequestRescopedEvent import com.atlassian.bitbucket.pull.PullRequest import com.atlassian.bitbucket.pull.PullRequestParticipant import com.atlassian.bitbucket.pull.PullRequestService import com.atlassian.bitbucket.user.SecurityService import com.atlassian.sal.api.component.ComponentLocator import static com.atlassian.bitbucket.pull.PullRequestParticipantStatus.UNAPPROVED def final REVIEWERS_WHICH_ALWAYS_WITHDRAW_APPROVAL = ['user-name'] 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.name in REVIEWERS_WHICH_ALWAYS_WITHDRAW_APPROVAL }.each { PullRequestParticipant reviewer -> securityService.impersonating(reviewer.user, "Unapproving pull-request on behalf of user").call { pullRequestService.setReviewerStatus( pullRequest.fromRef.repository.id, pullRequest.id, UNAPPROVED ) } } }
Discovered an issue? Report it here

Suggested for you