com.offbytwo.jenkins.model.MavenJobWithDetails Maven / Gradle / Ivy
The newest version!
package com.offbytwo.jenkins.model;
import static com.google.common.collect.Lists.transform;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpResponseException;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.offbytwo.jenkins.client.util.EncodingUtils;
import com.offbytwo.jenkins.helper.Range;
public class MavenJobWithDetails extends MavenJob {
private String displayName;
private boolean buildable;
private List builds;
private MavenBuild firstBuild;
private MavenBuild lastBuild;
private MavenBuild lastCompletedBuild;
private MavenBuild lastFailedBuild;
private MavenBuild lastStableBuild;
private MavenBuild lastSuccessfulBuild;
private MavenBuild lastUnstableBuild;
private MavenBuild lastUnsuccessfulBuild;
private int nextBuildNumber;
private List downstreamProjects;
private List upstreamProjects;
public MavenJobWithDetails() {
}
public String getDisplayName() {
return displayName;
}
public boolean isBuildable() {
return buildable;
}
/**
* This method will give you back the builds of a particular job.
*
* Note: Jenkins limits the number of results to a maximum of 100 builds
* which you will get back.. In case you have more than 100 build you
* won't get back all builds via this method. In such cases you need to use
* {@link #getAllBuilds()}.
*
* @return the list of {@link MavenBuild}. In case of no builds have been
* executed yet {@link Collections#emptyList()} will be returned.
*/
public List getBuilds() {
if (builds == null) {
return Collections.emptyList();
} else {
return Lists.transform(builds, new Function() {
@Override
public MavenBuild apply(MavenBuild from) {
return buildWithClient(from);
}
});
}
}
/**
* This method will give you back all builds which exists independent of the
* number. You should be aware that this can be much in some cases if you
* have more than 100 builds which is by default limited by Jenkins
* {@link #getBuilds()}. This method limits it to particular information
* which can be later used to get supplemental information about a
* particular build {@link Build#details()} to reduce the amount of data
* which needed to be transfered.
*
* @return the list of {@link Build}. In case of no builds have been
* executed yet return {@link Collections#emptyList()}.
* @throws IOException
* In case of failure.
* @see Jenkins
* Issue
*/
public List getAllBuilds() throws IOException {
String path = "/";
try {
List builds = client.get(path + "job/" + EncodingUtils.encode(this.getName())
+ "?tree=allBuilds[number[*],url[*],queueId[*]]", AllMavenBuilds.class).getAllBuilds();
if (builds == null) {
return Collections.emptyList();
} else {
return transform(builds, new Function() {
@Override
public MavenBuild apply(MavenBuild from) {
return buildWithClient(from);
}
});
}
} catch (HttpResponseException e) {
// TODO: Thinks about a better handling if the job does not exist?
if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
// TODO: Check this if this is necessary or a good idea?
return null;
}
throw e;
}
}
/**
*
*
* - {M,N}: From the M-th element (inclusive) to the N-th element
* (exclusive).
* - {M,}: From the M-th element (inclusive) to the end.
* - {,N}: From the first element (inclusive) to the N-th element
* (exclusive). The same as {0,N}.
* - {N}: Just retrieve the N-th element. The same as {N,N+1}.
*
*
* Note: At the moment there seemed to be no option to get the number of
* existing builds for a job. The only option is to get all builds via
* {@link #getAllBuilds()}.
*
* @param range
* {@link Range}
* @return the list of {@link Build}. In case of no builds have been
* executed yet return {@link Collections#emptyList()}.
* @throws IOException
* in case of an error.
*/
public List getAllBuilds(Range range) throws IOException {
String path = "/" + "job/" + EncodingUtils.encode(this.getName())
+ "?tree=allBuilds[number[*],url[*],queueId[*]]";
try {
List builds = client.get(path + range.getRangeString(), AllMavenBuilds.class).getAllBuilds();
if (builds == null) {
return Collections.emptyList();
} else {
return transform(builds, new Function() {
@Override
public MavenBuild apply(MavenBuild from) {
return buildWithClient(from);
}
});
}
} catch (HttpResponseException e) {
// TODO: Thinks about a better handline if the job does not exist?
if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
// TODO: Check this if this is necessary or a good idea?
return null;
}
throw e;
}
}
/**
* @return The firstBuild. If {@link #firstBuild} has never been run
* {@link MavenBuild#BUILD_HAS_NEVER_RUN} will be returned.
*/
public MavenBuild getFirstBuild() {
if (firstBuild == null) {
return MavenBuild.BUILD_HAS_NEVER_RUN;
} else {
return buildWithClient(firstBuild);
}
}
/**
* @return The lastBuild. If {@link #lastBuild} has never been run
* {@link MavenBuild#BUILD_HAS_NEVER_RUN} will be returned.
*/
public MavenBuild getLastBuild() {
if (lastBuild == null) {
return MavenBuild.BUILD_HAS_NEVER_RUN;
} else {
return buildWithClient(lastBuild);
}
}
/**
* @return The lastCompletedBuild. If {@link #lastCompletedBuild} has never
* been run {@link MavenBuild#BUILD_HAS_NEVER_RUN} will be returned.
*/
public MavenBuild getLastCompletedBuild() {
if (lastCompletedBuild == null) {
return MavenBuild.BUILD_HAS_NEVER_RUN;
} else {
return buildWithClient(lastCompletedBuild);
}
}
/**
* @return The lastFailedBuild. If {@link #lastFailedBuild} has never been
* run {@link MavenBuild#BUILD_HAS_NEVER_RUN} will be returned.
*/
public MavenBuild getLastFailedBuild() {
if (lastFailedBuild == null) {
return MavenBuild.BUILD_HAS_NEVER_RUN;
} else {
return buildWithClient(lastFailedBuild);
}
}
/**
* @return The lastStableBuild. If {@link #lastStableBuild} has never been
* run {@link MavenBuild#BUILD_HAS_NEVER_RUN} will be returned.
*/
public MavenBuild getLastStableBuild() {
if (lastStableBuild == null) {
return MavenBuild.BUILD_HAS_NEVER_RUN;
} else {
return buildWithClient(lastStableBuild);
}
}
/**
* @return The lastSuccessfulBuild. If {@link #lastSuccessfulBuild} has
* never been run {@link MavenBuild#BUILD_HAS_NEVER_RUN} will be
* returned.
*/
public MavenBuild getLastSuccessfulBuild() {
if (lastSuccessfulBuild == null) {
return MavenBuild.BUILD_HAS_NEVER_RUN;
} else {
return buildWithClient(lastSuccessfulBuild);
}
}
/**
* @return The lastUnstableBuild. If {@link #lastUnstableBuild} has never
* been run {@link MavenBuild#BUILD_HAS_NEVER_RUN} will be returned.
*/
public MavenBuild getLastUnstableBuild() {
if (lastUnstableBuild == null) {
return MavenBuild.BUILD_HAS_NEVER_RUN;
} else {
return buildWithClient(lastUnstableBuild);
}
}
/**
* @return The lastUnsuccessfulBuild. If {@link #lastUnsuccessfulBuild} has
* never been run {@link MavenBuild#BUILD_HAS_NEVER_RUN} will be
* returned.
*/
public MavenBuild getLastUnsuccessfulBuild() {
if (lastUnsuccessfulBuild == null) {
return MavenBuild.BUILD_HAS_NEVER_RUN;
} else {
return buildWithClient(lastUnsuccessfulBuild);
}
}
public int getNextBuildNumber() {
return nextBuildNumber;
}
public List getDownstreamProjects() {
if (downstreamProjects == null) {
return Collections.emptyList();
} else {
return Lists.transform(downstreamProjects, new MavenJobWithClient());
}
}
public List getUpstreamProjects() {
if (upstreamProjects == null) {
return Collections.emptyList();
} else {
return Lists.transform(upstreamProjects, new MavenJobWithClient());
}
}
public MavenBuild getBuildByNumber(final int buildNumber) {
Predicate isMatchingBuildNumber = new Predicate() {
@Override
public boolean apply(MavenBuild input) {
return input.getNumber() == buildNumber;
}
};
Optional optionalBuild = Iterables.tryFind(builds, isMatchingBuildNumber);
// TODO: Check if we could use Build#NO...instead of Null?
return optionalBuild.orNull() == null ? null : buildWithClient(optionalBuild.orNull());
}
private MavenBuild buildWithClient(MavenBuild from) {
MavenBuild ret = new MavenBuild(from);
ret.setClient(client);
return ret;
}
private class MavenJobWithClient implements Function {
@Override
public Job apply(Job job) {
job.setClient(client);
return job;
}
}
}