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 endpoint build api #59

Merged
merged 8 commits into from
Apr 28, 2017
Merged
Show file tree
Hide file tree
Changes from 6 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
4 changes: 4 additions & 0 deletions src/main/java/com/cdancy/bitbucket/rest/BitbucketApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.Closeable;

import com.cdancy.bitbucket.rest.features.AdminApi;
import com.cdancy.bitbucket.rest.features.BuildApi;
import com.cdancy.bitbucket.rest.features.BranchApi;
import com.cdancy.bitbucket.rest.features.CommentsApi;
import com.cdancy.bitbucket.rest.features.CommitsApi;
Expand Down Expand Up @@ -59,4 +60,7 @@ public interface BitbucketApi extends Closeable {

@Delegate
AdminApi adminApi();

@Delegate
BuildApi buildApi();
Copy link
Owner

Choose a reason for hiding this comment

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

Lets rename to BuildStatusApi to better align with this http path.

}
48 changes: 48 additions & 0 deletions src/main/java/com/cdancy/bitbucket/rest/domain/build/Status.java
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.build;

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

@AutoValue
public abstract class Status {

public enum StatusState {
SUCCESSFUL,
FAILED
}

public abstract long dateAdded();

public abstract String description();

public abstract String key();

public abstract String name();

public abstract StatusState state();

public abstract String url();

@SerializedNames({"dateAdded", "description", "key", "name", "state", "url"})
public static Status create(long dateAdded, String description, String key, String name, StatusState state,
String url) {
return new AutoValue_Status(dateAdded, description, key, name, state, url);
}
}
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.build;

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 StatusPage implements Page<Status>, ErrorsHolder {

@SerializedNames({"start", "limit", "size", "nextPageStart", "isLastPage", "values", "errors"})
public static StatusPage create(int start, int limit, int size, int nextPageStart, boolean isLastPage,
@Nullable List<Status> values, @Nullable List<Error> errors) {
return new AutoValue_StatusPage(start, limit, size, nextPageStart, isLastPage,
Utils.nullToEmpty(values), Utils.nullToEmpty(errors));
}
}
36 changes: 36 additions & 0 deletions src/main/java/com/cdancy/bitbucket/rest/domain/build/Summary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.build;

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

@AutoValue
public abstract class Summary {

public abstract long failed();

public abstract long inProgress();

public abstract long successful();

@SerializedNames({"failed", "inProgress", "successful"})
public static Summary create(long failed, long inProgress, long successful) {
return new AutoValue_Summary(failed, inProgress, successful);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public abstract class Reference {
@Nullable
public abstract String displayId();

@Nullable
public abstract String latestCommit();

Reference() {
}

Expand All @@ -47,8 +50,13 @@ public static Reference create(String id, MinimalRepository repository) {
return create(id, repository, displayId);
}

@SerializedNames({"id", "repository", "displayId"})
@Deprecated
public static Reference create(String id, MinimalRepository repository, String displayId) {
return new AutoValue_Reference(id != null ? id : "refs/heads/master", repository, displayId);
return create(id, repository, displayId, null);
}

@SerializedNames({"id", "repository", "displayId", "latestCommit"})
public static Reference create(String id, MinimalRepository repository, String displayId, String latestCommit) {
return new AutoValue_Reference(id != null ? id : "refs/heads/master", repository, displayId, latestCommit);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.cdancy.bitbucket.rest.domain.branch.BranchModel;
import com.cdancy.bitbucket.rest.domain.branch.BranchPage;
import com.cdancy.bitbucket.rest.domain.branch.BranchPermissionPage;
import com.cdancy.bitbucket.rest.domain.build.StatusPage;
import com.cdancy.bitbucket.rest.domain.comment.Comments;
import com.cdancy.bitbucket.rest.domain.common.Error;
import com.cdancy.bitbucket.rest.domain.participants.Participants;
Expand Down Expand Up @@ -104,6 +105,15 @@ public Object createOrPropagate(Throwable throwable) throws Exception {
}
}

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

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

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

public static BranchPermissionPage createBranchPermissionPageFromErrors(List<Error> errors) {
return BranchPermissionPage.create(-1, -1, -1, -1, true, null, errors);
}
Expand Down
59 changes: 59 additions & 0 deletions src/main/java/com/cdancy/bitbucket/rest/features/BuildApi.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.features;

import com.cdancy.bitbucket.rest.annotations.Documentation;
import com.cdancy.bitbucket.rest.domain.build.StatusPage;
import com.cdancy.bitbucket.rest.domain.build.Summary;
import com.cdancy.bitbucket.rest.fallbacks.BitbucketFallbacks;
import com.cdancy.bitbucket.rest.filters.BitbucketAuthentication;
import org.jclouds.javax.annotation.Nullable;
import org.jclouds.rest.annotations.Fallback;
import org.jclouds.rest.annotations.RequestFilters;

import javax.inject.Named;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

@Produces(MediaType.APPLICATION_JSON)
@RequestFilters(BitbucketAuthentication.class)
@Path("/rest/build-status/{jclouds.api-version}")
public interface BuildApi {

@Named("build:status")
@Documentation({"https://developer.atlassian.com/static/rest/bitbucket-server/4.14.4/bitbucket-build-rest.html#idm44911111531152"})
@Consumes(MediaType.APPLICATION_JSON)
@Path("/commits/{commitId}")
@Fallback(BitbucketFallbacks.StatusPageOnError.class)
@GET
StatusPage status(@PathParam("commitId") String commitId,
@Nullable @QueryParam("start") Integer start,
@Nullable @QueryParam("limit") Integer limit);

@Named("build:status-summary")
@Documentation({"https://developer.atlassian.com/static/rest/bitbucket-server/4.14.4/bitbucket-build-rest.html#idm44911111484336"})
@Consumes(MediaType.APPLICATION_JSON)
@Path("/commits/stats/{commitId}")
@GET
Summary summary(@PathParam("commitId") String commitId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.features;

import com.cdancy.bitbucket.rest.BaseBitbucketApiLiveTest;
import com.cdancy.bitbucket.rest.domain.build.StatusPage;
import com.cdancy.bitbucket.rest.domain.build.Summary;
import org.testng.annotations.Test;

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

@Test(groups = "live", testName = "BuildApiLiveTest")
public class BuildApiLiveTest extends BaseBitbucketApiLiveTest {

@Test
public void testGetStatusByNonExistentCommit() {
StatusPage statusPage = api().status(randomString(),0,100);
assertThat(statusPage).isNotNull();
assertThat(statusPage.size() == 0).isTrue();
}

@Test
public void testGetSummaryByNonExistentCommit() {
Summary statusPage = api().summary(randomString());
assertThat(statusPage).isNotNull();
assertThat(statusPage.successful() == 0).isTrue();
assertThat(statusPage.inProgress() == 0).isTrue();
assertThat(statusPage.failed() == 0).isTrue();
}

private BuildApi api() {
return api.buildApi();
}
}
Loading