[JENKINS-35797] Refactor implementation
* Move create() to BlueRunContainer. * Return BlueQueueItem on create()
This commit is contained in:
parent
dd8eca2713
commit
68df0bd94e
|
@ -9,6 +9,7 @@ import io.jenkins.blueocean.rest.hal.Link;
|
|||
import io.jenkins.blueocean.rest.model.BlueMultiBranchPipeline;
|
||||
import io.jenkins.blueocean.rest.model.BluePipeline;
|
||||
import io.jenkins.blueocean.rest.model.BluePipelineContainer;
|
||||
import io.jenkins.blueocean.rest.model.BlueQueueItem;
|
||||
import io.jenkins.blueocean.rest.model.BlueRun;
|
||||
import io.jenkins.blueocean.rest.model.BlueRunContainer;
|
||||
import io.jenkins.blueocean.service.embedded.util.FavoriteUtil;
|
||||
|
@ -243,6 +244,11 @@ public class MultiBranchPipelineImpl extends BlueMultiBranchPipeline {
|
|||
|
||||
return c.iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlueQueueItem create() {
|
||||
throw new ServiceException.NotImplementedException("This action is not supported");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -14,6 +14,7 @@ import hudson.model.StringParameterValue;
|
|||
import hudson.model.queue.ScheduleResult;
|
||||
import io.jenkins.blueocean.commons.ServiceException;
|
||||
import io.jenkins.blueocean.rest.hal.Link;
|
||||
import io.jenkins.blueocean.rest.model.BluePipeline;
|
||||
import io.jenkins.blueocean.rest.model.BlueQueueContainer;
|
||||
import io.jenkins.blueocean.rest.model.BlueQueueItem;
|
||||
import jenkins.model.Jenkins;
|
||||
|
@ -38,7 +39,7 @@ public class QueueContainerImpl extends BlueQueueContainer {
|
|||
|
||||
@Override
|
||||
public BlueQueueItem get(String name) {
|
||||
for (BlueQueueItem blueQueueItem : getQueuedItems()) {
|
||||
for (BlueQueueItem blueQueueItem : getQueuedItems(job, pipeline)) {
|
||||
if(name.equals(blueQueueItem.getId())){
|
||||
return blueQueueItem;
|
||||
}
|
||||
|
@ -46,47 +47,10 @@ public class QueueContainerImpl extends BlueQueueContainer {
|
|||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedules a build. If build already exists in the queue and the pipeline does not
|
||||
* support running multiple builds at the same time, return a reference to the existing
|
||||
* build.
|
||||
*
|
||||
* @return Qeueu item.
|
||||
*/
|
||||
@Override
|
||||
public BlueQueueItemCreateResponse createItem() {
|
||||
if (job instanceof Queue.Task) {
|
||||
ScheduleResult scheduleResult = Jenkins.getInstance()
|
||||
.getQueue()
|
||||
.schedule2((Queue.Task)job, 0, new CauseAction(new Cause.UserIdCause()));
|
||||
|
||||
if(scheduleResult.isAccepted()) {
|
||||
final Queue.Item item = scheduleResult.getItem();
|
||||
|
||||
BlueQueueItem queueItem = FluentIterableWrapper.from(getQueuedItems())
|
||||
.firstMatch(new Predicate<BlueQueueItem>() {
|
||||
@Override
|
||||
public boolean apply(@Nullable BlueQueueItem input) {
|
||||
return input.getId().equalsIgnoreCase(Long.toString(item.getId()));
|
||||
}
|
||||
}).orNull();
|
||||
|
||||
if (queueItem == null) {
|
||||
throw new ServiceException.UnexpectedErrorException("The queue item does not exist in the queue");
|
||||
} else {
|
||||
return new BlueQueueItemCreateResponse(queueItem);
|
||||
}
|
||||
} else {
|
||||
throw new ServiceException.UnexpectedErrorException("Queue item request was not accepted");
|
||||
}
|
||||
} else {
|
||||
throw new ServiceException.NotImplementedException("This pipeline type does not support being queued.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<BlueQueueItem> iterator() {
|
||||
return getQueuedItems().iterator();
|
||||
return getQueuedItems(job, pipeline).iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -97,7 +61,7 @@ public class QueueContainerImpl extends BlueQueueContainer {
|
|||
*
|
||||
* @return List of items newest first
|
||||
*/
|
||||
private List<BlueQueueItem> getQueuedItems() {
|
||||
public static List<BlueQueueItem> getQueuedItems(Job job, BluePipeline pipeline) {
|
||||
if(job instanceof BuildableItem) {
|
||||
BuildableItem task = (BuildableItem)job;
|
||||
List<Queue.Item> items = Jenkins.getInstance().getQueue().getItems(task);
|
||||
|
|
|
@ -1,14 +1,23 @@
|
|||
package io.jenkins.blueocean.service.embedded.rest;
|
||||
|
||||
import com.google.common.base.Predicate;
|
||||
import hudson.model.Cause;
|
||||
import hudson.model.CauseAction;
|
||||
import hudson.model.Job;
|
||||
import hudson.model.Queue;
|
||||
import hudson.model.queue.ScheduleResult;
|
||||
import hudson.util.RunList;
|
||||
import io.jenkins.blueocean.commons.ServiceException;
|
||||
import io.jenkins.blueocean.rest.hal.Link;
|
||||
import io.jenkins.blueocean.rest.model.BluePipeline;
|
||||
import io.jenkins.blueocean.rest.model.BlueQueueItem;
|
||||
import io.jenkins.blueocean.rest.model.BlueRun;
|
||||
import io.jenkins.blueocean.rest.model.BlueRunContainer;
|
||||
import jenkins.model.Jenkins;
|
||||
import org.jenkinsci.plugins.github.util.FluentIterableWrapper;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
|
@ -61,4 +70,42 @@ public class RunContainerImpl extends BlueRunContainer {
|
|||
public BluePipeline getPipeline(String name) {
|
||||
return pipeline;
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedules a build. If build already exists in the queue and the pipeline does not
|
||||
* support running multiple builds at the same time, return a reference to the existing
|
||||
* build.
|
||||
*
|
||||
* @return Qeueu item.
|
||||
*/
|
||||
@Override
|
||||
public BlueQueueItem create() {
|
||||
if (job instanceof Queue.Task) {
|
||||
ScheduleResult scheduleResult = Jenkins.getInstance()
|
||||
.getQueue()
|
||||
.schedule2((Queue.Task)job, 0, new CauseAction(new Cause.UserIdCause()));
|
||||
|
||||
if(scheduleResult.isAccepted()) {
|
||||
final Queue.Item item = scheduleResult.getItem();
|
||||
|
||||
BlueQueueItem queueItem = FluentIterableWrapper.from(QueueContainerImpl.getQueuedItems(job, pipeline))
|
||||
.firstMatch(new Predicate<BlueQueueItem>() {
|
||||
@Override
|
||||
public boolean apply(@Nullable BlueQueueItem input) {
|
||||
return input.getId().equalsIgnoreCase(Long.toString(item.getId()));
|
||||
}
|
||||
}).orNull();
|
||||
|
||||
if (queueItem == null) {
|
||||
throw new ServiceException.UnexpectedErrorException("The queue item does not exist in the queue");
|
||||
} else {
|
||||
return queueItem;
|
||||
}
|
||||
} else {
|
||||
throw new ServiceException.UnexpectedErrorException("Queue item request was not accepted");
|
||||
}
|
||||
} else {
|
||||
throw new ServiceException.NotImplementedException("This pipeline type does not support being queued.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -527,7 +527,7 @@ public class PipelineApiTest extends BaseTest {
|
|||
p1.scheduleBuild2(0).waitForStart();
|
||||
p2.scheduleBuild2(0).waitForStart();
|
||||
|
||||
Map r = request().post("/organizations/jenkins/pipelines/pipeline3/queue/new").build(Map.class);
|
||||
Map r = request().post("/organizations/jenkins/pipelines/pipeline3/runs/").build(Map.class);
|
||||
|
||||
Assert.assertNotNull(p3.getQueueItem());
|
||||
Assert.assertEquals(Long.toString(p3.getQueueItem().getId()), r.get("id"));
|
||||
|
|
|
@ -94,6 +94,11 @@ public abstract class BlueMultiBranchPipeline extends BluePipeline{
|
|||
public Iterator<BlueRun> iterator() {
|
||||
return Collections.emptyIterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public BlueQueueItem create() {
|
||||
throw new ServiceException.NotImplementedException("This action is not supported");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
@ -10,37 +10,4 @@ import org.kohsuke.stapler.verb.PUT;
|
|||
* @author Ivan Meredith
|
||||
*/
|
||||
public abstract class BlueQueueContainer extends Container<BlueQueueItem> {
|
||||
@WebMethod(name = "new")
|
||||
@JsonResponse
|
||||
@JsonOutputFilter(excludes = "state")
|
||||
@POST
|
||||
public abstract BlueQueueItemCreateResponse createItem();
|
||||
|
||||
/**
|
||||
* Response object for create item. is a mirror of BlueQueueItem.
|
||||
*/
|
||||
public static class BlueQueueItemCreateResponse {
|
||||
private BlueQueueItem item;
|
||||
|
||||
public BlueQueueItemCreateResponse(BlueQueueItem item) {
|
||||
this.item = item;
|
||||
|
||||
}
|
||||
|
||||
public String getId() {
|
||||
return item.getId();
|
||||
}
|
||||
|
||||
public String getPipeline() {
|
||||
return item.getPipeline();
|
||||
}
|
||||
|
||||
public String getQueuedTime() {
|
||||
return item.getQueuedTimeString();
|
||||
}
|
||||
|
||||
public int getExpectedBuildNumber() {
|
||||
return item.getExpectedBuildNumber();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,7 +17,7 @@ import static io.jenkins.blueocean.commons.JsonConverter.DATE_FORMAT_STRING;
|
|||
*/
|
||||
public abstract class BlueQueueItem extends Resource {
|
||||
|
||||
public static final String QUEUED_TIME = "qeueudTime";
|
||||
public static final String QUEUED_TIME = "queuedTime";
|
||||
|
||||
/**
|
||||
* @return Id of the item in the queue. Much be unique in the queue of a pipeline
|
||||
|
|
|
@ -1,5 +1,9 @@
|
|||
package io.jenkins.blueocean.rest.model;
|
||||
|
||||
import io.jenkins.blueocean.commons.stapler.TreeResponse;
|
||||
import org.kohsuke.stapler.WebMethod;
|
||||
import org.kohsuke.stapler.verb.POST;
|
||||
|
||||
/**
|
||||
* BlueRun API
|
||||
*
|
||||
|
@ -12,4 +16,9 @@ public abstract class BlueRunContainer extends Container<BlueRun> {
|
|||
* @return pipeline with the given name as parameter
|
||||
*/
|
||||
public abstract BluePipeline getPipeline(String name);
|
||||
|
||||
@POST
|
||||
@WebMethod(name = "")
|
||||
@TreeResponse
|
||||
public abstract BlueQueueItem create();
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue