Skip to content
Permalink
Browse files
[FIXED JENKINS-29776] Add Job DSL support for Promoted Builds Plugin
  • Loading branch information
denschu committed Mar 14, 2016
1 parent be96e51 commit 327d0233255d0a2c2806c3337daac97e8580905c
Showing with 1,101 additions and 33 deletions.
  1. +52 −3 README.md
  2. +18 −5 pom.xml
  3. +33 −18 src/main/java/hudson/plugins/promoted_builds/JobPropertyImpl.java
  4. +15 −7 src/main/java/hudson/plugins/promoted_builds/conditions/ManualCondition.java
  5. +171 −0 src/main/java/hudson/plugins/promoted_builds/integrations/jobdsl/ConditionsContext.java
  6. +28 −0 src/main/java/hudson/plugins/promoted_builds/integrations/jobdsl/JobDslManualCondition.java
  7. +79 −0 src/main/java/hudson/plugins/promoted_builds/integrations/jobdsl/JobDslPromotionProcess.java
  8. +126 −0 ...main/java/hudson/plugins/promoted_builds/integrations/jobdsl/JobDslPromotionProcessConverter.java
  9. +71 −0 src/main/java/hudson/plugins/promoted_builds/integrations/jobdsl/ManualConditionConverter.java
  10. +108 −0 src/main/java/hudson/plugins/promoted_builds/integrations/jobdsl/PromotionContext.java
  11. +59 −0 src/main/java/hudson/plugins/promoted_builds/integrations/jobdsl/PromotionsContext.java
  12. +138 −0 src/main/java/hudson/plugins/promoted_builds/integrations/jobdsl/PromotionsExtensionPoint.java
  13. +15 −0 src/main/java/hudson/plugins/promoted_builds/integrations/jobdsl/ReleasePromotionCondition.java
  14. +30 −0 ...t/java/hudson/plugins/promoted_builds/integrations/jobdsl/JobDslManualConditionConverterTest.java
  15. +54 −0 .../java/hudson/plugins/promoted_builds/integrations/jobdsl/JobDslPromotionProcessConverterTest.java
  16. +44 −0 ...st/java/hudson/plugins/promoted_builds/integrations/jobdsl/PromotionsDslContextExtensionTest.java
  17. +45 −0 src/test/resources/complex-example-dsl.groovy
  18. +15 −0 src/test/resources/example-dsl.groovy
@@ -122,9 +122,58 @@ These were retrieved from github [here][2].
* `PROMOTED_ID` - ID of the build being promoted
* ex: 2012-04-12_17-13-03
* `PROMOTED_USER_NAME` - the user who triggered the promotion
* `PROMOTED_JOB_FULL_NAME` - the full name of the promoted
job

* `PROMOTED_JOB_FULL_NAME` - the full name of the promoted job

## Job DSL support

```groovy
freeStyleJob(String jobname) {
properties{
promotions {
promotion {
name(String promotionName)
icon(String iconName)
conditions {
selfPromotion(boolean evenIfUnstable = true)
parameterizedSelfPromotion(boolean evenIfUnstable = true, String parameterName, String parameterValue)
releaseBuild()
downstream(boolean evenIfUnstable = true, String jobs)
upstream(String promotionNames)
manual(String user){
parameters{
textParam(String parameterName, String defaultValue, String description)
}
}
actions {
shell(String command)
}
}
}
}
}
```

See [StepContext](https://jenkinsci.github.io/job-dsl-plugin/#path/job-steps) in the API Viewer for full documentation about the possible actions.

### Example

```groovy
freeStyleJob('test-job') {
properties{
promotions {
promotion {
name('Development')
conditions {
manual('testuser')
}
actions {
shell('echo hello;')
}
}
}
}
}
```

## Contributing

23 pom.xml
@@ -4,7 +4,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.554.2</version>
<version>1.565</version>
</parent>

<artifactId>promoted-builds</artifactId>
@@ -13,8 +13,8 @@

<name>Jenkins promoted builds plugin</name>
<url>http://wiki.jenkins-ci.org/display/JENKINS/Promoted+Builds+Plugin</url>
<properties>

<properties>
<findbugs-maven-plugin.version>3.0.1</findbugs-maven-plugin.version>
<findbugs.failOnError>true</findbugs.failOnError>
</properties>
@@ -115,6 +115,19 @@
<version>1.5.3</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>job-dsl</artifactId>
<version>1.42</version>
<type>jar</type>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jvnet.hudson</groupId>
<artifactId>xstream</artifactId>
<version>1.4.7-jenkins-1</version>
<optional>true</optional>
</dependency>
</dependencies>

<build>
@@ -127,7 +140,7 @@
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>${findbugs-maven-plugin.version}</version>
<configuration>
<configuration>
<failOnError>${findbugs.failOnError}</failOnError>
<xmlOutput>true</xmlOutput>
</configuration>
@@ -151,4 +164,4 @@
</plugin>
</plugins>
</build>
</project>
</project>
@@ -1,5 +1,23 @@
package hudson.plugins.promoted_builds;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.apache.commons.lang.StringUtils;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.Ancestor;
import org.kohsuke.stapler.StaplerRequest;

import hudson.Extension;
import hudson.Util;
import hudson.model.AbstractBuild;
@@ -19,26 +37,10 @@
import hudson.model.listeners.ItemListener;
import hudson.remoting.Callable;
import hudson.util.IOUtils;
import jenkins.model.Jenkins;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.Ancestor;
import org.kohsuke.stapler.StaplerRequest;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.model.Jenkins;

/**
* Promotion processes defined for a project.
*
@@ -71,11 +73,23 @@ public JobPropertyImpl(AbstractProject<?,?> owner) throws Descriptor.FormExcepti
this.owner = owner;
init();
}
/**
* Programmatic construction.
*/
public JobPropertyImpl(JobPropertyImpl other, AbstractProject<?,?> owner) throws Descriptor.FormException, IOException {
this.owner = owner;
this.activeProcessNames.addAll(other.activeProcessNames);
loadAllProcesses(other.getRootDir());
}

/**
* Programmatic construction.
*/
@Restricted(NoExternalUse.class)
public JobPropertyImpl(Set<String> activeProcessNames) {
this.activeProcessNames.addAll(activeProcessNames);
}

private JobPropertyImpl(StaplerRequest req, JSONObject json) throws Descriptor.FormException, IOException {
// a hack to get the owning AbstractProject.
// this is needed here so that we can load items
@@ -109,6 +123,7 @@ private JobPropertyImpl(StaplerRequest req, JSONObject json) throws Descriptor.F
}
init();
}

private void loadAllProcesses(File rootDir) throws IOException {
File[] subdirs = rootDir.listFiles(new FileFilter() {
public boolean accept(File child) {
@@ -395,4 +410,4 @@ public JobPropertyImpl newInstance(StaplerRequest req, JSONObject json) throws D
}

private static final Logger LOGGER = Logger.getLogger(JobPropertyImpl.class.getName());
}
}
@@ -55,10 +55,18 @@ public class ManualCondition extends PromotionCondition {
public ManualCondition() {
}

/*
* Restrict the Condition to specific user(s)
* @since 2.24
*/
public String getUsers() {
return users;
}

public void setUsers(String users) {
this.users = users;
}

public List<ParameterDefinition> getParameterDefinitions() {
return parameterDefinitions;
}
@@ -151,10 +159,10 @@ public boolean isInGroupList() {
return false;
}
public Future<Promotion> approve(AbstractBuild<?,?> build, PromotionProcess promotionProcess, List<ParameterValue> paramValues) throws IOException{
if (canApprove(promotionProcess, build)) {
if (canApprove(promotionProcess, build)) {
// add approval to build
ManualApproval approval=new ManualApproval(promotionProcess.getName(), paramValues);
build.addAction(approval);
ManualApproval approval=new ManualApproval(promotionProcess.getName(), paramValues);
build.addAction(approval);
build.save();

// check for promotion
@@ -166,9 +174,9 @@ public List<ParameterValue> createDefaultValues(){
List<ParameterValue> paramValues = new ArrayList<ParameterValue>();

if (parameterDefinitions != null && !parameterDefinitions.isEmpty()) {
for (ParameterDefinition d:parameterDefinitions){
paramValues.add(d.getDefaultParameterValue());
}
for (ParameterDefinition d:parameterDefinitions){
paramValues.add(d.getDefaultParameterValue());
}
}
return paramValues;
}
@@ -184,7 +192,7 @@ public void doApprove(StaplerRequest req, StaplerResponse rsp,
@AncestorInPath PromotionProcess promotionProcess,
@AncestorInPath AbstractBuild<?,?> build) throws IOException, ServletException {

JSONObject formData = req.getSubmittedForm();
JSONObject formData = req.getSubmittedForm();

if (canApprove(promotionProcess, build)) {
List<ParameterValue> paramValues = new ArrayList<ParameterValue>();

0 comments on commit 327d023

Please sign in to comment.