Hi Everyone can any one help me In this ... How to create a branch automatically in Bitbucket when an issue in jira is transitioned from one status to another status . Can any one please help me on this thanks in advance
Yes, you certainly can. You would need ScriptRunner in your Jira instance.
There are two ways to do it. Basically you make a POST call using the following Rest API to create a branch in a workflow post function from Jira to BitBucket:
1. Setup the app links as mentioned in this documentation.
2. Following snippet works as I have tested:
import com.atlassian.applinks.api.ApplicationLinkResponseHandlerimport com.atlassian.applinks.api.ApplicationLinkResponseHandlerimport com.atlassian.applinks.api.ApplicationLinkServiceimport com.atlassian.applinks.api.application.bitbucket.BitbucketApplicationTypeimport com.atlassian.sal.api.component.ComponentLocatorimport groovy.json.JsonSlurperimport com.atlassian.sal.api.net.Requestimport com.atlassian.sal.api.net.Responseimport com.atlassian.sal.api.net.ResponseExceptionimport com.atlassian.sal.api.net.ResponseHandlerimport groovy.json.JsonBuilderdef appLinkService = ComponentLocator.getComponent(ApplicationLinkService)def appLink = appLinkService.getPrimaryApplicationLink(BitbucketApplicationType)def applicationLinkRequestFactory = appLink.createAuthenticatedRequestFactory()Map body = [ name: "test-branch", startPoint: "refs/heads/master",]def request = applicationLinkRequestFactory .createRequest(Request.MethodType.POST, "/rest/branch-utils/1.0/projects/JP/repos/just-repository/branches") .addHeader("Content-Type", "application/json") .setRequestBody(new JsonBuilder(body).toString()) .execute(new ApplicationLinkResponseHandler<Map>() {@Override Map credentialsRequired(Response response) throws ResponseException { return null }@Override Map handle(Response response) throws ResponseException { new JsonSlurper().parseText(response.getResponseBodyAsString()) as Map } })
You can use this sample script from our library as reference to make the same POST call:
import groovy.json.JsonOutputimport groovy.json.JsonBuilderimport groovyx.net.http.ContentTypeimport groovyx.net.http.HttpResponseDecoratorimport groovyx.net.http.RESTClientfinal externalUrl = "http://<BitBucket_Base_URL>/"def body = [ name: "test-branch", startPoint: "refs/heads/master",]def postResponse = post(externalUrl, "/rest/branch-utils/1.0/projects/JP/repos/just-repository/branches", new JsonBuilder(body).toString())def post(def hostUrl, def endpointAndQuery, def bodyJson) { def client = new RESTClient(hostUrl) client.setHeaders([ 'Accept' : ContentType.JSON, 'Authorization': "Basic ${'xxxx:xxxxxxxx'.bytes.encodeBase64()}" ]) client.handler.success = { HttpResponseDecorator response, json -> json } client.handler.failure = { HttpResponseDecorator response -> // Failure can be handled here log.error response.entity.content.text [:] } client.post( path: endpointAndQuery, contentType: ContentType.JSON, body: bodyJson )}
Please note that this will hardcode a username and password in your script.
Deleting a branch is just similar using a DELETE call on the same REST API.
I hope this helps!
It looks like you're new here. Sign in or register to get started.