All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.ngeor.yak4j.BitbucketApi Maven / Gradle / Ivy

There is a newer version: 0.13.1
Show newest version
package com.github.ngeor.yak4j;

import java.io.IOException;

import okhttp3.Credentials;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;

/**
 * Implementation of the Bitbucket Cloud REST API.
 */
class BitbucketApi {
    private final String username;
    private final String password;

    BitbucketApi(String username, String password) {
        this.username = username;
        this.password = password;
    }

    /**
     * Checks if a Bitbucket Cloud repository contains the given tag.
     * @param owner The owner of the repository.
     * @param slug The slug of the repository.
     * @param tag The tag to find.
     * @return true if the tag exists, false otherwise.
     */
    boolean tagExists(String owner, String slug, String tag) throws IOException {
        String url = "https://api.bitbucket.org/2.0/repositories/"
            + owner + "/" + slug + "/refs/tags?q=name+%3D+%22" + tag + "%22";
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
            .url(url)
            .header("Authorization", Credentials.basic(username, password))
            .build();

        Response response = client.newCall(request).execute();
        if (response == null) {
            throw new IllegalStateException("Got null response");
        }

        if (!response.isSuccessful()) {
            throw new IllegalStateException(String.format("Request for url %s failed: %d", url, response.code()));
        }

        ResponseBody body = response.body();
        if (body == null) {
            throw new IllegalStateException("Got null response body");
        }

        String responseAsString = body.string();
        if (responseAsString == null) {
            throw new IllegalStateException("Got null response body as string");
        }

        return responseAsString.contains(tag);
    }

    /**
     * Creates a new git tag in a Bitbucket repository.
     * @param owner The owner of the repository.
     * @param slug The slug of the repository.
     * @param tag The tag.
     * @param hash The git hash that the tag will point to.
     */
    void createTag(String owner, String slug, String tag, String hash) throws IOException {
        String url = "https://api.bitbucket.org/2.0/repositories/"
            + owner + "/" + slug + "/refs/tags";
        OkHttpClient client = new OkHttpClient();

        RequestBody requestBody = RequestBody.create(
            MediaType.parse("application/json; charset=utf-8"),
            "{ \"name\" : \"" + tag + "\", \"target\" : { \"hash\" : \"" + hash + "\" } }"
        );

        Request request = new Request.Builder()
            .url(url)
            .header("Authorization", Credentials.basic(username, password))
            .post(requestBody)
            .build();

        Response response = client.newCall(request).execute();
        if (response == null) {
            throw new IllegalStateException("Got null response");
        }

        if (!response.isSuccessful()) {
            throw new IllegalStateException("Request failed: " + response.code());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy