Commit 97f45725 authored by Greg Messner's avatar Greg Messner
Browse files

Added support for group and project variables (#330).

parent 7a08a31c
...@@ -13,9 +13,10 @@ import org.gitlab4j.api.GitLabApi.ApiVersion; ...@@ -13,9 +13,10 @@ import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.AccessLevel; import org.gitlab4j.api.models.AccessLevel;
import org.gitlab4j.api.models.Group; import org.gitlab4j.api.models.Group;
import org.gitlab4j.api.models.GroupFilter; import org.gitlab4j.api.models.GroupFilter;
import org.gitlab4j.api.models.GroupProjectsFilter;
import org.gitlab4j.api.models.Member; import org.gitlab4j.api.models.Member;
import org.gitlab4j.api.models.Project; import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.GroupProjectsFilter; import org.gitlab4j.api.models.Variable;
import org.gitlab4j.api.models.Visibility; import org.gitlab4j.api.models.Visibility;
/** /**
...@@ -175,13 +176,13 @@ public class GroupApi extends AbstractApi { ...@@ -175,13 +176,13 @@ public class GroupApi extends AbstractApi {
* *
* <pre><code>GitLab Endpoint: GET /groups/:id/subgroups</code></pre> * <pre><code>GitLab Endpoint: GET /groups/:id/subgroups</code></pre>
* *
* @param groupId the group ID to get the sub groups for * @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path, required
* @return a List&lt;Group&gt; containing the group's sub-groups * @return a List&lt;Group&gt; containing the group's sub-groups
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
* @since GitLab 10.3.0 * @since GitLab 10.3.0
*/ */
public List<Group> getSubGroups(Integer groupId) throws GitLabApiException { public List<Group> getSubGroups(Object groupIdOrPath) throws GitLabApiException {
return (getSubGroups(groupId, getDefaultPerPage()).all()); return (getSubGroups(groupIdOrPath, getDefaultPerPage()).all());
} }
/** /**
...@@ -189,14 +190,14 @@ public class GroupApi extends AbstractApi { ...@@ -189,14 +190,14 @@ public class GroupApi extends AbstractApi {
* *
* <pre><code>GitLab Endpoint: GET /groups/:id/subgroups</code></pre> * <pre><code>GitLab Endpoint: GET /groups/:id/subgroups</code></pre>
* *
* @param groupId the group ID to get the sub groups for * @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path, required
* @param itemsPerPage the number of Group instances that will be fetched per page * @param itemsPerPage the number of Group instances that will be fetched per page
* @return a Pager containing matching Group instances * @return a Pager containing matching Group instances
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
* @since GitLab 10.3.0 * @since GitLab 10.3.0
*/ */
public Pager<Group> getSubGroups(Integer groupId, int itemsPerPage) throws GitLabApiException { public Pager<Group> getSubGroups(Object groupIdOrPath, int itemsPerPage) throws GitLabApiException {
return (new Pager<Group>(this, Group.class, itemsPerPage, null, "groups", groupId, "subgroups")); return (new Pager<Group>(this, Group.class, itemsPerPage, null, "groups", getGroupIdOrPath(groupIdOrPath), "subgroups"));
} }
/** /**
...@@ -204,13 +205,13 @@ public class GroupApi extends AbstractApi { ...@@ -204,13 +205,13 @@ public class GroupApi extends AbstractApi {
* *
* <pre><code>GitLab Endpoint: GET /groups/:id/subgroups</code></pre> * <pre><code>GitLab Endpoint: GET /groups/:id/subgroups</code></pre>
* *
* @param groupId the group ID to get the sub groups for * @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path, required
* @return a Stream&lt;Group&gt; containing the group's sub-groups * @return a Stream&lt;Group&gt; containing the group's sub-groups
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
* @since GitLab 10.3.0 * @since GitLab 10.3.0
*/ */
public Stream<Group> getSubGroupsStream(Integer groupId) throws GitLabApiException { public Stream<Group> getSubGroupsStream(Object groupIdOrPath) throws GitLabApiException {
return (getSubGroups(groupId, getDefaultPerPage()).stream()); return (getSubGroups(groupIdOrPath, getDefaultPerPage()).stream());
} }
/** /**
...@@ -218,7 +219,7 @@ public class GroupApi extends AbstractApi { ...@@ -218,7 +219,7 @@ public class GroupApi extends AbstractApi {
* *
* <pre><code>GitLab Endpoint: GET /groups/:id/subgroups</code></pre> * <pre><code>GitLab Endpoint: GET /groups/:id/subgroups</code></pre>
* *
* @param groupId the group ID to get the sub groups for * @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path, required
* @param skipGroups skip the group IDs passed * @param skipGroups skip the group IDs passed
* @param allAvailable show all the groups you have access to (defaults to false for authenticated users) * @param allAvailable show all the groups you have access to (defaults to false for authenticated users)
* @param search return the list of authorized groups matching the search criteria * @param search return the list of authorized groups matching the search criteria
...@@ -230,9 +231,9 @@ public class GroupApi extends AbstractApi { ...@@ -230,9 +231,9 @@ public class GroupApi extends AbstractApi {
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
* @since GitLab 10.3.0 * @since GitLab 10.3.0
*/ */
public List<Group> getSubGroups(Integer groupId, List<Integer> skipGroups, Boolean allAvailable, String search, public List<Group> getSubGroups(Object groupIdOrPath, List<Integer> skipGroups, Boolean allAvailable, String search,
GroupOrderBy orderBy, SortOrder sortOrder, Boolean statistics, Boolean owned) throws GitLabApiException { GroupOrderBy orderBy, SortOrder sortOrder, Boolean statistics, Boolean owned) throws GitLabApiException {
return (getSubGroups(groupId, skipGroups, allAvailable, search, orderBy, sortOrder, statistics, owned, getDefaultPerPage()).all()); return (getSubGroups(groupIdOrPath, skipGroups, allAvailable, search, orderBy, sortOrder, statistics, owned, getDefaultPerPage()).all());
} }
/** /**
...@@ -240,7 +241,7 @@ public class GroupApi extends AbstractApi { ...@@ -240,7 +241,7 @@ public class GroupApi extends AbstractApi {
* *
* <pre><code>GitLab Endpoint: GET /groups/:id/subgroups</code></pre> * <pre><code>GitLab Endpoint: GET /groups/:id/subgroups</code></pre>
* *
* @param groupId the group ID to get the sub groups for * @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path, required
* @param skipGroups skip the group IDs passed * @param skipGroups skip the group IDs passed
* @param allAvailable show all the groups you have access to (defaults to false for authenticated users) * @param allAvailable show all the groups you have access to (defaults to false for authenticated users)
* @param search return the list of authorized groups matching the search criteria * @param search return the list of authorized groups matching the search criteria
...@@ -254,7 +255,7 @@ public class GroupApi extends AbstractApi { ...@@ -254,7 +255,7 @@ public class GroupApi extends AbstractApi {
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
* @since GitLab 10.3.0 * @since GitLab 10.3.0
*/ */
public List<Group> getSubGroups(Integer groupId, List<Integer> skipGroups, Boolean allAvailable, String search, public List<Group> getSubGroups(Object groupIdOrPath, List<Integer> skipGroups, Boolean allAvailable, String search,
GroupOrderBy orderBy, SortOrder sortOrder, Boolean statistics, Boolean owned, int page, int perPage) GroupOrderBy orderBy, SortOrder sortOrder, Boolean statistics, Boolean owned, int page, int perPage)
throws GitLabApiException { throws GitLabApiException {
Form formData = new GitLabApiForm() Form formData = new GitLabApiForm()
...@@ -267,7 +268,7 @@ public class GroupApi extends AbstractApi { ...@@ -267,7 +268,7 @@ public class GroupApi extends AbstractApi {
.withParam("owned", owned) .withParam("owned", owned)
.withParam(PAGE_PARAM, page) .withParam(PAGE_PARAM, page)
.withParam(PER_PAGE_PARAM, perPage); .withParam(PER_PAGE_PARAM, perPage);
Response response = get(Response.Status.OK, formData.asMap(), "groups", groupId, "subgroups"); Response response = get(Response.Status.OK, formData.asMap(), "groups", getGroupIdOrPath(groupIdOrPath), "subgroups");
return (response.readEntity(new GenericType<List<Group>>() {})); return (response.readEntity(new GenericType<List<Group>>() {}));
} }
...@@ -276,7 +277,7 @@ public class GroupApi extends AbstractApi { ...@@ -276,7 +277,7 @@ public class GroupApi extends AbstractApi {
* *
* <pre><code>GitLab Endpoint: GET /groups/:id/subgroups</code></pre> * <pre><code>GitLab Endpoint: GET /groups/:id/subgroups</code></pre>
* *
* @param groupId the group ID to get the sub groups for * @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path, required
* @param skipGroups skip the group IDs passed * @param skipGroups skip the group IDs passed
* @param allAvailable show all the groups you have access to (defaults to false for authenticated users) * @param allAvailable show all the groups you have access to (defaults to false for authenticated users)
* @param search return the list of authorized groups matching the search criteria * @param search return the list of authorized groups matching the search criteria
...@@ -289,7 +290,7 @@ public class GroupApi extends AbstractApi { ...@@ -289,7 +290,7 @@ public class GroupApi extends AbstractApi {
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
* @since GitLab 10.3.0 * @since GitLab 10.3.0
*/ */
public Pager<Group> getSubGroups(Integer groupId, List<Integer> skipGroups, Boolean allAvailable, String search, public Pager<Group> getSubGroups(Object groupIdOrPath, List<Integer> skipGroups, Boolean allAvailable, String search,
GroupOrderBy orderBy, SortOrder sortOrder, Boolean statistics, Boolean owned, int itemsPerPage) throws GitLabApiException { GroupOrderBy orderBy, SortOrder sortOrder, Boolean statistics, Boolean owned, int itemsPerPage) throws GitLabApiException {
Form formData = new GitLabApiForm() Form formData = new GitLabApiForm()
.withParam("skip_groups", skipGroups) .withParam("skip_groups", skipGroups)
...@@ -299,7 +300,7 @@ public class GroupApi extends AbstractApi { ...@@ -299,7 +300,7 @@ public class GroupApi extends AbstractApi {
.withParam("sort_order", sortOrder) .withParam("sort_order", sortOrder)
.withParam("statistics", statistics) .withParam("statistics", statistics)
.withParam("owned", owned); .withParam("owned", owned);
return (new Pager<Group>(this, Group.class, itemsPerPage, formData.asMap(), "groups", groupId, "subgroups")); return (new Pager<Group>(this, Group.class, itemsPerPage, formData.asMap(), "groups", getGroupIdOrPath(groupIdOrPath), "subgroups"));
} }
/** /**
...@@ -307,7 +308,7 @@ public class GroupApi extends AbstractApi { ...@@ -307,7 +308,7 @@ public class GroupApi extends AbstractApi {
* *
* <pre><code>GitLab Endpoint: GET /groups/:id/subgroups</code></pre> * <pre><code>GitLab Endpoint: GET /groups/:id/subgroups</code></pre>
* *
* @param groupId the group ID to get the sub groups for * @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path, required
* @param skipGroups skip the group IDs passed * @param skipGroups skip the group IDs passed
* @param allAvailable show all the groups you have access to (defaults to false for authenticated users) * @param allAvailable show all the groups you have access to (defaults to false for authenticated users)
* @param search return the list of authorized groups matching the search criteria * @param search return the list of authorized groups matching the search criteria
...@@ -319,9 +320,9 @@ public class GroupApi extends AbstractApi { ...@@ -319,9 +320,9 @@ public class GroupApi extends AbstractApi {
* @throws GitLabApiException if any exception occurs * @throws GitLabApiException if any exception occurs
* @since GitLab 10.3.0 * @since GitLab 10.3.0
*/ */
public Stream<Group> getSubGroupsStream(Integer groupId, List<Integer> skipGroups, Boolean allAvailable, String search, public Stream<Group> getSubGroupsStream(Object groupIdOrPath, List<Integer> skipGroups, Boolean allAvailable, String search,
GroupOrderBy orderBy, SortOrder sortOrder, Boolean statistics, Boolean owned) throws GitLabApiException { GroupOrderBy orderBy, SortOrder sortOrder, Boolean statistics, Boolean owned) throws GitLabApiException {
return (getSubGroups(groupId, skipGroups, allAvailable, search, orderBy, sortOrder, statistics, owned, getDefaultPerPage()).stream()); return (getSubGroups(groupIdOrPath, skipGroups, allAvailable, search, orderBy, sortOrder, statistics, owned, getDefaultPerPage()).stream());
} }
/** /**
...@@ -988,4 +989,148 @@ public class GroupApi extends AbstractApi { ...@@ -988,4 +989,148 @@ public class GroupApi extends AbstractApi {
delete(Response.Status.OK, null, "groups", getGroupIdOrPath(groupIdOrPath), "ldap_group_links", provider, cn); delete(Response.Status.OK, null, "groups", getGroupIdOrPath(groupIdOrPath), "ldap_group_links", provider, cn);
} }
/**
* Get list of a group’s variables.
*
* <pre><code>GitLab Endpoint: GET /groups/:id/variables</code></pre>
*
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
* @return a list of variables belonging to the specified group
* @throws GitLabApiException if any exception occurs
*/
public List<Variable> getVariables(Object groupIdOrPath) throws GitLabApiException {
return (getVariables(groupIdOrPath, getDefaultPerPage()).all());
}
/**
* Get a list of variables for the specified group in the specified page range.
*
* <pre><code>GitLab Endpoint: GET /groups/:id/variables</code></pre>
*
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
* @param page the page to get
* @param perPage the number of Variable instances per page
* @return a list of variables belonging to the specified group in the specified page range
* @throws GitLabApiException if any exception occurs
*/
public List<Variable> getVariables(Object groupIdOrPath, int page, int perPage) throws GitLabApiException {
Response response = get(Response.Status.OK, getPageQueryParams(page, perPage), "groups", getGroupIdOrPath(groupIdOrPath), "variables");
return (response.readEntity(new GenericType<List<Variable>>() {}));
}
/**
* Get a Pager of variables belonging to the specified group.
*
* <pre><code>GitLab Endpoint: GET /groups/:id/variables</code></pre>
*
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
* @param itemsPerPage the number of Variable instances that will be fetched per page
* @return a Pager of variables belonging to the specified group
* @throws GitLabApiException if any exception occurs
*/
public Pager<Variable> getVariables(Object groupIdOrPath, int itemsPerPage) throws GitLabApiException {
return (new Pager<Variable>(this, Variable.class, itemsPerPage, null, "groups", getGroupIdOrPath(groupIdOrPath), "variables"));
}
/**
* Get a Stream of variables belonging to the specified group.
*
* <pre><code>GitLab Endpoint: GET /groups/:id/variables</code></pre>
*
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
* @return a Stream of variables belonging to the specified group
* @throws GitLabApiException if any exception occurs
*/
public Stream<Variable> getVariablesStream(Object groupIdOrPath) throws GitLabApiException {
return (getVariables(groupIdOrPath, getDefaultPerPage()).stream());
}
/**
* Get the details of a group variable.
*
* <pre><code>GitLab Endpoint: GET /groups/:id/variables/:key</code></pre>
*
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
* @param key the key of an existing variable, required
* @return the Variable instance for the specified group variable
* @throws GitLabApiException if any exception occurs
*/
public Variable getVariable(Object groupIdOrPath, String key) throws GitLabApiException {
Response response = get(Response.Status.OK, null, "groups", getGroupIdOrPath(groupIdOrPath), "variables", key);
return (response.readEntity(Variable.class));
}
/**
* Get the details of a group variable as an Optional instance.
*
* <pre><code>GitLab Endpoint: GET /groups/:id/variables/:key</code></pre>
*
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
* @param key the key of an existing variable, required
* @return the Variable for the specified group variable as an Optional instance
*/
public Optional<Variable> getOptionalVariable(Object groupIdOrPath, String key) {
try {
return (Optional.ofNullable(getVariable(groupIdOrPath, key)));
} catch (GitLabApiException glae) {
return (GitLabApi.createOptionalFromException(glae));
}
}
/**
* Create a new group variable.
*
* <pre><code>GitLab Endpoint: POST /groups/:id/variables</code></pre>
*
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path, required
* @param key the key of a variable; must have no more than 255 characters; only A-Z, a-z, 0-9, and _ are allowed, required
* @param value the value for the variable, required
* @param isProtected whether the variable is protected, optional
* @return a Variable instance with the newly created variable
* @throws GitLabApiException if any exception occurs during execution
*/
public Variable createVariable(Object groupIdOrPath, String key, String value, Boolean isProtected) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm()
.withParam("key", key, true)
.withParam("value", value, true)
.withParam("protected", isProtected);
Response response = post(Response.Status.CREATED, formData, "groups", getGroupIdOrPath(groupIdOrPath), "variables");
return (response.readEntity(Variable.class));
}
/**
* Update a group variable.
*
* <pre><code>GitLab Endpoint: PUT /groups/:id/variables/:key</code></pre>
*
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path, required
* @param key the key of an existing variable, required
* @param value the value for the variable, required
* @param isProtected whether the variable is protected, optional
* @return a Variable instance with the updated variable
* @throws GitLabApiException if any exception occurs during execution
*/
public Variable updateVariable(Object groupIdOrPath, String key, String value, Boolean isProtected) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm()
.withParam("value", value, true)
.withParam("protected", isProtected);
Response response = putWithFormData(Response.Status.CREATED, formData, "groups", getGroupIdOrPath(groupIdOrPath), "variables", key);
return (response.readEntity(Variable.class));
}
/**
* Deletes a group variable.
*
* <pre><code>DELETE /groups/:id/variables/:key</code></pre>
*
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path, required
* @param key the key of an existing variable, required
* @throws GitLabApiException if any exception occurs
*/
public void deleteVariable(Object groupIdOrPath, String key) throws GitLabApiException {
delete(Response.Status.NO_CONTENT, null, "groups", getGroupIdOrPath(groupIdOrPath), "variables", key);
}
} }
...@@ -49,6 +49,7 @@ import org.gitlab4j.api.models.ProjectHook; ...@@ -49,6 +49,7 @@ import org.gitlab4j.api.models.ProjectHook;
import org.gitlab4j.api.models.ProjectUser; import org.gitlab4j.api.models.ProjectUser;
import org.gitlab4j.api.models.PushRules; import org.gitlab4j.api.models.PushRules;
import org.gitlab4j.api.models.Snippet; import org.gitlab4j.api.models.Snippet;
import org.gitlab4j.api.models.Variable;
import org.gitlab4j.api.models.Visibility; import org.gitlab4j.api.models.Visibility;
/** /**
...@@ -2395,4 +2396,152 @@ public class ProjectApi extends AbstractApi implements Constants { ...@@ -2395,4 +2396,152 @@ public class ProjectApi extends AbstractApi implements Constants {
Response response = putUpload(Response.Status.OK, "avatar", avatarFile, "projects", getProjectIdOrPath(projectIdOrPath)); Response response = putUpload(Response.Status.OK, "avatar", avatarFile, "projects", getProjectIdOrPath(projectIdOrPath));
return (response.readEntity(Project.class)); return (response.readEntity(Project.class));
} }
/**
* Get list of a project's variables.
*
* <pre><code>GitLab Endpoint: GET /groups/:id/variables</code></pre>
*
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
* @return a list of variables belonging to the specified project
* @throws GitLabApiException if any exception occurs
*/
public List<Variable> getVariables(Object projectIdOrPath) throws GitLabApiException {
return (getVariables(projectIdOrPath, getDefaultPerPage()).all());
}
/**
* Get a list of variables for the specified project in the specified page range.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/variables</code></pre>
*
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
* @param page the page to get
* @param perPage the number of Variable instances per page
* @return a list of variables belonging to the specified project in the specified page range
* @throws GitLabApiException if any exception occurs
*/
public List<Variable> getVariables(Object projectIdOrPath, int page, int perPage) throws GitLabApiException {
Response response = get(Response.Status.OK, getPageQueryParams(page, perPage), "projects", getProjectIdOrPath(projectIdOrPath), "variables");
return (response.readEntity(new GenericType<List<Variable>>() {}));
}
/**
* Get a Pager of variables belonging to the specified project.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/variables</code></pre>
*
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
* @param itemsPerPage the number of Variable instances that will be fetched per page
* @return a Pager of variables belonging to the specified project
* @throws GitLabApiException if any exception occurs
*/
public Pager<Variable> getVariables(Object projectIdOrPath, int itemsPerPage) throws GitLabApiException {
return (new Pager<Variable>(this, Variable.class, itemsPerPage, null, "projects", getProjectIdOrPath(projectIdOrPath), "variables"));
}
/**
* Get a Stream of variables belonging to the specified project.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/variables</code></pre>
*
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
* @return a Stream of variables belonging to the specified project
* @throws GitLabApiException if any exception occurs
*/
public Stream<Variable> getVariablesStream(Object projectIdOrPath) throws GitLabApiException {
return (getVariables(projectIdOrPath, getDefaultPerPage()).stream());
}
/**
* Get the details of a project variable.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/variables/:key</code></pre>
*
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
* @param key the key of an existing variable, required
* @return the Variable instance for the specified variable
* @throws GitLabApiException if any exception occurs
*/
public Variable getVariable(Object projectIdOrPath, String key) throws GitLabApiException {
Response response = get(Response.Status.OK, null, "projects", getProjectIdOrPath(projectIdOrPath), "variables", key);
return (response.readEntity(Variable.class));
}
/**
* Get the details of a variable as an Optional instance.
*
* <pre><code>GitLab Endpoint: GET /projects/:id/variables/:key</code></pre>
*
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
* @param key the key of an existing variable, required
* @return the Variable for the specified variable as an Optional instance
*/
public Optional<Variable> getOptionalVariable(Object projectIdOrPath, String key) {
try {
return (Optional.ofNullable(getVariable(projectIdOrPath, key)));
} catch (GitLabApiException glae) {
return (GitLabApi.createOptionalFromException(glae));
}
}
/**
* Create a new project variable.
*
* <pre><code>GitLab Endpoint: POST /projects/:id/variables</code></pre>
*
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
* @param key the key of a variable; must have no more than 255 characters; only A-Z, a-z, 0-9, and _ are allowed, required
* @param value the value for the variable, required
* @param isProtected whether the variable is protected, optional
* @param environmentScope the environment_scope of the variable, optional
* @return a Variable instance with the newly created variable
* @throws GitLabApiException if any exception occurs during execution
*/
public Variable createVariable(Object projectIdOrPath, String key, String value, Boolean isProtected, String environmentScope) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm()
.withParam("key", key, true)
.withParam("value", value, true)
.withParam("protected", isProtected)
.withParam("environment_scope", environmentScope);
Response response = post(Response.Status.CREATED, formData, "projects", getProjectIdOrPath(projectIdOrPath), "variables");
return (response.readEntity(Variable.class));
}
/**
* Update a project variable.
*
* <pre><code>GitLab Endpoint: PUT /projects/:id/variables/:key</code></pre>
*
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
* @param key the key of an existing variable, required
* @param value the value for the variable, required
* @param isProtected whether the variable is protected, optional
* @param environmentScope the environment_scope of the variable, optional
* @return a Variable instance with the updated variable
* @throws GitLabApiException if any exception occurs during execution
*/
public Variable updateVariable(Object projectIdOrPath, String key, String value, Boolean isProtected, String environmentScope) throws GitLabApiException {
GitLabApiForm formData = new GitLabApiForm()
.withParam("value", value, true)
.withParam("protected", isProtected)
.withParam("environment_scope", environmentScope);
Response response = putWithFormData(Response.Status.OK, formData, "projects", getProjectIdOrPath(projectIdOrPath), "variables", key);
return (response.readEntity(Variable.class));
}
/**
* Deletes a project variable.
*
* <pre><code>DELETE /projects/:id/variables/:key</code></pre>
*
* @param projectIdOrPath projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance, required
* @param key the key of an existing variable, required
* @throws GitLabApiException if any exception occurs
*/
public void deleteVariable(Object projectIdOrPath, String key) throws GitLabApiException {
delete(Response.Status.NO_CONTENT, null, "projects", getProjectIdOrPath(projectIdOrPath), "variables", key);
}
} }
...@@ -4,12 +4,19 @@ import javax.xml.bind.annotation.XmlAccessType; ...@@ -4,12 +4,19 @@ import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlRootElement;
import org.gitlab4j.api.utils.JacksonJson;
import com.fasterxml.jackson.annotation.JsonProperty;
@XmlRootElement @XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD) @XmlAccessorType(XmlAccessType.FIELD)
public class Variable { public class Variable {
private String key; private String key;
private String value; private String value;
@JsonProperty("protected")
private Boolean isProtected;
private String environmentScope;
public String getKey() { public String getKey() {
return key; return key;
...@@ -26,4 +33,25 @@ public class Variable { ...@@ -26,4 +33,25 @@ public class Variable {
public void setValue(String value) { public void setValue(String value) {
this.value = value; this.value = value;
} }
public Boolean getProtected() {
return isProtected;
}
public void setProtected(Boolean isProtected) {
this.isProtected = isProtected;
}
public String getEnvironmentScope() {
return environmentScope;
}
public void setEnvironmentScope(String environmentScope) {
this.environmentScope = environmentScope;
}
@Override
public String toString() {
return (JacksonJson.toJsonString(this));
}
} }
...@@ -79,6 +79,7 @@ import org.gitlab4j.api.models.SystemHook; ...@@ -79,6 +79,7 @@ import org.gitlab4j.api.models.SystemHook;
import org.gitlab4j.api.models.Tag; import org.gitlab4j.api.models.Tag;
import org.gitlab4j.api.models.TreeItem; import org.gitlab4j.api.models.TreeItem;
import org.gitlab4j.api.models.User; import org.gitlab4j.api.models.User;
import org.gitlab4j.api.models.Variable;
import org.gitlab4j.api.services.JiraService; import org.gitlab4j.api.services.JiraService;
import org.gitlab4j.api.services.SlackService; import org.gitlab4j.api.services.SlackService;
import org.gitlab4j.api.systemhooks.ProjectSystemHookEvent; import org.gitlab4j.api.systemhooks.ProjectSystemHookEvent;
...@@ -243,6 +244,12 @@ public class TestGitLabApiBeans { ...@@ -243,6 +244,12 @@ public class TestGitLabApiBeans {
assertTrue(compareJson(pipelineSchedule, "pipeline-schedule.json")); assertTrue(compareJson(pipelineSchedule, "pipeline-schedule.json"));
} }
@Test
public void testProjectVariables() throws Exception {
List<Variable> variables = unmarshalResourceList(Variable.class, "project-variables.json");
assertTrue(compareJson(variables, "project-variables.json"));
}
@Test @Test
public void testJob() throws Exception { public void testJob() throws Exception {
Job job = unmarshalResource(Job.class, "job.json"); Job job = unmarshalResource(Job.class, "job.json");
......
...@@ -26,6 +26,7 @@ package org.gitlab4j.api; ...@@ -26,6 +26,7 @@ package org.gitlab4j.api;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeTrue; import static org.junit.Assume.assumeTrue;
...@@ -33,6 +34,7 @@ import java.util.Arrays; ...@@ -33,6 +34,7 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Stream;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
...@@ -40,6 +42,7 @@ import org.gitlab4j.api.GitLabApi.ApiVersion; ...@@ -40,6 +42,7 @@ import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.AccessLevel; import org.gitlab4j.api.models.AccessLevel;
import org.gitlab4j.api.models.Group; import org.gitlab4j.api.models.Group;
import org.gitlab4j.api.models.Project; import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.Variable;
import org.gitlab4j.api.models.Visibility; import org.gitlab4j.api.models.Visibility;
import org.junit.AfterClass; import org.junit.AfterClass;
import org.junit.Before; import org.junit.Before;
...@@ -87,6 +90,7 @@ public class TestProjectApi { ...@@ -87,6 +90,7 @@ public class TestProjectApi {
private static final String TEST_PROJECT_NAME_2 = "test-gitlab4j-create-project-2"; 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 final String TEST_PROJECT_NAME_UPDATE = "test-gitlab4j-create-project-update";
private static final String TEST_XFER_PROJECT_NAME = "test-gitlab4j-xfer-project"; private static final String TEST_XFER_PROJECT_NAME = "test-gitlab4j-xfer-project";
private static final String TEST_VARIABLE_KEY_PREFIX = "TEST_VARIABLE_KEY_";
private static GitLabApi gitLabApi; private static GitLabApi gitLabApi;
public TestProjectApi() { public TestProjectApi() {
...@@ -150,7 +154,18 @@ public class TestProjectApi { ...@@ -150,7 +154,18 @@ public class TestProjectApi {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME); Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
List<Group> groups = gitLabApi.getGroupApi().getGroups(TEST_GROUP); List<Group> groups = gitLabApi.getGroupApi().getGroups(TEST_GROUP);
gitLabApi.getProjectApi().unshareProject(project.getId(), groups.get(0).getId()); gitLabApi.getProjectApi().unshareProject(project.getId(), groups.get(0).getId());
} catch (GitLabApiException ignore) {}
List<Variable> variables = gitLabApi.getProjectApi().getVariables(project);
if (variables != null) {
for (Variable variable : variables) {
if (variable.getKey().startsWith(TEST_VARIABLE_KEY_PREFIX)) {
gitLabApi.getProjectApi().deleteVariable(project, variable.getKey());
}
}
}
} catch (GitLabApiException ignore) {
}
} }
if (TEST_GROUP != null && TEST_GROUP_PROJECT != null) { if (TEST_GROUP != null && TEST_GROUP_PROJECT != null) {
...@@ -589,4 +604,44 @@ public class TestProjectApi { ...@@ -589,4 +604,44 @@ public class TestProjectApi {
Project transferedProject = gitLabApi.getProjectApi().transferProject(projectToTransfer, TEST_XFER_NAMESPACE); Project transferedProject = gitLabApi.getProjectApi().transferProject(projectToTransfer, TEST_XFER_NAMESPACE);
assertNotNull(transferedProject); assertNotNull(transferedProject);
} }
@Test
public void testVariables() throws GitLabApiException {
Project project = gitLabApi.getProjectApi().getProject(TEST_NAMESPACE, TEST_PROJECT_NAME);
assertNotNull(project);
String key = TEST_VARIABLE_KEY_PREFIX + TestUtils.getRandomInt() + "_" + TestUtils.getRandomInt();
String value = "TEST_VARIABLE_VALUE_" + TestUtils.getRandomInt() + "_" + TestUtils.getRandomInt();
Variable variable = gitLabApi.getProjectApi().createVariable(project, key, value, null, null);
assertNotNull(variable);
assertEquals(key, variable.getKey());
assertEquals(value, variable.getValue());
Stream<Variable> variables = gitLabApi.getProjectApi().getVariablesStream(project);
assertNotNull(variables);
Variable matchingVariable = variables.filter(v -> v.getKey().equals(key)).findAny().orElse(null);
assertNotNull(matchingVariable);
assertEquals(key, matchingVariable.getKey());
assertEquals(value, matchingVariable.getValue());
assertFalse(matchingVariable.getProtected());
assertNull(matchingVariable.getEnvironmentScope());
gitLabApi.getProjectApi().updateVariable(project, key, "NONE", true, "DEV");
variable = gitLabApi.getProjectApi().getVariable(project, key);
assertNotNull(variable);
assertEquals(key, variable.getKey());
assertEquals("NONE", variable.getValue());
assertTrue(variable.getProtected());
gitLabApi.getProjectApi().deleteVariable(project, key);
variables = gitLabApi.getProjectApi().getVariablesStream(project);
assertNotNull(variables);
matchingVariable = variables.filter(v -> v.getKey().equals(key)).findAny().orElse(null);
assertNull(matchingVariable);
}
} }
...@@ -62,4 +62,23 @@ public class TestUtils { ...@@ -62,4 +62,23 @@ public class TestUtils {
public static final String getProperty(String key) { public static final String getProperty(String key) {
return (testProperties.getProperty(key)); return (testProperties.getProperty(key));
} }
/**
* Get a random integer between 1 and the specified value (inclusive).
*
* @param maxValue the maximum value to return
* @return a random integer between 1 and the specified value (inclusive)
*/
public static final int getRandomInt(int maxValue) {
return ((int)(Math.random() * maxValue + 1));
}
/**
* Get a random integer between 1 and Integer.MAX_VALUE (inclusive).
*
* @return a random integer between 1 and Integer.MAX_VALUE (inclusive)
*/
public static final int getRandomInt() {
return (getRandomInt(Integer.MAX_VALUE));
}
} }
[
{
"key": "TEST_VARIABLE_1",
"value": "TEST_1",
"protected": true,
"environment_scope": "*"
},
{
"key": "TEST_VARIABLE_2",
"value": "TEST_2",
"protected": false
},
{
"key": "TEST_VARIABLE_3",
"value": "TEST_3",
"environment_scope": "DEV"
},
{
"key": "TEST_VARIABLE_4",
"value": "TEST_4"
}
]
\ No newline at end of file
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