Skip to content
Permalink
Browse files

[JENKINS-28071] generalise CLI's BuildCommand target from AbstractPro…

…ject to Job (#1979)

* [JENKINS-28071] generalise BuildCommand target from AbstractProject to Job

This allows e.g. WorkflowJob to build, fixing symptom [JENKINS-29826]

* fixup! [JENKINS-28071] generalise BuildCommand target from AbstractProject to Job

* fixup! [JENKINS-28071] generalise BuildCommand target from AbstractProject to Job
  • Loading branch information
corngood authored and oleg-nenashev committed May 14, 2016
1 parent 1e6afba commit 6ceccc0cc4ff20915f395accf9775c27975c5c3d
@@ -28,19 +28,24 @@
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Cause.UserIdCause;
import hudson.model.CauseAction;
import hudson.model.Job;
import hudson.model.Run;
import hudson.model.ParametersAction;
import hudson.model.ParameterValue;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.ParameterDefinition;
import hudson.Extension;
import hudson.AbortException;
import hudson.model.Queue;
import hudson.model.Item;
import hudson.model.TaskListener;
import hudson.model.User;
import hudson.model.queue.QueueTaskFuture;
import hudson.scm.PollingResult.Change;
import hudson.util.EditDistance;
import hudson.util.StreamTaskListener;

import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.Option;
@@ -54,6 +59,8 @@
import java.io.PrintStream;

import jenkins.model.Jenkins;
import jenkins.model.ParameterizedJobMixIn;
import jenkins.triggers.SCMTriggerItem;

/**
* Builds a job, and optionally waits until its completion.
@@ -68,7 +75,7 @@ public String getShortDescription() {
}

@Argument(metaVar="JOB",usage="Name of the job to build",required=true)
public AbstractProject<?,?> job;
public Job<?,?> job;

@Option(name="-f", usage="Follow the build progress. Like -s only interrupts are not passed through to the build.")
public boolean follow = false;
@@ -138,28 +145,31 @@ protected int run() throws Exception {
}

if (checkSCM) {
if (job.poll(new StreamTaskListener(stdout, getClientCharset())).change == Change.NONE) {
SCMTriggerItem item = SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(job);
if (item == null)
throw new AbortException(job.getFullDisplayName()+" has no SCM trigger, but checkSCM was specified");
if (!item.poll(new StreamTaskListener(stdout, getClientCharset())).hasChanges())
return 0;
}
}

if (!job.isBuildable()) {
String msg = Messages.BuildCommand_CLICause_CannotBuildUnknownReasons(job.getFullDisplayName());
if (job.isDisabled()) {
if (job instanceof AbstractProject<?, ?> && ((AbstractProject<?, ?>)job).isDisabled()) {
msg = Messages.BuildCommand_CLICause_CannotBuildDisabled(job.getFullDisplayName());
} else if (job.isHoldOffBuildUntilSave()){
msg = Messages.BuildCommand_CLICause_CannotBuildConfigNotSaved(job.getFullDisplayName());
}
throw new IllegalStateException(msg);
}

QueueTaskFuture<? extends AbstractBuild> f = job.scheduleBuild2(0, new CLICause(Jenkins.getAuthentication().getName()), a);
Queue.Item item = ParameterizedJobMixIn.scheduleBuild2(job, 0, new CauseAction(new CLICause(Jenkins.getAuthentication().getName())), a);
QueueTaskFuture<? extends Run<?,?>> f = item != null ? (QueueTaskFuture)item.getFuture() : null;

if (wait || sync || follow) {
if (f == null) {
throw new IllegalStateException(BUILD_SCHEDULING_REFUSED);
}
AbstractBuild b = f.waitForStart(); // wait for the start
Run<?,?> b = f.waitForStart(); // wait for the start
stdout.println("Started "+b.getFullDisplayName());
stdout.flush();

@@ -0,0 +1,53 @@
/*
* The MIT License
*
* Copyright (c) 2004-2009, Sun Microsystems, 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 hudson.cli.handlers;

import hudson.model.Job;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.OptionDef;
import org.kohsuke.args4j.spi.Setter;
import org.kohsuke.MetaInfServices;
import org.kohsuke.args4j.spi.OptionHandler;

/**
* Refer to {@link Job} by its name.
*
* @author Kohsuke Kawaguchi
*/
@MetaInfServices(OptionHandler.class)
@SuppressWarnings("rawtypes")
public class JobOptionHandler extends GenericItemOptionHandler<Job> {
public JobOptionHandler(CmdLineParser parser, OptionDef option, Setter<Job> setter) {
super(parser, option, setter);
}

@Override protected Class<Job> type() {
return Job.class;
}

@Override
public String getDefaultMetaVariable() {
return "JOB";
}
}

0 comments on commit 6ceccc0

Please sign in to comment.
You can’t perform that action at this time.