2323 */
2424package com .cloudbees .jenkins .plugins .bitbucket .api ;
2525
26+ import com .cloudbees .jenkins .plugins .bitbucket .api .buildstatus .BitbucketBuildStatusCustomizer ;
27+ import com .fasterxml .jackson .annotation .JsonAnyGetter ;
2628import com .fasterxml .jackson .annotation .JsonIgnore ;
29+ import com .fasterxml .jackson .annotation .JsonValue ;
2730import edu .umd .cs .findbugs .annotations .NonNull ;
2831import 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 ;
2936import org .kohsuke .accmod .Restricted ;
3037import org .kohsuke .accmod .restrictions .DoNotUse ;
3138
3239public class BitbucketBuildStatus {
33-
3440 /**
3541 * Enumeration of possible Bitbucket commit notification states
3642 */
@@ -43,20 +49,18 @@ public enum Status {
4349 CANCELLED ("CANCELLED" ),
4450 SUCCESSFUL ("SUCCESSFUL" );
4551
46- private final String status ;
47-
48- Status (final String status ) {
49- this .status = status ;
50- }
52+ @ JsonValue
53+ private final String label ;
5154
52- @ Override
53- public String toString () {
54- return status ;
55+ Status (final String label ) {
56+ this .label = label ;
5557 }
5658 }
5759
5860 /**
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.
6064 */
6165 @ JsonIgnore
6266 private String hash ;
@@ -107,9 +111,16 @@ public String toString() {
107111 */
108112 private int buildNumber ;
109113
114+ /**
115+ * A set of new informations.
116+ */
117+ private Map <String , Object > optionalData ;
118+
110119 // Used for marshalling/unmarshalling
111120 @ Restricted (DoNotUse .class )
112- public BitbucketBuildStatus () {}
121+ public BitbucketBuildStatus () {
122+ this .optionalData = new HashMap <>();
123+ }
113124
114125 public BitbucketBuildStatus (String hash ,
115126 String description ,
@@ -141,7 +152,9 @@ public BitbucketBuildStatus(@NonNull BitbucketBuildStatus other) {
141152 this .name = other .name ;
142153 this .refname = other .refname ;
143154 this .buildDuration = other .buildDuration ;
155+ this .buildNumber = other .buildNumber ;
144156 this .parent = other .parent ;
157+ this .optionalData = other .optionalData != null ? new HashMap <>(other .optionalData ) : new HashMap <>();
145158 }
146159
147160 public String getHash () {
@@ -160,8 +173,8 @@ public void setDescription(String description) {
160173 this .description = description ;
161174 }
162175
163- public String getState () {
164- return state . toString () ;
176+ public Status getState () {
177+ return state ;
165178 }
166179
167180 public void setState (Status state ) {
@@ -223,4 +236,83 @@ public void setParent(String parent) {
223236 public String getParent () {
224237 return parent ;
225238 }
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+
226318}
0 commit comments