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 possibility to get branch Model Condition #68

Merged
merged 6 commits into from
May 25, 2017
Merged
Show file tree
Hide file tree
Changes from 5 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,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.branch;

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

@AutoValue
public abstract class BranchConfiguration {

@Nullable
public abstract String refId();

public abstract boolean useDefault();

BranchConfiguration() {
}

@SerializedNames({ "refId", "useDefault" })
public static BranchConfiguration create(String refId, boolean useDefault) {
return new AutoValue_BranchConfiguration(refId, useDefault);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.branch;

import com.cdancy.bitbucket.rest.domain.common.Error;
import com.cdancy.bitbucket.rest.domain.common.ErrorsHolder;
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 BranchModelConfiguration implements ErrorsHolder {

@Nullable
public abstract BranchConfiguration development();

@Nullable
public abstract BranchConfiguration production();

public abstract List<Type> types();

BranchModelConfiguration() {
}

@SerializedNames({ "development", "production", "types", "errors" })
public static BranchModelConfiguration create(BranchConfiguration development, BranchConfiguration production,
List<Type> types, List<Error> errors) {
return new AutoValue_BranchModelConfiguration(Utils.nullToEmpty(errors), development, production, Utils.nullToEmpty(types));
}
}
25 changes: 21 additions & 4 deletions src/main/java/com/cdancy/bitbucket/rest/domain/branch/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,41 @@

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

import org.jclouds.javax.annotation.Nullable;
import org.jclouds.json.SerializedNames;

import com.google.auto.value.AutoValue;

@AutoValue
public abstract class Type {

public abstract String id();
public enum TypeId {
BUGFIX,
FEATURE,
HOTFIX,
RELEASE
}

public abstract TypeId id();

@Nullable
public abstract String displayName();

public abstract String prefix();

@Nullable
public abstract Boolean enabled();

Type() {
}

@SerializedNames({ "id", "displayName", "prefix" })
public static Type create(String id, String displayName, String prefix) {
return new AutoValue_Type(id, displayName, prefix);
@Deprecated
public static Type create(TypeId id, String displayName, String prefix) {
return create(id, displayName, prefix, null);
}

@SerializedNames({ "id", "displayName", "prefix", "enabled" })
public static Type create(TypeId id, String displayName, String prefix, Boolean enabled) {
return new AutoValue_Type(id, displayName, prefix, enabled);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.cdancy.bitbucket.rest.domain.admin.UserPage;
import com.cdancy.bitbucket.rest.domain.branch.Branch;
import com.cdancy.bitbucket.rest.domain.branch.BranchModel;
import com.cdancy.bitbucket.rest.domain.branch.BranchModelConfiguration;
import com.cdancy.bitbucket.rest.domain.branch.BranchPage;
import com.cdancy.bitbucket.rest.domain.branch.BranchPermissionPage;
import com.cdancy.bitbucket.rest.domain.build.StatusPage;
Expand Down Expand Up @@ -88,6 +89,15 @@ public Object createOrPropagate(Throwable throwable) throws Exception {
}
}

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

public static final class BranchPageOnError implements Fallback<Object> {
public Object createOrPropagate(Throwable throwable) throws Exception {
if (checkNotNull(throwable, "throwable") != null) {
Expand Down Expand Up @@ -294,6 +304,10 @@ public static BranchModel createBranchModelFromErrors(List<Error> errors) {
return BranchModel.create(null, null, null, errors);
}

public static BranchModelConfiguration createBranchModelConfigurationFromErrors(List<Error> errors) {
return BranchModelConfiguration.create(null, null, null, errors);
}

public static BranchPage createBranchPageFromErrors(List<Error> errors) {
return BranchPage.create(-1, -1, -1, -1, true, null, errors);
}
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/com/cdancy/bitbucket/rest/features/BranchApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,20 @@
import com.cdancy.bitbucket.rest.annotations.Documentation;
import com.cdancy.bitbucket.rest.domain.branch.Branch;
import com.cdancy.bitbucket.rest.domain.branch.BranchModel;
import com.cdancy.bitbucket.rest.domain.branch.BranchModelConfiguration;
import com.cdancy.bitbucket.rest.domain.branch.BranchPage;
import com.cdancy.bitbucket.rest.domain.branch.BranchPermission;
import com.cdancy.bitbucket.rest.domain.branch.BranchPermissionPage;
import com.cdancy.bitbucket.rest.fallbacks.BitbucketFallbacks;
import com.cdancy.bitbucket.rest.filters.BitbucketAuthentication;
import com.cdancy.bitbucket.rest.options.CreateBranch;

import org.jclouds.javax.annotation.Nullable;
import org.jclouds.rest.annotations.BinderParam;
import org.jclouds.rest.annotations.Fallback;
import org.jclouds.rest.annotations.Payload;
import org.jclouds.rest.annotations.PayloadParam;
import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.binders.BindToJsonPayload;

import javax.inject.Named;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
Expand All @@ -46,8 +45,7 @@
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.jclouds.javax.annotation.Nullable;

import javax.inject.Named;
import java.util.List;

@Produces(MediaType.APPLICATION_JSON)
Expand Down Expand Up @@ -120,6 +118,14 @@ Branch getDefault(@PathParam("project") String project,
BranchModel model(@PathParam("project") String project,
@PathParam("repo") String repo);

@Named("branch:get-model-configuration")
@Consumes(MediaType.APPLICATION_JSON)
@Path("/branch-utils/{jclouds.api-version}/projects/{project}/repos/{repo}/branchmodel/configuration")
@Fallback(BitbucketFallbacks.BranchModelConfigurationOnError.class)
@GET
BranchModelConfiguration getModelConfiguration(@PathParam("project") String project,
@PathParam("repo") String repo);

@Named("branch:list-branch-permission")
@Documentation({"https://developer.atlassian.com/static/rest/bitbucket-server/4.14.1/bitbucket-ref-restriction-rest.html#idm45354011023456"})
@Consumes(MediaType.APPLICATION_JSON)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
Copy link
Owner

Choose a reason for hiding this comment

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

Should this file be here? Doesn't seem to be used

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I use in another PR (#69).

* 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.options;

import com.cdancy.bitbucket.rest.domain.branch.BranchConfiguration;
import com.cdancy.bitbucket.rest.domain.branch.BranchModelConfiguration;
import com.cdancy.bitbucket.rest.domain.branch.Type;
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 CreateBranchModelConfiguration {

public abstract BranchConfiguration development();

@Nullable
public abstract BranchConfiguration production();

public abstract List<Type> types();

CreateBranchModelConfiguration() {
}

public static CreateBranchModelConfiguration create(BranchModelConfiguration configuration) {
return new AutoValue_CreateBranchModelConfiguration(configuration.development(), configuration.production(), configuration.types());
}

@SerializedNames({ "development", "production", "types" })
public static CreateBranchModelConfiguration create(BranchConfiguration development, BranchConfiguration production, List<Type> types) {
return new AutoValue_CreateBranchModelConfiguration(development, production, types);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,16 @@

Copy link
Owner

Choose a reason for hiding this comment

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

Another LiveTest to check for errors? Maybe named something like testGetBranchModelConfigurationOnError()?

package com.cdancy.bitbucket.rest.features;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;

import com.cdancy.bitbucket.rest.BaseBitbucketApiLiveTest;
import com.cdancy.bitbucket.rest.domain.branch.Branch;
import com.cdancy.bitbucket.rest.domain.branch.BranchModel;
import com.cdancy.bitbucket.rest.domain.branch.BranchModelConfiguration;
import com.cdancy.bitbucket.rest.domain.branch.BranchPage;
import com.cdancy.bitbucket.rest.domain.branch.BranchPermission;
import com.cdancy.bitbucket.rest.domain.branch.BranchPermissionEnumType;
import com.cdancy.bitbucket.rest.domain.branch.BranchPermissionPage;
import com.cdancy.bitbucket.rest.domain.branch.Matcher;
import com.cdancy.bitbucket.rest.domain.branch.Type;
import com.cdancy.bitbucket.rest.domain.pullrequest.User;
import com.cdancy.bitbucket.rest.options.CreateBranch;
import org.testng.annotations.AfterClass;
Expand All @@ -37,6 +36,9 @@
import java.util.ArrayList;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;

@Test(groups = "live", testName = "BranchApiLiveTest", singleThreaded = true)
public class BranchApiLiveTest extends BaseBitbucketApiLiveTest {

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

String defaultBranchId = "refs/heads/master";

Expand All @@ -62,6 +65,7 @@ public void init() {
assertThat(branch).isNotNull();
assertThat(branch.errors().isEmpty()).isTrue();
defaultBranchId = branch.id();
commitHash = branch.latestCommit();
}

@Test
Expand Down Expand Up @@ -136,12 +140,49 @@ public void testDeleteBranchPermission() {
}
}

@Test(dependsOnMethods = {"testCreateBranch", "testListBranches"})
public void testGetBranchModelConfiguration() {
branchModelConfiguration = api().getModelConfiguration(projectKey, repoKey);
checkDefaultBranchConfiguration();
Copy link
Owner

Choose a reason for hiding this comment

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

If we don't need to do this check twice than lets remove the checkDefaultBranchConfiguration method and instead add that code here.

Copy link
Owner

Choose a reason for hiding this comment

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

Seen you pushed a commit but missed this. Not sure if you were planning on addressing with a subsequent commit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If I remove it, I need to readd it in another PR (#69)

Copy link
Owner

Choose a reason for hiding this comment

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

Gotcha ... yeah looking at that PR now. Ok lets keep it then.

}

private void checkDefaultBranchConfiguration() {
assertThat(branchModelConfiguration).isNotNull();
assertThat(branchModelConfiguration.errors().isEmpty()).isTrue();
assertThat(branchModelConfiguration.development().refId()).isNull();
assertThat(branchModelConfiguration.development().useDefault()).isTrue();
assertThat(branchModelConfiguration.production()).isNull();
assertThat(branchModelConfiguration.types().size() == 4);
for (Type type : branchModelConfiguration.types()) {
switch (type.id()) {
case BUGFIX:
assertThat(type.prefix()).isEqualTo("bugfix/");
break;
case HOTFIX:
assertThat(type.prefix()).isEqualTo("hotfix/");
break;
case FEATURE:
assertThat(type.prefix()).isEqualTo("feature/");
break;
case RELEASE:
assertThat(type.prefix()).isEqualTo("release/");
break;
default:
break;
}
assertThat(type.enabled()).isTrue();
}
}

@AfterClass
public void fin() {
boolean success = api().updateDefault(projectKey, repoKey, defaultBranchId);
assertThat(success).isTrue();
success = api().delete(projectKey, repoKey, "refs/heads/" + branchName);
assertThat(success).isTrue();
if (branchModelConfiguration != null) {
checkDefaultBranchConfiguration();
Copy link
Owner

Choose a reason for hiding this comment

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

Do we need to do this check twice?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, This line is for update

}
}

private BranchApi api() {
Expand Down
Loading