Skip to content

Commit

Permalink
[JENKINS-42473] Use a better way of getting the flow execution
Browse files Browse the repository at this point in the history
Not sure yet if this will actually solve the problem, but it seems
superior regardless. I lifted this from ReplayAction.
  • Loading branch information
abayer committed Mar 4, 2017
1 parent 0c7d4fd commit 944d34f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
Expand Up @@ -48,6 +48,8 @@ import org.jenkinsci.plugins.workflow.actions.TagsAction
import org.jenkinsci.plugins.workflow.cps.CpsFlowExecution
import org.jenkinsci.plugins.workflow.cps.CpsScript
import org.jenkinsci.plugins.workflow.cps.CpsThread
import org.jenkinsci.plugins.workflow.flow.FlowExecution
import org.jenkinsci.plugins.workflow.flow.FlowExecutionOwner
import org.jenkinsci.plugins.workflow.graphanalysis.LinearBlockHoppingScanner
import org.jenkinsci.plugins.workflow.cps.nodes.StepEndNode
import org.jenkinsci.plugins.workflow.cps.nodes.StepStartNode
Expand Down Expand Up @@ -193,7 +195,7 @@ public class Utils {

static void attachDeclarativeActions(CpsScript script) {
WorkflowRun r = script.$build()
ModelASTPipelineDef model = Converter.parseFromCpsScript(script)
ModelASTPipelineDef model = Converter.parseFromWorkflowRun(r)

ModelASTStages stages = model.stages

Expand Down Expand Up @@ -402,4 +404,13 @@ public class Utils {
}
return true
}

static CpsFlowExecution getExecutionForRun(WorkflowRun run) {
FlowExecutionOwner owner = ((FlowExecutionOwner.Executable) run).asFlowExecutionOwner()
if (owner == null) {
return null
}
FlowExecution exec = owner.getOrNull()
return exec instanceof CpsFlowExecution ? (CpsFlowExecution) exec : null
}
}
Expand Up @@ -26,16 +26,14 @@ package org.jenkinsci.plugins.pipeline.modeldefinition.parser
import com.cloudbees.groovy.cps.NonCPS
import com.github.fge.jsonschema.util.JsonLoader
import org.jenkinsci.plugins.pipeline.modeldefinition.shaded.com.fasterxml.jackson.databind.JsonNode
import org.jenkinsci.plugins.pipeline.modeldefinition.shaded.com.fasterxml.jackson.databind.ObjectMapper
import org.jenkinsci.plugins.pipeline.modeldefinition.shaded.com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule
import org.jenkinsci.plugins.pipeline.modeldefinition.Utils
import com.github.fge.jsonschema.exceptions.ProcessingException
import com.github.fge.jsonschema.main.JsonSchema
import com.github.fge.jsonschema.report.ProcessingReport
import com.github.fge.jsonschema.tree.JsonTree
import com.github.fge.jsonschema.tree.SimpleJsonTree
import jenkins.model.Jenkins
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
Expand All @@ -45,9 +43,9 @@ import org.jenkinsci.plugins.pipeline.modeldefinition.ASTSchema
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTPipelineDef
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTStep
import org.jenkinsci.plugins.workflow.cps.CpsFlowExecution
import org.jenkinsci.plugins.workflow.cps.CpsScript
import org.jenkinsci.plugins.workflow.cps.CpsThread
import org.jenkinsci.plugins.workflow.cps.GroovyShellDecorator
import org.jenkinsci.plugins.workflow.job.WorkflowRun

import java.security.CodeSource
import java.security.cert.Certificate
Expand Down Expand Up @@ -194,13 +192,13 @@ public class Converter {
}

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

String rawScript = execution.script

Expand Down

0 comments on commit 944d34f

Please sign in to comment.