Skip to content

Commit 814ceb5

Browse files
j0nathan33cdancy
authored andcommitted
Add new endpoint build api (#59)
* Add new api call for get information from bamboo * Fix test and Add new LiveTest * Add missing latestCommit in object Reference * Fix build * Fix missing file * Edit test after codeReview * Rename BuildApi to BuildStatusApi after codeReview * Fix typo
1 parent 8172725 commit 814ceb5

14 files changed

+406
-4
lines changed

src/main/java/com/cdancy/bitbucket/rest/BitbucketApi.java

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.io.Closeable;
2121

2222
import com.cdancy.bitbucket.rest.features.AdminApi;
23+
import com.cdancy.bitbucket.rest.features.BuildStatusApi;
2324
import com.cdancy.bitbucket.rest.features.BranchApi;
2425
import com.cdancy.bitbucket.rest.features.CommentsApi;
2526
import com.cdancy.bitbucket.rest.features.CommitsApi;
@@ -59,4 +60,7 @@ public interface BitbucketApi extends Closeable {
5960

6061
@Delegate
6162
AdminApi adminApi();
63+
64+
@Delegate
65+
BuildStatusApi buildStatusApi();
6266
}
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.build;
19+
20+
import com.google.auto.value.AutoValue;
21+
import org.jclouds.json.SerializedNames;
22+
23+
@AutoValue
24+
public abstract class Status {
25+
26+
public enum StatusState {
27+
SUCCESSFUL,
28+
FAILED
29+
}
30+
31+
public abstract long dateAdded();
32+
33+
public abstract String description();
34+
35+
public abstract String key();
36+
37+
public abstract String name();
38+
39+
public abstract StatusState state();
40+
41+
public abstract String url();
42+
43+
@SerializedNames({"dateAdded", "description", "key", "name", "state", "url"})
44+
public static Status create(long dateAdded, String description, String key, String name, StatusState state,
45+
String url) {
46+
return new AutoValue_Status(dateAdded, description, key, name, state, url);
47+
}
48+
}
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.build;
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.domain.common.Page;
23+
import com.cdancy.bitbucket.rest.utils.Utils;
24+
import com.google.auto.value.AutoValue;
25+
import org.jclouds.javax.annotation.Nullable;
26+
import org.jclouds.json.SerializedNames;
27+
28+
import java.util.List;
29+
30+
@AutoValue
31+
public abstract class StatusPage implements Page<Status>, ErrorsHolder {
32+
33+
@SerializedNames({"start", "limit", "size", "nextPageStart", "isLastPage", "values", "errors"})
34+
public static StatusPage create(int start, int limit, int size, int nextPageStart, boolean isLastPage,
35+
@Nullable List<Status> values, @Nullable List<Error> errors) {
36+
return new AutoValue_StatusPage(start, limit, size, nextPageStart, isLastPage,
37+
Utils.nullToEmpty(values), Utils.nullToEmpty(errors));
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.build;
19+
20+
import com.google.auto.value.AutoValue;
21+
import org.jclouds.json.SerializedNames;
22+
23+
@AutoValue
24+
public abstract class Summary {
25+
26+
public abstract long failed();
27+
28+
public abstract long inProgress();
29+
30+
public abstract long successful();
31+
32+
@SerializedNames({"failed", "inProgress", "successful"})
33+
public static Summary create(long failed, long inProgress, long successful) {
34+
return new AutoValue_Summary(failed, inProgress, successful);
35+
}
36+
}

src/main/java/com/cdancy/bitbucket/rest/domain/pullrequest/Reference.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public abstract class Reference {
3434
@Nullable
3535
public abstract String displayId();
3636

37+
@Nullable
38+
public abstract String latestCommit();
39+
3740
Reference() {
3841
}
3942

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

50-
@SerializedNames({"id", "repository", "displayId"})
53+
@Deprecated
5154
public static Reference create(String id, MinimalRepository repository, String displayId) {
52-
return new AutoValue_Reference(id != null ? id : "refs/heads/master", repository, displayId);
55+
return create(id, repository, displayId, null);
56+
}
57+
58+
@SerializedNames({"id", "repository", "displayId", "latestCommit"})
59+
public static Reference create(String id, MinimalRepository repository, String displayId, String latestCommit) {
60+
return new AutoValue_Reference(id != null ? id : "refs/heads/master", repository, displayId, latestCommit);
5361
}
5462
}

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

+14
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.cdancy.bitbucket.rest.domain.branch.BranchModel;
3030
import com.cdancy.bitbucket.rest.domain.branch.BranchPage;
3131
import com.cdancy.bitbucket.rest.domain.branch.BranchPermissionPage;
32+
import com.cdancy.bitbucket.rest.domain.build.StatusPage;
3233
import com.cdancy.bitbucket.rest.domain.comment.Comments;
3334
import com.cdancy.bitbucket.rest.domain.common.Error;
3435
import com.cdancy.bitbucket.rest.domain.participants.Participants;
@@ -104,6 +105,15 @@ public Object createOrPropagate(Throwable throwable) throws Exception {
104105
}
105106
}
106107

108+
public static final class StatusPageOnError implements Fallback<Object> {
109+
public Object createOrPropagate(Throwable throwable) throws Exception {
110+
if (checkNotNull(throwable, "throwable") != null) {
111+
return createStatusPageFromErrors(getErrors(throwable.getMessage()));
112+
}
113+
throw propagate(throwable);
114+
}
115+
}
116+
107117
public static final class BranchPermissionPageOnError implements Fallback<Object> {
108118
public Object createOrPropagate(Throwable throwable) throws Exception {
109119
if (checkNotNull(throwable, "throwable") != null) {
@@ -273,6 +283,10 @@ public static UserPage createUserPageFromErrors(List<Error> errors) {
273283
return UserPage.create(-1, -1, -1, -1, true, null, errors);
274284
}
275285

286+
public static StatusPage createStatusPageFromErrors(List<Error> errors) {
287+
return StatusPage.create(-1, -1, -1, -1, true, null, errors);
288+
}
289+
276290
public static BranchPermissionPage createBranchPermissionPageFromErrors(List<Error> errors) {
277291
return BranchPermissionPage.create(-1, -1, -1, -1, true, null, errors);
278292
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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.features;
19+
20+
import com.cdancy.bitbucket.rest.annotations.Documentation;
21+
import com.cdancy.bitbucket.rest.domain.build.StatusPage;
22+
import com.cdancy.bitbucket.rest.domain.build.Summary;
23+
import com.cdancy.bitbucket.rest.fallbacks.BitbucketFallbacks;
24+
import com.cdancy.bitbucket.rest.filters.BitbucketAuthentication;
25+
import org.jclouds.javax.annotation.Nullable;
26+
import org.jclouds.rest.annotations.Fallback;
27+
import org.jclouds.rest.annotations.RequestFilters;
28+
29+
import javax.inject.Named;
30+
import javax.ws.rs.Consumes;
31+
import javax.ws.rs.GET;
32+
import javax.ws.rs.Path;
33+
import javax.ws.rs.PathParam;
34+
import javax.ws.rs.Produces;
35+
import javax.ws.rs.QueryParam;
36+
import javax.ws.rs.core.MediaType;
37+
38+
@Produces(MediaType.APPLICATION_JSON)
39+
@RequestFilters(BitbucketAuthentication.class)
40+
@Path("/rest/build-status/{jclouds.api-version}")
41+
public interface BuildStatusApi {
42+
43+
@Named("build:status")
44+
@Documentation({"https://developer.atlassian.com/static/rest/bitbucket-server/4.14.4/bitbucket-build-rest.html#idm44911111531152"})
45+
@Consumes(MediaType.APPLICATION_JSON)
46+
@Path("/commits/{commitId}")
47+
@Fallback(BitbucketFallbacks.StatusPageOnError.class)
48+
@GET
49+
StatusPage status(@PathParam("commitId") String commitId,
50+
@Nullable @QueryParam("start") Integer start,
51+
@Nullable @QueryParam("limit") Integer limit);
52+
53+
@Named("build:status-summary")
54+
@Documentation({"https://developer.atlassian.com/static/rest/bitbucket-server/4.14.4/bitbucket-build-rest.html#idm44911111484336"})
55+
@Consumes(MediaType.APPLICATION_JSON)
56+
@Path("/commits/stats/{commitId}")
57+
@GET
58+
Summary summary(@PathParam("commitId") String commitId);
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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.features;
19+
20+
import com.cdancy.bitbucket.rest.BaseBitbucketApiLiveTest;
21+
import com.cdancy.bitbucket.rest.domain.build.StatusPage;
22+
import com.cdancy.bitbucket.rest.domain.build.Summary;
23+
import org.testng.annotations.Test;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
27+
@Test(groups = "live", testName = "BuildStatusApiLiveTest")
28+
public class BuildStatusApiLiveTest extends BaseBitbucketApiLiveTest {
29+
30+
@Test
31+
public void testGetStatusByNonExistentCommit() {
32+
StatusPage statusPage = api().status(randomString(),0,100);
33+
assertThat(statusPage).isNotNull();
34+
assertThat(statusPage.size() == 0).isTrue();
35+
}
36+
37+
@Test
38+
public void testGetSummaryByNonExistentCommit() {
39+
Summary statusPage = api().summary(randomString());
40+
assertThat(statusPage).isNotNull();
41+
assertThat(statusPage.successful() == 0).isTrue();
42+
assertThat(statusPage.inProgress() == 0).isTrue();
43+
assertThat(statusPage.failed() == 0).isTrue();
44+
}
45+
46+
private BuildStatusApi api() {
47+
return api.buildStatusApi();
48+
}
49+
}

0 commit comments

Comments
 (0)