Skip to content
Prev Previous commit
Next Next commit
Also check that pipeline jobs aren't running
Don't allow to start a build if there is something still building.
  • Loading branch information
juhasipo committed Oct 22, 2016
commit 65bad1e06f2caa912b016522fae0b4fab875f104
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import com.tw.go.plugin.model.Revision;
import com.tw.go.plugin.util.ListUtil;
import com.tw.go.plugin.util.StringUtil;
import in.ashwanthkumar.gocd.github.jsonapi.PipelineHistory;
import in.ashwanthkumar.gocd.github.jsonapi.PipelineStatus;
import in.ashwanthkumar.gocd.github.jsonapi.Server;
import in.ashwanthkumar.gocd.github.jsonapi.ServerFactory;
Expand Down Expand Up @@ -371,20 +372,26 @@ GoPluginApiResponse handleLatestRevisionSince(GoPluginApiRequest goPluginApiRequ
}
}

private boolean canSchedule(Server server, Option<String> pipelineName) throws IOException {
if (pipelineName.isEmpty()) {
boolean canSchedule(Server server, Option<String> pipelineNameOption) throws IOException {
if (pipelineNameOption.isEmpty()) {
LOGGER.debug("Pipeline name not given. Can schedule");
return true;
}

LOGGER.info(String.format("Check can schedule pipeline %s", pipelineName.get()));
final String pipelineName = pipelineNameOption.get();

PipelineStatus pipelineStatus = server.getPipelineStatus(pipelineName.get());
LOGGER.info(String.format("Check can schedule pipeline %s", pipelineName));

PipelineStatus pipelineStatus = server.getPipelineStatus(pipelineName);
if (pipelineStatus != null) {
LOGGER.info(String.format(" Pipeline schedulable: %s", pipelineStatus.schedulable));
return pipelineStatus.schedulable;
if (pipelineStatus.schedulable) {
PipelineHistory pipelineHistory = server.getPipelineHistory(pipelineName);
return pipelineHistory == null
|| !pipelineHistory.isPipelineRunningOrScheduled();
} else {
return false;
}
} else {
LOGGER.debug(" Could not fetch pipeline status. Allow to schedule");
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public List<Pipeline> getPipelines() {
return pipelines;
}

boolean isPipelineRunningOrScheduled() {
public boolean isPipelineRunningOrScheduled() {
for (Pipeline pipeline : pipelines) {
if (pipeline.isRunningOrScheduled()) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,10 @@ public class PipelineStatus {
@SerializedName("schedulable")
public boolean schedulable;

public PipelineStatus() {
}

public PipelineStatus(boolean schedulable) {
this.schedulable = schedulable;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.tw.go.plugin.model.GitConfig;
import com.tw.go.plugin.model.ModifiedFile;
import com.tw.go.plugin.model.Revision;
import in.ashwanthkumar.gocd.github.jsonapi.PipelineHistory;
import in.ashwanthkumar.gocd.github.jsonapi.PipelineStatus;
import in.ashwanthkumar.gocd.github.jsonapi.Server;
import in.ashwanthkumar.gocd.github.jsonapi.ServerFactory;
Expand All @@ -21,6 +22,7 @@
import in.ashwanthkumar.gocd.github.util.GitFactory;
import in.ashwanthkumar.gocd.github.util.GitFolderFactory;
import in.ashwanthkumar.gocd.github.util.JSONUtils;
import in.ashwanthkumar.utils.lang.option.Option;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -252,6 +254,107 @@ public void shouldBuildBlacklistedBranchIfBlacklistingNotEnabled() throws Except
assertThat(response.responseCode(), is(200));
}

@Test
public void shouldSchedule() throws Exception {
GitFactory gitFactory = mock(GitFactory.class);
GitFolderFactory gitFolderFactory = mock(GitFolderFactory.class);
GitHubPRBuildPlugin plugin = new GitHubPRBuildPlugin(
new GerritProvider(),
gitFactory,
gitFolderFactory,
mockServerFactory(),
mockGoApplicationAccessor()
);

Server server = mock(Server.class);
when(server.getPipelineStatus("pipeline")).thenReturn(new PipelineStatus(true));
PipelineHistory pipelineHistory = mockPipelineHistory(false);
when(server.getPipelineHistory("pipeline")).thenReturn(pipelineHistory);

assertThat(plugin.canSchedule(server, Option.option("pipeline")), is(true));
}

@Test
public void shouldNotScheduleIfPipelineNotSchedulable() throws Exception {
GitFactory gitFactory = mock(GitFactory.class);
GitFolderFactory gitFolderFactory = mock(GitFolderFactory.class);
GitHubPRBuildPlugin plugin = new GitHubPRBuildPlugin(
new GerritProvider(),
gitFactory,
gitFolderFactory,
mockServerFactory(),
mockGoApplicationAccessor()
);

Server server = mock(Server.class);
when(server.getPipelineStatus("pipeline")).thenReturn(new PipelineStatus(false));
PipelineHistory pipelineHistory = mockPipelineHistory(false);
when(server.getPipelineHistory("pipeline")).thenReturn(pipelineHistory);

assertThat(plugin.canSchedule(server, Option.option("pipeline")), is(false));
}

@Test
public void shouldNotScheduleIfPipelineHistoryContainsRunningJobs() throws Exception {
GitFactory gitFactory = mock(GitFactory.class);
GitFolderFactory gitFolderFactory = mock(GitFolderFactory.class);
GitHubPRBuildPlugin plugin = new GitHubPRBuildPlugin(
new GerritProvider(),
gitFactory,
gitFolderFactory,
mockServerFactory(),
mockGoApplicationAccessor()
);

Server server = mock(Server.class);
when(server.getPipelineStatus("pipeline")).thenReturn(new PipelineStatus(true));
PipelineHistory pipelineHistory = mockPipelineHistory(true);
when(server.getPipelineHistory("pipeline")).thenReturn(pipelineHistory);

assertThat(plugin.canSchedule(server, Option.option("pipeline")), is(false));
}

@Test
public void shouldScheduleIfNoPipelineStatusFound() throws Exception {
GitFactory gitFactory = mock(GitFactory.class);
GitFolderFactory gitFolderFactory = mock(GitFolderFactory.class);
GitHubPRBuildPlugin plugin = new GitHubPRBuildPlugin(
new GerritProvider(),
gitFactory,
gitFolderFactory,
mockServerFactory(),
mockGoApplicationAccessor()
);

Server server = mock(Server.class);

assertThat(plugin.canSchedule(server, Option.option("pipeline")), is(true));
}

@Test
public void shouldScheduleIfNoPipelineHistoryFound() throws Exception {
GitFactory gitFactory = mock(GitFactory.class);
GitFolderFactory gitFolderFactory = mock(GitFolderFactory.class);
GitHubPRBuildPlugin plugin = new GitHubPRBuildPlugin(
new GerritProvider(),
gitFactory,
gitFolderFactory,
mockServerFactory(),
mockGoApplicationAccessor()
);

Server server = mock(Server.class);
when(server.getPipelineStatus("pipeline")).thenReturn(new PipelineStatus(true));

assertThat(plugin.canSchedule(server, Option.option("pipeline")), is(true));
}

private PipelineHistory mockPipelineHistory(boolean isRunning) {
PipelineHistory pipelineHistory = mock(PipelineHistory.class);
when(pipelineHistory.isPipelineRunningOrScheduled()).thenReturn(isRunning);
return pipelineHistory;
}

private GoPluginApiRequest mockRequest() {
GoPluginApiRequest request = mock(GoPluginApiRequest.class);
when(request.requestBody()).thenReturn("{\n" +
Expand Down