23
23
*/
24
24
package com .cloudbees .jenkins .plugins .bitbucket .api ;
25
25
26
+ import com .cloudbees .jenkins .plugins .bitbucket .api .buildstatus .BitbucketBuildStatusCustomizer ;
27
+ import com .fasterxml .jackson .annotation .JsonAnyGetter ;
26
28
import com .fasterxml .jackson .annotation .JsonIgnore ;
29
+ import com .fasterxml .jackson .annotation .JsonValue ;
27
30
import edu .umd .cs .findbugs .annotations .NonNull ;
28
31
import edu .umd .cs .findbugs .annotations .Nullable ;
32
+ import java .util .Collections ;
33
+ import java .util .HashMap ;
34
+ import java .util .Map ;
35
+ import java .util .Objects ;
29
36
import org .kohsuke .accmod .Restricted ;
30
37
import org .kohsuke .accmod .restrictions .DoNotUse ;
31
38
32
39
public class BitbucketBuildStatus {
33
-
34
40
/**
35
41
* Enumeration of possible Bitbucket commit notification states
36
42
*/
@@ -43,20 +49,18 @@ public enum Status {
43
49
CANCELLED ("CANCELLED" ),
44
50
SUCCESSFUL ("SUCCESSFUL" );
45
51
46
- private final String status ;
47
-
48
- Status (final String status ) {
49
- this .status = status ;
50
- }
52
+ @ JsonValue
53
+ private final String label ;
51
54
52
- @ Override
53
- public String toString () {
54
- return status ;
55
+ Status (final String label ) {
56
+ this .label = label ;
55
57
}
56
58
}
57
59
58
60
/**
59
- * The commit hash to set the status on
61
+ * The commit hash to set the status on.
62
+ * <p>
63
+ * This is not part of the payload.
60
64
*/
61
65
@ JsonIgnore
62
66
private String hash ;
@@ -107,9 +111,16 @@ public String toString() {
107
111
*/
108
112
private int buildNumber ;
109
113
114
+ /**
115
+ * A set of new informations.
116
+ */
117
+ private Map <String , Object > optionalData ;
118
+
110
119
// Used for marshalling/unmarshalling
111
120
@ Restricted (DoNotUse .class )
112
- public BitbucketBuildStatus () {}
121
+ public BitbucketBuildStatus () {
122
+ this .optionalData = new HashMap <>();
123
+ }
113
124
114
125
public BitbucketBuildStatus (String hash ,
115
126
String description ,
@@ -141,7 +152,9 @@ public BitbucketBuildStatus(@NonNull BitbucketBuildStatus other) {
141
152
this .name = other .name ;
142
153
this .refname = other .refname ;
143
154
this .buildDuration = other .buildDuration ;
155
+ this .buildNumber = other .buildNumber ;
144
156
this .parent = other .parent ;
157
+ this .optionalData = other .optionalData != null ? new HashMap <>(other .optionalData ) : new HashMap <>();
145
158
}
146
159
147
160
public String getHash () {
@@ -160,8 +173,8 @@ public void setDescription(String description) {
160
173
this .description = description ;
161
174
}
162
175
163
- public String getState () {
164
- return state . toString () ;
176
+ public Status getState () {
177
+ return state ;
165
178
}
166
179
167
180
public void setState (Status state ) {
@@ -223,4 +236,83 @@ public void setParent(String parent) {
223
236
public String getParent () {
224
237
return parent ;
225
238
}
239
+
240
+ /**
241
+ * This represent additional informations contributed by
242
+ * {@link BitbucketBuildStatusCustomizer}s.
243
+ * <p>
244
+ * The contents of this map will be added to the root of the sent payload.
245
+ * <p>
246
+ * For example:
247
+ *
248
+ * <pre>
249
+ * buildStatus.addOptionalData("testResults", new TestResult(1, 2, 3));
250
+ * buildStatus.addOptionalData("optX", true);
251
+ * </pre>
252
+ *
253
+ * Will be serialised as:
254
+ *
255
+ * <pre>
256
+ * {
257
+ * "description": "The build is in progress..."
258
+ * ...
259
+ * "testResult": {
260
+ * "successful": 5,
261
+ * "failed": 2,
262
+ * "skipped": 1
263
+ * },
264
+ * "optX": true
265
+ * }
266
+ * </pre>
267
+ *
268
+ * @return an unmodifiable map of extra informations
269
+ */
270
+ @ JsonAnyGetter
271
+ public Map <String , Object > getOptionalData () {
272
+ return Collections .unmodifiableMap (optionalData );
273
+ }
274
+
275
+ /**
276
+ * Add a new attribute to the payload to send. If the attribute has already
277
+ * been valued than it is ignored.
278
+ *
279
+ * @param attribute attribute of build status, refer to the Bitbucket API
280
+ * @param value bean to associate to the given attribute name
281
+ * @see <a href="https://developer.atlassian.com/cloud/bitbucket/rest/api-group-commit-statuses/#api-repositories-workspace-repo-slug-commit-commit-statuses-build-post">Cloud REST API</a>
282
+ * @see <a href="https://developer.atlassian.com/server/bitbucket/rest/v906/api-group-builds-and-deployments/#api-api-latest-projects-projectkey-repos-repositoryslug-commits-commitid-builds-post">Data Center REST API</a>
283
+ */
284
+ public void addOptionalData (String attribute , Object value ) {
285
+ this .optionalData .putIfAbsent (attribute , value );
286
+ }
287
+
288
+ @ Override
289
+ public int hashCode () {
290
+ return Objects .hash (buildDuration , buildNumber , description , hash , key , name , parent , refname , state , url , optionalData );
291
+ }
292
+
293
+ @ Override
294
+ public boolean equals (Object obj ) {
295
+ if (this == obj ) {
296
+ return true ;
297
+ }
298
+ if (obj == null ) {
299
+ return false ;
300
+ }
301
+ if (getClass () != obj .getClass ()) {
302
+ return false ;
303
+ }
304
+ BitbucketBuildStatus other = (BitbucketBuildStatus ) obj ;
305
+ return buildDuration == other .buildDuration
306
+ && buildNumber == other .buildNumber
307
+ && Objects .equals (description , other .description )
308
+ && Objects .equals (hash , other .hash )
309
+ && Objects .equals (key , other .key )
310
+ && Objects .equals (name , other .name )
311
+ && Objects .equals (parent , other .parent )
312
+ && Objects .equals (refname , other .refname )
313
+ && state == other .state
314
+ && Objects .equals (url , other .url )
315
+ && Objects .equals (optionalData , other .optionalData );
316
+ }
317
+
226
318
}
0 commit comments