-
-
Save antonydevanchi/fdedda28b07a03dbfa3ee11d272c1743 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.onresolve.base.test.rest.jstestutils | |
import com.atlassian.bitbucket.auth.AuthenticationContext | |
import com.atlassian.bitbucket.permission.Permission | |
import com.atlassian.bitbucket.permission.PermissionService | |
import com.atlassian.bitbucket.project.ProjectService | |
import com.atlassian.bitbucket.repository.RepositoryService | |
import com.atlassian.bitbucket.repository.RepositoryUpdateRequest | |
import com.atlassian.sal.api.component.ComponentLocator | |
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate | |
import groovy.json.JsonSlurper | |
import groovy.transform.BaseScript | |
import javax.ws.rs.core.MultivaluedMap | |
import javax.ws.rs.core.Response | |
@BaseScript CustomEndpointDelegate delegate | |
def authenticationContext = ComponentLocator.getComponent(AuthenticationContext) | |
def repositoryService = ComponentLocator.getComponent(RepositoryService) | |
def permissionService = ComponentLocator.getComponent(PermissionService) | |
moveRepo(httpMethod: "POST") { MultivaluedMap queryParams, String body -> | |
def params = new JsonSlurper().parseText(body) | |
def repoId = params["id"] | |
def sourceRepo = repositoryService.getById(repoId as Integer) | |
def currentUser = authenticationContext.getCurrentUser() | |
if (permissionService.hasRepositoryPermission(currentUser, sourceRepo, Permission.REPO_ADMIN)) { | |
def projectService = ComponentLocator.getComponent(ProjectService) | |
def archiveProject = projectService.getByKey("ARCHIVE") | |
assert archiveProject // require archive project to be present | |
def builder = new RepositoryUpdateRequest.Builder(sourceRepo).project(archiveProject).name(sourceRepo.name + "_" + sourceRepo.id) | |
repositoryService.update(builder.build()) | |
// if you want you can redirect to the new repo location instead | |
Response.noContent().build() | |
} | |
else { | |
Response.status(Response.Status.FORBIDDEN).build() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment