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 delete branch Model Condition #70

Merged
merged 27 commits into from
Jun 2, 2017
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
e2e10aa
Initial commit of branchModel
j0nathan33 May 23, 2017
796b972
Add FallBacks error for BranchModel
j0nathan33 May 23, 2017
2743b5c
Add mockTest + LiveTest
j0nathan33 May 23, 2017
afc4c7f
First commit of BranchModel update
j0nathan33 May 23, 2017
ba3dbfe
Fix build
j0nathan33 May 23, 2017
61156b5
Merge branch 'branchModel-list' into branchModel-Update
j0nathan33 May 23, 2017
a0796de
First commit of branch model condition for delete
j0nathan33 May 23, 2017
b41030f
Fix build
j0nathan33 May 23, 2017
a1ffd3b
Merge branch 'master' of https://github.com/cdancy/bitbucket-rest int…
j0nathan33 May 24, 2017
413b87a
Merge branch 'branchModel-list' into branchModel-Update
j0nathan33 May 24, 2017
193ac27
Merge branch 'branchModel-Update' into branchModel-delete
j0nathan33 May 24, 2017
8b48dea
Edit code after codeReview
j0nathan33 May 25, 2017
4243a25
Merge branch 'branchModel-list' into branchModel-Update
j0nathan33 May 25, 2017
0b643a6
Fix live Test
j0nathan33 May 25, 2017
95c7b5d
Merge branch 'branchModel-list' into branchModel-delete
j0nathan33 May 25, 2017
ec29dc1
Merge branch 'branchModel-Update' into branchModel-delete
j0nathan33 May 25, 2017
6fa0729
Remove duplicate Test
j0nathan33 May 25, 2017
d269843
Fix Test
j0nathan33 May 25, 2017
7b3eccd
Fix build
j0nathan33 May 25, 2017
c3d32ac
Merge branch 'master' of https://github.com/cdancy/bitbucket-rest int…
j0nathan33 May 26, 2017
413a1ce
Merge branch 'branchModel-Update' into branchModel-delete
j0nathan33 May 26, 2017
c100016
Merge branch 'master' of https://github.com/cdancy/bitbucket-rest int…
j0nathan33 May 29, 2017
83bc6b0
Change gitAttribute for windows
j0nathan33 May 29, 2017
2b1b803
Fix build
May 29, 2017
e2ec092
Remove line don't work in some enviromnent
May 29, 2017
b8069f4
Revert "Change gitAttribute for windows"
May 29, 2017
a441fe9
Add annotation Documentation
May 29, 2017
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
18 changes: 18 additions & 0 deletions src/main/java/com/cdancy/bitbucket/rest/features/BranchApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.cdancy.bitbucket.rest.fallbacks.BitbucketFallbacks;
import com.cdancy.bitbucket.rest.filters.BitbucketAuthentication;
import com.cdancy.bitbucket.rest.options.CreateBranch;
import com.cdancy.bitbucket.rest.options.CreateBranchModelConfiguration;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.rest.annotations.BinderParam;
import org.jclouds.rest.annotations.Fallback;
Expand Down Expand Up @@ -126,6 +127,23 @@ BranchModel model(@PathParam("project") String project,
BranchModelConfiguration getModelConfiguration(@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)
@PUT
BranchModelConfiguration updateModelConfiguration(@PathParam("project") String project,
@PathParam("repo") String repo,
@BinderParam(BindToJsonPayload.class) CreateBranchModelConfiguration config);

@Named("branch:delete-model-configuration")
@Consumes(MediaType.APPLICATION_JSON)
@Path("/branch-utils/{jclouds.api-version}/projects/{project}/repos/{repo}/branchmodel/configuration")
@Fallback(BitbucketFallbacks.FalseOnError.class)
@DELETE
boolean deleteModelConfiguration(@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.

@documentation annotation for this? Searched online and couldn't find anything

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup, this endpoint isn't in documentation.

Source : https://jira.atlassian.com/browse/BSERV-5411

Copy link
Owner

Choose a reason for hiding this comment

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

@j0nathan33 can you add the Documentation annotation and use that jira ISSUE?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok, I will do it.

@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
Expand Up @@ -29,6 +29,7 @@
import com.cdancy.bitbucket.rest.domain.branch.Type;
import com.cdancy.bitbucket.rest.domain.pullrequest.User;
import com.cdancy.bitbucket.rest.options.CreateBranch;
import com.cdancy.bitbucket.rest.options.CreateBranchModelConfiguration;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -146,18 +147,66 @@ public void testGetBranchModelConfiguration() {
checkDefaultBranchConfiguration();
}

@Test(dependsOnMethods = {"testCreateBranch", "testListBranches"})
@Test(dependsOnMethods = {"testGetBranchModelConfiguration", "testCreateBranch", "testListBranches"})
public void testUpdateBranchModelConfiguration() {
List<Type> types = new ArrayList<>();
types.add(Type.create(Type.TypeId.BUGFIX, null, "bug/", false));
types.add(Type.create(Type.TypeId.HOTFIX, null, "hot/", true));
types.add(Type.create(Type.TypeId.RELEASE, null, "rel/", true));
types.add(Type.create(Type.TypeId.FEATURE, null, "fea/", true));
CreateBranchModelConfiguration configuration = CreateBranchModelConfiguration.create(branchModelConfiguration.development(), null, types);


BranchModelConfiguration branchModelConfiguration = api().updateModelConfiguration(projectKey, repoKey, configuration);
assertThat(branchModelConfiguration).isNotNull();
assertThat(branchModelConfiguration.errors().isEmpty()).isTrue();
assertThat(branchModelConfiguration.development().refId()).isNotNull();
Copy link
Owner

Choose a reason for hiding this comment

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

Removed this check in master as it was null for me on my version of Bitbucket.

assertThat(branchModelConfiguration.production()).isNull();
assertThat(branchModelConfiguration.types().size() == 4);
for (Type type : branchModelConfiguration.types()) {
switch (type.id()) {
case BUGFIX:
assertThat(type.prefix()).isEqualTo("bug/");
assertThat(type.enabled()).isFalse();
break;
case HOTFIX:
assertThat(type.prefix()).isEqualTo("hot/");
assertThat(type.enabled()).isTrue();
break;
case RELEASE:
assertThat(type.prefix()).isEqualTo("rel/");
assertThat(type.enabled()).isTrue();
break;
case FEATURE:
assertThat(type.prefix()).isEqualTo("fea/");
assertThat(type.enabled()).isTrue();
break;
default:
break;
}
}
}

@Test(dependsOnMethods = {"testGetBranchModelConfiguration", "testCreateBranch", "testListBranches", "testUpdateBranchModelConfiguration"})
public void testDeleteBranchModelConfiguration() {
boolean success = api().deleteModelConfiguration(projectKey, repoKey);
assertThat(success).isTrue();
}

@Test(dependsOnMethods = {"testDeleteBranchModelConfiguration", "testGetBranchModelConfiguration", "testCreateBranch",
"testListBranches", "testUpdateBranchModelConfiguration"})
public void testGetBranchModelConfigurationOnError() {
BranchModelConfiguration configuration = api().getModelConfiguration(projectKey, "12345");
assertThat(configuration.errors()).isNotEmpty();
assertThat(configuration.development()).isNull();
assertThat(configuration.production()).isNull();
BranchModelConfiguration configuration = api().getModelConfiguration(projectKey, repoKey);
assertThat(configuration.types()).isEmpty();
assertThat(configuration.production()).isNull();
assertThat(configuration.development()).isNull();
assertThat(configuration.errors()).isNotEmpty();
}

private void checkDefaultBranchConfiguration() {
assertThat(branchModelConfiguration).isNotNull();
assertThat(branchModelConfiguration.errors().isEmpty()).isTrue();
assertThat(branchModelConfiguration.development().refId()).isNotNull();
Copy link
Owner

Choose a reason for hiding this comment

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

Removed this check as well as it was null for me.

assertThat(branchModelConfiguration.production()).isNull();
assertThat(branchModelConfiguration.types().size() == 4);
for (Type type : branchModelConfiguration.types()) {
Expand Down Expand Up @@ -187,6 +236,11 @@ public void fin() {
assertThat(success).isTrue();
success = api().delete(projectKey, repoKey, "refs/heads/" + branchName);
assertThat(success).isTrue();
if (branchModelConfiguration != null) {
branchModelConfiguration = api().updateModelConfiguration(projectKey, repoKey,
CreateBranchModelConfiguration.create(branchModelConfiguration));
checkDefaultBranchConfiguration();
}
}

private BranchApi api() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,19 @@
import com.cdancy.bitbucket.rest.BitbucketApi;
import com.cdancy.bitbucket.rest.BitbucketApiMetadata;
import com.cdancy.bitbucket.rest.domain.branch.Branch;
import com.cdancy.bitbucket.rest.domain.branch.BranchConfiguration;
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.internal.BaseBitbucketMockTest;
import com.cdancy.bitbucket.rest.options.CreateBranch;
import com.cdancy.bitbucket.rest.options.CreateBranchModelConfiguration;
import com.google.common.collect.ImmutableMap;
import com.squareup.okhttp.mockwebserver.MockResponse;
import com.squareup.okhttp.mockwebserver.MockWebServer;
Expand Down Expand Up @@ -363,4 +366,99 @@ public void testGetBranchModelConfigurationOnError() throws Exception {
server.shutdown();
}
}

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

server.enqueue(new MockResponse().setBody(payloadFromResource("/branch-model-configuration.json")).setResponseCode(200));
BitbucketApi baseApi = api(server.getUrl("/"));
BranchApi api = baseApi.branchApi();
try {
String projectKey = "PRJ";
String repoKey = "myrepo";

BranchConfiguration branchConfiguration = BranchConfiguration.create("test", false);
List<Type> types = new ArrayList<>();
types.add(Type.create(Type.TypeId.BUGFIX, "test", "test", true));

CreateBranchModelConfiguration branchModelConfiguration = CreateBranchModelConfiguration.create(branchConfiguration, null, types);

BranchModelConfiguration configuration = api.updateModelConfiguration(projectKey, repoKey, branchModelConfiguration);
assertThat(configuration).isNotNull();
assertThat(configuration.errors().isEmpty()).isTrue();
assertThat(configuration.types().size() > 0).isTrue();
assertSent(server, "PUT", "/rest/branch-utils/" + BitbucketApiMetadata.API_VERSION
+ "/projects/" + projectKey + "/repos/" + repoKey + "/branchmodel/configuration");
} finally {
baseApi.close();
server.shutdown();
}
}

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

server.enqueue(new MockResponse().setBody(payloadFromResource("/branch-model-configuration-error.json")).setResponseCode(400));
BitbucketApi baseApi = api(server.getUrl("/"));
BranchApi api = baseApi.branchApi();
try {
String projectKey = "PRJ";
String repoKey = "myrepo";

BranchConfiguration branchConfiguration = BranchConfiguration.create("test", false);
List<Type> types = new ArrayList<>();
types.add(Type.create(Type.TypeId.BUGFIX, "test", "test", true));

CreateBranchModelConfiguration branchModelConfiguration = CreateBranchModelConfiguration.create(branchConfiguration, null, types);

BranchModelConfiguration configuration = api.updateModelConfiguration(projectKey, repoKey, branchModelConfiguration);
assertThat(configuration).isNotNull();
assertThat(configuration.errors()).isNotEmpty();
assertThat(configuration.production()).isNull();
assertThat(configuration.development()).isNull();
assertSent(server, "PUT", "/rest/branch-utils/" + BitbucketApiMetadata.API_VERSION
+ "/projects/" + projectKey + "/repos/" + repoKey + "/branchmodel/configuration");
} finally {
baseApi.close();
server.shutdown();
}
}

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

server.enqueue(new MockResponse().setResponseCode(204));
BitbucketApi baseApi = api(server.getUrl("/"));
BranchApi api = baseApi.branchApi();
try {
String projectKey = "PRJ";
String repoKey = "myrepo";
boolean success = api.deleteModelConfiguration(projectKey, repoKey);
assertThat(success).isTrue();
assertSent(server, "DELETE", "/rest/branch-utils/" + BitbucketApiMetadata.API_VERSION
+ "/projects/" + projectKey + "/repos/" + repoKey + "/branchmodel/configuration");
} finally {
baseApi.close();
server.shutdown();
}
}

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

server.enqueue(new MockResponse().setBody(payloadFromResource("/repository-not-exist.json")).setResponseCode(404));
BitbucketApi baseApi = api(server.getUrl("/"));
BranchApi api = baseApi.branchApi();
try {
String projectKey = "PRJ";
String repoKey = "myrepo";
boolean success = api.deleteModelConfiguration(projectKey, repoKey);
assertThat(success).isFalse();
assertSent(server, "DELETE", "/rest/branch-utils/" + BitbucketApiMetadata.API_VERSION
+ "/projects/" + projectKey + "/repos/" + repoKey + "/branchmodel/configuration");
} finally {
baseApi.close();
server.shutdown();
}
}
}