Skip to content
Permalink
Browse files

[FIXED JENKINS-14406] Adding CLI for updating next build number

  • Loading branch information
olivergondza committed Sep 23, 2013
1 parent 0017395 commit cd593d7c7a130f24914a68b6b4502fc4d0ab8738
@@ -0,0 +1,61 @@
<!--
Tests require version 1.526 of jenkins-core and jenkins-test-harness. Keep in
separate pom until the plugin itself starts to depend on newer version.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.526</version>
</parent>

<artifactId>next-build-number</artifactId>
<version>1.1-SNAPSHOT</version>
<packaging>hpi</packaging>
<name>Hudson Next Build Number Plugin</name>
<url>https://wiki.jenkins-ci.org/display/JENKINS/Next+Build+Number+Plugin</url>
<description>Sets the build number Hudson will use for a job's next build</description>

<scm>
<connection>scm:git:git://github.com/jenkinsci/next-build-number-plugin.git</connection>
<developerConnection>scm:git:git@github.com:jenkinsci/next-build-number-plugin.git</developerConnection>
<url>https://github.com/jenkinsci/next-build-number-plugin</url>
</scm>

<developers>
<developer>
<id>dty</id>
<name>Dean Yu</name>
</developer>
</developers>

<repositories>
<repository>
<id>repo.jenkins-ci.org</id>
<url>http://repo.jenkins-ci.org/public/</url>
</repository>
</repositories>

<pluginRepositories>
<pluginRepository>
<id>repo.jenkins-ci.org</id>
<url>http://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>

<dependencies>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.2.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.main</groupId>
<artifactId>jenkins-test-harness</artifactId>
<version>1.526</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
@@ -37,6 +37,10 @@ THE SOFTWARE.
<url>https://wiki.jenkins-ci.org/display/JENKINS/Next+Build+Number+Plugin</url>
<description>Sets the build number Hudson will use for a job's next build</description>

<properties>
<maven.test.skip>true</maven.test.skip><!-- see pom-for-test.xml -->
</properties>

<scm>
<connection>scm:git:git://github.com/jenkinsci/next-build-number-plugin.git</connection>
<developerConnection>scm:git:git@github.com:jenkinsci/next-build-number-plugin.git</developerConnection>
@@ -0,0 +1,78 @@
/*
* The MIT License
*
* Copyright (c) 2013, Red Hat, 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.nextbuildnumber;

import hudson.Extension;
import hudson.model.Item;
import hudson.model.TopLevelItem;
import hudson.model.Job;

import java.io.IOException;

import org.kohsuke.args4j.Argument;

/**
* @author ogondza
*/
@Extension
public class CLICommand extends hudson.cli.CLICommand {

/*package*/ static final int INVALID_NEXT_BUILD_NUMBER = -2;

@Argument(index=0, required=true, metaVar="JOB", usage="Name of the job")
private TopLevelItem item;

@Argument(index=1, required=true, metaVar="BUILD_NUMBER", usage="Next build number")
private int number;

@Override
public String getShortDescription() {

return Messages.CLIDescription();
}

@Override
public String getName() {

return "set-next-build-number";
}

@Override
protected int run() throws IOException {

item.checkPermission(Item.CONFIGURE);

Job<?, ?> job = ((Job<?, ?>) item);

job.updateNextBuildNumber(number);

if (job.getNextBuildNumber() == number) return 0;

stderr.println(Messages.InvalidBuildNumber(
Integer.toString(number),
Integer.toString(job.getNextBuildNumber())
));
return INVALID_NEXT_BUILD_NUMBER;
}
}
@@ -21,3 +21,5 @@
# THE SOFTWARE.

DisplayName=Set Next Build Number
CLIDescription=Set build number to be used for next build.
InvalidBuildNumber=Provided build number {0} is less than last build number {1}.
@@ -0,0 +1,108 @@
/*
* The MIT License
*
* Copyright 2013 Red Hat, 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.nextbuildnumber;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.text.IsEmptyString.isEmptyString;
import hudson.model.FreeStyleBuild;
import hudson.model.Item;
import hudson.model.FreeStyleProject;
import jenkins.model.Jenkins;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

public class CLICommandTest {

private CLICommandInvoker command;

@Rule public final JenkinsRule j = new JenkinsRule();

@Before public void setUp() {

command = new CLICommandInvoker(j, new CLICommand());
}

@Test public void updateShouldFailIfJobDoesNotExist() {

final CLICommandInvoker.Result result = command
.authorizedTo(Jenkins.READ, Item.READ, Item.CONFIGURE)
.invokeWithArgs("project", "42")
;

assertThat(result.stderr(), containsString("No such job 'project'"));
assertThat("No output expected", result.stdout(), isEmptyString());
assertThat("Command is expected to fail", result.returnCode(), equalTo(-1));
}

@Test public void updateShouldFailWithoutJobConfigurePermission() throws Exception {

j.createFreeStyleProject("project");

final CLICommandInvoker.Result result = command
.authorizedTo(Jenkins.READ, Item.READ)
.invokeWithArgs("project", "42")
;

assertThat(result.stderr(), containsString("user is missing the Job/Configure permission"));
assertThat("No output expected", result.stdout(), isEmptyString());
assertThat("Command is expected to fail", result.returnCode(), equalTo(-1));
}

@Test public void updateShouldFailIfNewNumberIsLessThanTheOldOne() throws Exception {

FreeStyleProject project = j.createFreeStyleProject("project");
project.updateNextBuildNumber(42);
project.scheduleBuild2(0).get(); // Next number is 43 now

final CLICommandInvoker.Result result = command
.authorizedTo(Jenkins.READ, Item.READ, Item.CONFIGURE)
.invokeWithArgs("project", "42")
;

assertThat("No output expected", result.stdout(), isEmptyString());
assertThat("Command is expected to fail", result.returnCode(), equalTo(CLICommand.INVALID_NEXT_BUILD_NUMBER));
assertThat(result.stderr(), containsString("Provided build number 42 is less than last build number 43"));
assertThat("Build number has not changed", project.getNextBuildNumber(), equalTo(43));
}

@Test public void updateShouldBumpTheNextBuildNumber() throws Exception {

FreeStyleProject project = j.createFreeStyleProject("project");

final CLICommandInvoker.Result result = command
.authorizedTo(Jenkins.READ, Item.READ, Item.CONFIGURE)
.invokeWithArgs("project", "42")
;

FreeStyleBuild build = project.scheduleBuild2(0).get();

assertThat("No error output expected", result.stderr(), isEmptyString());
assertThat(build.number, equalTo(42));
assertThat("Command is expected to succeed", result.returnCode(), equalTo(0));
}
}

0 comments on commit cd593d7

Please sign in to comment.