Commit 8d917f17 authored by Jens Goldhammer's avatar Jens Goldhammer Committed by Greg Messner
Browse files

feat(runners): additions for runners api (#226)

feat(runners): additions for RunnersApi
- add register and delete runner
- enhance runner entity for ipAdress field
- introduce first runners tests
parent 2b6673ad
......@@ -411,4 +411,49 @@ public class RunnersApi extends AbstractApi {
Response response = delete(Response.Status.OK, formData.asMap(), "projects", projectId, "runners");
return (response.readEntity(Runner.class));
}
/**
* Register a new runner for the gitlab instance.
*
* POST /runners/
*
* @param token the token of the project (for project specific runners) or the token from the admin page.
* @param description The description of a runner
* @param active The state of a runner; can be set to true or false
* @param tagList The list of tags for a runner; put array of tags, that should be finally assigned to a runner
* @param runUntagged Flag indicating the runner can execute untagged jobs
* @param locked Flag indicating the runner is locked
* @return RunnerDetail instance.
* @throws GitLabApiException if any exception occurs
*/
public RunnerDetail registerRunner(String token, String description, Boolean active, List<String> tagList,
Boolean runUntagged, Boolean locked, Integer maximumTimeout) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm()
.withParam("token", token, true)
.withParam("description", description, false)
.withParam("active", active, false)
.withParam("locked", locked, false)
.withParam("run_untagged", runUntagged, false)
.withParam("tag_list", tagList, false)
.withParam("maximum_timeout", maximumTimeout, false);
Response response = post(Response.Status.CREATED, formData.asMap(), "runners");
return (response.readEntity(RunnerDetail.class));
}
/**
* Deletes a registed Runner.
*
* DELETE /runners/
*
* @throws GitLabApiException if any exception occurs
*/
public void deleteRunner(String token) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm()
.withParam("token", token, true);
delete(Response.Status.NO_CONTENT, formData.asMap(), "runners");
}
}
......@@ -19,6 +19,8 @@ public class Runner {
private String name;
private Boolean online;
private RunnerStatus status;
private String ipAddress;
/**
* Enum to use for RunnersApi filtering.
......@@ -44,6 +46,9 @@ public class Runner {
}
}
public Integer getId() {
return id;
}
......@@ -99,4 +104,12 @@ public class Runner {
public void setStatus(RunnerStatus status) {
this.status = status;
}
public String getIpAddress() {
return ipAddress;
}
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
}
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 Greg Messner <greg@messners.com>
*
* 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.gitlab4j.api;
import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.*;
import org.glassfish.jersey.internal.guava.Lists;
import org.junit.*;
import org.junit.runners.MethodSorters;
import javax.ws.rs.core.Response;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import static org.junit.Assert.*;
import static org.junit.Assume.assumeTrue;
/**
* In order for these tests to run you must set the following properties in ~/test-gitlab4j.properties
* <p>
* TEST_NAMESPACE
* TEST_PROJECT_NAME
* TEST_HOST_URL
* TEST_PRIVATE_TOKEN
* TEST_GROUP_PROJECT
* <p>
* If any of the above are NULL, all tests in this class will be skipped.
* <p>
* NOTE: &amp;FixMethodOrder(MethodSorters.NAME_ASCENDING) is very important to insure that the tests are in the correct order
*/
@FixMethodOrder(MethodSorters.JVM)
public class TestRunnersApi {
// The following needs to be set to your test repository
private static final String TEST_NAMESPACE;
private static final String TEST_PROJECT_NAME;
private static final String TEST_HOST_URL;
private static final String TEST_PRIVATE_TOKEN;
private static final String TEST_GROUP;
private static final String TEST_GROUP_PROJECT;
static {
TEST_NAMESPACE = TestUtils.getProperty("TEST_NAMESPACE");
TEST_PROJECT_NAME = TestUtils.getProperty("TEST_PROJECT_NAME");
TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
TEST_GROUP = TestUtils.getProperty("TEST_GROUP");
TEST_GROUP_PROJECT = TestUtils.getProperty("TEST_GROUP_PROJECT");
}
private static final String TEST_PROJECT_NAME_1 = "test-gitlab4j-create-project";
private static final String TEST_PROJECT_NAME_2 = "test-gitlab4j-create-project-2";
private static final String TEST_PROJECT_NAME_UPDATE = "test-gitlab4j-create-project-update";
private static GitLabApi gitLabApi;
public TestRunnersApi() {
super();
}
@BeforeClass
public static void setup() throws GitLabApiException {
String problems = "";
if (TEST_NAMESPACE == null || TEST_NAMESPACE.trim().isEmpty()) {
problems += "TEST_NAMESPACE cannot be empty\n";
}
if (TEST_HOST_URL == null || TEST_HOST_URL.trim().isEmpty()) {
problems += "TEST_HOST_URL cannot be empty\n";
}
if (TEST_PRIVATE_TOKEN == null || TEST_PRIVATE_TOKEN.trim().isEmpty()) {
problems += "TEST_PRIVATE_TOKEN cannot be empty\n";
}
if (problems.isEmpty()) {
gitLabApi = new GitLabApi(ApiVersion.V4, TEST_HOST_URL, TEST_PRIVATE_TOKEN);
} else {
System.err.print(problems);
}
List<Runner> allRunners = gitLabApi.getRunnersApi().getAllRunners();
if (allRunners.size() > 0) {
for (Runner runner : allRunners) {
RunnerDetail runnerDetail = gitLabApi.getRunnersApi().getRunnerDetail(runner.getId());
gitLabApi.getRunnersApi().deleteRunner(runnerDetail.getToken());
}
allRunners = gitLabApi.getRunnersApi().getAllRunners();
assertEquals(0, allRunners.size());
}
}
/**
* creates a new runner for a random project
*/
private static void createRunner() throws GitLabApiException {
// WHEN
Project project = gitLabApi.getProjectApi().getProjects().get(0);
project = gitLabApi.getProjectApi().getProject(project.getId());
String runnersToken = project.getRunnersToken();
// THEN
gitLabApi.getRunnersApi().registerRunner(runnersToken,
"Junit registered runner", true,
Arrays.asList("wow"), false,
false, null);
// ASSERT
List<Runner> allRunners = gitLabApi.getRunnersApi().getAllRunners();
}
@Before
public void beforeMethod() throws GitLabApiException {
assumeTrue(gitLabApi != null);
List<Runner> allRunners = gitLabApi.getRunnersApi().getAllRunners();
for (Runner runner : allRunners) {
RunnerDetail runnerDetail = gitLabApi.getRunnersApi().getRunnerDetail(runner.getId());
gitLabApi.getRunnersApi().deleteRunner(runnerDetail.getToken());
}
}
@Test
public void shouldHaveRunnerDetails() throws GitLabApiException {
createRunner();
List<Runner> runners = gitLabApi.getRunnersApi().getAllRunners();
assertEquals(1, runners.size());
assertNotNull("Description should not be null", runners.get(0).getDescription());
}
@Test
public void shouldDeleteRunner() throws GitLabApiException {
createRunner();
createRunner();
createRunner();
List<Runner> allRunners = gitLabApi.getRunnersApi().getAllRunners();
assertEquals(3, allRunners.size());
for (Runner runner : allRunners) {
RunnerDetail runnerDetail = gitLabApi.getRunnersApi().getRunnerDetail(runner.getId());
gitLabApi.getRunnersApi().deleteRunner(runnerDetail.getToken());
}
allRunners = gitLabApi.getRunnersApi().getAllRunners();
assertEquals(0, allRunners.size());
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment