Skip to content
This repository has been archived by the owner on Aug 27, 2024. It is now read-only.

Fix pipeline checkout #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,8 @@ public WorkspaceSnapshot snapshot(Run<?,?> build, FilePath ws, String includeGlo
return new WorkspaceSnapshotZip();
case "TARONLY":
{
OutputStream os = new BufferedOutputStream(FilePath.TarCompression.NONE.compress(new FileOutputStream(wss)));
try {
try (OutputStream os = new BufferedOutputStream(FilePath.TarCompression.NONE.compress(new FileOutputStream(wss)))) {
ws.tar(os, scanner);
} finally {
os.close();
}

return new WorkspaceSnapshotTarOnly();
Expand Down Expand Up @@ -298,43 +295,69 @@ private boolean doPerform(boolean betterOrEqualToCriteria, FilePath ws, String r
}
}
}

public static final class WorkspaceSnapshotTar extends WorkspaceSnapshot {

public static abstract class WorkspaceSnapshotBase extends WorkspaceSnapshot {
public abstract void restoreTo(Run<?, ?> run, FilePath fp, TaskListener tl) throws IOException, InterruptedException;
}

public static final class WorkspaceSnapshotTar extends WorkspaceSnapshotBase {
@Override
public void restoreTo(AbstractBuild<?,?> owner, FilePath dst, TaskListener listener) throws IOException, InterruptedException {
File wss = new File(owner.getRootDir(), CloneWorkspaceUtil.getFileNameForMethod("TAR"));
new FilePath(wss).untar(dst, FilePath.TarCompression.GZIP);
}

@Override
public void restoreTo(Run<?, ?> run, FilePath fp, TaskListener tl) throws IOException, InterruptedException {
File wss = new File(run.getRootDir(), CloneWorkspaceUtil.getFileNameForMethod("TAR"));
new FilePath(wss).untar(fp, FilePath.TarCompression.GZIP);
}
}

public static final class WorkspaceSnapshotTarOnly extends WorkspaceSnapshot {
public static final class WorkspaceSnapshotTarOnly extends WorkspaceSnapshotBase {
@Override
public void restoreTo(AbstractBuild<?,?> owner, FilePath dst, TaskListener listener) throws IOException, InterruptedException {
File wss = new File(owner.getRootDir(), CloneWorkspaceUtil.getFileNameForMethod("TARONLY"));
new FilePath(wss).untar(dst, FilePath.TarCompression.NONE);
}

@Override
public void restoreTo(Run<?, ?> run, FilePath fp, TaskListener tl) throws IOException, InterruptedException {
File wss = new File(run.getRootDir(), CloneWorkspaceUtil.getFileNameForMethod("TARONLY"));
new FilePath(wss).untar(fp, FilePath.TarCompression.NONE);
}
}

public static final class WorkspaceSnapshotTarNative extends WorkspaceSnapshot {
public static final class WorkspaceSnapshotTarNative extends WorkspaceSnapshotBase {
@Override
public void restoreTo(AbstractBuild<?, ?> owner, FilePath dst, TaskListener listener) throws IOException, InterruptedException {
}

public void restoreTo(AbstractBuild<?, ?> owner, FilePath dst, TaskListener listener, Launcher launcher) throws IOException, InterruptedException {
@Override
public void restoreTo(Run<?, ?> run, FilePath fp, TaskListener tl) throws IOException, InterruptedException {
}

public void restoreTo(Run<?, ?> owner, FilePath dst, TaskListener listener, Launcher launcher) throws IOException, InterruptedException {
File wss = new File(owner.getRootDir(), CloneWorkspaceUtil.getFileNameForMethod("TAR-NATIVE"));
NativeTarUtil.unarchive(wss, dst, launcher, listener);
}
}

public static final class WorkspaceSnapshotZip extends WorkspaceSnapshot {
public static final class WorkspaceSnapshotZip extends WorkspaceSnapshotBase {
@Override
public void restoreTo(AbstractBuild<?,?> owner, FilePath dst, TaskListener listener) throws IOException, InterruptedException {
File wss = new File(owner.getRootDir(), CloneWorkspaceUtil.getFileNameForMethod("ZIP"));
new FilePath(wss).unzip(dst);
}

@Override
public void restoreTo(Run<?, ?> run, FilePath fp, TaskListener tl) throws IOException, InterruptedException {
File wss = new File(run.getRootDir(), CloneWorkspaceUtil.getFileNameForMethod("ZIP"));
new FilePath(wss).unzip(fp);
}
}

@Symbol("CloneWorkspace")
@Symbol("cloneWorkspace")
@Extension public static class DescriptorImpl extends BuildStepDescriptor<Publisher> {
public DescriptorImpl() {
super(CloneWorkspacePublisher.class);
Expand Down
39 changes: 25 additions & 14 deletions src/main/java/hudson/plugins/cloneworkspace/CloneWorkspaceSCM.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import hudson.WorkspaceSnapshot;
import hudson.Extension;
import static hudson.Util.fixEmptyAndTrim;
import hudson.model.Job;
import org.apache.commons.collections.ListUtils;

import java.io.IOException;
Expand All @@ -57,6 +58,8 @@
import net.sf.json.JSONObject;
import hudson.model.ParameterDefinition;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.Run;
import hudson.scm.NullChangeLogParser;
import jenkins.model.Jenkins;

import org.kohsuke.stapler.StaplerRequest;
Expand Down Expand Up @@ -115,17 +118,18 @@ public String getParamParentJobName(AbstractBuild<?, ?> build) {
*/
public Snapshot resolve(String parentJob) throws ResolvedFailedException {
Jenkins h = Jenkins.getInstance();
AbstractProject<?,?> job = h.getItemByFullName(parentJob, AbstractProject.class);
if(job==null) {
if(h.getItemByFullName(parentJob)==null) {
Job<?,?> job = h.getItemByFullName(parentJob, Job.class);
if(job==null) {
if(h.getItemByFullName(parentJob)==null) {
AbstractProject nearest = AbstractProject.findNearest(parentJob);
throw new ResolvedFailedException(Messages.CloneWorkspaceSCM_NoSuchJob(parentJob,nearest.getFullName()));
} else
throw new ResolvedFailedException(Messages.CloneWorkspaceSCM_IncorrectJobType(parentJob));
}


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

if(b==null)
throw new ResolvedFailedException(Messages.CloneWorkspaceSCM_NoBuild(criteria,parentJob));
Expand Down Expand Up @@ -162,7 +166,7 @@ public boolean checkout(AbstractBuild build, Launcher launcher, FilePath workspa
/**
* Called after checkout has finished to copy the changelog from the parent build.
*/
private boolean calcChangeLog(AbstractBuild<?,?> parentBuild, File changelogFile, BuildListener listener) throws IOException, InterruptedException {
private boolean calcChangeLog(Run<?,?> parentBuild, File changelogFile, BuildListener listener) throws IOException, InterruptedException {
FilePath parentChangeLog = new FilePath(new File(parentBuild.getRootDir(), "changelog.xml"));
if (parentChangeLog.exists()) {
FilePath childChangeLog = new FilePath(changelogFile);
Expand All @@ -183,11 +187,18 @@ public ChangeLogParser createChangeLogParser() {
}

try {
return resolve(getParamParentJobName(lastBuild)).getParent().getProject().getScm().createChangeLogParser();
Run<?,?> r = resolve(getParamParentJobName(lastBuild)).getParent();
if(r instanceof AbstractBuild<?,?>) {
return ((AbstractBuild<?,?>)r).getProject().getScm().createChangeLogParser();
} else {
return new NullChangeLogParser();
}
} catch (ResolvedFailedException e) {
return null;
}
}
}



private AbstractProject getContainingProject() {
for( AbstractProject p : Jenkins.getInstance().getAllItems(AbstractProject.class) ) {
Expand Down Expand Up @@ -385,22 +396,22 @@ private ResolvedFailedException(String message) {

private static class Snapshot {
final WorkspaceSnapshot snapshot;
final AbstractBuild<?,?> parent;
final Run<?,?> parent;

private Snapshot(WorkspaceSnapshot snapshot, AbstractBuild<?,?> parent) {
private Snapshot(WorkspaceSnapshot snapshot, Run<?,?> parent) {
this.snapshot = snapshot;
this.parent = parent;
}

void restoreTo(FilePath dst,TaskListener listener, Launcher launcher) throws IOException, InterruptedException {
void restoreTo(FilePath dst, TaskListener listener, Launcher launcher) throws IOException, InterruptedException {
if(snapshot instanceof CloneWorkspacePublisher.WorkspaceSnapshotTarNative) {
((CloneWorkspacePublisher.WorkspaceSnapshotTarNative)snapshot).restoreTo(parent,dst, listener, launcher);
} else {
snapshot.restoreTo(parent, dst, listener);
((CloneWorkspacePublisher.WorkspaceSnapshotTarNative)snapshot).restoreTo(parent, dst, listener, launcher);
} else if(snapshot instanceof CloneWorkspacePublisher.WorkspaceSnapshotBase) {
((CloneWorkspacePublisher.WorkspaceSnapshotBase)snapshot).restoreTo(parent, dst, listener);
}
}

AbstractBuild<?,?> getParent() {
Run<?,?> getParent() {
return parent;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,15 @@ public static Run<?,?> getMostRecentRunForCriteria(AbstractProject<?,?> project,
return getMostRecentRunForCriteria(project.getLastBuild(), getResultForCriteria(criteria));
}

public static Run<?,?> getMostRecentRunForCriteria(AbstractBuild<?,?> baseBuild, String criteria) {
public static Run<?,?> getMostRecentRunForCriteria(Run<?,?> baseBuild, String criteria) {
return getMostRecentRunForCriteria(baseBuild, getResultForCriteria(criteria));
}

public static Run<?,?> getMostRecentRunForCriteria(Run<?,?> baseBuild, Result criteriaResult) {
if ((baseBuild == null)
|| ((!baseBuild.isBuilding()) && (baseBuild.getResult() != null)
&& (baseBuild.getResult().isBetterOrEqualTo(criteriaResult)))) {
&& (baseBuild.getResult().isBetterOrEqualTo(criteriaResult)))
|| ((baseBuild.isBuilding()) && criteriaResult.equals(Result.FAILURE))) { // Only jobs with "Any" condition will be true
return baseBuild;
}
else {
Expand Down