com.github.ngeor.yak4j.BitbucketApi Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of yak4j-bitbucket-maven-plugin Show documentation
Show all versions of yak4j-bitbucket-maven-plugin Show documentation
yak shaving for Java: A Maven plugin which can create Bitbucket tags
package com.github.ngeor.yak4j;
import java.io.IOException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Implementation of the Bitbucket Cloud REST API.
*/
class BitbucketApi {
private final RestClient restClient;
/**
* Creates an instance of this class.
* @param restClient The REST client.
*/
BitbucketApi(RestClient restClient) {
this.restClient = restClient;
}
/**
* 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";
String responseAsString = restClient.get(url);
return responseAsString.contains(tag);
}
/**
* Gets the tag that represents the biggest version.
* @param owner The owner of the repository.
* @param slug The slug of the repository.
* @return The tag name of the biggest version.
*/
String tagOfBiggestVersion(String owner, String slug) throws IOException {
String url = "https://api.bitbucket.org/2.0/repositories/"
+ owner + "/" + slug + "/refs/tags?sort=-name";
String responseAsString = restClient.get(url);
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
BitbucketTags tagsResponse = objectMapper.readValue(responseAsString, BitbucketTags.class);
if (tagsResponse == null) {
return null;
}
BitbucketTag[] values = tagsResponse.getValues();
if (values == null || values.length <= 0) {
return null;
}
return values[0].getName();
}
/**
* 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";
String body = "{ \"name\" : \"" + tag + "\", \"target\" : { \"hash\" : \"" + hash + "\" } }";
restClient.post(url, body);
}
}