Commit ff284499 authored by David Lam's avatar David Lam Committed by Greg Messner
Browse files

Runners API (#154)

Added RunnersApi implementation and tests.
parent 8e00f6e0
......@@ -152,6 +152,7 @@ The API has been broken up into sub APIs classes to make it easier to learn and
&nbsp;&nbsp;[ProtectedBranchesApi](#protectedbranchesapi) <br/>
&nbsp;&nbsp;[RepositoryApi](#repositoryapi)<br/>
&nbsp;&nbsp;[RepositoryFileApi](#repositoryfileapi)<br/>
&nbsp;&nbsp;[RunnersApi](#runnersapi) <br/>
&nbsp;&nbsp;[ServicesApi](#servicesapi)<br/>
&nbsp;&nbsp;[SessionApi](#sessionapi)<br/>
&nbsp;&nbsp;[SystemHooksApi](#systemhooksapi)<br/>
......@@ -279,6 +280,12 @@ List<Branch> branches = gitLabApi.getRepositoryApi().getBranches();
RepositoryFile file = gitLabApi.getRepositoryFileApi().getFile("file-path", 1234, "ref");
```
#### RunnersApi
```java
// Get All Runners.
List<Runner> runners = api.getRunnersApi().getAllRunners();
```
#### ServicesApi
```java
// Activates the gitlab-ci service.
......
......@@ -55,8 +55,10 @@ public class GitLabApi {
private NotificationSettingsApi notificationSettingsApi;
private PipelineApi pipelineApi;
private ProjectApi projectApi;
private ProtectedBranchesApi protectedBranchesApi;
private RepositoryApi repositoryApi;
private RepositoryFileApi repositoryFileApi;
private RunnersApi runnersApi;
private ServicesApi servicesApi;
private SessionApi sessionApi;
private SystemHooksApi systemHooksApi;
......@@ -65,7 +67,6 @@ public class GitLabApi {
private LabelsApi labelsApi;
private NotesApi notesApi;
private EventsApi eventsApi;
private ProtectedBranchesApi protectedBranchesApi;
/**
* Create a new GitLabApi instance that is logically a duplicate of this instance, with the exception off sudo state.
......@@ -284,8 +285,7 @@ public class GitLabApi {
* @throws GitLabApiException GitLabApiException if any exception occurs during execution
*/
public static GitLabApi oauth2Login(ApiVersion apiVersion, String url, String username, CharSequence password,
String secretToken, Map<String, Object> clientConfigProperties, boolean ignoreCertificateErrors)
throws GitLabApiException {
String secretToken, Map<String, Object> clientConfigProperties, boolean ignoreCertificateErrors) throws GitLabApiException {
if (username == null || username.trim().length() == 0) {
throw new IllegalArgumentException("both username and email cannot be empty or null");
......@@ -318,7 +318,7 @@ public class GitLabApi {
/**
* <p>Logs into GitLab using provided {@code username} and {@code password}, and creates a new {@code GitLabApi} instance
* using returned private token and the specified GitLab API version.</p>
*
*
* <strong>NOTE</strong>: For GitLab servers 10.2 and above this will utilize OAUTH2 for login. For GitLab servers prior to
* 10.2, the Session API login is utilized.
*
......@@ -338,7 +338,7 @@ public class GitLabApi {
/**
* <p>Logs into GitLab using provided {@code username} and {@code password}, and creates a new {@code GitLabApi} instance
* using returned private token using GitLab API version 4.</p>
*
*
* <strong>NOTE</strong>: For GitLab servers 10.2 and above this will utilize OAUTH2 for login. For GitLab servers prior to
* 10.2, the Session API login is utilized.
*
......@@ -357,7 +357,7 @@ public class GitLabApi {
/**
* <p>Logs into GitLab using provided {@code username} and {@code password}, and creates a new {@code GitLabApi} instance
* using returned private token and the specified GitLab API version.</p>
*
*
* <strong>NOTE</strong>: For GitLab servers 10.2 and above this will utilize OAUTH2 for login. For GitLab servers prior to
* 10.2, the Session API login is utilized.
*
......@@ -402,7 +402,7 @@ public class GitLabApi {
/**
* <p>Logs into GitLab using provided {@code username} and {@code password}, and creates a new {@code GitLabApi} instance
* using returned private token using GitLab API version 4.</p>
*
*
* <strong>NOTE</strong>: For GitLab servers 10.2 and above this will utilize OAUTH2 for login. For GitLab servers prior to
* 10.2, the Session API login is utilized.
*
......@@ -1049,6 +1049,25 @@ public class GitLabApi {
return (projectApi);
}
/**
* Gets the ProtectedBranchesApi instance owned by this GitLabApi instance. The ProtectedBranchesApi is used
* to perform all protection related actions on a branch of a project.
*
* @return the ProtectedBranchesApi instance owned by this GitLabApi instance
*/
public ProtectedBranchesApi getProtectedBranchesApi() {
if (this.protectedBranchesApi == null) {
synchronized (this) {
if (this.protectedBranchesApi == null) {
this.protectedBranchesApi = new ProtectedBranchesApi(this);
}
}
}
return (this.protectedBranchesApi);
}
/**
* Gets the RepositoryApi instance owned by this GitLabApi instance. The RepositoryApi is used
* to perform all repository related API calls.
......@@ -1087,6 +1106,25 @@ public class GitLabApi {
return (repositoryFileApi);
}
/**
* Gets the RunnersApi instance owned by this GitLabApi instance. The RunnersApi is used
* to perform all Runner related API calls.
*
* @return the RunnerApi instance owned by this GitLabApi instance
*/
public RunnersApi getRunnersApi() {
if (runnersApi == null) {
synchronized (this) {
if (runnersApi == null) {
runnersApi = new RunnersApi(this);
}
}
}
return (runnersApi);
}
/**
* Gets the ServicesApi instance owned by this GitLabApi instance. The ServicesApi is used
* to perform all services related API calls.
......@@ -1126,7 +1164,7 @@ public class GitLabApi {
}
/**
* Gets the SystemHooksApi instance owned by this GitLabApi instance. All methods
* Gets the SystemHooksApi instance owned by this GitLabApi instance. All methods
* require administrator authorization.
*
* @return the SystemHooksApi instance owned by this GitLabApi instance
......@@ -1163,25 +1201,6 @@ public class GitLabApi {
return (userApi);
}
/**
* Gets the ProtectedBranchesApi instance owned by this GitLabApi instance. The ProtectedBranchesApi is used
* to perform all protection related actions on a branch of a project.
*
* @return the ProtectedBranchesApi instance owned by this GitLabApi instance
*/
public ProtectedBranchesApi getProtectedBranchesApi() {
if (this.protectedBranchesApi == null) {
synchronized (this) {
if (this.protectedBranchesApi == null) {
this.protectedBranchesApi = new ProtectedBranchesApi(this);
}
}
}
return (this.protectedBranchesApi);
}
/**
* Create and return an Optional instance associated with a GitLabApiException.
*
......
package org.gitlab4j.api;
import org.gitlab4j.api.models.Job;
import org.gitlab4j.api.models.JobStatus;
import org.gitlab4j.api.models.Runner;
import org.gitlab4j.api.models.RunnerDetail;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Response;
import java.util.List;
/**
* This class provides an entry point to all the GitLab API repository files calls.
*/
public class RunnersApi extends AbstractApi {
public RunnersApi(GitLabApi gitLabApi) {
super(gitLabApi);
}
/**
* Get a list of all available runners available to the user.
*
* GET /runners
*
* @return List of Runners
* @throws GitLabApiException if any exception occurs
*/
public List<Runner> getRunners() throws GitLabApiException {
return getRunners(null);
}
/**
* Get a list of specific runners available to the user.
*
* GET /runners
*
* @param scope The scope of specific runners to show, one of: active, paused, online; showing all runners null
* @return List of Runners
* @throws GitLabApiException if any exception occurs
*/
public List<Runner> getRunners(Runner.RunnerStatus scope) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm()
.withParam("scope", scope, false);
Response response = get(Response.Status.OK, formData.asMap(), "runners");
return (response.readEntity(new GenericType<List<Runner>>() {
}));
}
/**
* Get a list of all available runners available to the user.
*
* GET /runners
*
* @param itemsPerPage the number of Runner instances that will be fetched per page
* @return a Pager containing the Runners for the user
* @throws GitLabApiException if any exception occurs
*/
public Pager<Runner> getRunners(int itemsPerPage) throws GitLabApiException {
return getRunners(null, itemsPerPage);
}
/**
* Get a list of specific runners available to the user.
*
* GET /runners
*
* @param scope The scope of specific runners to show, one of: active, paused, online; showing all runners null
* @param itemsPerPage The number of Runner instances that will be fetched per page
* @return a Pager containing the Runners for the user
* @throws GitLabApiException if any exception occurs
*/
public Pager<Runner> getRunners(Runner.RunnerStatus scope, int itemsPerPage) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm()
.withParam("scope", scope, false);
return (new Pager<>(this, Runner.class, itemsPerPage, formData.asMap(), "runners"));
}
/**
* Get a list of all runners in the GitLab instance (specific and shared). Access is restricted to users with admin privileges.
*
* GET /runners/all
*
* @return List of Runners
* @throws GitLabApiException if any exception occurs
*/
public List<Runner> getAllRunners() throws GitLabApiException {
return getAllRunners(null);
}
/**
* Get a list of all runners in the GitLab instance (specific and shared). Access is restricted to users with admin privileges.
*
* GET /runners/all
*
* @param scope The scope of specific runners to show, one of: active, paused, online; showing all runners null
* @return List of Runners
* @throws GitLabApiException if any exception occurs
*/
public List<Runner> getAllRunners(Runner.RunnerStatus scope) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm()
.withParam("scope", scope, false);
Response response = get(Response.Status.OK, formData.asMap(), "runners", "all");
return (response.readEntity(new GenericType<List<Runner>>() {
}));
}
/**
* Get a list of all runners in the GitLab instance (specific and shared). Access is restricted to users with admin privileges.
*
* GET /runners/all
*
* @param itemsPerPage The number of Runner instances that will be fetched per page
* @return List of Runners
* @throws GitLabApiException if any exception occurs
*/
public Pager<Runner> getAllRunners(int itemsPerPage) throws GitLabApiException {
return getAllRunners(null, itemsPerPage);
}
/**
* Get a list of all runners in the GitLab instance (specific and shared). Access is restricted to users with admin privileges.
*
* GET /runners/all
*
* @param scope The scope of specific runners to show, one of: active, paused, online; showing all runners null
* @param itemsPerPage The number of Runner instances that will be fetched per page
* @return a Pager containing the Runners
* @throws GitLabApiException if any exception occurs
*/
public Pager<Runner> getAllRunners(Runner.RunnerStatus scope, int itemsPerPage) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm()
.withParam("scope", scope, false);
return (new Pager<>(this, Runner.class, itemsPerPage, formData.asMap(), "runners"));
}
/**
* Get details of a runner.
*
* GET /runners/:id
*
* @param runnerId Runner id to get details for
* @return RunnerDetail instance.
* @throws GitLabApiException if any exception occurs
*/
public RunnerDetail getRunnerDetail(Integer runnerId) throws GitLabApiException {
if (runnerId == null) {
throw new RuntimeException("runnerId cannot be null");
}
Response response = get(Response.Status.OK, null, "runners", runnerId);
return (response.readEntity(RunnerDetail.class));
}
/**
* Update details of a runner.
*
* PUT /runners/:id
*
* @param runnerId The ID of a runner
* @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
* @param accessLevel The access_level of the runner; not_protected or ref_protected
* @return RunnerDetail instance.
* @throws GitLabApiException if any exception occurs
*/
public RunnerDetail updateRunner(Integer runnerId, String description, Boolean active, List<String> tagList,
Boolean runUntagged, Boolean locked, RunnerDetail.RunnerAccessLevel accessLevel) throws GitLabApiException {
if (runnerId == null) {
throw new RuntimeException("runnerId cannot be null");
}
GitLabApiForm formData = new GitLabApiForm()
.withParam("description", description, false)
.withParam("active", active, false)
.withParam("tag_list", tagList, false)
.withParam("run_untagged", runUntagged, false)
.withParam("locked", locked, false)
.withParam("access_level", accessLevel, false);
Response response = put(Response.Status.OK, formData.asMap(), "runners", runnerId);
return (response.readEntity(RunnerDetail.class));
}
/**
* Remove a runner.
*
* DELETE /runners/:id
*
* @param runnerId The ID of a runner
* @throws GitLabApiException if any exception occurs
*/
public void removeRunner(Integer runnerId) throws GitLabApiException {
if (runnerId == null) {
throw new RuntimeException("runnerId cannot be null");
}
delete(Response.Status.NO_CONTENT, null, "runners", runnerId);
}
/**
* List jobs that are being processed or were processed by specified Runner.
*
* GET /runners/:id/jobs
*
* @param runnerId The ID of a runner
* @return List jobs that are being processed or were processed by specified Runner
* @throws GitLabApiException if any exception occurs
*/
public List<Job> getJobs(Integer runnerId) throws GitLabApiException {
if (runnerId == null) {
throw new RuntimeException("runnerId cannot be null");
}
return getJobs(runnerId, null);
}
/**
* List jobs that are being processed or were processed by specified Runner.
*
* GET /runners/:id/jobs
*
* @param runnerId The ID of a runner
* @param status Status of the job; one of: running, success, failed, canceled
* @return List jobs that are being processed or were processed by specified Runner
* @throws GitLabApiException if any exception occurs
*/
public List<Job> getJobs(Integer runnerId, JobStatus status) throws GitLabApiException {
if (runnerId == null) {
throw new RuntimeException("runnerId cannot be null");
}
GitLabApiForm formData = new GitLabApiForm()
.withParam("status", status, false);
Response response = get(Response.Status.OK, formData.asMap(), "runners", runnerId, "jobs");
return (response.readEntity(new GenericType<List<Job>>() {
}));
}
/**
* List jobs that are being processed or were processed by specified Runner.
*
* GET /runners/:id/jobs
*
* @param runnerId The ID of a runner
* @param itemsPerPage The number of Runner instances that will be fetched per page
* @return a Pager containing the Jobs for the Runner
* @throws GitLabApiException if any exception occurs
*/
public Pager<Job> getJobs(Integer runnerId, int itemsPerPage) throws GitLabApiException {
if (runnerId == null) {
throw new RuntimeException("runnerId cannot be null");
}
return getJobs(runnerId, null, itemsPerPage);
}
/**
* List jobs that are being processed or were processed by specified Runner.
*
* GET /runners/:id/jobs
*
* @param runnerId The ID of a runner
* @param status Status of the job; one of: running, success, failed, canceled
* @param itemsPerPage The number of Runner instances that will be fetched per page
* @return a Pager containing the Jobs for the Runner
* @throws GitLabApiException if any exception occurs
*/
public Pager<Job> getJobs(Integer runnerId, JobStatus status, int itemsPerPage) throws GitLabApiException {
if (runnerId == null) {
throw new RuntimeException("runnerId cannot be null");
}
GitLabApiForm formData = new GitLabApiForm()
.withParam("status", status, false);
return (new Pager<>(this, Job.class, itemsPerPage, formData.asMap(), "runners", runnerId, "jobs"));
}
/**
* List all runners (specific and shared) available in the project. Shared runners are listed if at least one
* shared runner is defined and shared runners usage is enabled in the project's settings.
*
* GET /projects/:id/runners
*
* @param projectId The ID of the project owned by the authenticated user
* @return List of all Runner available in the project
*/
public List<Runner> getProjectRunners(Integer projectId) throws GitLabApiException {
if (projectId == null) {
throw new RuntimeException("projectId cannot be null");
}
Response response = get(Response.Status.OK, null, "projects", projectId, "runners");
return (response.readEntity(new GenericType<List<Runner>>() {
}));
}
/**
* List all runners (specific and shared) available in the project. Shared runners are listed if at least one
* shared runner is defined and shared runners usage is enabled in the project's settings.
*
* GET /projects/:id/runners
*
* @param projectId The ID of the project owned by the authenticated user
* @return Pager of all Runner available in the project
*/
public Pager<Runner> getProjectRunners(Integer projectId, int itemsPerPage) throws GitLabApiException {
if (projectId == null) {
throw new RuntimeException("projectId cannot be null");
}
return (new Pager<>(this, Runner.class, itemsPerPage, null, "projects", projectId, "runners"));
}
/**
* Enable an available specific runner in the project.
*
* POST /projects/:id/runners
*
* @param projectId The ID of the project owned by the authenticated user
* @param runnerId The ID of a runner
* @return Runner instance of the Runner enabled
* @throws GitLabApiException if any exception occurs
*/
public Runner enableRunner(Integer projectId, Integer runnerId) throws GitLabApiException {
if (projectId == null || runnerId == null) {
throw new RuntimeException("projectId or runnerId cannot be null");
}
GitLabApiForm formData = new GitLabApiForm()
.withParam("runner_id", runnerId, true);
Response response = get(Response.Status.OK, formData.asMap(), "projects", projectId, "runners");
return (response.readEntity(Runner.class));
}
/**
* Disable a specific runner from the project. It works only if the project isn't the only project associated with
* the specified runner. If so, an error is returned. Use the {@link{#removeRunner(Integer)}} instead.
*
* DELETE /projects/:id/runners/:runner_id
*
* @param projectId The ID of the project owned by the authenticated user
* @param runnerId The ID of a runner
* @return Runner instance of the Runner disabled
* @throws GitLabApiException if any exception occurs
*/
public Runner disableRunner(Integer projectId, Integer runnerId) throws GitLabApiException {
if (projectId == null || runnerId == null) {
throw new RuntimeException("projectId or runnerId cannot be null");
}
GitLabApiForm formData = new GitLabApiForm()
.withParam("runner_id", runnerId, true);
Response response = delete(Response.Status.OK, formData.asMap(), "projects", projectId, "runners");
return (response.readEntity(Runner.class));
}
}
package org.gitlab4j.api.models;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import org.gitlab4j.api.utils.JacksonJsonEnumHelper;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
......@@ -13,6 +17,32 @@ public class Runner {
private Boolean active;
private Boolean is_shared;
private String name;
private Boolean online;
private RunnerStatus status;
/**
* Enum to use for RunnersApi filtering.
*/
public enum RunnerStatus {
SPECIFIC, SHARED, ACTIVE, ONLINE, PAUSED, OFFLINE;
private static JacksonJsonEnumHelper<RunnerStatus> enumHelper =
new JacksonJsonEnumHelper<>(RunnerStatus.class);
@JsonCreator
public static RunnerStatus forValue(String value) {
return enumHelper.forValue(value);
}
@JsonValue
public String toValue() {
return (enumHelper.toString(this));
}
@Override
public String toString() {
return (enumHelper.toString(this));
}
}
public Integer getId() {
return id;
......@@ -53,4 +83,20 @@ public class Runner {
public void setName(String name) {
this.name = name;
}
public Boolean getOnline() {
return this.online;
}
public void setOnline(Boolean online) {
this.online = online;
}
public RunnerStatus getStatus() {
return this.status;
}
public void setStatus(RunnerStatus status) {
this.status = status;
}
}
package org.gitlab4j.api.models;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import org.gitlab4j.api.utils.JacksonJsonEnumHelper;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class RunnerDetail extends Runner {
private String platform;
private List<Project> projects;
private String token;
private String revision;
private List<String> tagList;
private String version;
private RunnerAccessLevel accessLevel;
/**
* Enum to use for RunnerDetail accessLevel property.
*/
public enum RunnerAccessLevel {
NOT_PROTECTED, REF_PROTECTED;
private static JacksonJsonEnumHelper<RunnerAccessLevel> enumHelper =
new JacksonJsonEnumHelper<>(RunnerAccessLevel.class);
@JsonCreator
public static RunnerAccessLevel forValue(String value) {
return enumHelper.forValue(value);
}
@JsonValue
public String toValue() {
return (enumHelper.toString(this));
}
@Override
public String toString() {
return (enumHelper.toString(this));
}
}
public String getPlatform() {
return this.platform;
}
public void setPlatform(String platform) {
this.platform = platform;
}
public List<Project> getProjects() {
return this.projects;
}
public void setProjects(List<Project> projects) {
this.projects = projects;
}
public String getToken() {
return this.token;
}
public void setToken(String token) {
this.token = token;
}
public String getRevision() {
return this.revision;
}
public void setRevision(String revision) {
this.revision = revision;
}
public List<String> getTagList() {
return this.tagList;
}
public void setTagList(List<String> tagList) {
this.tagList = tagList;
}
public String getVersion() {
return this.version;
}
public void setVersion(String version) {
this.version = version;
}
public RunnerAccessLevel getAccessLevel() {
return this.accessLevel;
}
public void setAccessLevel(RunnerAccessLevel accessLevel) {
this.accessLevel = accessLevel;
}
}
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