Skip to content

Commit

Permalink
Merge pull request #24 from jglick/workflow-JENKINS-26918
Browse files Browse the repository at this point in the history
[JENKINS-26918] Workflow support
  • Loading branch information
ikedam committed Dec 24, 2015
2 parents a120472 + 0fa6ec9 commit fd45589
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 11 deletions.
23 changes: 21 additions & 2 deletions pom.xml
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.509.4</version>
<version>1.609.1</version>
</parent>

<name>Groovy Postbuild</name>
Expand Down Expand Up @@ -64,11 +64,30 @@
<url>http://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>
<properties>
<workflow.version>1.11</workflow.version>
</properties>
<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>script-security</artifactId>
<version>1.5</version>
<version>1.15</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>matrix-project</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-cps</artifactId>
<version>${workflow.version}</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>${workflow.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Expand Up @@ -67,13 +67,13 @@ public class GroovyPostbuildRecorder extends Recorder implements MatrixAggregata
private final boolean runForMatrixParent;

public static class BadgeManager {
private AbstractBuild<?, ?> build;
private final BuildListener listener;
private Run<?, ?> build;
private final TaskListener listener;
private final Result scriptFailureResult;
private final Set<AbstractBuild<?, ?>> builds = new HashSet<AbstractBuild<?,?>>();
private final Set<Run<?, ?>> builds = new HashSet<Run<?, ?>>();
private EnvVars envVars;

public BadgeManager(AbstractBuild<?, ?> build, BuildListener listener, Result scriptFailureResult) {
public BadgeManager(Run<?, ?> build, TaskListener listener, Result scriptFailureResult) {
setBuild(build);
try {
this.envVars = build.getEnvironment(listener);
Expand Down Expand Up @@ -107,22 +107,22 @@ public Hudson getHudson() {
return Hudson.getInstance();
}
// TBD: @Whitelisted
public AbstractBuild<?, ?> getBuild() {
public Run<?, ?> getBuild() {
return build;
}
public void setBuild(AbstractBuild<?, ?> build) {
public void setBuild(Run<?, ?> build) {
if(build != null) {
this.build = build;
builds.add(build);
}
}
public boolean setBuildNumber(int buildNumber) {
AbstractBuild<?, ?> newBuild = build.getProject().getBuildByNumber(buildNumber);
Run<?, ?> newBuild = build.getParent().getBuildByNumber(buildNumber);
setBuild(newBuild);
return (newBuild != null);
}
// TBD: @Whitelisted
public BuildListener getListener() {
public TaskListener getListener() {
return listener;
}

Expand Down Expand Up @@ -366,7 +366,7 @@ public final boolean perform(final AbstractBuild<?, ?> build, final Launcher lau
badgeManager.buildScriptFailed(e);
scriptResult = false;
}
for(AbstractBuild<?, ?> b : badgeManager.builds) {
for (Run<?, ?> b : badgeManager.builds) {
b.save();
}

Expand Down
@@ -0,0 +1,59 @@
/*
* The MIT License
*
* Copyright 2015 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.jvnet.hudson.plugins.groovypostbuild;

import hudson.Extension;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.util.LogTaskListener;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jenkinsci.plugins.workflow.cps.CpsScript;
import org.jenkinsci.plugins.workflow.cps.GlobalVariable;

/**
* Exposes {@link org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildRecorder.BadgeManager} to Workflow scripts as {@code manager}.
*/
@Extension
public class WorkflowManager extends GlobalVariable {

@Override
public String getName() {
return "manager";
}

@Override
public Object getValue(CpsScript script) throws Exception {
Run<?,?> build = script.$build();
if (build == null) {
throw new IllegalStateException("cannot find associated build");
}
// TODO currently no way to get access to WorkflowRun.listener
TaskListener listener = new LogTaskListener(Logger.getLogger(WorkflowManager.class.getName()), Level.WARNING);
return new GroovyPostbuildRecorder.BadgeManager(build, listener, Result.FAILURE);
}

}
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License
Copyright 2015 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.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler">
<st:include page="/org/jvnet/hudson/plugins/groovypostbuild/GroovyPostbuildRecorder/help.jelly"/>
</j:jelly>
@@ -0,0 +1,49 @@
/*
* The MIT License
*
* Copyright 2015 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.jvnet.hudson.plugins.groovypostbuild;

import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.jvnet.hudson.test.Issue;
import org.jvnet.hudson.test.JenkinsRule;

public class WorkflowTest {

@Rule
public JenkinsRule r = new JenkinsRule();

@Issue("JENKINS-26918")
@Test
public void usingManager() throws Exception {
WorkflowJob p = r.jenkins.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition("manager.addWarningBadge 'stuff is broken'", true));
WorkflowRun b = r.assertBuildStatusSuccess(p.scheduleBuild2(0));
assertEquals("stuff is broken", b.getAction(GroovyPostbuildAction.class).getText());
}

}

0 comments on commit fd45589

Please sign in to comment.