Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #4 from ikedam/feature/JENKINS-28439_DisableJob
[JENKINS-28439] New operation: disable job
  • Loading branch information
ikedam committed May 17, 2015
2 parents 7ed6e5b + ba32648 commit b9e7262
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 0 deletions.
@@ -0,0 +1,116 @@
/*
* The MIT License
*
* Copyright (c) 2012-2013 IKEDA Yasuyuki
*
* 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 jp.ikedam.jenkins.plugins.jobcopy_builder;

import java.io.PrintStream;
import java.io.Serializable;

import org.w3c.dom.Document;
import org.w3c.dom.Node;

import hudson.EnvVars;
import hudson.Extension;
import hudson.model.Descriptor;

import org.kohsuke.stapler.DataBoundConstructor;

/**
* Disables the job if the job copied from is enabled.
*/
public class DisableOperation extends AbstractXmlJobcopyOperation implements Serializable
{
private static final long serialVersionUID = 1L;

/**
* The internal class to work with views.
*
* The following files are used (put in main/resource directory in the source tree).
* <dl>
* <dt>config.jelly</dt>
* <dd>shown in the job configuration page, as an additional view to a Jobcopy build step.</dd>
* </dl>
*/
@Extension
public static class DescriptorImpl extends Descriptor<JobcopyOperation>
{
/**
* Returns the string to be shown in a job configuration page,
* in the dropdown of &quot;Add Copy Operation&quot;.
*
* @return the display name
* @see hudson.model.Descriptor#getDisplayName()
*/
@Override
public String getDisplayName()
{
return Messages.DisableOperation_DisplayName();
}
}

/**
* Constructor to initialize with the input parameters.
*
* For there's no input parameters. Nothing to do.
*/
@DataBoundConstructor
public DisableOperation()
{
}

/**
* Returns modified XML Document of the job configuration.
*
* Disables the job: updates the value in /PROJECT/disabled to true.
*
* @param doc XML Document of the job to be copied (job/NAME/config.xml)
* @param env Variables defined in the build.
* @param logger The output stream to log.
* @return modified XML Document. Return null if an error occurs.
* @see jp.ikedam.jenkins.plugins.jobcopy_builder.AbstractXmlJobcopyOperation#perform(org.w3c.dom.Document, hudson.EnvVars, java.io.PrintStream)
*/
@Override
public Document perform(Document doc, EnvVars env, PrintStream logger)
{
logger.print("Disabling Job...");
try
{
// Retrieve the node holding the enable/disable configuration.
Node disabledNode = getNode(doc, "/*/disabled");
if(disabledNode == null){
logger.println("Failed to fetch disabled node.");
return null;
}

logger.println(String.format("%s: %s -> true", getXpath(disabledNode), disabledNode.getTextContent()));
disabledNode.setTextContent("true");

return doc;
}catch(Exception e){
logger.print("Error occured in XML operation");
e.printStackTrace(logger);
return null;
}
}
}

Expand Up @@ -24,6 +24,7 @@ JobCopyBuilder.DisplayName=Copy Job
AdditionalFileset.DisplayName=Additional Copy Files
ReplaceOperation.DisplayName=Replace String
EnableOperation.DisplayName=Enable Job
DisableOperation.DisplayName=Disable Job
CopiedjobinfoAction.DisplayName=Copied Job
JobCopyBuilder.JobName.empty=Cannot be blank.
JobCopyBuilder.JobName.exists=Specified job already exists. Check "Overwrite", or a build will fail if the job exists at the execution time.
Expand Down
Expand Up @@ -28,6 +28,8 @@ AdditionalFileset.DisplayName=\u8ffd\u52a0\u3067\u30b3\u30d4\u30fc\u3059\u308b\u
ReplaceOperation.DisplayName=\u6587\u5b57\u5217\u3092\u7f6e\u304d\u63db\u3048\u308b
# EnableOperation.DisplayName=ジョブを有効にする
EnableOperation.DisplayName=\u30b8\u30e7\u30d6\u3092\u6709\u52b9\u306b\u3059\u308b
# DisableOperation.DisplayName=ジョブを無効にする
DisableOperation.DisplayName=\u30b8\u30e7\u30d6\u3092\u7121\u52b9\u306b\u3059\u308b
# CopiedjobinfoAction.DisplayName=コピーしたジョブ
CopiedjobinfoAction.DisplayName=\u30b3\u30d4\u30fc\u3057\u305f\u30b8\u30e7\u30d6
# JobCopyBuilder.JobName.empty=値を指定してください
Expand Down
@@ -0,0 +1,80 @@
/*
* The MIT License
*
* Copyright (c) 2015 IKEDA Yasuyuki
*
* 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 jp.ikedam.jenkins.plugins.jobcopy_builder;

import java.util.Arrays;
import java.util.Collections;

import hudson.model.FreeStyleProject;

import org.jvnet.hudson.test.HudsonTestCase;

/**
* Tests for {@link DisableOperation}
*/
public class DisableOperationJenkinsTest extends HudsonTestCase
{
public void testDisableOperation() throws Exception
{
FreeStyleProject copiee = createFreeStyleProject();
copiee.enable();
copiee.save();

FreeStyleProject copier = createFreeStyleProject();
copier.getBuildersList().add(new JobcopyBuilder(
copiee.getFullName(),
"copied",
false,
Arrays.<JobcopyOperation>asList(
new DisableOperation()
),
Collections.<AdditionalFileset>emptyList()
));
assertBuildStatusSuccess(copier.scheduleBuild2(0));

FreeStyleProject copied = jenkins.getItemByFullName("copied", FreeStyleProject.class);
assertTrue(copied.isDisabled());
}

public void testConfiguration() throws Exception
{
JobcopyBuilder expected = new JobcopyBuilder(
"from",
"to",
false,
Arrays.<JobcopyOperation>asList(
new DisableOperation()
),
Collections.<AdditionalFileset>emptyList()
);

FreeStyleProject p = createFreeStyleProject();
p.getBuildersList().add(expected);
configRoundtrip(p);
JobcopyBuilder actual = p.getBuildersList().get(JobcopyBuilder.class);
// assertEqualDataBoundBeans(expected, actual); // This cause NPE as actual.additionalFilesetList gets null.
assertEqualDataBoundBeans(expected.getJobcopyOperationList(), actual.getJobcopyOperationList());
}
}

0 comments on commit b9e7262

Please sign in to comment.