From d4734caf5c5ef81bb6241811a68e8af2b63633d3 Mon Sep 17 00:00:00 2001 From: "Holger Partsch (D057760)" Date: Tue, 27 Feb 2018 10:27:53 +0100 Subject: [PATCH] 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. --- .../jenkins/model/BuildWithDetails.java | 53 ++++++++++++- .../offbytwo/jenkins/model/ChangeSetTest.java | 74 +++++++++++++++++++ 2 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 jenkins-client/src/test/java/com/offbytwo/jenkins/model/ChangeSetTest.java diff --git a/jenkins-client/src/main/java/com/offbytwo/jenkins/model/BuildWithDetails.java b/jenkins-client/src/main/java/com/offbytwo/jenkins/model/BuildWithDetails.java index 2feea4f9..0ea806da 100644 --- a/jenkins-client/src/main/java/com/offbytwo/jenkins/model/BuildWithDetails.java +++ b/jenkins-client/src/main/java/com/offbytwo/jenkins/model/BuildWithDetails.java @@ -6,6 +6,7 @@ package com.offbytwo.jenkins.model; +import com.fasterxml.jackson.annotation.JsonProperty; import com.google.common.base.Predicate; import com.google.common.base.Strings; import com.google.common.collect.ImmutableMap; @@ -124,6 +125,8 @@ public BuildResult getResult() { private String consoleOutputText; private String consoleOutputHtml; private BuildChangeSet changeSet; + @JsonProperty("changeSets") + private List changeSets; private String builtOn; private List culprits; @@ -467,14 +470,56 @@ public ConsoleLog getConsoleOutputText(int bufferOffset) throws IOException { } + /** + * Returns the change set of a build if available. + * + * If a build performs several scm checkouts (i.e. pipeline builds), the change set of the first + * checkout is returned. To get the complete list of change sets for all checkouts, use + * {@link #getChangeSets()} + * + * If no checkout is performed, null is returned. + * + * @return The change set of the build. + * + */ public BuildChangeSet getChangeSet() { - return changeSet; + BuildChangeSet result; + if (changeSet != null) { + result = changeSet; + } else if (changeSets != null && !changeSets.isEmpty()) { + result = changeSets.get(0); + } else { + result = null; + } + return result; } public void setChangeSet(BuildChangeSet changeSet) { this.changeSet = changeSet; } + /** + * Returns the complete list of change sets for all checkout the build has performed. If no + * checkouts have been performed, returns null. + * + * @return The complete list of change sets of the build. + */ + public List getChangeSets() { + List result; + if (changeSets != null) { + result = changeSets; + } else if (changeSet != null) { + result = Collections.singletonList(changeSet); + } else { + result = null; + } + return result; + } + + public void setChangeSets(List changeSets) { + this.changeSets = changeSets; + } + public List getCulprits() { return culprits; } @@ -528,6 +573,11 @@ public boolean equals(Object obj) { return false; } else if (!changeSet.equals(other.changeSet)) return false; + if (changeSets == null) { + if (other.changeSets != null) + return false; + } else if (!changeSets.equals(other.changeSets)) + return false; if (consoleOutputHtml == null) { if (other.consoleOutputHtml != null) return false; @@ -583,6 +633,7 @@ public int hashCode() { result = prime * result + (building ? 1231 : 1237); result = prime * result + ((builtOn == null) ? 0 : builtOn.hashCode()); result = prime * result + ((changeSet == null) ? 0 : changeSet.hashCode()); + result = prime * result + ((changeSets == null) ? 0 : changeSets.hashCode()); result = prime * result + ((consoleOutputHtml == null) ? 0 : consoleOutputHtml.hashCode()); result = prime * result + ((consoleOutputText == null) ? 0 : consoleOutputText.hashCode()); result = prime * result + ((culprits == null) ? 0 : culprits.hashCode()); diff --git a/jenkins-client/src/test/java/com/offbytwo/jenkins/model/ChangeSetTest.java b/jenkins-client/src/test/java/com/offbytwo/jenkins/model/ChangeSetTest.java new file mode 100644 index 00000000..b1e7ae33 --- /dev/null +++ b/jenkins-client/src/test/java/com/offbytwo/jenkins/model/ChangeSetTest.java @@ -0,0 +1,74 @@ +package com.offbytwo.jenkins.model; + +import static com.fasterxml.jackson.databind.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES; +import static org.junit.Assert.*; + + +import org.junit.Test; + + +import com.fasterxml.jackson.databind.ObjectMapper; + +public class ChangeSetTest { + + private BuildWithDetails getBuildFromJson(String json) throws Exception { + ObjectMapper mapper = new ObjectMapper(); + mapper.disable(FAIL_ON_UNKNOWN_PROPERTIES); + return mapper.readValue(json, BuildWithDetails.class); + } + + final String changeSetExampleJson = " {" + + " \"_class\" : \"hudson.plugins.git.GitChangeSetList\"," + + " \"items\" : [" + + " {" + + " \"_class\" : \"hudson.plugins.git.GitChangeSet\"," + + " \"affectedPaths\" : [" + + " \"README.md\"" + + " ]," + + " \"commitId\" : \"ba40ff32c60f692918c1d51f5c80842124ed04af\"," + + " \"timestamp\" : 1519306808000," + + " \"author\" : {" + + " \"absoluteUrl\" : \"https://my.jenkins/user/john.doe\"," + + " \"fullName\" : \"john.doe\"" + + " }," + + " \"authorEmail\" : \"john.doe@flap.com\"," + + + " \"comment\" : \"longer\\ncommit message\\n\"," + + " \"date\" : \"2018-02-22 13:40:08 +0000\"," + + " \"id\" : \"ba40ff32c60f692918c1d51f5c80842124ed04af\"," + + " \"msg\" : \"longer\"," + + " \"paths\" : [" + + " {" + + " \"editType\" : \"edit\"," + + " \"file\" : \"README.md\"" + + " }" + + " ]" + + " }" + + " ]," + + " \"kind\" : \"git\"" + + " }"; + + + @Test + public void getChangeSet__forBuildWithChangeSetAsSingleObject() throws Exception { + String json = String.format("{ \"changeSet\" : %s }", changeSetExampleJson); + + BuildWithDetails examinee = getBuildFromJson(json); + + BuildChangeSetItem item = examinee.getChangeSet().getItems().get(0); + assertEquals(item.getAuthor().getFullName(), "john.doe"); + + } + + @Test + public void getChangeSet__forBuildWithChangeSetsAsList() throws Exception { + String json = String.format("{ \"changeSets\" : [ %s ] }", changeSetExampleJson); + + BuildWithDetails examinee = getBuildFromJson(json); + + BuildChangeSetItem item = examinee.getChangeSet().getItems().get(0); + assertEquals(item.getAuthor().getFullName(), "john.doe"); + + assertEquals(examinee.getChangeSets().size(), 1); + } +}