Skip to content

Commit

Permalink
[JENKINS-21238] Ability to run the generated job immediately after ge…
Browse files Browse the repository at this point in the history
…neration
  • Loading branch information
syl20bnr committed Feb 16, 2014
1 parent dcb03d9 commit 396f195
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 7 deletions.
17 changes: 12 additions & 5 deletions src/main/java/org/jenkinsci/plugins/jobgenerator/GeneratorRun.java
Expand Up @@ -291,7 +291,7 @@ public GeneratorImpl() {

protected Result doRun(BuildListener listener) throws Exception {
// TODO syl20bnr: This function is a big mess. I plan to
// refactoring it when it comes to add some tests.
// refactor it for testing purpose.
if(!this.checkParameters(listener)){
return Result.FAILURE;
}
Expand Down Expand Up @@ -348,6 +348,7 @@ protected Result doRun(BuildListener listener) throws Exception {
"hudson.model.ParametersDefinitionProperty");
this.removeNodeIfNoChild(doc, "generatedJobName");
this.removeNodeIfNoChild(doc, "generatedDisplayJobName");
this.removeNodeIfNoChild(doc, "autoRunJob");
// Evaluate builders (Single step)
List vroots = doc.selectNodes("//org.jenkinsci.plugins." +
"conditionalbuildstep.singlestep.SingleConditionalBuilder");
Expand Down Expand Up @@ -415,23 +416,29 @@ protected Result doRun(BuildListener listener) throws Exception {
InputStream is = new ByteArrayInputStream(
doc.asXML().getBytes("UTF-8"));
// System.out.println(doc.asXML());
AbstractItem item =
(AbstractItem) Jenkins.getInstance().getItem(expName);
AbstractProject item =
(AbstractProject) Jenkins.getInstance().getItem(expName);
if(item != null){
StreamSource ss = new StreamSource(is);
item.updateByXml(ss);
LOGGER.info(String.format("Updated configuration of " +
"job %s", expName));
}
else{
Jenkins.getInstance().createProjectFromXML(expName, is);
item = (AbstractProject)
Jenkins.getInstance().createProjectFromXML(
expName, is);
LOGGER.info(String.format("Created job %s", expName));
}
// save generated job name
GeneratedJobBuildAction action =
new GeneratedJobBuildAction(expName, item!=null);
getBuild().addAction(action);
getBuild().addAction(action);
// auto run the job
if(job.getAutoRunJob()){
Cause.UserIdCause cause = new Cause.UserIdCause();
item.scheduleBuild(5, cause);
}
}
return Result.SUCCESS;
}
Expand Down
Expand Up @@ -73,6 +73,7 @@ public class JobGenerator extends Project<JobGenerator, GeneratorRun>
private transient String customWorkspace = null;
private String generatedJobName = "";
private String generatedDisplayJobName = "";
private boolean autoRunJob = false;

@DataBoundConstructor
public JobGenerator(ItemGroup parent, String name) {
Expand Down Expand Up @@ -185,13 +186,15 @@ protected void submit(StaplerRequest req, StaplerResponse rsp)
String k = "plugin-jobgenerator-GeneratedJobConfig";
JSONObject o;

if(json.has(k)){
if(json.has(k)){
o = json.getJSONObject(k);
if(o != null) {
k = "generatedJobName";
if(o.has(k)){ this.generatedJobName = o.getString(k); }
k = "generatedDisplayJobName";
if(o.has(k)){ this.generatedDisplayJobName = o.getString(k); }
k = "autoRunJob";
if(o.has(k)){ this.autoRunJob = o.getBoolean(k);}
}
}

Expand Down Expand Up @@ -257,6 +260,13 @@ public void setGeneratedDisplayJobName(String name){
this.generatedDisplayJobName = name;
}

public boolean getAutoRunJob(){
return this.autoRunJob;
}
public void setAutoRunJob(boolean value){
this.autoRunJob = value;
}

public String getCustomWorkspace() {
return this.customWorkspace;
}
Expand Down
Expand Up @@ -28,12 +28,16 @@ THE SOFTWARE.
xmlns:l="/lib/layout" xmlns:t="/lib/hudson"
xmlns:f="/lib/form" xmlns:i="jelly:fmt">
<st:include page="${it.descriptor.defaultEntriesPage}" />
<f:section name="plugin-jobgenerator-GeneratedJobConfig" title="${%Generated Job Additional Configuration}">
<f:section name="plugin-jobgenerator-GeneratedJobConfig"
title="${%Generated Job Additional Configuration}">
<f:entry title="${%Generated Job Name}" field="generatedJobName">
<f:textbox />
</f:entry>
<f:entry title="${%Generated Job Display Name}" field="generatedDisplayJobName">
<f:textbox />
</f:entry>
<f:entry title="${%Automatically run the generated job}" field="autoRunJob">
<f:checkbox />
</f:entry>
</f:section>
</j:jelly>
@@ -0,0 +1,28 @@
<!--
The MIT License
Copyright (c) 2012-2013, Sylvain Benner.
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.
-->

<div>
If checked, the generated job will be automatically run when the generation
is complete.
</div>

0 comments on commit 396f195

Please sign in to comment.