Skip to content
Permalink
Browse files
[JENKINS-26101] Proof of concept of flow definition loaded entirely f…
…rom a local SCM checkout.

Originally-Committed-As: 50be69d7db0fff857f044bf725bcb0f1d8d0e7a2
  • Loading branch information
jglick committed Jan 21, 2015
1 parent 963cfbe commit c4204850101b4f232ea6c164f66f6ec36cc076a4
@@ -98,11 +98,14 @@ public CpsFlowExecution create(FlowExecutionOwner handle, Action... actions) thr
}

@Override
@SuppressWarnings("deprecation")
public CpsFlowExecution create(FlowExecutionOwner owner, List<? extends Action> actions) throws IOException {
for (Action a : actions) {
if (a instanceof CpsFlowFactoryAction) {
CpsFlowFactoryAction fa = (CpsFlowFactoryAction) a;
return fa.create(this,owner,actions);
} else if (a instanceof CpsFlowFactoryAction2) {
return ((CpsFlowFactoryAction2) a).create(this, owner, actions);
}
}
return new CpsFlowExecution(sandbox ? script : ScriptApproval.get().using(script, GroovyLanguage.get()), sandbox, owner);
@@ -31,10 +31,7 @@
import java.util.List;

/**
* {@link Action} that can be passed to {@link CpsFlowDefinition#create(FlowExecutionOwner, List)}
* that can take over the instantiation of {@link CpsFlowExecution}.
*
* @author Kohsuke Kawaguchi
* @deprecated Use {@link CpsFlowFactoryAction2} instead.
*/
public interface CpsFlowFactoryAction extends Action {
CpsFlowExecution create(CpsFlowDefinition def, FlowExecutionOwner owner, List<? extends Action> actions) throws IOException;
@@ -0,0 +1,41 @@
/*
* The MIT License
*
* Copyright 2015 Jesse Glick.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.workflow.cps;

import hudson.model.Action;
import java.io.IOException;
import java.util.List;
import org.jenkinsci.plugins.workflow.flow.FlowDefinition;
import org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner;

/**
* {@link Action} that can be passed to {@link FlowDefinition#create(FlowExecutionOwner, List)}
* that can take over the instantiation of {@link CpsFlowExecution}.
*/
public interface CpsFlowFactoryAction2 extends Action {

CpsFlowExecution create(FlowDefinition def, FlowExecutionOwner owner, List<? extends Action> actions) throws IOException;

}
@@ -0,0 +1,117 @@
/*
* The MIT License
*
* Copyright 2015 Jesse Glick.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package org.jenkinsci.plugins.workflow.cps;

import hudson.Extension;
import hudson.FilePath;
import hudson.model.Action;
import hudson.model.Job;
import hudson.model.Queue;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.model.TopLevelItem;
import hudson.scm.SCM;
import hudson.scm.SCMDescriptor;
import hudson.util.LogTaskListener;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.model.Jenkins;
import org.jenkinsci.plugins.workflow.cps.persistence.PersistIn;
import static org.jenkinsci.plugins.workflow.cps.persistence.PersistenceContext.JOB;
import org.jenkinsci.plugins.workflow.flow.FlowDefinition;
import org.jenkinsci.plugins.workflow.flow.FlowDefinitionDescriptor;
import org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;

@PersistIn(JOB)
public class CpsScmFlowDefinition extends FlowDefinition {

private final SCM scm;
private final String scriptPath;

@DataBoundConstructor public CpsScmFlowDefinition(SCM scm, String scriptPath) {
this.scm = scm;
this.scriptPath = scriptPath;
}

public SCM getScm() {
return scm;
}

public String getScriptPath() {
return scriptPath;
}

@Override public CpsFlowExecution create(FlowExecutionOwner owner, List<? extends Action> actions) throws IOException {
TaskListener listener = new LogTaskListener(Logger.getLogger(CpsScmFlowDefinition.class.getName()), Level.INFO); // TODO introduce an overload accepting TaskListener
for (Action a : actions) {
if (a instanceof CpsFlowFactoryAction2) {
return ((CpsFlowFactoryAction2) a).create(this, owner, actions);
}
}
FilePath dir;
Queue.Executable _build = owner.getExecutable();
if (!(_build instanceof Run)) {
throw new IOException("can only check out SCM into a Run");
}
Run<?,?> build = (Run<?,?>) _build;
if (build.getParent() instanceof TopLevelItem) {
dir = Jenkins.getInstance().getWorkspaceFor((TopLevelItem) build.getParent()).withSuffix("@script");
} else { // should not happen, but just in case:
dir = new FilePath(owner.getRootDir());
}
try {
scm.checkout(build, Jenkins.getInstance().createLauncher(listener), dir, listener, /* TODO consider whether to add to changelog */null, /* TODO consider whether to include in polling */null);
FilePath scriptFile = dir.child(scriptPath);
if (!scriptFile.absolutize().getRemote().replace('\\', '/').startsWith(dir.absolutize().getRemote().replace('\\', '/') + '/')) { // TODO need some FilePath.isInside(FilePath) method
throw new IOException(scriptFile + " is not inside " + dir);
}
return new CpsFlowExecution(scriptFile.readToString(), true, owner);
} catch (InterruptedException x) {
throw new IOException(x); // TODO overload should also permit InterruptedException to be thrown
}
}

@Extension public static class DescriptorImpl extends FlowDefinitionDescriptor {

@Override public String getDisplayName() {
return "Groovy CPS DSL from SCM";
}

public Collection<? extends SCMDescriptor<?>> getApplicableDescriptors() {
StaplerRequest req = Stapler.getCurrentRequest();
return SCM._for(req != null ? req.findAncestorObject(Job.class) : null);
}

// TODO migrate doGenerateSnippet to a helper class

}

}
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License
Copyright 2015 Jesse Glick.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:dropdownDescriptorSelector field="scm" title="SCM" descriptors="${descriptor.applicableDescriptors}"/>
<f:entry field="scriptPath" title="${%Script Path}">
<f:textbox default="flow.groovy"/>
</f:entry>
</j:jelly>

0 comments on commit c420485

Please sign in to comment.