Skip to content

Instantly share code, notes, and snippets.

@jechlin
Created May 20, 2016 12:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jechlin/58532e84618b3c7cbf3e474fd46591ad to your computer and use it in GitHub Desktop.
Save jechlin/58532e84618b3c7cbf3e474fd46591ad 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