From 5807954872d077645b87c8c06d5124938d846136 Mon Sep 17 00:00:00 2001 From: Jakub Zacek Date: Thu, 31 May 2018 08:48:08 +0200 Subject: [PATCH] Fixed #309 - Added better support for Maven Modules - slight modifications based on removement of Guava --- ReleaseNotes.md | 9 +- .../com/offbytwo/jenkins/model/BaseModel.java | 9 +- .../jenkins/model/BuildWithDetails.java | 11 ++ .../java/com/offbytwo/jenkins/model/Job.java | 7 +- .../jenkins/model/JobWithDetails.java | 19 +++- .../jenkins/model/MavenJobWithDetails.java | 8 +- .../offbytwo/jenkins/model/MavenModule.java | 51 ++++++++- .../jenkins/model/MavenModuleWithDetails.java | 105 ++++++++++++++++++ 8 files changed, 198 insertions(+), 21 deletions(-) create mode 100644 jenkins-client/src/main/java/com/offbytwo/jenkins/model/MavenModuleWithDetails.java diff --git a/ReleaseNotes.md b/ReleaseNotes.md index 45fc71da..e69a8019 100644 --- a/ReleaseNotes.md +++ b/ReleaseNotes.md @@ -2,6 +2,12 @@ ## Release 0.4.0 (NOT RELEASED YET) + * [Fixed Issue 309][issue-309] + + * Added possibility to get mode detailed data from Maven Modules from Jobs/Builds + + Thanks for that to [Jakub Zacek](https://github.com/dawon). + * [Fixed Issue 395][issue-395] * Remove google guava lib @@ -187,7 +193,7 @@ Added new methods to JenkinsServer for stopping and restarting Jenkins. The methods are restart(Boolean crumbFlag), safeRestart(Boolean crumbFlag), exit(Boolean crumbFlag) and safeExit(Boolean crumbFlag) Thanks for that to [Chids](https://github.com/Chids-gs). - + ## Release 0.3.7 * Changed Eclipse Formatting configuration. @@ -1140,6 +1146,7 @@ TestReport testReport = mavenJob.getLastSuccessfulBuild().getTestReport(); [issue-291]: https://github.com/jenkinsci/java-client-api/issues/291 [issue-298]: https://github.com/jenkinsci/java-client-api/issues/298 [issue-301]: https://github.com/jenkinsci/java-client-api/issues/301 +[issue-309]: https://github.com/jenkinsci/java-client-api/issues/309 [issue-394]: https://github.com/jenkinsci/java-client-api/issues/394 [issue-395]: https://github.com/jenkinsci/java-client-api/issues/395 [issue-396]: https://github.com/jenkinsci/java-client-api/issues/396 diff --git a/jenkins-client/src/main/java/com/offbytwo/jenkins/model/BaseModel.java b/jenkins-client/src/main/java/com/offbytwo/jenkins/model/BaseModel.java index 9c850ba2..5275d36b 100644 --- a/jenkins-client/src/main/java/com/offbytwo/jenkins/model/BaseModel.java +++ b/jenkins-client/src/main/java/com/offbytwo/jenkins/model/BaseModel.java @@ -7,6 +7,7 @@ package com.offbytwo.jenkins.model; import com.offbytwo.jenkins.client.JenkinsHttpConnection; +import java.util.function.Predicate; /** * The base model. @@ -17,8 +18,7 @@ public class BaseModel { * The class. */ private String _class; - - + /** * Get the class. * @return class @@ -47,4 +47,9 @@ public BaseModel setClient(final JenkinsHttpConnection client) { this.client = client; return this; } + + protected static Predicate isBuildNumberEqualTo(int buildNumber) { + return build -> build.getNumber() == buildNumber; + } + } 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 ab9e201d..d3ce5842 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 @@ -533,6 +533,17 @@ public InputStream downloadArtifact(Artifact a) throws IOException, URISyntaxExc ""); return client.getFile(artifactUri); } + + /** + * Returns {@link MavenModuleWithDetails} based on its name + * + * @param name module name + * @return {@link MavenModuleWithDetails} + * @throws IOException in case of error. + */ + public MavenModuleWithDetails getModule(String name) throws IOException { + return client.get(getUrl() + name, MavenModuleWithDetails.class); + } @Override public boolean equals(Object obj) { diff --git a/jenkins-client/src/main/java/com/offbytwo/jenkins/model/Job.java b/jenkins-client/src/main/java/com/offbytwo/jenkins/model/Job.java index ba1968a1..7d5313fd 100644 --- a/jenkins-client/src/main/java/com/offbytwo/jenkins/model/Job.java +++ b/jenkins-client/src/main/java/com/offbytwo/jenkins/model/Job.java @@ -6,16 +6,13 @@ package com.offbytwo.jenkins.model; -import static org.apache.commons.lang.StringUtils.join; - +import com.offbytwo.jenkins.client.util.EncodingUtils; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.util.Map; - -import com.offbytwo.jenkins.client.util.EncodingUtils; import java.util.function.Function; import java.util.stream.Collectors; @@ -186,4 +183,6 @@ public String apply(Map.Entry entry) { return EncodingUtils.formParameter(entry.getKey()) + "=" + EncodingUtils.formParameter(entry.getValue()); } } + + } diff --git a/jenkins-client/src/main/java/com/offbytwo/jenkins/model/JobWithDetails.java b/jenkins-client/src/main/java/com/offbytwo/jenkins/model/JobWithDetails.java index b5894015..f13903cf 100644 --- a/jenkins-client/src/main/java/com/offbytwo/jenkins/model/JobWithDetails.java +++ b/jenkins-client/src/main/java/com/offbytwo/jenkins/model/JobWithDetails.java @@ -18,7 +18,6 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; -import java.util.function.Predicate; import static com.offbytwo.jenkins.helper.FunctionalHelper.SET_CLIENT; import static java.util.stream.Collectors.toList; @@ -31,7 +30,7 @@ public class JobWithDetails extends Job { private boolean buildable; - private List builds; + private List builds = Collections.emptyList(); private Build firstBuild; @@ -438,10 +437,6 @@ public List getUpstreamProjects() { } } - private static Predicate isBuildNumberEqualTo(int buildNumber) { - return build -> build.getNumber() == buildNumber; - } - public QueueItem getQueueItem() { return this.queueItem; } @@ -456,6 +451,18 @@ public QueueItem getQueueItem() { public Optional getBuildByNumber(final int buildNumber) { return builds.stream().filter(isBuildNumberEqualTo(buildNumber)).findFirst(); } + + /** + * Get a module of a {@link Job} + * + * @param moduleName name of the {@link MavenModule} + * @return The {@link MavenModuleWithDetails} selected by the given module name + * @throws java.io.IOException in case of errors. + * + */ + public MavenModuleWithDetails getModule(String moduleName) throws IOException { + return client.get(getUrl() + moduleName, MavenModuleWithDetails.class); + } /** * Empty description to be used for {@link #updateDescription(String)} or diff --git a/jenkins-client/src/main/java/com/offbytwo/jenkins/model/MavenJobWithDetails.java b/jenkins-client/src/main/java/com/offbytwo/jenkins/model/MavenJobWithDetails.java index bb3f0c62..3eaf7fc0 100644 --- a/jenkins-client/src/main/java/com/offbytwo/jenkins/model/MavenJobWithDetails.java +++ b/jenkins-client/src/main/java/com/offbytwo/jenkins/model/MavenJobWithDetails.java @@ -1,7 +1,6 @@ package com.offbytwo.jenkins.model; import com.offbytwo.jenkins.client.util.EncodingUtils; -import com.offbytwo.jenkins.helper.FunctionalHelper; import com.offbytwo.jenkins.helper.Range; import org.apache.http.HttpStatus; import org.apache.http.client.HttpResponseException; @@ -10,7 +9,6 @@ import java.util.Collections; import java.util.List; import java.util.Optional; -import java.util.function.Predicate; import static com.offbytwo.jenkins.helper.FunctionalHelper.SET_CLIENT; import static java.util.stream.Collectors.toList; @@ -276,11 +274,7 @@ public List getUpstreamProjects() { } } - private static Predicate isBuildNumberEqualTo(int buildNumber) { - return build -> build.getNumber() == buildNumber; - } - - /** + /** * @param buildNumber The build you would like to select. * @return Optional which contains the {@link MavenBuild}. */ diff --git a/jenkins-client/src/main/java/com/offbytwo/jenkins/model/MavenModule.java b/jenkins-client/src/main/java/com/offbytwo/jenkins/model/MavenModule.java index 07c61bb1..91c7ef1c 100644 --- a/jenkins-client/src/main/java/com/offbytwo/jenkins/model/MavenModule.java +++ b/jenkins-client/src/main/java/com/offbytwo/jenkins/model/MavenModule.java @@ -1,10 +1,59 @@ +/* + * Copyright (c) 2018 Cosmin Stejerean, Karl Heinz Marbaise, and contributors. + * + * Distributed under the MIT license: http://opensource.org/licenses/MIT + */ package com.offbytwo.jenkins.model; +import java.io.IOException; import java.util.List; - +/** + * + * @author Karl Heinz Marbaise, Ricardo Zanini, René Scheibe, Jakub Zacek + */ public class MavenModule extends BaseModel { private List moduleRecords; + private String name; + private String url; + private String color; + private String displayName; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public String getDisplayName() { + return displayName; + } + + public void setDisplayName(String displayName) { + this.displayName = displayName; + } + + public MavenModuleWithDetails details() throws IOException { + return client.get(url, MavenModuleWithDetails.class); + } public List getModuleRecords() { return moduleRecords; diff --git a/jenkins-client/src/main/java/com/offbytwo/jenkins/model/MavenModuleWithDetails.java b/jenkins-client/src/main/java/com/offbytwo/jenkins/model/MavenModuleWithDetails.java new file mode 100644 index 00000000..d168a45e --- /dev/null +++ b/jenkins-client/src/main/java/com/offbytwo/jenkins/model/MavenModuleWithDetails.java @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2018 Cosmin Stejerean, Karl Heinz Marbaise, and contributors. + * + * Distributed under the MIT license: http://opensource.org/licenses/MIT + */ +package com.offbytwo.jenkins.model; + +import static com.offbytwo.jenkins.helper.FunctionalHelper.SET_CLIENT; +import static java.util.stream.Collectors.toList; + +import java.io.IOException; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +/** + * Model Class for Maven Modules + * + * @author Jakub Zacek + */ +public class MavenModuleWithDetails extends BaseModel { + + private List builds = Collections.emptyList(); + private List actions = Collections.emptyList(); + private String displayName; + private BuildResult result; + private String url; + private long duration; + private long timestamp; + + public List getActions() { + return actions; + } + + public void setActions(List actions) { + this.actions = actions; + } + + public void setBuilds(List builds) { + this.builds = builds; + } + + public List getBuilds() { + return builds.stream() + .map(SET_CLIENT(this.getClient())) + .collect(toList()); + } + + public Build getBuildByNumber(final int buildNumber) { + return builds.stream() + .filter(isBuildNumberEqualTo(buildNumber)) + .map(SET_CLIENT(this.getClient())) + .findFirst() + .orElse(Build.BUILD_HAS_NEVER_RUN); + } + + public String getDisplayName() { + return displayName; + } + + public void setDisplayName(String displayName) { + this.displayName = displayName; + } + + public BuildResult getResult() { + return result; + } + + public void setResult(BuildResult result) { + this.result = result; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public long getTimestamp() { + return timestamp; + } + + public void setTimestamp(long timestamp) { + this.timestamp = timestamp; + } + + public long getDuration() { + return duration; + } + + public void setDuration(long duration) { + this.duration = duration; + } + + public String getConsoleOutputText() throws IOException { + return client.get(getUrl() + "/logText/progressiveText"); + } + + public TestReport getTestReport() throws IOException { + return client.get(this.getUrl() + "/testReport/?depth=1", TestReport.class); + } + +}