Skip to content

Commit

Permalink
Merge pull request #14 from abayer/jenkins-38152
Browse files Browse the repository at this point in the history
JENKINS-38152 - expose execution model on WorkflowRun
  • Loading branch information
abayer committed Sep 19, 2016
2 parents 7ae4f28 + e396159 commit b385ba5
Show file tree
Hide file tree
Showing 26 changed files with 399 additions and 10 deletions.
50 changes: 50 additions & 0 deletions EXTENDING.md
@@ -0,0 +1,50 @@
# Information on extending/integrating with Pipeline Model Definition

## API Endpoints

All API endpoints are exposed under `JENKINS_URL/pipeline-model-converter` currently.

### JSON Schema
* *URL*: `JENKINS_URL/pipeline-model-converter/schema`
* *Parameters*: None
* *Info*: JSON schema for the JSON representation of the model.
* *Returns*: The JSON schema

### Validation of `Jenkinsfile`
* *URL*: `JENKINS_URL/pipeine-model-converter/validateJenkinsfile`
* *Parameters*: `jenkinsfile` - the `Jenkinsfile` contents
* *Info*: Takes a `Jenkinsfile` and validates its syntax, semantics, steps, parameters etc on the Jenkins master.
* *Returns*: JSON with a `result` field that will either be `success` or `failure`. If `failure`, there'll be an additional array in the `errors` field of the error messages encountered.

### Validation of JSON representation
* *URL*: `JENKINS_URL/pipeine-model-converter/validateJson`
* *Parameters*: `json` - the JSON representation
* *Info*: Takes a JSON representation of the model and validates its syntax, semantics, steps, parameters etc on the Jenkins master.
* *Returns*: JSON with a `result` field that will either be `success` or `failure`. If `failure`, there'll be an additional array in the `errors` field of the error messages encountered.

### Conversion to JSON representation from Jenkinsfile
* *URL*: `JENKINS_URL/pipeline-model-converter/toJson`
* *Parameters*: `jenkinsfile` - the `Jenkinsfile` contents
* *Info*: Takes a `Jenkinsfile` and converts it to the JSON representation for its `pipeline` step.
* *Returns*: JSON with a `result` field that will either be `success` or `failure`. If `success`, the JSON representation will be in the `json` field. If `failure`, there'll be an additional array in the `errors` field of the error messages encountered.

### Conversion to Jenkinsfile from JSON representation
* *URL*: `JENKINS_URL/pipeline-model-converter/toJenkinsfile`
* *Parameters*: `json` - the JSON representation of the model
* *Info*: Takes the JSON representation of the model and converts it to the contents for a `Jenkinsfile` invoking the `pipeline` step.
* *Returns*: JSON with a `result` field that will either be `success` or `failure`. If `success`, the `Jenkinsfile` contents will be in the `jenkinsfile` field. If `failure`, there'll be an additional array in the `errors` field of the error messages encountered.

## Java API

### JSON schema
* *API call*: `org.jenkinsci.plugins.pipeline.parser.Converter.getJSONSchema()`
* *Parameters*: None
* *Returns*: Returns the JSON representation schema

### `WorkflowRun` execution model
* *API call*: `someWorkflowRun.getAction(ExecutionModelAction.class).getStages()`
* *Parameters*: None
* *Returns*: The `ModelASTStages` for a particular `WorkflowRun`. This is generated at the beginning of build execution, stripped of
`sourceLocation` references for serialization purposes, and attached to the `WorkflowRun`. Stage and branch execution order can be found here,
even when the build hasn't gotten to those stages or branches yet, since the model dictates execution order.

Expand Up @@ -26,10 +26,25 @@ package org.jenkinsci.plugins.pipeline.modeldefinition;


import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
import org.jenkinsci.plugins.pipeline.modeldefinition.actions.ExecutionModelAction
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTArgumentList
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTBranch
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTKey
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTNamedArgumentList
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTPipelineDef
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTPositionalArgumentList
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTSingleArgument
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTStage
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTStages
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTStep
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTTreeStep
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTValue
import org.jenkinsci.plugins.pipeline.modeldefinition.model.NestedModel
import org.jenkinsci.plugins.pipeline.modeldefinition.model.StepBlockWithOtherArgs
import org.jenkinsci.plugins.pipeline.modeldefinition.parser.Converter
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.Whitelisted
import org.jenkinsci.plugins.workflow.cps.CpsScript
import org.jenkinsci.plugins.workflow.job.WorkflowRun

import java.lang.reflect.ParameterizedType;

Expand Down Expand Up @@ -151,4 +166,18 @@ public class Utils {
return false
}
}

@Whitelisted
static void attachExecutionModel(CpsScript script) {
WorkflowRun r = script.$build()
ModelASTPipelineDef model = Converter.parseFromCpsScript(script)

ModelASTStages stages = model.stages

stages.removeSourceLocation()

r.addAction(new ExecutionModelAction(stages))
}


}
Expand Up @@ -67,4 +67,10 @@ public final class ModelASTAgent extends ModelASTElement {
}
return "agent ${argStr}\n"
}

@Override
public void removeSourceLocation() {
super.removeSourceLocation()
args.removeSourceLocation()
}
}
Expand Up @@ -49,4 +49,10 @@ public final class ModelASTBranch extends ModelASTElement {
public String toGroovy() {
return steps.collect { it.toGroovy() }.join("\n") + "\n"
}

@Override
public void removeSourceLocation() {
super.removeSourceLocation()
steps.each { it.removeSourceLocation() }
}
}
Expand Up @@ -67,4 +67,10 @@ public final class ModelASTBuildCondition extends ModelASTElement {
public String toGroovy() {
return "${condition} {\n${branch.toGroovy()}\n}\n"
}

@Override
public void removeSourceLocation() {
super.removeSourceLocation()
branch.removeSourceLocation()
}
}
Expand Up @@ -74,4 +74,11 @@ public abstract class ModelASTElement {
// No-op
}

/**
* Removes the source location value from this element.
*/
public void removeSourceLocation() {
sourceLocation = null
}

}
Expand Up @@ -76,4 +76,14 @@ public final class ModelASTEnvironment extends ModelASTElement {
}.join("\n")
return "environment {\n${keysAndValues}\n}\n"
}

@Override
public void removeSourceLocation() {
super.removeSourceLocation()

variables.each { k, v ->
k.removeSourceLocation()
v.removeSourceLocation()
}
}
}
Expand Up @@ -66,4 +66,14 @@ public final class ModelASTNamedArgumentList extends ModelASTArgumentList {
"${k.toGroovy()}: ${v.toGroovy()}"
}.join(", ")
}

@Override
public void removeSourceLocation() {
super.removeSourceLocation()

arguments.each { k, v ->
k.removeSourceLocation()
v.removeSourceLocation()
}
}
}
Expand Up @@ -72,4 +72,13 @@ public final class ModelASTNotifications extends ModelASTElement {
public String toGroovy() {
return "notifications {\n${conditions.collect { it.toGroovy() }.join("\n")}\n}\n"
}

@Override
public void removeSourceLocation() {
super.removeSourceLocation()

conditions.each { c ->
c.removeSourceLocation()
}
}
}
Expand Up @@ -108,6 +108,16 @@ public final class ModelASTPipelineDef extends ModelASTElement {
return prettyLines.join("\n")
}

@Override
public void removeSourceLocation() {
super.removeSourceLocation()
stages.removeSourceLocation()
notifications.removeSourceLocation()
postBuild.removeSourceLocation()
environment.removeSourceLocation()
tools.removeSourceLocation()
}

private String indent(int count) {
return " " * count
}
Expand Down
Expand Up @@ -71,4 +71,13 @@ public final class ModelASTPositionalArgumentList extends ModelASTArgumentList {
v.toGroovy()
}.join(", ")
}

@Override
public void removeSourceLocation() {
super.removeSourceLocation()

arguments.each { v ->
v.removeSourceLocation()
}
}
}
Expand Up @@ -74,4 +74,12 @@ public final class ModelASTPostBuild extends ModelASTElement {
public String toGroovy() {
return "postBuild {\n${conditions.collect { it.toGroovy() }.join("\n")}\n}\n"
}

@Override
public void removeSourceLocation() {
super.removeSourceLocation()
conditions.each { c ->
c.removeSourceLocation()
}
}
}
Expand Up @@ -35,4 +35,10 @@ public final class ModelASTSingleArgument extends ModelASTArgumentList {
return value.toGroovy()
}

@Override
public void removeSourceLocation() {
super.removeSourceLocation()
value.removeSourceLocation()
}

}
Expand Up @@ -65,4 +65,12 @@ public final class ModelASTStage extends ModelASTElement {

return retString.toString()
}

@Override
public void removeSourceLocation() {
super.removeSourceLocation()
branches.each { b ->
b.removeSourceLocation()
}
}
}
Expand Up @@ -71,4 +71,12 @@ public final class ModelASTStages extends ModelASTElement {
}.join("\n")
return "stages {\n${stagesString}\n}\n"
}

@Override
public void removeSourceLocation() {
super.removeSourceLocation()
stages.each { s ->
s.removeSourceLocation()
}
}
}
Expand Up @@ -48,4 +48,10 @@ public class ModelASTStep extends ModelASTElement {
public String toGroovy() {
return "${name}(${args.toGroovy()})"
}

@Override
public void removeSourceLocation() {
super.removeSourceLocation()
args.removeSourceLocation()
}
}
Expand Up @@ -75,4 +75,13 @@ public final class ModelASTTools extends ModelASTElement {
}.join("\n")
return "tools {\n${keysAndValues}\n}\n"
}

@Override
public void removeSourceLocation() {
super.removeSourceLocation()
tools.each { k, v ->
k.removeSourceLocation()
v.removeSourceLocation()
}
}
}
Expand Up @@ -50,4 +50,12 @@ public class ModelASTTreeStep extends ModelASTStep {
return retString
}

@Override
public void removeSourceLocation() {
super.removeSourceLocation()
children.each { c ->
c.removeSourceLocation()
}
}

}
Expand Up @@ -31,11 +31,14 @@ import com.github.fge.jsonschema.main.JsonSchema
import com.github.fge.jsonschema.main.JsonSchemaFactory
import com.github.fge.jsonschema.report.ProcessingReport
import net.sf.json.JSONObject
import org.apache.commons.lang.reflect.FieldUtils
import org.codehaus.groovy.control.CompilationFailedException
import org.codehaus.groovy.control.CompilationUnit
import org.codehaus.groovy.control.CompilerConfiguration
import org.codehaus.groovy.control.SourceUnit
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTPipelineDef
import org.jenkinsci.plugins.workflow.cps.CpsFlowExecution
import org.jenkinsci.plugins.workflow.cps.CpsScript

import java.security.CodeSource
import java.security.cert.Certificate
Expand Down Expand Up @@ -145,4 +148,19 @@ public class Converter {

return model[0];
}

/**
* Converts the raw script from a {@link CpsScript} into {@link ModelASTPipelineDef}
*
* @param cpsScript The {@link CpsScript} to pull from.
* @return A parsed and validated {@link ModelASTPipelineDef}
*/
public static ModelASTPipelineDef parseFromCpsScript(CpsScript cpsScript) {
CpsFlowExecution execution = (CpsFlowExecution) FieldUtils.readField(cpsScript, "execution", true)

String rawScript = execution.script

return scriptToPipelineDef(rawScript)
}

}
@@ -0,0 +1,40 @@
/*
* The MIT License
*
* Copyright (c) 2016, CloudBees, Inc.
*
* 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.pipeline.modeldefinition.actions;

import hudson.model.InvisibleAction;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTStages;

public class ExecutionModelAction extends InvisibleAction {
private final ModelASTStages stages;

public ExecutionModelAction(ModelASTStages s) {
this.stages = s;
}

public ModelASTStages getStages() {
return stages;
}
}

0 comments on commit b385ba5

Please sign in to comment.