Skip to content

Commit aeaa211

Browse files
committed
Add readme
1 parent ec0a7b8 commit aeaa211

File tree

5 files changed

+237
-19
lines changed

5 files changed

+237
-19
lines changed

README.md

Lines changed: 123 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,123 @@
1-
# Buildkite-Api-Client
2-
Buildkite REST Api Client for Java
1+
# Buildkite-Api-Client Java REST Api Client
2+
3+
## What is it?
4+
5+
This library provides an easy to use client library for interacting with the [Buildkite Rest API](https://buildkite.com/docs/apis/rest-api) (V2).
6+
7+
## How to use this library
8+
9+
This client library is released on Maven Central. Add a new dependency to your project's POM file:
10+
11+
```xml
12+
<dependency>
13+
<groupId>org.sourcelab</groupId>
14+
<artifactId>buildkite-api-client</artifactId>
15+
<version>0.1.0</version>
16+
</dependency>
17+
```
18+
19+
20+
#### Example Code:
21+
```java
22+
/*
23+
* Create a new configuration object passing your API token.
24+
* Your API Token can be found/created at: https://buildkite.com/user/api-access-tokens
25+
*/
26+
final Configuration configuration = Configuration.newBuilder()
27+
.withApiToken("your-api-token-here")
28+
.build();
29+
30+
/*
31+
* Create an instance of BuildkiteClient, passing your configuration.
32+
*/
33+
final BuildkiteClient client = new BuildkiteClient(configuration);
34+
35+
/*
36+
* Making requests by calling the public methods available on BuildkiteClient.
37+
*
38+
* For example, to retrieve information about your own user:
39+
* https://buildkite.com/docs/apis/rest-api/user
40+
*/
41+
final CurrentUserResponse myUser = client.getUser();
42+
43+
/*
44+
* To retrieve your organizations:
45+
* https://buildkite.com/docs/apis/rest-api/organizations#list-organizations
46+
*/
47+
final ListOrganizationsResponse organizations = client.listOrganizations();
48+
49+
/*
50+
* To search for builds:
51+
*/
52+
final BuildFilters filters = BuildFilters.newBuilder()
53+
// Retrieve 1st page, with each page having 25 results
54+
.withPageOptions(1, 25)
55+
// Filtering for builds with state 'failed'
56+
.withStateChooser().failed()
57+
// And created within the last day
58+
.withCreatedFrom(ZonedDateTime.now().minusDays(1))
59+
.build();
60+
final ListBuildsResponse buildsResult = client.listBuilds(filters);
61+
62+
/*
63+
* To retrieve the 3rd page of the above query simply do:
64+
*/
65+
if (buildsResult.hasNextPage()) {
66+
final ListBuildsResponse buildsPage2 = client.nextPage(buildsPage1);
67+
}
68+
69+
/*
70+
* See BuildkiteClient for other available operations and end points.
71+
*/
72+
```
73+
74+
Public methods available on BuildkiteClient can be [found here](src/main/java/org/sourcelab/buildkite/api/client/BuildkiteClient.java#L111)
75+
76+
# Contributing
77+
78+
Found a bug? Think you've got an awesome feature you want to add? We welcome contributions!
79+
80+
81+
## Submitting a Contribution
82+
83+
1. Search for an existing issue. If none exists, create a new issue so that other contributors can keep track of what you are trying to add/fix and offer suggestions (or let you know if there is already an effort in progress). Be sure to clearly state the problem you are trying to solve and an explanation of why you want to use the strategy you're proposing to solve it.
84+
1. Fork this repository on GitHub and create a branch for your feature.
85+
1. Clone your fork and branch to your local machine.
86+
1. Commit changes to your branch.
87+
1. Push your work up to GitHub.
88+
1. Submit a pull request so that we can review your changes.
89+
90+
*Make sure that you rebase your branch off of master before opening a new pull request. We might also ask you to rebase it if master changes after you open your pull request.*
91+
92+
## Acceptance Criteria
93+
94+
We love contributions, but it's important that your pull request adhere to some of the standards we maintain in this repository.
95+
96+
- All tests must be passing!
97+
- All code changes require tests!
98+
- All code changes must be consistent with our checkstyle rules.
99+
- Great inline comments.
100+
101+
# Other Notes
102+
103+
104+
## Releasing
105+
106+
Steps for proper release:
107+
- Update release version: `mvn versions:set -DnewVersion=X.Y.Z`
108+
- Validate and then commit version: `mvn versions:commit`
109+
- Update CHANGELOG and README files.
110+
- Merge to master.
111+
- Deploy to Maven Central: `mvn clean deploy -P release`
112+
- Create release on Github project.
113+
114+
115+
## Changelog
116+
117+
The format is based on [Keep a Changelog](http://keepachangelog.com/)
118+
and this project adheres to [Semantic Versioning](http://semver.org/).
119+
120+
[View Changelog](CHANGELOG.md)
121+
122+
123+

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class RetryMultipleJobsOptions {
2929
private final String pipelineSlug;
3030
private final long buildNumber;
3131
private final Set<String> jobIds;
32+
private final boolean throwOnError;
3233

3334
/**
3435
* Builder instance for {@link RetryMultipleJobsOptions}.
@@ -41,11 +42,12 @@ public static RetryMultipleJobsOptionsBuilder newBuilder() {
4142
/**
4243
* Constructor.
4344
*/
44-
public RetryMultipleJobsOptions(final String organizationSlug, final String pipelineIdSlug, final long buildNumber, final Collection<String> jobIds) {
45+
public RetryMultipleJobsOptions(final String organizationSlug, final String pipelineIdSlug, final long buildNumber, final Collection<String> jobIds, final boolean throwOnError) {
4546
this.organizationSlug = organizationSlug;
4647
this.pipelineSlug = pipelineIdSlug;
4748
this.buildNumber = buildNumber;
4849
this.jobIds = new HashSet<>(jobIds);
50+
this.throwOnError = throwOnError;
4951
}
5052

5153
public String getOrganizationSlug() {
@@ -64,13 +66,25 @@ public Set<String> getJobIds() {
6466
return jobIds;
6567
}
6668

69+
/**
70+
* @return If true, if any single request fails, it will not process the remaining requests and immediately
71+
* throw an exception.
72+
*
73+
* If false, any errors will be captured by the return result, and further requests will continue.
74+
*
75+
*/
76+
public boolean isThrowOnError() {
77+
return throwOnError;
78+
}
79+
6780
@Override
6881
public String toString() {
6982
return "RetryMultipleJobsOptions{"
7083
+ "\n\torganizationSlug='" + organizationSlug + '\''
7184
+ "\n\tpipelineSlug='" + pipelineSlug + '\''
7285
+ "\n\tbuildNumber=" + buildNumber
7386
+ "\n\tjobIds=" + jobIds
87+
+ "\n\tthrowOnError=" + throwOnError
7488
+ "\n}";
7589
}
7690
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public final class RetryMultipleJobsOptionsBuilder {
2828
private String pipelineSlug = null;
2929
private Long buildNumber = null;
3030
private Set<String> jobIds = new HashSet<>();
31+
private boolean throwOnError = true;
3132

3233
public RetryMultipleJobsOptionsBuilder() {
3334
}
@@ -57,6 +58,11 @@ public RetryMultipleJobsOptionsBuilder withJobIds(final Collection<String> jobId
5758
return this;
5859
}
5960

61+
public RetryMultipleJobsOptionsBuilder withThrowExceptionOnError(final boolean throwExceptionOnError) {
62+
this.throwOnError = throwExceptionOnError;
63+
return this;
64+
}
65+
6066
/**
6167
* Create new {@link RetryMultipleJobsOptions} instance.
6268
* @return Create new {@link RetryMultipleJobsOptions} instance.
@@ -75,6 +81,6 @@ public RetryMultipleJobsOptions build() {
7581
if (jobIds.isEmpty()) {
7682
throw new BuilderValidationException("At least one JobId must be provided.");
7783
}
78-
return new RetryMultipleJobsOptions(organizationSlug, pipelineSlug, buildNumber, jobIds);
84+
return new RetryMultipleJobsOptions(organizationSlug, pipelineSlug, buildNumber, jobIds, throwOnError);
7985
}
8086
}

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

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,63 @@
1717

1818
package org.sourcelab.buildkite.api.client.response;
1919

20+
import org.sourcelab.buildkite.api.client.BuildkiteClient;
21+
import org.sourcelab.buildkite.api.client.exception.BuildkiteException;
22+
import org.sourcelab.buildkite.api.client.request.RetryMultipleJobsOptions;
23+
import org.sourcelab.buildkite.api.client.util.BuildkiteClientUtils;
24+
2025
import java.util.ArrayList;
2126
import java.util.Collections;
2227
import java.util.HashMap;
28+
import java.util.HashSet;
2329
import java.util.List;
2430
import java.util.Map;
2531
import java.util.Set;
2632

2733
/**
2834
* Results set from retrying multiple jobs.
35+
* {@link BuildkiteClientUtils#retryMultipleJobs(RetryMultipleJobsOptions, BuildkiteClient)}
2936
*/
3037
public class MultipleRetriedJobsResults {
38+
/**
39+
* Maps Original Job Id => The new retried job instance.
40+
*/
3141
private final Map<String,Job> originalJobIdsMappedToUpdatedJobs;
3242

43+
/**
44+
* Maps Original Job Id => The new retried job instance.
45+
*/
46+
private final Map<String, BuildkiteException> originalJobIdsMappedToErrors;
47+
3348
/**
3449
* Constructor.
50+
*
3551
* @param originalJobIdsMappedToUpdatedJobs Original Job Id mapped to the updated Job.
52+
* @param originalJobIdsMappedToErrors Original Job Id mapped to any error that occurred during the request.
3653
*/
37-
public MultipleRetriedJobsResults(final Map<String, Job> originalJobIdsMappedToUpdatedJobs) {
54+
public MultipleRetriedJobsResults(final Map<String, Job> originalJobIdsMappedToUpdatedJobs, final Map<String, BuildkiteException> originalJobIdsMappedToErrors) {
3855
this.originalJobIdsMappedToUpdatedJobs = Collections.unmodifiableMap(new HashMap<>(originalJobIdsMappedToUpdatedJobs));
56+
this.originalJobIdsMappedToErrors = Collections.unmodifiableMap(new HashMap<>(originalJobIdsMappedToErrors));
3957
}
4058

4159
/**
4260
* Ids of the jobs that were retried.
4361
* @return Ids of the jobs that were retried.
4462
*/
4563
public Set<String> getOriginalJobIds() {
46-
return originalJobIdsMappedToUpdatedJobs.keySet();
64+
// Merge keys from both sets.
65+
final Set<String> jobIds = new HashSet<>(originalJobIdsMappedToUpdatedJobs.keySet());
66+
jobIds.addAll(originalJobIdsMappedToErrors.keySet());
67+
return jobIds;
68+
}
69+
70+
/**
71+
* Check of a Retried job instance exists for the original job id.
72+
* @param originalJobId Id of the job that was retried.
73+
* @return true if an instance exists, false if not.
74+
*/
75+
public boolean hasRetriedJob(final String originalJobId) {
76+
return originalJobIdsMappedToUpdatedJobs.containsKey(originalJobId);
4777
}
4878

4979
/**
@@ -52,8 +82,8 @@ public Set<String> getOriginalJobIds() {
5282
* @return The new Job instance.
5383
*/
5484
public Job getRetriedJobByOriginalJobId(final String originalJobId) {
55-
if (!originalJobIdsMappedToUpdatedJobs.containsKey(originalJobId)) {
56-
throw new IllegalArgumentException("Job Id " + originalJobId + " was not retried and has no result");
85+
if (!hasRetriedJob(originalJobId)) {
86+
throw new IllegalArgumentException("Job Id " + originalJobId + " was not retried and has no result.");
5787
}
5888
return originalJobIdsMappedToUpdatedJobs.get(originalJobId);
5989
}
@@ -67,17 +97,55 @@ public Map<String, Job> getOriginalJobIdsMappedToUpdatedJobs() {
6797
}
6898

6999
/**
70-
* Original Job Id mapped to the new retried job instance.
71-
* @return Original Job Id mapped to the new retried job instance.
100+
* All of the new retried job instances.
101+
* @return All of the new retried job instances.
72102
*/
73103
public List<Job> getRetriedJobs() {
74104
return new ArrayList<>(originalJobIdsMappedToUpdatedJobs.values());
75105
}
76106

107+
/**
108+
* Check of a given Job id had an error.
109+
* @param originalJobId Id of the job that was retried.
110+
* @return true if an error occurred/exists, false if not.
111+
*/
112+
public boolean didJobHaveError(final String originalJobId) {
113+
return originalJobIdsMappedToErrors.containsKey(originalJobId);
114+
}
115+
116+
/**
117+
* Get the error associated with the original job id.
118+
* @param originalJobId Id of the job that was retried.
119+
* @return The error that occurred.
120+
*/
121+
public BuildkiteException getErrorByOriginalJobId(final String originalJobId) {
122+
if (!didJobHaveError(originalJobId)) {
123+
throw new IllegalArgumentException("Job Id " + originalJobId + " did not have an error.");
124+
}
125+
return originalJobIdsMappedToErrors.get(originalJobId);
126+
}
127+
128+
/**
129+
* Original Job Id mapped to the error associated with it.
130+
* @return Original Job Id mapped to the error associated with it.
131+
*/
132+
public Map<String, BuildkiteException> getOriginalJobIdsMappedToErrors() {
133+
return originalJobIdsMappedToErrors;
134+
}
135+
136+
/**
137+
* All the errors.
138+
* @return All the errors.
139+
*/
140+
public List<BuildkiteException> getErrors() {
141+
return new ArrayList<>(originalJobIdsMappedToErrors.values());
142+
}
143+
77144
@Override
78145
public String toString() {
79146
return "MultipleRetriedJobsResults{"
80147
+ "\n\toriginalJobIdsMappedToUpdatedJobs=" + originalJobIdsMappedToUpdatedJobs
148+
+ "\n\toriginalJobIdsMappedToErrors=" + originalJobIdsMappedToErrors
81149
+ "\n}";
82150
}
83151
}

src/main/java/org/sourcelab/buildkite/api/client/util/BuildkiteClientUtils.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,16 +127,25 @@ public static MultipleRetriedJobsResults retryMultipleJobs(
127127
final BuildkiteClient client
128128
) {
129129
final Map<String, Job> updatedJobs = new HashMap<>();
130+
final Map<String, BuildkiteException> errors = new HashMap<>();
131+
130132
options.getJobIds().forEach((jobId) -> {
131-
final Job retriedJob = client.retryJob(RetryJobOptions.newBuilder()
132-
.withJobId(jobId)
133-
.withBuildNumber(options.getBuildNumber())
134-
.withPipelineSlug(options.getPipelineSlug())
135-
.withOrganizationSlug(options.getOrganizationSlug())
136-
.build()
137-
);
138-
updatedJobs.put(jobId, retriedJob);
133+
try {
134+
final Job retriedJob = client.retryJob(RetryJobOptions.newBuilder()
135+
.withJobId(jobId)
136+
.withBuildNumber(options.getBuildNumber())
137+
.withPipelineSlug(options.getPipelineSlug())
138+
.withOrganizationSlug(options.getOrganizationSlug())
139+
.build()
140+
);
141+
updatedJobs.put(jobId, retriedJob);
142+
} catch (final BuildkiteException error) {
143+
if (options.isThrowOnError()) {
144+
throw error;
145+
}
146+
errors.put(jobId, error);
147+
}
139148
});
140-
return new MultipleRetriedJobsResults(updatedJobs);
149+
return new MultipleRetriedJobsResults(updatedJobs, errors);
141150
}
142151
}

0 commit comments

Comments
 (0)