Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add listCategories at ProjectApi and RepositoryApi #320

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.cdancy.bitbucket.rest.domain.category;

import java.util.List;

/**
* This interface should NOT be applied to "option" like classes and/or used in instances where this is applied to
* outgoing http traffic. This interface should ONLY be used for classes modeled after incoming http traffic.
*/
public interface CategoriesHolder {

List<Category> categories();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.cdancy.bitbucket.rest.domain.category;

import org.jclouds.json.SerializedNames;

import com.google.auto.value.AutoValue;

@AutoValue
public abstract class Category {

public abstract int id();
public abstract String title();

Category() {
}

@SerializedNames({"id", "title"})
public static Category create(final int id, final String title) {
return new AutoValue_Category(id, title);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.cdancy.bitbucket.rest.domain.category;

import org.jclouds.json.SerializedNames;

import com.google.auto.value.AutoValue;

@AutoValue
public abstract class ProjectCategoriesPage {

/**
* will contain error messages, if the call fails.
*/
public abstract String message();

/**
* the categories assigned to the project
*/
public abstract ProjectCategory result();

ProjectCategoriesPage() {}

@SerializedNames({"message", "result"})
public static ProjectCategoriesPage create(final String message, final ProjectCategory result) {
return new AutoValue_ProjectCategoriesPage(message, result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.cdancy.bitbucket.rest.domain.category;

import java.util.List;

import org.jclouds.json.SerializedNames;

import com.google.auto.value.AutoValue;

@AutoValue
public abstract class ProjectCategory implements ProjectHolder, CategoriesHolder {

ProjectCategory() {}

@SerializedNames({"projectId", "projectKey", "projectName", "categories"})
public static ProjectCategory create(final String projectId,
final String projectKey,
final String projectName,
final List<Category> categories) {
return new AutoValue_ProjectCategory(projectId, projectKey, projectName, categories);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.cdancy.bitbucket.rest.domain.category;

/**
* This interface should NOT be applied to "option" like classes and/or used in instances where this is applied to
* outgoing http traffic. This interface should ONLY be used for classes modeled after incoming http traffic.
*/
public interface ProjectHolder {

String projectId();
String projectKey();
String projectName();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.cdancy.bitbucket.rest.domain.category;

import org.jclouds.json.SerializedNames;

import com.google.auto.value.AutoValue;

@AutoValue
public abstract class RepositoryCategoriesPage {

/**
* will contain error messages, if the call fails.
*/
public abstract String message();

/**
* the categories assigned to the project/repository
*/
public abstract RepositoryCategory result();

RepositoryCategoriesPage() {}

@SerializedNames({"message", "result"})
public static RepositoryCategoriesPage create(final String message, final RepositoryCategory result) {
return new AutoValue_RepositoryCategoriesPage(message, result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.cdancy.bitbucket.rest.domain.category;

import java.util.List;

import org.jclouds.json.SerializedNames;

import com.google.auto.value.AutoValue;

@AutoValue
public abstract class RepositoryCategory implements ProjectHolder, CategoriesHolder {

public abstract String repositoryId();

public abstract String repositoryName();

public abstract String repositorySlug();

RepositoryCategory() {}

@SerializedNames({ "projectId", "projectKey", "projectName", "categories", "repositoryId", "repositoryName", "repositorySlug" })
public static RepositoryCategory create(final String projectId,
final String projectKey,
final String projectName,
final List<Category> categories,
final String repositoryId,
final String repositoryName,
final String repositorySlug) {
return new AutoValue_RepositoryCategory(projectId, projectKey, projectName,
categories,
repositoryId, repositoryName, repositorySlug);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.cdancy.bitbucket.rest.features;

import com.cdancy.bitbucket.rest.annotations.Documentation;
import com.cdancy.bitbucket.rest.domain.category.ProjectCategoriesPage;
import com.cdancy.bitbucket.rest.domain.common.RequestStatus;
import javax.inject.Named;
import javax.ws.rs.Consumes;
Expand Down Expand Up @@ -148,4 +149,12 @@ RequestStatus deletePermissionsByGroup(@PathParam("project") String project,
ProjectPermissionsPage listPermissionsByGroup(@PathParam("project") String project,
@Nullable @QueryParam("start") Integer start,
@Nullable @QueryParam("limit") Integer limit);

@Named("project:list-categories")
@Documentation({"https://communardo.atlassian.net/wiki/spaces/KB/pages/27687460/REST+API+for+Categories+for+Bitbucket"})
@Consumes(MediaType.APPLICATION_JSON)
@Path("/categories/latest/project/{project}")
@Fallback(BitbucketFallbacks.ProjectPermissionsPageOnError.class)
@GET
ProjectCategoriesPage listCategories(@PathParam("project") String project);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.cdancy.bitbucket.rest.features;

import com.cdancy.bitbucket.rest.annotations.Documentation;
import com.cdancy.bitbucket.rest.domain.category.RepositoryCategoriesPage;
import com.cdancy.bitbucket.rest.domain.common.RequestStatus;
import com.cdancy.bitbucket.rest.domain.labels.LabelsPage;
import com.cdancy.bitbucket.rest.domain.repository.PermissionsPage;
Expand Down Expand Up @@ -206,6 +207,15 @@ PermissionsPage listPermissionsByGroup(@PathParam("project") String project,
@Nullable @QueryParam("start") Integer start,
@Nullable @QueryParam("limit") Integer limit);

@Named("repository:list-categories")
@Documentation({"https://communardo.atlassian.net/wiki/spaces/KB/pages/27687460/REST+API+for+Categories+for+Bitbucket"})
@Consumes(MediaType.APPLICATION_JSON)
@Path("/categories/latest/project/{project}/repository/{repositorySlug}")
@Fallback(BitbucketFallbacks.RepositoryOnError.class)
@GET
RepositoryCategoriesPage listCategories(@PathParam("project") String project,
@PathParam("repositorySlug") String repoSlug);

@Named("repository:getLabels")
@Documentation({"https://developer.atlassian.com/static/rest/bitbucket-server/latest/bitbucket-rest.html#idp273"})
@Consumes(MediaType.APPLICATION_JSON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.util.Map;

import com.cdancy.bitbucket.rest.domain.category.ProjectCategoriesPage;
import com.cdancy.bitbucket.rest.domain.project.ProjectPermissionsPage;
import org.testng.annotations.Test;

Expand Down Expand Up @@ -516,4 +517,26 @@ public void testListPermissionByGroupOnError() throws Exception {
server.shutdown();
}
}

public void testListCategories() throws Exception {
final MockWebServer server = mockWebServer();
server.enqueue(new MockResponse().setBody(payloadFromResource("/project-categories.json")).setResponseCode(200));
final BitbucketApi baseApi = api(server.getUrl("/"));
final ProjectApi api = baseApi.projectApi();
try {
final ProjectCategoriesPage projectCategoriesPage = api.listCategories(projectKey);
assertThat(projectCategoriesPage).isNotNull();
assertThat(projectCategoriesPage.result()).isNotNull();
assertThat(projectCategoriesPage.result().categories()).hasSize(2);
assertThat(projectCategoriesPage.result().projectKey()).isEqualTo(projectKey);
assertThat(projectCategoriesPage.result().categories().get(0).title()).isEqualTo("cat1");
assertThat(projectCategoriesPage.result().categories().get(1).title()).isEqualTo("cat2");
assertSent(server,
getMethod,
"/rest/categories/latest/project/" + projectKey);
} finally {
baseApi.close();
server.shutdown();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.cdancy.bitbucket.rest.BitbucketApi;
import com.cdancy.bitbucket.rest.BitbucketApiMetadata;
import com.cdancy.bitbucket.rest.domain.category.RepositoryCategoriesPage;
import com.cdancy.bitbucket.rest.domain.common.RequestStatus;
import com.cdancy.bitbucket.rest.domain.labels.LabelsPage;
import com.cdancy.bitbucket.rest.domain.repository.MergeConfig;
Expand Down Expand Up @@ -860,4 +861,27 @@ public void testGetLabels() throws Exception {
server.shutdown();
}
}

public void testListCategories() throws Exception {
final MockWebServer server = mockWebServer();
server.enqueue(new MockResponse().setBody(payloadFromResource("/repository-categories.json")).setResponseCode(200));
final BitbucketApi baseApi = api(server.getUrl("/"));
final RepositoryApi api = baseApi.repositoryApi();
try {
final RepositoryCategoriesPage repositoryCategoriesPage = api.listCategories(projectKey, repoKey);
assertThat(repositoryCategoriesPage).isNotNull();
assertThat(repositoryCategoriesPage.result()).isNotNull();
assertThat(repositoryCategoriesPage.result().categories()).hasSize(2);
assertThat(repositoryCategoriesPage.result().projectKey()).isEqualTo(projectKey);
assertThat(repositoryCategoriesPage.result().repositorySlug()).isEqualTo(repoKey);
assertThat(repositoryCategoriesPage.result().categories().get(0).title()).isEqualTo("cat1");
assertThat(repositoryCategoriesPage.result().categories().get(1).title()).isEqualTo("cat2");
assertSent(server,
getMethod,
"/rest/categories/latest/project/" + projectKey + "/repository/" + repoKey);
} finally {
baseApi.close();
server.shutdown();
}
}
}
19 changes: 19 additions & 0 deletions src/test/resources/project-categories.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"message": "",
"result": {
"projectId": 1,
"projectKey": "PRJ",
"projectName": "ProjectName",
"publicRepository": false,
"categories": [
{
"id": 1,
"title": "cat1"
},
{
"id": 2,
"title": "cat2"
}
]
}
}
22 changes: 22 additions & 0 deletions src/test/resources/repository-categories.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"message": "",
"result": {
"repositoryId": 1,
"repositoryName": "repoName",
"repositorySlug": "my-repo",
"categories": [
{
"id": 1,
"title": "cat1"
},
{
"id": 2,
"title": "cat2"
}
],
"projectId": 1,
"projectKey": "PRJ",
"projectName": "ProjectName",
"publicRepository": false
}
}