Neetesh Narvaria
Neetesh Narvaria

Reputation: 107

how to read BitBucket/Stash branches and their linked JIRA tickets with Java

I want to read bitbucket/stash branches and their respected Jira issue, when i was looking for it and found Atlassian APIs.

I couldn't find any proper examples in this, like how do I connect to bitbucket server, get information of any project, and read branches of that project, and also if there is any Jira issue is linked with the branch.

Any help or direction would be great.

I am looking for a java example for this.

Upvotes: 1

Views: 1179

Answers (1)

gil.fernandes
gil.fernandes

Reputation: 14621

I have found a possible way to interact with the API and get the repositories and user information from Bitbucket. Yet I had to use OAuth authentication which involved a browser callback. This might not be very convenient for a standalone application.

So here is what I have done:

  1. Created an OAuth consumer on my BitBucket account. This is fairly well described on this page: https://confluence.atlassian.com/bitbucket/oauth-on-bitbucket-cloud-238027431.html. After doing this I had a consumer with a key and a secret.

  2. I executed a GET query directly on the browser this link: https://bitbucket.org/site/oauth2/authorize?client_id={client_id}&response_type=code. The {client_id} is actually the key generated in the previous item 1. This lead me to a page where I could confirm the grants to the OAuth app. After confirming the grants the browser page gets re-directed to a page list like this one: http://giltest.org/?code=LcY6FmeyvjqM2xnTyN . The code "LcY6FmeyvjqM2xnTyN" is the code is going to be used to generate the authorization code in the next step.

  3. Now we generate the authorization code which allows to access the BitBucket REST API. I have created some Java code for that in a Maven project. See below for more details on the pom.xml. Using the Unirest library I have created these methods which retrieve the access token:

access token retrieval

public JsonNode accessToken(String clientId, String secret, String code) throws UnirestException {
    return Unirest.post("https://bitbucket.org/site/oauth2/access_token").basicAuth(clientId, secret)
                .field("grant_type", "authorization_code").field("code", code)
                .asJson().getBody();
}

The response of this method will contain the access token in JSON:

{"access_token":"xetOO4xZU-xxxxxxxx--LRmbQrmBkDfHIKfE1vz1ZEGnbUyt5UI31ErKojnecuGWxxxxxxxxxx=","refresh_token":"jbad7ajwVWxxxxx","scopes":"webhook snippet:write issue:write pullrequest:write project:write team:write account:write","token_type":"bearer","expires_in":3600}

You can extract the token directly with this method:

public String accessTokenExtract(String clientId, String secret, String code) throws UnirestException {
    return Unirest.post("https://bitbucket.org/site/oauth2/access_token").basicAuth(clientId, secret)
            .field("grant_type", "authorization_code").field("code", code)
            .asJson().getBody().getObject().get("access_token").toString();
}
  1. After this you can extract the access token from the JSON and use it in a request. See the "Making Requests" section on this page. Here is a sample Java code method which gets the Bitbucket user information:

Show user information request

public String showUserInformation(String authorizationCode) throws UnirestException {
    return Unirest.get("https://api.bitbucket.org/2.0/user")
            .header("Authorization", String.format("Bearer %s", authorizationCode)).asString().getBody();
}

Show repositories

public String listRepositories(String authorizationCode) throws UnirestException {
    return Unirest.get("https://api.bitbucket.org/2.0/repositories")
            .header("Authorization", String.format("Bearer %s", authorizationCode)).asString().getBody();
}

More information on the REST API can be found here:

https://developer.atlassian.com/bitbucket/api/2/reference/resource/

Just for reference: Here is the pom.xml of my toy project:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.fernandes</groupId>
    <artifactId>bitbucket.experiments</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <junit.version>5.0.0-M4</junit.version>
        <bitbucket.version>4.0.0-m27</bitbucket.version>
    </properties>

    <repositories>
        <repository>
            <id>Atlassian</id>
            <url>https://maven.atlassian.com/content/repositories/atlassian-public</url>
        </repository>
    </repositories>

    <dependencies>
        <dependency>
            <groupId>com.mashape.unirest</groupId>
            <artifactId>unirest-java</artifactId>
            <version>1.4.9</version>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.assertj/assertj-core -->
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>3.8.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

Upvotes: 1

Related Questions