Skip to content

Commit 8d917f1

Browse files
jgoldhammergmessner
authored andcommitted
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
1 parent 2b6673a commit 8d917f1

File tree

3 files changed

+249
-0
lines changed

3 files changed

+249
-0
lines changed

src/main/java/org/gitlab4j/api/RunnersApi.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,4 +411,49 @@ public Runner disableRunner(Integer projectId, Integer runnerId) throws GitLabAp
411411
Response response = delete(Response.Status.OK, formData.asMap(), "projects", projectId, "runners");
412412
return (response.readEntity(Runner.class));
413413
}
414+
415+
/**
416+
* Register a new runner for the gitlab instance.
417+
*
418+
* POST /runners/
419+
*
420+
* @param token the token of the project (for project specific runners) or the token from the admin page.
421+
* @param description The description of a runner
422+
* @param active The state of a runner; can be set to true or false
423+
* @param tagList The list of tags for a runner; put array of tags, that should be finally assigned to a runner
424+
* @param runUntagged Flag indicating the runner can execute untagged jobs
425+
* @param locked Flag indicating the runner is locked
426+
* @return RunnerDetail instance.
427+
* @throws GitLabApiException if any exception occurs
428+
*/
429+
public RunnerDetail registerRunner(String token, String description, Boolean active, List<String> tagList,
430+
Boolean runUntagged, Boolean locked, Integer maximumTimeout) throws GitLabApiException {
431+
432+
GitLabApiForm formData = new GitLabApiForm()
433+
.withParam("token", token, true)
434+
.withParam("description", description, false)
435+
.withParam("active", active, false)
436+
.withParam("locked", locked, false)
437+
.withParam("run_untagged", runUntagged, false)
438+
.withParam("tag_list", tagList, false)
439+
.withParam("maximum_timeout", maximumTimeout, false);
440+
Response response = post(Response.Status.CREATED, formData.asMap(), "runners");
441+
return (response.readEntity(RunnerDetail.class));
442+
}
443+
444+
/**
445+
* Deletes a registed Runner.
446+
*
447+
* DELETE /runners/
448+
*
449+
* @throws GitLabApiException if any exception occurs
450+
*/
451+
public void deleteRunner(String token) throws GitLabApiException {
452+
GitLabApiForm formData = new GitLabApiForm()
453+
.withParam("token", token, true);
454+
455+
delete(Response.Status.NO_CONTENT, formData.asMap(), "runners");
456+
}
457+
458+
414459
}

src/main/java/org/gitlab4j/api/models/Runner.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public class Runner {
1919
private String name;
2020
private Boolean online;
2121
private RunnerStatus status;
22+
private String ipAddress;
23+
2224

2325
/**
2426
* Enum to use for RunnersApi filtering.
@@ -44,6 +46,9 @@ public String toString() {
4446
}
4547
}
4648

49+
50+
51+
4752
public Integer getId() {
4853
return id;
4954
}
@@ -99,4 +104,12 @@ public RunnerStatus getStatus() {
99104
public void setStatus(RunnerStatus status) {
100105
this.status = status;
101106
}
107+
108+
public String getIpAddress() {
109+
return ipAddress;
110+
}
111+
112+
public void setIpAddress(String ipAddress) {
113+
this.ipAddress = ipAddress;
114+
}
102115
}
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2017 Greg Messner <greg@messners.com>
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy of
7+
* this software and associated documentation files (the "Software"), to deal in
8+
* the Software without restriction, including without limitation the rights to
9+
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10+
* the Software, and to permit persons to whom the Software is furnished to do so,
11+
* subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18+
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19+
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20+
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22+
*/
23+
24+
package org.gitlab4j.api;
25+
26+
import org.gitlab4j.api.GitLabApi.ApiVersion;
27+
import org.gitlab4j.api.models.*;
28+
import org.glassfish.jersey.internal.guava.Lists;
29+
import org.junit.*;
30+
import org.junit.runners.MethodSorters;
31+
32+
import javax.ws.rs.core.Response;
33+
import java.util.Arrays;
34+
import java.util.List;
35+
import java.util.Map;
36+
import java.util.Optional;
37+
38+
import static org.junit.Assert.*;
39+
import static org.junit.Assume.assumeTrue;
40+
41+
/**
42+
* In order for these tests to run you must set the following properties in ~/test-gitlab4j.properties
43+
* <p>
44+
* TEST_NAMESPACE
45+
* TEST_PROJECT_NAME
46+
* TEST_HOST_URL
47+
* TEST_PRIVATE_TOKEN
48+
* TEST_GROUP_PROJECT
49+
* <p>
50+
* If any of the above are NULL, all tests in this class will be skipped.
51+
* <p>
52+
* NOTE: &amp;FixMethodOrder(MethodSorters.NAME_ASCENDING) is very important to insure that the tests are in the correct order
53+
*/
54+
@FixMethodOrder(MethodSorters.JVM)
55+
public class TestRunnersApi {
56+
57+
// The following needs to be set to your test repository
58+
private static final String TEST_NAMESPACE;
59+
private static final String TEST_PROJECT_NAME;
60+
private static final String TEST_HOST_URL;
61+
private static final String TEST_PRIVATE_TOKEN;
62+
private static final String TEST_GROUP;
63+
private static final String TEST_GROUP_PROJECT;
64+
65+
static {
66+
TEST_NAMESPACE = TestUtils.getProperty("TEST_NAMESPACE");
67+
TEST_PROJECT_NAME = TestUtils.getProperty("TEST_PROJECT_NAME");
68+
TEST_HOST_URL = TestUtils.getProperty("TEST_HOST_URL");
69+
TEST_PRIVATE_TOKEN = TestUtils.getProperty("TEST_PRIVATE_TOKEN");
70+
TEST_GROUP = TestUtils.getProperty("TEST_GROUP");
71+
TEST_GROUP_PROJECT = TestUtils.getProperty("TEST_GROUP_PROJECT");
72+
}
73+
74+
private static final String TEST_PROJECT_NAME_1 = "test-gitlab4j-create-project";
75+
private static final String TEST_PROJECT_NAME_2 = "test-gitlab4j-create-project-2";
76+
private static final String TEST_PROJECT_NAME_UPDATE = "test-gitlab4j-create-project-update";
77+
private static GitLabApi gitLabApi;
78+
79+
public TestRunnersApi() {
80+
super();
81+
}
82+
83+
@BeforeClass
84+
public static void setup() throws GitLabApiException {
85+
86+
String problems = "";
87+
if (TEST_NAMESPACE == null || TEST_NAMESPACE.trim().isEmpty()) {
88+
problems += "TEST_NAMESPACE cannot be empty\n";
89+
}
90+
91+
if (TEST_HOST_URL == null || TEST_HOST_URL.trim().isEmpty()) {
92+
problems += "TEST_HOST_URL cannot be empty\n";
93+
}
94+
95+
if (TEST_PRIVATE_TOKEN == null || TEST_PRIVATE_TOKEN.trim().isEmpty()) {
96+
problems += "TEST_PRIVATE_TOKEN cannot be empty\n";
97+
}
98+
99+
if (problems.isEmpty()) {
100+
gitLabApi = new GitLabApi(ApiVersion.V4, TEST_HOST_URL, TEST_PRIVATE_TOKEN);
101+
} else {
102+
System.err.print(problems);
103+
}
104+
105+
List<Runner> allRunners = gitLabApi.getRunnersApi().getAllRunners();
106+
107+
if (allRunners.size() > 0) {
108+
109+
for (Runner runner : allRunners) {
110+
RunnerDetail runnerDetail = gitLabApi.getRunnersApi().getRunnerDetail(runner.getId());
111+
gitLabApi.getRunnersApi().deleteRunner(runnerDetail.getToken());
112+
}
113+
114+
allRunners = gitLabApi.getRunnersApi().getAllRunners();
115+
116+
assertEquals(0, allRunners.size());
117+
}
118+
119+
}
120+
121+
/**
122+
* creates a new runner for a random project
123+
*/
124+
private static void createRunner() throws GitLabApiException {
125+
126+
// WHEN
127+
Project project = gitLabApi.getProjectApi().getProjects().get(0);
128+
project = gitLabApi.getProjectApi().getProject(project.getId());
129+
String runnersToken = project.getRunnersToken();
130+
131+
// THEN
132+
gitLabApi.getRunnersApi().registerRunner(runnersToken,
133+
"Junit registered runner", true,
134+
Arrays.asList("wow"), false,
135+
false, null);
136+
137+
// ASSERT
138+
List<Runner> allRunners = gitLabApi.getRunnersApi().getAllRunners();
139+
140+
}
141+
142+
143+
@Before
144+
public void beforeMethod() throws GitLabApiException {
145+
assumeTrue(gitLabApi != null);
146+
147+
List<Runner> allRunners = gitLabApi.getRunnersApi().getAllRunners();
148+
149+
for (Runner runner : allRunners) {
150+
RunnerDetail runnerDetail = gitLabApi.getRunnersApi().getRunnerDetail(runner.getId());
151+
gitLabApi.getRunnersApi().deleteRunner(runnerDetail.getToken());
152+
}
153+
154+
}
155+
156+
@Test
157+
public void shouldHaveRunnerDetails() throws GitLabApiException {
158+
159+
createRunner();
160+
161+
List<Runner> runners = gitLabApi.getRunnersApi().getAllRunners();
162+
163+
assertEquals(1, runners.size());
164+
assertNotNull("Description should not be null", runners.get(0).getDescription());
165+
166+
}
167+
168+
@Test
169+
public void shouldDeleteRunner() throws GitLabApiException {
170+
171+
createRunner();
172+
createRunner();
173+
createRunner();
174+
175+
List<Runner> allRunners = gitLabApi.getRunnersApi().getAllRunners();
176+
assertEquals(3, allRunners.size());
177+
178+
179+
for (Runner runner : allRunners) {
180+
RunnerDetail runnerDetail = gitLabApi.getRunnersApi().getRunnerDetail(runner.getId());
181+
gitLabApi.getRunnersApi().deleteRunner(runnerDetail.getToken());
182+
}
183+
184+
allRunners = gitLabApi.getRunnersApi().getAllRunners();
185+
assertEquals(0, allRunners.size());
186+
187+
188+
}
189+
190+
191+
}

0 commit comments

Comments
 (0)