Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[FIXED JENKINS-40642] Add additional imports for parse/translate
@NonCPS and @Library were the most glaring missing import, but I
replicated the standard CpsGroovyShell imports - @NonCPS's package,
hudson.model, jenkins.model, and anything provided by a GroovyShellDecorator.
  • Loading branch information
abayer committed Dec 22, 2016
1 parent a1963cd commit 29da053
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 18 deletions.
33 changes: 18 additions & 15 deletions pipeline-model-definition/pom.xml
Expand Up @@ -74,27 +74,27 @@
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>2.21</version>
<version>2.23</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-api</artifactId>
<version>2.7</version>
<version>2.8</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>2.8</version>
<version>2.9</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-multibranch</artifactId>
<version>2.9</version>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-scm-step</artifactId>
<version>2.2</version>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
Expand Down Expand Up @@ -129,7 +129,17 @@
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-support</artifactId>
<version>2.10</version>
<version>2.11</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps-global-lib</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>scm-api</artifactId>
<version>1.3</version>
</dependency>

<!-- JSON schema stuff -->
Expand Down Expand Up @@ -165,13 +175,6 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps-global-lib</artifactId>
<version>2.3</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>pipeline-graph-analysis</artifactId>
Expand All @@ -196,14 +199,14 @@
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-scm-step</artifactId>
<version>2.2</version>
<version>2.3</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-support</artifactId>
<version>2.10</version>
<version>2.11</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
Expand Down
Expand Up @@ -23,6 +23,7 @@
*/
package org.jenkinsci.plugins.pipeline.modeldefinition.parser

import com.cloudbees.groovy.cps.NonCPS
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule
Expand All @@ -35,11 +36,14 @@ 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.codehaus.groovy.control.customizers.ImportCustomizer
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.GroovyShellDecorator
import org.jenkinsci.plugins.workflow.libs.Library

import java.security.CodeSource
import java.security.cert.Certificate
Expand Down Expand Up @@ -92,7 +96,7 @@ public class Converter {
*/
public static ModelASTPipelineDef urlToPipelineDef(URL src) {
CompilationUnit cu = new CompilationUnit(
CompilerConfiguration.DEFAULT,
makeCompilerConfiguration(),
new CodeSource(src, new Certificate[0]),
new GroovyClassLoader());
cu.addSource(src);
Expand All @@ -108,14 +112,28 @@ public class Converter {
*/
public static ModelASTPipelineDef scriptToPipelineDef(String script) {
CompilationUnit cu = new CompilationUnit(
CompilerConfiguration.DEFAULT,
makeCompilerConfiguration(),
new CodeSource(new URL("file", "", DEFAULT_CODE_BASE), (Certificate[]) null),
new GroovyClassLoader())
cu.addSource(PIPELINE_SCRIPT_NAME, script)

return compilationUnitToPipelineDef(cu)
}

private static CompilerConfiguration makeCompilerConfiguration() {
CompilerConfiguration cc = new CompilerConfiguration();

ImportCustomizer ic = new ImportCustomizer();
ic.addStarImports(NonCPS.class.getPackage().getName());
ic.addStarImports("hudson.model","jenkins.model");
for (GroovyShellDecorator d : GroovyShellDecorator.all()) {
d.customizeImports(null, ic);
}

cc.addCompilationCustomizers(ic);

return cc;
}
/**
* Takes a {@link CompilationUnit}, copmiles it with the {@link ModelParser} injected, and returns the resulting
* {@link ModelASTPipelineDef}
Expand All @@ -140,7 +158,7 @@ public class Converter {

public static List<ModelASTStep> scriptToPlainSteps(String script) {
CompilationUnit cu = new CompilationUnit(
CompilerConfiguration.DEFAULT,
makeCompilerConfiguration(),
new CodeSource(new URL("file", "", DEFAULT_CODE_BASE), (Certificate[]) null),
new GroovyClassLoader())
cu.addSource(PIPELINE_SCRIPT_NAME, script)
Expand Down
@@ -1,5 +1,6 @@
package org.jenkinsci.plugins.pipeline.modeldefinition.parser;

import com.cloudbees.groovy.cps.NonCPS;
import hudson.Extension;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.classgen.GeneratorContext;
Expand All @@ -8,6 +9,7 @@
import org.codehaus.groovy.control.CompilerConfiguration;
import org.codehaus.groovy.control.SourceUnit;
import org.codehaus.groovy.control.customizers.CompilationCustomizer;
import org.codehaus.groovy.control.customizers.ImportCustomizer;
import org.jenkinsci.plugins.pipeline.modeldefinition.parser.ModelParser;
import org.jenkinsci.plugins.workflow.cps.CpsFlowExecution;
import org.jenkinsci.plugins.workflow.cps.GroovyShellDecorator;
Expand All @@ -23,6 +25,12 @@
public class GroovyShellDecoratorImpl extends GroovyShellDecorator {
@Override
public void configureCompiler(@CheckForNull CpsFlowExecution context, CompilerConfiguration cc) {
ImportCustomizer ic = new ImportCustomizer();
ic.addStarImports(NonCPS.class.getPackage().getName());
ic.addStarImports("hudson.model","jenkins.model");
this.customizeImports(context, ic);
cc.addCompilationCustomizers(ic);

cc.addCompilationCustomizers(new CompilationCustomizer(CompilePhase.CANONICALIZATION) {
@Override
public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) throws CompilationFailedException {
Expand Down
Expand Up @@ -26,6 +26,7 @@
import com.google.common.base.Predicate;
import hudson.model.Result;
import hudson.model.Slave;
import jenkins.plugins.git.GitSCMSource;
import org.jenkinsci.plugins.pipeline.modeldefinition.actions.ExecutionModelAction;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTBranch;
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTScriptBlock;
Expand All @@ -40,13 +41,17 @@
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.graphanalysis.DepthFirstScanner;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.libs.GlobalLibraries;
import org.jenkinsci.plugins.workflow.libs.LibraryConfiguration;
import org.jenkinsci.plugins.workflow.libs.SCMSourceRetriever;
import org.jenkinsci.plugins.workflow.pipelinegraphanalysis.GenericStatus;
import org.jenkinsci.plugins.workflow.pipelinegraphanalysis.StatusAndTiming;
import org.junit.BeforeClass;
import org.junit.Test;
import org.jvnet.hudson.test.Issue;

import java.util.Collection;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -372,4 +377,21 @@ public boolean apply(FlowNode input) {
}
};
}

@Issue("JENKINS-40642")
@Test
public void libraryAnnotation() throws Exception {
otherRepo.init();
otherRepo.write("vars/myecho.groovy", "def call() {echo 'something special'}");
otherRepo.write("vars/myecho.txt", "Says something very special!");
otherRepo.git("add", "vars");
otherRepo.git("commit", "--message=init");
GlobalLibraries.get().setLibraries(Collections.singletonList(
new LibraryConfiguration("echo-utils",
new SCMSourceRetriever(new GitSCMSource(null, otherRepo.toString(), "", "*", "", true)))));

expect("libraryAnnotation")
.logContains("something special")
.go();
}
}
@@ -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.
*/

@Library('echo-utils@master')
import myecho

pipeline {
agent none
stages {
stage("foo") {
steps {
myecho()
}
}
}
}



0 comments on commit 29da053

Please sign in to comment.