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 new attribute "AccessKey" for branchPermission #53

Merged
merged 10 commits into from
Mar 13, 2017
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@
package com.cdancy.bitbucket.rest.domain.branch;

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

import java.util.ArrayList;
import java.util.List;

@AutoValue
Expand All @@ -37,9 +39,23 @@ public abstract class BranchPermission {

public abstract List<String> groups();

@SerializedNames({"id", "type", "matcher", "users", "groups"})
@Nullable
public abstract List<Long> accessKeys();

public static BranchPermission createWithId(Long id, BranchPermissionEnumType type, Matcher matcher,
List<User> users, List<String> groups, List<Long> accessKeysId) {
return new AutoValue_BranchPermission(id, type, matcher, users, groups, accessKeysId);
}

@SerializedNames({"id", "type", "matcher", "users", "groups", "accessKeys"})
public static BranchPermission create(Long id, BranchPermissionEnumType type, Matcher matcher,
List<User> users, List<String> groups) {
return new AutoValue_BranchPermission(id, type, matcher, users, groups);
List<User> users, List<String> groups, @Nullable List<AccessKey> accessKeys) {
List<Long> accessKeyId = new ArrayList<>();
if (accessKeys != null) {
for (AccessKey accessKey : accessKeys) {
accessKeyId.add(accessKey.key().id());
}
}
return BranchPermission.createWithId(id, type, matcher, users, groups, accessKeyId);
}
}
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.sshkey;

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

@AutoValue
public abstract class AccessKey {

public abstract Key key();

@SerializedNames({"key"})
public static AccessKey create(Key key) {
return new AutoValue_AccessKey(key);
}
}
38 changes: 38 additions & 0 deletions src/main/java/com/cdancy/bitbucket/rest/domain/sshkey/Key.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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.sshkey;

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

@AutoValue
public abstract class Key {

@Nullable
public abstract Long id();

public abstract String text();

public abstract String label();

@SerializedNames({"id", "text", "label"})
public static Key create(@Nullable Long id, String text, String label) {
return new AutoValue_Key(id, text, label);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,12 @@ public void testGetNewDefaultBranch() {
@Test (dependsOnMethods = "testGetNewDefaultBranch")
public void testCreateBranchPermission() {
List<String> groupPermission = new ArrayList<>();
groupPermission.add(existingGroup);
groupPermission.add("Test12354");
List<Long> listAccessKey = new ArrayList<>();
listAccessKey.add(123L);
List<BranchPermission> listBranchPermission = new ArrayList<>();
listBranchPermission.add(BranchPermission.create(null, BranchPermissionEnumType.FAST_FORWARD_ONLY,
Matcher.create(Matcher.MatcherId.RELEASE, true), new ArrayList<User>(), groupPermission));
listBranchPermission.add(BranchPermission.createWithId(null, BranchPermissionEnumType.FAST_FORWARD_ONLY,
Matcher.create(Matcher.MatcherId.RELEASE, true), new ArrayList<User>(), groupPermission, listAccessKey));

boolean success = api().updateBranchPermission(projectKey, repoKey, listBranchPermission);
assertThat(success).isTrue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ public void testListBranchePermissions() throws Exception {
assertThat(branch.errors().isEmpty()).isTrue();
assertThat(branch.values().size() > 0).isTrue();
assertThat(839L == branch.values().get(0).id()).isTrue();
assertThat(2 == branch.values().get(0).accessKeys().size()).isTrue();

Map<String, ?> queryParams = ImmutableMap.of("limit", 1);
assertSent(server, "GET", "/rest/branch-permissions/2.0"
Expand Down Expand Up @@ -276,15 +277,18 @@ public void testUpdateBranchesPermissions() throws Exception {
BitbucketApi baseApi = api(server.getUrl("/"));
BranchApi api = baseApi.branchApi();
try {
String projectKey = "PRJ";
String repoKey = "myrepo";

List<String> groupPermission = new ArrayList<>();
groupPermission.add("Test12354");
List<Long> listAccessKey = new ArrayList<>();
listAccessKey.add(123L);
List<BranchPermission> listBranchPermission = new ArrayList<>();
listBranchPermission.add(BranchPermission.create(839L, BranchPermissionEnumType.FAST_FORWARD_ONLY,
Matcher.create(Matcher.MatcherId.RELEASE, true), new ArrayList<User>(), groupPermission));
listBranchPermission.add(BranchPermission.createWithId(839L, BranchPermissionEnumType.FAST_FORWARD_ONLY,
Matcher.create(Matcher.MatcherId.RELEASE, true), new ArrayList<User>(), groupPermission,
listAccessKey));

String projectKey = "PRJ";
String repoKey = "myrepo";
boolean success = api.updateBranchPermission(projectKey, repoKey, listBranchPermission);
assertThat(success).isTrue();
assertSent(server, "POST", "/rest/branch-permissions/2.0"
Expand Down
18 changes: 17 additions & 1 deletion src/test/resources/branch-permission-list.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,23 @@
}
},
"type": "fast-forward-only",
"users": []
"users": [],
"accessKeys": [
{
"key": {
"id": 158,
"label": "test213",
"text": "ssh-rsa fasdfadsfdsf test213"
}
},
{
"key": {
"id": 126,
"label": "test123",
"text": "ssh-rsa gagjfdsggfds23434 test123"
}
}
]
}
]
}