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

List group permission for repo #61

Merged
merged 3 commits into from
May 16, 2017
Merged
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,32 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.cdancy.bitbucket.rest.domain.repository;

import com.google.auto.value.AutoValue;
import org.jclouds.json.SerializedNames;

@AutoValue
public abstract class Group {

public abstract String name();

@SerializedNames({"name"})
public static Group create(String name) {
return new AutoValue_Group(name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.cdancy.bitbucket.rest.domain.repository;

import com.cdancy.bitbucket.rest.domain.pullrequest.User;
import com.google.auto.value.AutoValue;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.json.SerializedNames;

@AutoValue
public abstract class Permissions {

public enum PermissionsType {
REPO_ADMIN,
REPO_WRITE,
REPO_READ
}

@Nullable
public abstract User user();

@Nullable
public abstract Group group();

public abstract PermissionsType permission();

@SerializedNames({"user", "group", "permission"})
public static Permissions create(User user, Group group, PermissionsType type) {
return new AutoValue_Permissions(user, group, type);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.cdancy.bitbucket.rest.domain.repository;

import com.cdancy.bitbucket.rest.domain.common.Error;
import com.cdancy.bitbucket.rest.domain.common.ErrorsHolder;
import com.cdancy.bitbucket.rest.domain.common.Page;
import com.cdancy.bitbucket.rest.utils.Utils;
import com.google.auto.value.AutoValue;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.json.SerializedNames;

import java.util.List;

@AutoValue
public abstract class PermissionsPage implements Page<Permissions>, ErrorsHolder {

@SerializedNames({ "start", "limit", "size", "nextPageStart", "isLastPage", "values", "errors" })
public static PermissionsPage create(int start, int limit, int size, int nextPageStart, boolean isLastPage,
@Nullable List<Permissions> values, @Nullable List<Error> errors) {
return new AutoValue_PermissionsPage(start, limit, size, nextPageStart, isLastPage,
Utils.nullToEmpty(values), Utils.nullToEmpty(errors));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.cdancy.bitbucket.rest.domain.pullrequest.ChangePage;
import com.cdancy.bitbucket.rest.domain.pullrequest.CommentPage;
import com.cdancy.bitbucket.rest.domain.pullrequest.MergeStatus;
import com.cdancy.bitbucket.rest.domain.repository.PermissionsPage;
import com.cdancy.bitbucket.rest.domain.repository.Repository;
import com.cdancy.bitbucket.rest.domain.tags.Tag;
import org.jclouds.Fallback;
Expand Down Expand Up @@ -195,6 +196,15 @@ public Object createOrPropagate(Throwable throwable) throws Exception {
}
}

public static final class PermissionsPageOnError implements Fallback<Object> {
public Object createOrPropagate(Throwable throwable) throws Exception {
if (checkNotNull(throwable, "throwable") != null) {
return createPermissionsPageFromErrors(getErrors(throwable.getMessage()));
}
throw propagate(throwable);
}
}

public static final class ProjectOnError implements Fallback<Object> {
public Object createOrPropagate(Throwable throwable) throws Exception {
if (checkNotNull(throwable, "throwable") != null) {
Expand Down Expand Up @@ -323,6 +333,10 @@ public static RepositoryPage createRepositoryPageFromErrors(List<Error> errors)
return RepositoryPage.create(-1, -1, -1, -1, true, null, errors);
}

public static PermissionsPage createPermissionsPageFromErrors(List<Error> errors) {
return PermissionsPage.create(-1, -1, -1, -1, true, null, errors);
}

public static Project createProjectFromErrors(List<Error> errors) {
return Project.create(null, -1, null, null, false, null, null, errors);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import com.cdancy.bitbucket.rest.domain.repository.PermissionsPage;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.rest.annotations.BinderParam;
import org.jclouds.rest.annotations.Fallback;
Expand Down Expand Up @@ -82,4 +83,15 @@ boolean delete(@PathParam("project") String project,
RepositoryPage list(@PathParam("project") String project,
@Nullable @QueryParam("start") Integer start,
@Nullable @QueryParam("limit") Integer limit);

@Named("repository:list-Permissions-Group")
@Documentation({"https://developer.atlassian.com/static/rest/bitbucket-server/5.0.0/bitbucket-rest.html#idm45659054969200"})
@Consumes(MediaType.APPLICATION_JSON)
@Path("/{project}/repos/{repo}/permissions/groups")
@Fallback(BitbucketFallbacks.PermissionsPageOnError.class)
@GET
PermissionsPage listPermissionsGroup(@PathParam("project") String project,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@j0nathan33 FYI ... I'm going to rename this on master to listGroupPermissions.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignore that ... looks like we already did something similar :)

@PathParam("repo") String repo,
@Nullable @QueryParam("start") Integer start,
@Nullable @QueryParam("limit") Integer limit);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import java.util.List;

import com.cdancy.bitbucket.rest.domain.repository.PermissionsPage;
import org.assertj.core.api.Condition;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
Expand Down Expand Up @@ -112,6 +113,12 @@ public void testCreateRepositoryWithIllegalName() {
assertThat(repository.errors().isEmpty()).isFalse();
}

@Test(dependsOnMethods = "testGetRepository")
public void testListPermissionGroup() {
PermissionsPage permissionsPage = api().listPermissionsGroup(projectKey, repoKey, 0, 100);
assertThat(permissionsPage.values()).isEmpty();
}

@AfterClass
public void fin() {
boolean success = api.projectApi().delete(projectKey);
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.repository.PermissionsPage;
import org.testng.annotations.Test;

import com.cdancy.bitbucket.rest.BitbucketApi;
Expand Down Expand Up @@ -220,7 +221,7 @@ public void testGetRepositoryListWithLimit() throws Exception {
server.shutdown();
}
}

public void testGetRepositoryListNonExistent() throws Exception {
MockWebServer server = mockEtcdJavaWebServer();

Expand All @@ -238,4 +239,52 @@ public void testGetRepositoryListNonExistent() throws Exception {
server.shutdown();
}
}

public void testListPermissionGroup() throws Exception {
MockWebServer server = mockEtcdJavaWebServer();

server.enqueue(new MockResponse().setBody(payloadFromResource("/repository-permission-group.json")).setResponseCode(200));
BitbucketApi baseApi = api(server.getUrl("/"));
RepositoryApi api = baseApi.repositoryApi();
try {
String projectKey = "PRJ1";
String repoKey = "1234";
PermissionsPage permissionsPage = api.listPermissionsGroup(projectKey, repoKey, 0, 100);
assertThat(permissionsPage).isNotNull();
assertThat(permissionsPage.errors()).isEmpty();
assertThat(permissionsPage.size() == 1).isTrue();
assertThat(permissionsPage.values().get(0).user() == null).isTrue();
assertThat(permissionsPage.values().get(0).group().name().equals("test12345")).isTrue();

Map<String, ?> queryParams = ImmutableMap.of("limit", 100, "start", 0);
assertSent(server, "GET", "/rest/api/" + BitbucketApiMetadata.API_VERSION
+ "/projects/" + projectKey + "/repos/" + repoKey + "/permissions/groups", queryParams);
} finally {
baseApi.close();
server.shutdown();
}
}

public void testListPermissionGroupOnError() throws Exception {
MockWebServer server = mockEtcdJavaWebServer();

server.enqueue(new MockResponse().setBody(payloadFromResource("/repository-permission-group-error.json")).setResponseCode(404));
BitbucketApi baseApi = api(server.getUrl("/"));
RepositoryApi api = baseApi.repositoryApi();
try {
String projectKey = "PRJ1";
String repoKey = "1234";
PermissionsPage permissionsPage = api.listPermissionsGroup(projectKey, repoKey, 0, 100);
assertThat(permissionsPage).isNotNull();
assertThat(permissionsPage.values()).isEmpty();
assertThat(permissionsPage.errors()).isNotEmpty();

Map<String, ?> queryParams = ImmutableMap.of("limit", 100, "start", 0);
assertSent(server, "GET", "/rest/api/" + BitbucketApiMetadata.API_VERSION
+ "/projects/" + projectKey + "/repos/" + repoKey + "/permissions/groups", queryParams);
} finally {
baseApi.close();
server.shutdown();
}
}
}
9 changes: 9 additions & 0 deletions src/test/resources/repository-permission-group-error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"errors": [
{
"context": null,
"exceptionName": "com.atlassian.bitbucket.project.NoSuchProjectException",
"message": "Project test123 does not exist."
}
]
}
14 changes: 14 additions & 0 deletions src/test/resources/repository-permission-group.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"isLastPage": true,
"limit": 25,
"size": 1,
"start": 0,
"values": [
{
"group": {
"name": "test12345"
},
"permission": "REPO_WRITE"
}
]
}