Skip to content

Commit 86867ab

Browse files
committed
add organizations endpoint
1 parent d424294 commit 86867ab

File tree

12 files changed

+735
-2
lines changed

12 files changed

+735
-2
lines changed

src/main/java/org/sourcelab/buildkite/api/client/BuildkiteClient.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
import org.sourcelab.buildkite.api.client.request.GetUserRequest;
3333
import org.sourcelab.buildkite.api.client.request.ListBuildsRequest;
3434
import org.sourcelab.buildkite.api.client.request.ListEmojisRequest;
35+
import org.sourcelab.buildkite.api.client.request.ListOrganizationsRequest;
36+
import org.sourcelab.buildkite.api.client.request.OrganizationFilters;
37+
import org.sourcelab.buildkite.api.client.request.OrganizationFiltersBuilder;
3538
import org.sourcelab.buildkite.api.client.request.PageOptions;
3639
import org.sourcelab.buildkite.api.client.request.PageableRequest;
3740
import org.sourcelab.buildkite.api.client.request.PingRequest;
@@ -41,15 +44,19 @@
4144
import org.sourcelab.buildkite.api.client.response.Emoji;
4245
import org.sourcelab.buildkite.api.client.response.ErrorResponse;
4346
import org.sourcelab.buildkite.api.client.response.ListBuildsResponse;
47+
import org.sourcelab.buildkite.api.client.response.ListOrganizationsResponse;
4448
import org.sourcelab.buildkite.api.client.response.MetaResponse;
49+
import org.sourcelab.buildkite.api.client.response.Organization;
4550
import org.sourcelab.buildkite.api.client.response.PageableResponse;
4651
import org.sourcelab.buildkite.api.client.response.PagingLinks;
4752
import org.sourcelab.buildkite.api.client.response.PingResponse;
4853
import org.sourcelab.buildkite.api.client.response.parser.ErrorResponseParser;
54+
import org.sourcelab.buildkite.api.client.response.parser.ListOrganizationsResponseParser;
4955

5056
import java.io.IOException;
5157
import java.util.List;
5258
import java.util.Objects;
59+
import java.util.Optional;
5360

5461
/**
5562
* API Client for Buildkite's REST Api.
@@ -112,6 +119,61 @@ public CurrentUserResponse getUser() throws BuildkiteException {
112119
return executeRequest(new GetUserRequest());
113120
}
114121

122+
/**
123+
* Retrieve all Organizations accessible to the current user/API access token.
124+
* Results will be paged.
125+
*
126+
* @return All Organizations accessible to the current user/API access token. Results
127+
* will be paged if the number of results exceeds 30.
128+
* @throws BuildkiteException if API returns an error response.
129+
*/
130+
public ListOrganizationsResponse listOrganizations() throws BuildkiteException {
131+
return listOrganizations(OrganizationFilters.newBuilder());
132+
}
133+
134+
/**
135+
* Retrieve all Organizations accessible to the current user/API access token.
136+
* Results will be paged.
137+
*
138+
* @param filters Filter criteria.
139+
* @return All Organizations accessible to the current user/API access token. Results
140+
* will be paged if the number of results exceeds 30.
141+
* @throws BuildkiteException if API returns an error response.
142+
*/
143+
public ListOrganizationsResponse listOrganizations(final OrganizationFiltersBuilder filters) throws BuildkiteException {
144+
Objects.requireNonNull(filters);
145+
return listOrganizations(filters.build());
146+
}
147+
148+
/**
149+
* Retrieve all Organizations accessible to the current user/API access token.
150+
* Results will be paged.
151+
*
152+
* @param filters Filter criteria.
153+
* @return All Organizations accessible to the current user/API access token. Results
154+
* will be paged if the number of results exceeds 30.
155+
* @throws BuildkiteException if API returns an error response.
156+
*/
157+
public ListOrganizationsResponse listOrganizations(final OrganizationFilters filters) throws BuildkiteException {
158+
Objects.requireNonNull(filters);
159+
return executeRequest(new ListOrganizationsRequest(filters));
160+
}
161+
162+
/**
163+
* Retrieve specific organization via its Organization slug id.
164+
*
165+
* @param organizationSlugId Slug of the organization to retrieve.
166+
* @return Organization matching the slug, if found.
167+
* @throws BuildkiteException if API returns an error response.
168+
*/
169+
public Optional<Organization> getOrganization(final String organizationSlugId) throws BuildkiteException {
170+
Objects.requireNonNull(organizationSlugId);
171+
final ListOrganizationsResponse response = listOrganizations(OrganizationFilters.newBuilder()
172+
.withOrganization(organizationSlugId)
173+
);
174+
return response.getOrganizationBySlug(organizationSlugId);
175+
}
176+
115177
/**
116178
* Retrieves metadata endpoint.
117179
* @see <a href="https://buildkite.com/docs/apis/rest-api/meta#get-meta-information">https://buildkite.com/docs/apis/rest-api/meta#get-meta-information</a>
@@ -148,6 +210,7 @@ public ListBuildsResponse listBuilds() {
148210
/**
149211
* Retrieve all builds which match the supplied search criteria.
150212
*
213+
* @param filtersBuilder Filter criteria.
151214
* @return All builds which match the supplied search criteria. Results will be paged if the number of results
152215
* exceeds 30 (or the page limit specified in the search criteria).
153216
*
@@ -160,6 +223,7 @@ public ListBuildsResponse listBuilds(final BuildFiltersBuilder filtersBuilder) {
160223
/**
161224
* Retrieve all builds which match the supplied search criteria.
162225
*
226+
* @param filters Filter criteria.
163227
* @return All builds which match the supplied search criteria. Results will be paged if the number of results
164228
* exceeds 30 (or the page limit specified in the search criteria).
165229
*

src/main/java/org/sourcelab/buildkite/api/client/request/BuildFilters.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
/**
2828
* Optional Properties to filter Builds.
2929
*/
30-
public class BuildFilters {
30+
public class BuildFilters implements Filters {
3131
private final Set<String> branches;
3232
private final Set<String> commits;
3333
private final ZonedDateTime createdFrom;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.sourcelab.buildkite.api.client.request;
2+
3+
/**
4+
* Copyright 2023 SourceLab.org https://github.com/SourceLabOrg/Buildkite-Api-Client
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
7+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
8+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
9+
* persons to whom the Software is furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
12+
* Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
15+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
17+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18+
*/
19+
public interface Filters {
20+
public PageOptions getPageOptions();
21+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Copyright 2023 SourceLab.org https://github.com/SourceLabOrg/Buildkite-Api-Client
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
7+
* persons to whom the Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*/
17+
18+
package org.sourcelab.buildkite.api.client.request;
19+
20+
import org.sourcelab.buildkite.api.client.response.ListOrganizationsResponse;
21+
import org.sourcelab.buildkite.api.client.response.parser.ListOrganizationsResponseParser;
22+
import org.sourcelab.buildkite.api.client.response.parser.ResponseParser;
23+
24+
import java.util.Objects;
25+
26+
public class ListOrganizationsRequest extends GetRequest<ListOrganizationsResponse> implements PageableRequest<ListOrganizationsResponse> {
27+
private final OrganizationFilters filters;
28+
private PageOptions pageOptions;
29+
30+
/**
31+
* Constructor.
32+
* @param filters Search Criteria.
33+
*/
34+
public ListOrganizationsRequest(final OrganizationFilters filters) {
35+
Objects.requireNonNull(filters);
36+
this.filters = filters;
37+
this.pageOptions = filters.getPageOptions() == null ? PageOptions.getDefault() : filters.getPageOptions();
38+
}
39+
40+
@Override
41+
public String getPath() {
42+
// TODO need to urlencode these?
43+
if (filters.hasOrgIdSlug()) {
44+
return "/v2/organizations/" + filters.getOrgIdSlug();
45+
}
46+
return "/v2/organizations";
47+
}
48+
49+
@Override
50+
public RequestParameters getRequestParameters() {
51+
final RequestParametersBuilder builder = RequestParameters.newBuilder();
52+
53+
// Paging options
54+
builder.withParameter("per_page", pageOptions.getPerPage());
55+
builder.withParameter("page", pageOptions.getPage());
56+
57+
return builder.build();
58+
}
59+
60+
@Override
61+
public ResponseParser<ListOrganizationsResponse> getResponseParser() {
62+
return new ListOrganizationsResponseParser(this);
63+
}
64+
65+
@Override
66+
public void updatePageOptions(final PageOptions pageOptions) {
67+
this.pageOptions = Objects.requireNonNull(pageOptions);
68+
}
69+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* Copyright 2023 SourceLab.org https://github.com/SourceLabOrg/Buildkite-Api-Client
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
7+
* persons to whom the Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*/
17+
18+
package org.sourcelab.buildkite.api.client.request;
19+
20+
/**
21+
* Optional Properties to filter Organizations.
22+
*/
23+
public class OrganizationFilters implements Filters {
24+
private final PageOptions pageOptions;
25+
private final String orgIdSlug;
26+
27+
/**
28+
* Builder for {@link OrganizationFilters}.
29+
* @return Builder for {@link OrganizationFilters}.
30+
*/
31+
public static OrganizationFiltersBuilder newBuilder() {
32+
return new OrganizationFiltersBuilder();
33+
}
34+
35+
/**
36+
* Constructor.
37+
* Use {@link BuildFiltersBuilder} to create instances.
38+
*/
39+
public OrganizationFilters(
40+
final PageOptions pageOptions,
41+
final String orgIdSlug
42+
) {
43+
this.pageOptions = pageOptions == null ? PageOptions.getDefault() : pageOptions;
44+
this.orgIdSlug = orgIdSlug;
45+
}
46+
47+
public String getOrgIdSlug() {
48+
return orgIdSlug;
49+
}
50+
51+
public boolean hasOrgIdSlug() {
52+
return orgIdSlug != null;
53+
}
54+
55+
public PageOptions getPageOptions() {
56+
return pageOptions;
57+
}
58+
59+
@Override
60+
public String toString() {
61+
return "OrganizationFilters{"
62+
+ "pageOptions=" + pageOptions
63+
+ ", orgIdSlug='" + orgIdSlug + '\''
64+
+ '}';
65+
}
66+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/**
2+
* Copyright 2023 SourceLab.org https://github.com/SourceLabOrg/Buildkite-Api-Client
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
5+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
6+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
7+
* persons to whom the Software is furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
13+
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
14+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
15+
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
16+
*/
17+
18+
package org.sourcelab.buildkite.api.client.request;
19+
20+
/**
21+
* Builder for {@link BuildFilters}.
22+
*/
23+
public final class OrganizationFiltersBuilder {
24+
private PageOptions pageOptions = null;
25+
private String orgIdSlug = null;
26+
27+
/**
28+
* Constructor.
29+
*/
30+
public OrganizationFiltersBuilder() {
31+
}
32+
33+
/**
34+
* Set the number of results per page.
35+
* @param perPage Set the number of results per page.
36+
* @return BuildOrganizationFiltersBuilder for method chaining.
37+
*/
38+
public OrganizationFiltersBuilder withPerPage(final int perPage) {
39+
if (this.pageOptions == null) {
40+
withPageOptions(PageOptions.getDefault());
41+
}
42+
return withPageOptions(new PageOptions(pageOptions.getPage(), perPage));
43+
}
44+
45+
/**
46+
* Set the page to retrieve.
47+
* @param page Set the page to retrieve.
48+
* @return BuildOrganizationFiltersBuilder for method chaining.
49+
*/
50+
public OrganizationFiltersBuilder withPage(final int page) {
51+
if (this.pageOptions == null) {
52+
withPageOptions(PageOptions.getDefault());
53+
}
54+
return withPageOptions(new PageOptions(page, pageOptions.getPerPage()));
55+
}
56+
57+
/**
58+
* Apply Paging Options.
59+
* @param page Set the page to retrieve.
60+
* @param perPage Set the number of results per page.
61+
* @return BuildOrganizationFiltersBuilder for method chaining.
62+
*/
63+
public OrganizationFiltersBuilder withPageOptions(final int page, final int perPage) {
64+
return withPageOptions(new PageOptions(page, perPage));
65+
}
66+
67+
/**
68+
* Apply Paging Options.
69+
* @param pageOptions Paging options to apply.
70+
* @return BuildOrganizationFiltersBuilder for method chaining.
71+
*/
72+
public OrganizationFiltersBuilder withPageOptions(final PageOptions pageOptions) {
73+
this.pageOptions = pageOptions;
74+
return this;
75+
}
76+
77+
public OrganizationFiltersBuilder withOrganization(final String orgIdSlug) {
78+
this.orgIdSlug = orgIdSlug;
79+
return this;
80+
}
81+
82+
/**
83+
* New OrganizationFilters instance using configured properties.
84+
* @return New OrganizationFilters instance using configured properties.
85+
*/
86+
public OrganizationFilters build() {
87+
return new OrganizationFilters(
88+
pageOptions,
89+
orgIdSlug
90+
);
91+
}
92+
}

src/main/java/org/sourcelab/buildkite/api/client/response/ListBuildsResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public List<Build> getBuilds() {
7272
* The total number of builds found.
7373
* @return The total number of builds found.
7474
*/
75-
public int countBuilds() {
75+
public int count() {
7676
return getBuilds().size();
7777
}
7878

0 commit comments

Comments
 (0)