Skip to content

Instantly share code, notes, and snippets.

@antonydevanchi
Forked from jechlin/MoveRepository.groovy
Created May 27, 2018 05:25
Show Gist options
  • Save antonydevanchi/fdedda28b07a03dbfa3ee11d272c1743 to your computer and use it in GitHub Desktop.
Save antonydevanchi/fdedda28b07a03dbfa3ee11d272c1743 to your computer and use it in GitHub Desktop.
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