Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[JENKINS-26101] Proof of concept of flow definition loaded entirely f…
…rom a local SCM checkout. Originally-Committed-As: 50be69d7db0fff857f044bf725bcb0f1d8d0e7a2
- Loading branch information
Showing
5 changed files
with
194 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
cps/src/main/java/org/jenkinsci/plugins/workflow/cps/CpsFlowFactoryAction2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
|
||
} |
117 changes: 117 additions & 0 deletions
117
cps/src/main/java/org/jenkinsci/plugins/workflow/cps/CpsScmFlowDefinition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
cps/src/main/resources/org/jenkinsci/plugins/workflow/cps/CpsScmFlowDefinition/config.jelly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |