title: groovy script to get private Bitbucket Branches by making an api call
body: <p>I want to show all the branch names dynamically using a groovy script by making an api call to bitbucket private repo. I am following this article <a href="https://mgrebenets.github.io/mobile%20ci/2015/02/08/bitbucket-branches-job-dsl" rel="nofollow noreferrer">link</a>.In this article top is using https , instead I like to use ssh.</p>
  
  <pre><code>String baseUrl = "https://bitbucket.org/api"
  String version = "1.0"
  String organization = "i4niac"
  String repository = "mirepoix"
  
  
  String branchesUrl = [baseUrl, version, "repositories", organization, repository, "branches"].join("/")
  
  
  String username = "i4niac"
  String password = "mypassword"
  // Create authorization header using Base64 encoding
  String userpass = username + ":" + password;
  String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());
  // Create URL
  URL url = branchesUrl.toURL()
  // Open connection
  URLConnection connection = url.openConnection()
  // Set authorization header
  connection.setRequestProperty ("Authorization", basicAuth)
  // Open input stream
  InputStream inputStream = connection.getInputStream()
  // Get JSON output
  def branchesJson = new groovy.json.JsonSlurper().parseText(inputStream.text)
  // Close the stream
  inputStream.close()
  </code></pre>