diff --git a/src/main/java/org/gitlab4j/api/RunnersApi.java b/src/main/java/org/gitlab4j/api/RunnersApi.java index 42f899a53f86b2dea2e41bd0049a0f1b3fb7f68b..61e72ca29e9b0b409c59fdf6c0ab3c03cb50c4ae 100644 --- a/src/main/java/org/gitlab4j/api/RunnersApi.java +++ b/src/main/java/org/gitlab4j/api/RunnersApi.java @@ -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 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"); + } + + } diff --git a/src/main/java/org/gitlab4j/api/models/Runner.java b/src/main/java/org/gitlab4j/api/models/Runner.java index 1cab3610a3221f546f849b4e5f4073cc48fdb556..880e5897ed2e8ba2d4872bf32f662c07f7058e88 100644 --- a/src/main/java/org/gitlab4j/api/models/Runner.java +++ b/src/main/java/org/gitlab4j/api/models/Runner.java @@ -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; + } } diff --git a/src/test/java/org/gitlab4j/api/TestRunnersApi.java b/src/test/java/org/gitlab4j/api/TestRunnersApi.java new file mode 100644 index 0000000000000000000000000000000000000000..564739452212cb4215b4d58f2c63acf0b4fd3803 --- /dev/null +++ b/src/test/java/org/gitlab4j/api/TestRunnersApi.java @@ -0,0 +1,191 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2017 Greg Messner + * + * 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 + *

+ * TEST_NAMESPACE + * TEST_PROJECT_NAME + * TEST_HOST_URL + * TEST_PRIVATE_TOKEN + * TEST_GROUP_PROJECT + *

+ * If any of the above are NULL, all tests in this class will be skipped. + *

+ * NOTE: &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 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 allRunners = gitLabApi.getRunnersApi().getAllRunners(); + + } + + + @Before + public void beforeMethod() throws GitLabApiException { + assumeTrue(gitLabApi != null); + + List 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 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 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()); + + + } + + +}