Skip to content

Commit f9ffc63

Browse files
j0nathan33cdancy
authored andcommitted
Add possibility to get branch Model Condition (#68)
* Initial commit of branchModel * Add FallBacks error for BranchModel * Add mockTest + LiveTest * Fix build * Edit code after codeReview
1 parent 05184ef commit f9ffc63

File tree

10 files changed

+324
-18
lines changed

10 files changed

+324
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.cdancy.bitbucket.rest.domain.branch;
19+
20+
import com.google.auto.value.AutoValue;
21+
import org.jclouds.javax.annotation.Nullable;
22+
import org.jclouds.json.SerializedNames;
23+
24+
@AutoValue
25+
public abstract class BranchConfiguration {
26+
27+
@Nullable
28+
public abstract String refId();
29+
30+
public abstract boolean useDefault();
31+
32+
BranchConfiguration() {
33+
}
34+
35+
@SerializedNames({ "refId", "useDefault" })
36+
public static BranchConfiguration create(String refId, boolean useDefault) {
37+
return new AutoValue_BranchConfiguration(refId, useDefault);
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.cdancy.bitbucket.rest.domain.branch;
19+
20+
import com.cdancy.bitbucket.rest.domain.common.Error;
21+
import com.cdancy.bitbucket.rest.domain.common.ErrorsHolder;
22+
import com.cdancy.bitbucket.rest.utils.Utils;
23+
import com.google.auto.value.AutoValue;
24+
import org.jclouds.javax.annotation.Nullable;
25+
import org.jclouds.json.SerializedNames;
26+
27+
import java.util.List;
28+
29+
@AutoValue
30+
public abstract class BranchModelConfiguration implements ErrorsHolder {
31+
32+
@Nullable
33+
public abstract BranchConfiguration development();
34+
35+
@Nullable
36+
public abstract BranchConfiguration production();
37+
38+
public abstract List<Type> types();
39+
40+
BranchModelConfiguration() {
41+
}
42+
43+
@SerializedNames({ "development", "production", "types", "errors" })
44+
public static BranchModelConfiguration create(BranchConfiguration development, BranchConfiguration production,
45+
List<Type> types, List<Error> errors) {
46+
return new AutoValue_BranchModelConfiguration(Utils.nullToEmpty(errors), development, production, Utils.nullToEmpty(types));
47+
}
48+
}

src/main/java/com/cdancy/bitbucket/rest/domain/branch/Type.java

+21-4
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,41 @@
1717

1818
package com.cdancy.bitbucket.rest.domain.branch;
1919

20+
import org.jclouds.javax.annotation.Nullable;
2021
import org.jclouds.json.SerializedNames;
2122

2223
import com.google.auto.value.AutoValue;
2324

2425
@AutoValue
2526
public abstract class Type {
2627

27-
public abstract String id();
28+
public enum TypeId {
29+
BUGFIX,
30+
FEATURE,
31+
HOTFIX,
32+
RELEASE
33+
}
34+
35+
public abstract TypeId id();
2836

37+
@Nullable
2938
public abstract String displayName();
3039

3140
public abstract String prefix();
3241

42+
@Nullable
43+
public abstract Boolean enabled();
44+
3345
Type() {
3446
}
3547

36-
@SerializedNames({ "id", "displayName", "prefix" })
37-
public static Type create(String id, String displayName, String prefix) {
38-
return new AutoValue_Type(id, displayName, prefix);
48+
@Deprecated
49+
public static Type create(TypeId id, String displayName, String prefix) {
50+
return create(id, displayName, prefix, null);
51+
}
52+
53+
@SerializedNames({ "id", "displayName", "prefix", "enabled" })
54+
public static Type create(TypeId id, String displayName, String prefix, Boolean enabled) {
55+
return new AutoValue_Type(id, displayName, prefix, enabled);
3956
}
4057
}

src/main/java/com/cdancy/bitbucket/rest/fallbacks/BitbucketFallbacks.java

+14
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import com.cdancy.bitbucket.rest.domain.admin.UserPage;
2222
import com.cdancy.bitbucket.rest.domain.branch.Branch;
2323
import com.cdancy.bitbucket.rest.domain.branch.BranchModel;
24+
import com.cdancy.bitbucket.rest.domain.branch.BranchModelConfiguration;
2425
import com.cdancy.bitbucket.rest.domain.branch.BranchPage;
2526
import com.cdancy.bitbucket.rest.domain.branch.BranchPermissionPage;
2627
import com.cdancy.bitbucket.rest.domain.build.StatusPage;
@@ -90,6 +91,15 @@ public Object createOrPropagate(Throwable throwable) throws Exception {
9091
}
9192
}
9293

94+
public static final class BranchModelConfigurationOnError implements Fallback<Object> {
95+
public Object createOrPropagate(Throwable throwable) throws Exception {
96+
if (checkNotNull(throwable, "throwable") != null) {
97+
return createBranchModelConfigurationFromErrors(getErrors(throwable.getMessage()));
98+
}
99+
throw propagate(throwable);
100+
}
101+
}
102+
93103
public static final class BranchPageOnError implements Fallback<Object> {
94104
public Object createOrPropagate(Throwable throwable) throws Exception {
95105
if (checkNotNull(throwable, "throwable") != null) {
@@ -314,6 +324,10 @@ public static BranchModel createBranchModelFromErrors(List<Error> errors) {
314324
return BranchModel.create(null, null, null, errors);
315325
}
316326

327+
public static BranchModelConfiguration createBranchModelConfigurationFromErrors(List<Error> errors) {
328+
return BranchModelConfiguration.create(null, null, null, errors);
329+
}
330+
317331
public static BranchPage createBranchPageFromErrors(List<Error> errors) {
318332
return BranchPage.create(-1, -1, -1, -1, true, null, errors);
319333
}

src/main/java/com/cdancy/bitbucket/rest/features/BranchApi.java

+11-5
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,20 @@
2020
import com.cdancy.bitbucket.rest.annotations.Documentation;
2121
import com.cdancy.bitbucket.rest.domain.branch.Branch;
2222
import com.cdancy.bitbucket.rest.domain.branch.BranchModel;
23+
import com.cdancy.bitbucket.rest.domain.branch.BranchModelConfiguration;
2324
import com.cdancy.bitbucket.rest.domain.branch.BranchPage;
2425
import com.cdancy.bitbucket.rest.domain.branch.BranchPermission;
2526
import com.cdancy.bitbucket.rest.domain.branch.BranchPermissionPage;
2627
import com.cdancy.bitbucket.rest.fallbacks.BitbucketFallbacks;
2728
import com.cdancy.bitbucket.rest.filters.BitbucketAuthentication;
2829
import com.cdancy.bitbucket.rest.options.CreateBranch;
29-
30+
import org.jclouds.javax.annotation.Nullable;
3031
import org.jclouds.rest.annotations.BinderParam;
3132
import org.jclouds.rest.annotations.Fallback;
3233
import org.jclouds.rest.annotations.Payload;
3334
import org.jclouds.rest.annotations.PayloadParam;
3435
import org.jclouds.rest.annotations.RequestFilters;
3536
import org.jclouds.rest.binders.BindToJsonPayload;
36-
37-
import javax.inject.Named;
3837
import javax.ws.rs.Consumes;
3938
import javax.ws.rs.DELETE;
4039
import javax.ws.rs.GET;
@@ -46,8 +45,7 @@
4645
import javax.ws.rs.QueryParam;
4746
import javax.ws.rs.core.MediaType;
4847

49-
import org.jclouds.javax.annotation.Nullable;
50-
48+
import javax.inject.Named;
5149
import java.util.List;
5250

5351
@Produces(MediaType.APPLICATION_JSON)
@@ -120,6 +118,14 @@ Branch getDefault(@PathParam("project") String project,
120118
BranchModel model(@PathParam("project") String project,
121119
@PathParam("repo") String repo);
122120

121+
@Named("branch:get-model-configuration")
122+
@Consumes(MediaType.APPLICATION_JSON)
123+
@Path("/branch-utils/{jclouds.api-version}/projects/{project}/repos/{repo}/branchmodel/configuration")
124+
@Fallback(BitbucketFallbacks.BranchModelConfigurationOnError.class)
125+
@GET
126+
BranchModelConfiguration getModelConfiguration(@PathParam("project") String project,
127+
@PathParam("repo") String repo);
128+
123129
@Named("branch:list-branch-permission")
124130
@Documentation({"https://developer.atlassian.com/static/rest/bitbucket-server/4.14.1/bitbucket-ref-restriction-rest.html#idm45354011023456"})
125131
@Consumes(MediaType.APPLICATION_JSON)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.cdancy.bitbucket.rest.options;
19+
20+
import com.cdancy.bitbucket.rest.domain.branch.BranchConfiguration;
21+
import com.cdancy.bitbucket.rest.domain.branch.BranchModelConfiguration;
22+
import com.cdancy.bitbucket.rest.domain.branch.Type;
23+
import com.google.auto.value.AutoValue;
24+
import org.jclouds.javax.annotation.Nullable;
25+
import org.jclouds.json.SerializedNames;
26+
27+
import java.util.List;
28+
29+
@AutoValue
30+
public abstract class CreateBranchModelConfiguration {
31+
32+
public abstract BranchConfiguration development();
33+
34+
@Nullable
35+
public abstract BranchConfiguration production();
36+
37+
public abstract List<Type> types();
38+
39+
CreateBranchModelConfiguration() {
40+
}
41+
42+
public static CreateBranchModelConfiguration create(BranchModelConfiguration configuration) {
43+
return new AutoValue_CreateBranchModelConfiguration(configuration.development(), configuration.production(), configuration.types());
44+
}
45+
46+
@SerializedNames({ "development", "production", "types" })
47+
public static CreateBranchModelConfiguration create(BranchConfiguration development, BranchConfiguration production, List<Type> types) {
48+
return new AutoValue_CreateBranchModelConfiguration(development, production, types);
49+
}
50+
}

src/test/java/com/cdancy/bitbucket/rest/features/BranchApiLiveTest.java

+49-3
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,16 @@
1717

1818
package com.cdancy.bitbucket.rest.features;
1919

20-
import static org.assertj.core.api.Assertions.assertThat;
21-
import static org.assertj.core.api.Assertions.fail;
22-
2320
import com.cdancy.bitbucket.rest.BaseBitbucketApiLiveTest;
2421
import com.cdancy.bitbucket.rest.domain.branch.Branch;
2522
import com.cdancy.bitbucket.rest.domain.branch.BranchModel;
23+
import com.cdancy.bitbucket.rest.domain.branch.BranchModelConfiguration;
2624
import com.cdancy.bitbucket.rest.domain.branch.BranchPage;
2725
import com.cdancy.bitbucket.rest.domain.branch.BranchPermission;
2826
import com.cdancy.bitbucket.rest.domain.branch.BranchPermissionEnumType;
2927
import com.cdancy.bitbucket.rest.domain.branch.BranchPermissionPage;
3028
import com.cdancy.bitbucket.rest.domain.branch.Matcher;
29+
import com.cdancy.bitbucket.rest.domain.branch.Type;
3130
import com.cdancy.bitbucket.rest.domain.pullrequest.User;
3231
import com.cdancy.bitbucket.rest.options.CreateBranch;
3332
import org.testng.annotations.AfterClass;
@@ -37,6 +36,9 @@
3736
import java.util.ArrayList;
3837
import java.util.List;
3938

39+
import static org.assertj.core.api.Assertions.assertThat;
40+
import static org.assertj.core.api.Assertions.fail;
41+
4042
@Test(groups = "live", testName = "BranchApiLiveTest", singleThreaded = true)
4143
public class BranchApiLiveTest extends BaseBitbucketApiLiveTest {
4244

@@ -53,6 +55,7 @@ public class BranchApiLiveTest extends BaseBitbucketApiLiveTest {
5355
String commitHash = "5284b6cec569346855710b535dafb915423110c2";
5456
String existingGroup = "dev-group";
5557
Long branchPermissionId = null;
58+
BranchModelConfiguration branchModelConfiguration = null;
5659

5760
String defaultBranchId = "refs/heads/master";
5861

@@ -62,6 +65,7 @@ public void init() {
6265
assertThat(branch).isNotNull();
6366
assertThat(branch.errors().isEmpty()).isTrue();
6467
defaultBranchId = branch.id();
68+
commitHash = branch.latestCommit();
6569
}
6670

6771
@Test
@@ -136,6 +140,48 @@ public void testDeleteBranchPermission() {
136140
}
137141
}
138142

143+
@Test(dependsOnMethods = {"testCreateBranch", "testListBranches"})
144+
public void testGetBranchModelConfiguration() {
145+
branchModelConfiguration = api().getModelConfiguration(projectKey, repoKey);
146+
checkDefaultBranchConfiguration();
147+
}
148+
149+
@Test(dependsOnMethods = {"testCreateBranch", "testListBranches"})
150+
public void testGetBranchModelConfigurationOnError() {
151+
BranchModelConfiguration configuration = api().getModelConfiguration(projectKey, "12345");
152+
assertThat(configuration.errors()).isNotEmpty();
153+
assertThat(configuration.development()).isNull();
154+
assertThat(configuration.production()).isNull();
155+
assertThat(configuration.types()).isEmpty();
156+
}
157+
158+
private void checkDefaultBranchConfiguration() {
159+
assertThat(branchModelConfiguration).isNotNull();
160+
assertThat(branchModelConfiguration.errors().isEmpty()).isTrue();
161+
assertThat(branchModelConfiguration.development().refId()).isNotNull();
162+
assertThat(branchModelConfiguration.production()).isNull();
163+
assertThat(branchModelConfiguration.types().size() == 4);
164+
for (Type type : branchModelConfiguration.types()) {
165+
switch (type.id()) {
166+
case BUGFIX:
167+
assertThat(type.prefix()).isEqualTo("bugfix/");
168+
break;
169+
case HOTFIX:
170+
assertThat(type.prefix()).isEqualTo("hotfix/");
171+
break;
172+
case FEATURE:
173+
assertThat(type.prefix()).isEqualTo("feature/");
174+
break;
175+
case RELEASE:
176+
assertThat(type.prefix()).isEqualTo("release/");
177+
break;
178+
default:
179+
break;
180+
}
181+
assertThat(type.enabled()).isTrue();
182+
}
183+
}
184+
139185
@AfterClass
140186
public void fin() {
141187
boolean success = api().updateDefault(projectKey, repoKey, defaultBranchId);

0 commit comments

Comments
 (0)