Skip to content

Commit 9755a2f

Browse files
committed
convert Build.State and Job.State fields into Enums
1 parent 1a5b574 commit 9755a2f

File tree

8 files changed

+307
-7
lines changed

8 files changed

+307
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
The format is based on [Keep a Changelog](http://keepachangelog.com/)
33
and this project adheres to [Semantic Versioning](http://semver.org/).
44

5+
## 0.2.0 (02/21/2023)
6+
- Breaking Change. Job::getState() and Build::getState() now return ENUM values.
7+
58
## 0.1.2 (02/21/2023)
69
- Add support for annotations end point.
710

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>org.sourcelab</groupId>
88
<artifactId>buildkite-api-client</artifactId>
9-
<version>0.1.2</version>
9+
<version>0.2.0</version>
1010
<packaging>jar</packaging>
1111

1212
<!-- Require Maven 3.3.9 -->

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class Build {
2929
private final String url;
3030
private final String webUrl;
3131
private final long number;
32-
private final String state;
32+
private final BuildState state;
3333
private final boolean blocked;
3434
private final String message;
3535
private final String commit;
@@ -81,7 +81,7 @@ public Build(
8181
this.url = url;
8282
this.webUrl = webUrl;
8383
this.number = number;
84-
this.state = state;
84+
this.state = BuildState.createFromString(state);
8585
this.blocked = blocked;
8686
this.message = message;
8787
this.commit = commit;
@@ -119,7 +119,7 @@ public long getNumber() {
119119
return number;
120120
}
121121

122-
public String getState() {
122+
public BuildState getState() {
123123
return state;
124124
}
125125

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/**
2+
* Copyright 2023 SourceLab.org https://github.com/SourceLabOrg/Buildkite-Api-Client
3+
* <p>
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+
* <p>
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
* <p>
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.response;
19+
20+
public enum BuildState {
21+
CREATING,
22+
SCHEDULED,
23+
RUNNING,
24+
PASSED,
25+
CANCELING,
26+
CANCELED,
27+
FAILING,
28+
FAILED,
29+
BLOCKED,
30+
SKIPPED,
31+
NOT_RUN,
32+
BROKEN,
33+
// Fall through value
34+
UNKNOWN;
35+
36+
/**
37+
* Create enum from String value as returned from the API.
38+
* @param value the string representation.
39+
* @return Enum value, or UNKNOWN if passed an unhandled value.
40+
*/
41+
public static BuildState createFromString(final String value) {
42+
if (value == null) {
43+
return UNKNOWN;
44+
}
45+
46+
switch (value.trim().toLowerCase()) {
47+
case "creating":
48+
return CREATING;
49+
case "scheduled":
50+
return SCHEDULED;
51+
case "running":
52+
return RUNNING;
53+
case "passed":
54+
return PASSED;
55+
case "canceling":
56+
return CANCELING;
57+
case "canceled":
58+
return CANCELED;
59+
case "failing":
60+
return FAILING;
61+
case "failed":
62+
return FAILED;
63+
case "blocked":
64+
return BLOCKED;
65+
case "skipped":
66+
return SKIPPED;
67+
case "not_run":
68+
return NOT_RUN;
69+
case "broken":
70+
return BROKEN;
71+
default:
72+
return UNKNOWN;
73+
}
74+
}
75+
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class Job {
2828
private final String type;
2929
private final String name;
3030
private final String stepKey;
31-
private final String state;
31+
private final JobState state;
3232
private final String webUrl;
3333
private final String logUrl;
3434
private final String rawLogUrl;
@@ -94,7 +94,7 @@ public Job(
9494
this.type = type;
9595
this.name = name;
9696
this.stepKey = stepKey;
97-
this.state = state;
97+
this.state = JobState.createFromString(state);
9898
this.webUrl = webUrl;
9999
this.logUrl = logUrl;
100100
this.rawLogUrl = rawLogUrl;
@@ -136,7 +136,7 @@ public String getStepKey() {
136136
return stepKey;
137137
}
138138

139-
public String getState() {
139+
public JobState getState() {
140140
return state;
141141
}
142142

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/**
2+
* Copyright 2023 SourceLab.org https://github.com/SourceLabOrg/Buildkite-Api-Client
3+
* <p>
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+
* <p>
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
* <p>
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.response;
19+
20+
public enum JobState {
21+
SKIPPED,
22+
ACCEPTED,
23+
ASSIGNED,
24+
SCHEDULED,
25+
WAITING,
26+
PENDING,
27+
WAITING_FAILED,
28+
BLOCKED_FAILED,
29+
UNBLOCKED_FAILED,
30+
TIMED_OUT,
31+
BROKEN,
32+
FAILED,
33+
LIMITING,
34+
LIMITED,
35+
BLOCKED,
36+
CANCELED,
37+
TIMING_OUT,
38+
RUNNING,
39+
UNBLOCKED,
40+
CANCELING,
41+
PASSED,
42+
FINISHED,
43+
44+
// Fall through value.
45+
UNKNOWN;
46+
47+
/**
48+
* Create enum from String value as returned from the API.
49+
* @param value the string representation.
50+
* @return Enum value, or UNKNOWN if passed an unhandled value.
51+
*/
52+
public static JobState createFromString(final String value) {
53+
if (value == null) {
54+
return UNKNOWN;
55+
}
56+
switch (value.trim().toLowerCase()) {
57+
case "skipped":
58+
return SKIPPED;
59+
case "accepted":
60+
return ACCEPTED;
61+
case "assigned":
62+
return ASSIGNED;
63+
case "scheduled":
64+
return SCHEDULED;
65+
case "waiting":
66+
return WAITING;
67+
case "pending":
68+
return PENDING;
69+
70+
case "waiting_failed":
71+
return WAITING_FAILED;
72+
case "blocked_failed":
73+
return BLOCKED_FAILED;
74+
case "unblocked_failed":
75+
return UNBLOCKED_FAILED;
76+
case "timed_out":
77+
return TIMED_OUT;
78+
case "broken":
79+
return BROKEN;
80+
case "failed":
81+
return FAILED;
82+
83+
case "limiting":
84+
return LIMITING;
85+
case "limited":
86+
return LIMITED;
87+
case "blocked":
88+
return BLOCKED;
89+
case "canceled":
90+
return CANCELED;
91+
case "timing_out":
92+
return TIMING_OUT;
93+
94+
case "running":
95+
return RUNNING;
96+
case "unblocked":
97+
return UNBLOCKED;
98+
case "canceling":
99+
return CANCELING;
100+
101+
case "passed":
102+
return PASSED;
103+
case "finished":
104+
return FINISHED;
105+
106+
default:
107+
return UNKNOWN;
108+
}
109+
}
110+
111+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Copyright 2023 SourceLab.org https://github.com/SourceLabOrg/Buildkite-Api-Client
3+
* <p>
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+
* <p>
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
* <p>
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+
package org.sourcelab.buildkite.api.client.util;
18+
19+
import org.sourcelab.buildkite.api.client.response.BuildState;
20+
21+
/**
22+
* Helpful utilities around Build State field and the BuildState enum.
23+
*/
24+
public class BuildStateUtils {
25+
/**
26+
* Check if the provided BuildState value is able to be cancelled.
27+
*
28+
* @param state The state to check.
29+
* @return true if the state is cancellable, false if not.
30+
*/
31+
public static boolean isStateCancellable(final BuildState state) {
32+
if (state == null) {
33+
return false;
34+
}
35+
switch (state) {
36+
case CREATING:
37+
case SCHEDULED:
38+
case FAILING:
39+
case RUNNING:
40+
case BLOCKED:
41+
return true;
42+
}
43+
return false;
44+
}
45+
46+
/**
47+
* Check if the provided BuildState value is considered "finished".
48+
*
49+
* @param state The state to check.
50+
* @return true if the state is considered "finished", false if not.
51+
*/
52+
public static boolean isStateConsideredFinished(final BuildState state) {
53+
if (state == null) {
54+
return true;
55+
}
56+
57+
switch (state) {
58+
case FAILED:
59+
case PASSED:
60+
case SKIPPED:
61+
case NOT_RUN:
62+
case BROKEN:
63+
return true;
64+
}
65+
return false;
66+
}
67+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* Copyright 2023 SourceLab.org https://github.com/SourceLabOrg/Buildkite-Api-Client
3+
* <p>
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+
* <p>
9+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
10+
* Software.
11+
* <p>
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.util;
19+
20+
import org.sourcelab.buildkite.api.client.response.JobState;
21+
22+
/**
23+
* Helpful utilities around Job State field and the JobState enum.
24+
*/
25+
public class JobStateUtil {
26+
27+
/**
28+
* Check if the provided JobState retryable.
29+
* @param state The JobState to check.
30+
* @return true if the job state is retryable, false if not.
31+
*/
32+
public static boolean isRetryableState(final JobState state) {
33+
if (state == null) {
34+
return false;
35+
}
36+
switch (state) {
37+
case CANCELED:
38+
case FAILED:
39+
return true;
40+
default:
41+
return false;
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)