Skip to content

Commit d4734ca

Browse files
committed
Fixed #321 handle change set lists
pipeline builds present change sets as list, because there can be several checkouts. We add a further filed+getter such that this data gets deserialized.
1 parent 62623fe commit d4734ca

File tree

2 files changed

+126
-1
lines changed

2 files changed

+126
-1
lines changed

jenkins-client/src/main/java/com/offbytwo/jenkins/model/BuildWithDetails.java

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

77
package com.offbytwo.jenkins.model;
88

9+
import com.fasterxml.jackson.annotation.JsonProperty;
910
import com.google.common.base.Predicate;
1011
import com.google.common.base.Strings;
1112
import com.google.common.collect.ImmutableMap;
@@ -124,6 +125,8 @@ public BuildResult getResult() {
124125
private String consoleOutputText;
125126
private String consoleOutputHtml;
126127
private BuildChangeSet changeSet;
128+
@JsonProperty("changeSets")
129+
private List<BuildChangeSet> changeSets;
127130
private String builtOn;
128131
private List<BuildChangeSetAuthor> culprits;
129132

@@ -467,14 +470,56 @@ public ConsoleLog getConsoleOutputText(int bufferOffset) throws IOException {
467470
}
468471

469472

473+
/**
474+
* Returns the change set of a build if available.
475+
*
476+
* If a build performs several scm checkouts (i.e. pipeline builds), the change set of the first
477+
* checkout is returned. To get the complete list of change sets for all checkouts, use
478+
* {@link #getChangeSets()}
479+
*
480+
* If no checkout is performed, null is returned.
481+
*
482+
* @return The change set of the build.
483+
*
484+
*/
470485
public BuildChangeSet getChangeSet() {
471-
return changeSet;
486+
BuildChangeSet result;
487+
if (changeSet != null) {
488+
result = changeSet;
489+
} else if (changeSets != null && !changeSets.isEmpty()) {
490+
result = changeSets.get(0);
491+
} else {
492+
result = null;
493+
}
494+
return result;
472495
}
473496

474497
public void setChangeSet(BuildChangeSet changeSet) {
475498
this.changeSet = changeSet;
476499
}
477500

501+
/**
502+
* Returns the complete list of change sets for all checkout the build has performed. If no
503+
* checkouts have been performed, returns null.
504+
*
505+
* @return The complete list of change sets of the build.
506+
*/
507+
public List<BuildChangeSet> getChangeSets() {
508+
List<BuildChangeSet> result;
509+
if (changeSets != null) {
510+
result = changeSets;
511+
} else if (changeSet != null) {
512+
result = Collections.singletonList(changeSet);
513+
} else {
514+
result = null;
515+
}
516+
return result;
517+
}
518+
519+
public void setChangeSets(List<BuildChangeSet> changeSets) {
520+
this.changeSets = changeSets;
521+
}
522+
478523
public List<BuildChangeSetAuthor> getCulprits() {
479524
return culprits;
480525
}
@@ -528,6 +573,11 @@ public boolean equals(Object obj) {
528573
return false;
529574
} else if (!changeSet.equals(other.changeSet))
530575
return false;
576+
if (changeSets == null) {
577+
if (other.changeSets != null)
578+
return false;
579+
} else if (!changeSets.equals(other.changeSets))
580+
return false;
531581
if (consoleOutputHtml == null) {
532582
if (other.consoleOutputHtml != null)
533583
return false;
@@ -583,6 +633,7 @@ public int hashCode() {
583633
result = prime * result + (building ? 1231 : 1237);
584634
result = prime * result + ((builtOn == null) ? 0 : builtOn.hashCode());
585635
result = prime * result + ((changeSet == null) ? 0 : changeSet.hashCode());
636+
result = prime * result + ((changeSets == null) ? 0 : changeSets.hashCode());
586637
result = prime * result + ((consoleOutputHtml == null) ? 0 : consoleOutputHtml.hashCode());
587638
result = prime * result + ((consoleOutputText == null) ? 0 : consoleOutputText.hashCode());
588639
result = prime * result + ((culprits == null) ? 0 : culprits.hashCode());
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.offbytwo.jenkins.model;
2+
3+
import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES;
4+
import static org.junit.Assert.*;
5+
6+
7+
import org.junit.Test;
8+
9+
10+
import com.fasterxml.jackson.databind.ObjectMapper;
11+
12+
public class ChangeSetTest {
13+
14+
private BuildWithDetails getBuildFromJson(String json) throws Exception {
15+
ObjectMapper mapper = new ObjectMapper();
16+
mapper.disable(FAIL_ON_UNKNOWN_PROPERTIES);
17+
return mapper.readValue(json, BuildWithDetails.class);
18+
}
19+
20+
final String changeSetExampleJson = " {" +
21+
" \"_class\" : \"hudson.plugins.git.GitChangeSetList\"," +
22+
" \"items\" : [" +
23+
" {" +
24+
" \"_class\" : \"hudson.plugins.git.GitChangeSet\"," +
25+
" \"affectedPaths\" : [" +
26+
" \"README.md\"" +
27+
" ]," +
28+
" \"commitId\" : \"ba40ff32c60f692918c1d51f5c80842124ed04af\"," +
29+
" \"timestamp\" : 1519306808000," +
30+
" \"author\" : {" +
31+
" \"absoluteUrl\" : \"https://my.jenkins/user/john.doe\","
32+
+ " \"fullName\" : \"john.doe\"" +
33+
" }," +
34+
" \"authorEmail\" : \"[email protected]\","
35+
+
36+
" \"comment\" : \"longer\\ncommit message\\n\"," +
37+
" \"date\" : \"2018-02-22 13:40:08 +0000\"," +
38+
" \"id\" : \"ba40ff32c60f692918c1d51f5c80842124ed04af\"," +
39+
" \"msg\" : \"longer\"," +
40+
" \"paths\" : [" +
41+
" {" +
42+
" \"editType\" : \"edit\"," +
43+
" \"file\" : \"README.md\"" +
44+
" }" +
45+
" ]" +
46+
" }" +
47+
" ]," +
48+
" \"kind\" : \"git\"" +
49+
" }";
50+
51+
52+
@Test
53+
public void getChangeSet__forBuildWithChangeSetAsSingleObject() throws Exception {
54+
String json = String.format("{ \"changeSet\" : %s }", changeSetExampleJson);
55+
56+
BuildWithDetails examinee = getBuildFromJson(json);
57+
58+
BuildChangeSetItem item = examinee.getChangeSet().getItems().get(0);
59+
assertEquals(item.getAuthor().getFullName(), "john.doe");
60+
61+
}
62+
63+
@Test
64+
public void getChangeSet__forBuildWithChangeSetsAsList() throws Exception {
65+
String json = String.format("{ \"changeSets\" : [ %s ] }", changeSetExampleJson);
66+
67+
BuildWithDetails examinee = getBuildFromJson(json);
68+
69+
BuildChangeSetItem item = examinee.getChangeSet().getItems().get(0);
70+
assertEquals(item.getAuthor().getFullName(), "john.doe");
71+
72+
assertEquals(examinee.getChangeSets().size(), 1);
73+
}
74+
}

0 commit comments

Comments
 (0)