Navigation Menu

Skip to content

Commit

Permalink
[FIXED JENKINS-9779] Process parent job name for parameters.
Browse files Browse the repository at this point in the history
  • Loading branch information
abayer committed Jan 17, 2012
1 parent a92fbe2 commit 703b61c
Showing 1 changed file with 52 additions and 17 deletions.
69 changes: 52 additions & 17 deletions src/main/java/hudson/plugins/cloneworkspace/CloneWorkspaceSCM.java
Expand Up @@ -30,10 +30,11 @@
import hudson.scm.SCMRevisionState; import hudson.scm.SCMRevisionState;
import static hudson.scm.PollingResult.BUILD_NOW; import static hudson.scm.PollingResult.BUILD_NOW;
import static hudson.scm.PollingResult.NO_CHANGES; import static hudson.scm.PollingResult.NO_CHANGES;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject; import hudson.model.AbstractProject;
import hudson.model.Descriptor; import hudson.model.Descriptor;
import hudson.model.TaskListener; import hudson.model.TaskListener;
import hudson.model.AbstractBuild; import hudson.model.ParametersAction;
import hudson.model.BuildListener; import hudson.model.BuildListener;
import hudson.model.Hudson; import hudson.model.Hudson;
import hudson.model.Result; import hudson.model.Result;
Expand Down Expand Up @@ -88,33 +89,52 @@ public CloneWorkspaceSCM(String parentJobName, String criteria) {
this.parentJobName = parentJobName; this.parentJobName = parentJobName;
this.criteria = criteria; this.criteria = criteria;
} }

/**
* Get the parent job name. Process it for parameters if needed.
*
* @return Parent job name.
*/
public String getParamParentJobName(AbstractBuild<?, ?> build) {
String original = parentJobName;
if (build != null) {
ParametersAction parameters = build.getAction(ParametersAction.class);
if (parameters != null) {
original = parameters.substitute(build, original);
}
}

return original;
}



/** /**
* Obtains the {@link WorkspaceSnapshot} object that this {@link SCM} points to, * Obtains the {@link WorkspaceSnapshot} object that this {@link SCM} points to,
* or throws {@link ResolvedFailedException} upon failing. * or throws {@link ResolvedFailedException} upon failing.
* *
* @param parentJob Processed parent job name.
* @return never null. * @return never null.
*/ */
public Snapshot resolve() throws ResolvedFailedException { public Snapshot resolve(String parentJob) throws ResolvedFailedException {
Hudson h = Hudson.getInstance(); Hudson h = Hudson.getInstance();
AbstractProject<?,?> job = h.getItemByFullName(parentJobName, AbstractProject.class); AbstractProject<?,?> job = h.getItemByFullName(parentJob, AbstractProject.class);
if(job==null) { if(job==null) {
if(h.getItemByFullName(parentJobName)==null) { if(h.getItemByFullName(parentJob)==null) {
AbstractProject nearest = AbstractProject.findNearest(parentJobName); AbstractProject nearest = AbstractProject.findNearest(parentJob);
throw new ResolvedFailedException(Messages.CloneWorkspaceSCM_NoSuchJob(parentJobName,nearest.getFullName())); throw new ResolvedFailedException(Messages.CloneWorkspaceSCM_NoSuchJob(parentJob,nearest.getFullName()));
} else } else
throw new ResolvedFailedException(Messages.CloneWorkspaceSCM_IncorrectJobType(parentJobName)); throw new ResolvedFailedException(Messages.CloneWorkspaceSCM_IncorrectJobType(parentJob));
} }




AbstractBuild<?,?> b = CloneWorkspaceUtil.getMostRecentBuildForCriteria(job,criteria); AbstractBuild<?,?> b = CloneWorkspaceUtil.getMostRecentBuildForCriteria(job,criteria);


if(b==null) if(b==null)
throw new ResolvedFailedException(Messages.CloneWorkspaceSCM_NoBuild(criteria,parentJobName)); throw new ResolvedFailedException(Messages.CloneWorkspaceSCM_NoBuild(criteria,parentJob));


WorkspaceSnapshot snapshot = b.getAction(WorkspaceSnapshot.class); WorkspaceSnapshot snapshot = b.getAction(WorkspaceSnapshot.class);
if(snapshot==null) if(snapshot==null)
throw new ResolvedFailedException(Messages.CloneWorkspaceSCM_NoWorkspace(parentJobName,criteria)); throw new ResolvedFailedException(Messages.CloneWorkspaceSCM_NoWorkspace(parentJob,criteria));


return new Snapshot(snapshot,b); return new Snapshot(snapshot,b);
} }
Expand All @@ -123,9 +143,9 @@ public Snapshot resolve() throws ResolvedFailedException {
public boolean checkout(AbstractBuild build, Launcher launcher, FilePath workspace, BuildListener listener, File changelogFile) throws IOException, InterruptedException { public boolean checkout(AbstractBuild build, Launcher launcher, FilePath workspace, BuildListener listener, File changelogFile) throws IOException, InterruptedException {
try { try {
workspace.deleteContents(); workspace.deleteContents();

String parentJob = getParamParentJobName(build);
Snapshot snapshot = resolve(); Snapshot snapshot = resolve(parentJob);
listener.getLogger().println("Restoring workspace from build #" + snapshot.getParent().getNumber() + " of project " + parentJobName); listener.getLogger().println("Restoring workspace from build #" + snapshot.getParent().getNumber() + " of project " + parentJob);
snapshot.restoreTo(workspace,listener); snapshot.restoreTo(workspace,listener);


// write out the parent build number file // write out the parent build number file
Expand Down Expand Up @@ -161,13 +181,26 @@ private boolean calcChangeLog(AbstractBuild<?,?> parentBuild, File changelogFile


@Override @Override
public ChangeLogParser createChangeLogParser() { public ChangeLogParser createChangeLogParser() {
AbstractProject<?,?> p = getContainingProject();
final AbstractBuild lastBuild = p.getLastBuild();

try { try {
return resolve().getParent().getProject().getScm().createChangeLogParser(); return resolve(getParamParentJobName(lastBuild)).getParent().getProject().getScm().createChangeLogParser();
} catch (ResolvedFailedException e) { } catch (ResolvedFailedException e) {
return null; return null;
} }
} }


private AbstractProject getContainingProject() {
for( AbstractProject p : Hudson.getInstance().getAllItems(AbstractProject.class) ) {
SCM scm = p.getScm();
if (scm != null && scm.getClass() == this.getClass() && this.equals(scm)) {
return p;
}
}
return null;
}

@Override @Override
public DescriptorImpl getDescriptor() { public DescriptorImpl getDescriptor() {
return (DescriptorImpl)super.getDescriptor(); return (DescriptorImpl)super.getDescriptor();
Expand Down Expand Up @@ -238,8 +271,10 @@ public SCMRevisionState calcRevisionsFromBuild(AbstractBuild build, Launcher lau


@Override @Override
protected PollingResult compareRemoteRevisionWith(AbstractProject<?,?> project, Launcher launcher, FilePath workspace, final TaskListener listener, SCMRevisionState _baseline) throws IOException, InterruptedException { protected PollingResult compareRemoteRevisionWith(AbstractProject<?,?> project, Launcher launcher, FilePath workspace, final TaskListener listener, SCMRevisionState _baseline) throws IOException, InterruptedException {
final AbstractBuild lastBuild = project.getLastBuild();
String parentJob = getParamParentJobName(lastBuild);
Hudson h = Hudson.getInstance(); Hudson h = Hudson.getInstance();
AbstractProject<?,?> parentProject = h.getItemByFullName(parentJobName, AbstractProject.class); AbstractProject<?,?> parentProject = h.getItemByFullName(parentJob, AbstractProject.class);
if (parentProject==null) { if (parentProject==null) {
// Disable this project if the parent project no longer exists or doesn't exist in the first place. // Disable this project if the parent project no longer exists or doesn't exist in the first place.
listener.getLogger().println("The CloneWorkspace parent project for " + project + " does not exist, project will be disabled."); listener.getLogger().println("The CloneWorkspace parent project for " + project + " does not exist, project will be disabled.");
Expand All @@ -251,7 +286,7 @@ protected PollingResult compareRemoteRevisionWith(AbstractProject<?,?> project,


Snapshot s = null; Snapshot s = null;
try { try {
s = resolve(); s = resolve(parentJob);
} catch (ResolvedFailedException e) { } catch (ResolvedFailedException e) {
listener.getLogger().println(e.getMessage()); listener.getLogger().println(e.getMessage());
return new PollingResult(baseline, baseline, PollingResult.Change.NONE); return new PollingResult(baseline, baseline, PollingResult.Change.NONE);
Expand All @@ -264,14 +299,14 @@ protected PollingResult compareRemoteRevisionWith(AbstractProject<?,?> project,
} }
else { else {
if (s.getParent().getNumber() > baseline.parentBuildNumber) { if (s.getParent().getNumber() > baseline.parentBuildNumber) {
listener.getLogger().println("Build #" + s.getParent().getNumber() + " of project " + parentJobName listener.getLogger().println("Build #" + s.getParent().getNumber() + " of project " + parentJob
+ " is newer than build #" + baseline.parentBuildNumber + ", so a new build of " + " is newer than build #" + baseline.parentBuildNumber + ", so a new build of "
+ project + " will be run."); + project + " will be run.");
return new PollingResult(baseline, new CloneWorkspaceSCMRevisionState(s.getParent().getNumber()), PollingResult.Change.SIGNIFICANT); return new PollingResult(baseline, new CloneWorkspaceSCMRevisionState(s.getParent().getNumber()), PollingResult.Change.SIGNIFICANT);
// return BUILD_NOW; // return BUILD_NOW;
} }
else { else {
listener.getLogger().println("Build #" + s.getParent().getNumber() + " of project " + parentJobName listener.getLogger().println("Build #" + s.getParent().getNumber() + " of project " + parentJob
+ " is NOT newer than build #" + baseline.parentBuildNumber + ", so no new build of " + " is NOT newer than build #" + baseline.parentBuildNumber + ", so no new build of "
+ project + " will be run."); + project + " will be run.");
return new PollingResult(baseline, baseline, PollingResult.Change.NONE); return new PollingResult(baseline, baseline, PollingResult.Change.NONE);
Expand Down

0 comments on commit 703b61c

Please sign in to comment.