Skip to content

Commit 18d6902

Browse files
authored
Merge pull request #770 from BlackYoup/group-custom-attributes
Groups: add support for custom attributes
2 parents 0287ee5 + e311cac commit 18d6902

File tree

3 files changed

+346
-0
lines changed

3 files changed

+346
-0
lines changed

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

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.File;
44
import java.util.Date;
55
import java.util.List;
6+
import java.util.Objects;
67
import java.util.Optional;
78
import java.util.stream.Stream;
89

@@ -15,6 +16,7 @@
1516
import org.gitlab4j.api.models.AccessRequest;
1617
import org.gitlab4j.api.models.AuditEvent;
1718
import org.gitlab4j.api.models.Badge;
19+
import org.gitlab4j.api.models.CustomAttribute;
1820
import org.gitlab4j.api.models.Group;
1921
import org.gitlab4j.api.models.GroupFilter;
2022
import org.gitlab4j.api.models.GroupParams;
@@ -1725,4 +1727,123 @@ public void unshareGroup(Object groupIdOrPath, Integer sharedWithGroupId) throws
17251727
delete(Response.Status.NO_CONTENT, null,
17261728
"groups", getGroupIdOrPath(groupIdOrPath), "share", sharedWithGroupId);
17271729
}
1730+
1731+
/**
1732+
* Get all custom attributes for the specified group.
1733+
*
1734+
* <pre><code>GitLab Endpoint: GET /groups/:id/custom_attributes</code></pre>
1735+
*
1736+
* @param groupIdOrPath the group in the form of an Integer(ID), String(path), or Group instance
1737+
* @return a list of group's CustomAttributes
1738+
* @throws GitLabApiException if any exception occurs
1739+
*/
1740+
public List<CustomAttribute> getCustomAttributes(final Object groupIdOrPath) throws GitLabApiException {
1741+
return (getCustomAttributes(groupIdOrPath, getDefaultPerPage()).all());
1742+
}
1743+
1744+
/**
1745+
* Get a Pager of custom attributes for the specified group.
1746+
*
1747+
* <pre><code>GitLab Endpoint: GET /groups/:id/custom_attributes</code></pre>
1748+
*
1749+
* @param groupIdOrPath the group in the form of an Integer(ID), String(path), or Group instance
1750+
* @param itemsPerPage the number of items per page
1751+
* @return a Pager of group's custom attributes
1752+
* @throws GitLabApiException if any exception occurs
1753+
*/
1754+
public Pager<CustomAttribute> getCustomAttributes(final Object groupIdOrPath, int itemsPerPage) throws GitLabApiException {
1755+
return (new Pager<CustomAttribute>(this, CustomAttribute.class, itemsPerPage, null,
1756+
"groups", getGroupIdOrPath(groupIdOrPath), "custom_attributes"));
1757+
}
1758+
1759+
/**
1760+
* Get a Stream of all custom attributes for the specified group.
1761+
*
1762+
* <pre><code>GitLab Endpoint: GET /groups/:id/custom_attributes</code></pre>
1763+
*
1764+
* @param groupIdOrPath the group in the form of an Integer(ID), String(path), or Group instance
1765+
* @return a Stream of group's custom attributes
1766+
* @throws GitLabApiException if any exception occurs
1767+
*/
1768+
public Stream<CustomAttribute> getCustomAttributesStream(final Object groupIdOrPath) throws GitLabApiException {
1769+
return (getCustomAttributes(groupIdOrPath, getDefaultPerPage()).stream());
1770+
}
1771+
1772+
/**
1773+
* Get a single custom attribute for the specified group.
1774+
*
1775+
* <pre><code>GitLab Endpoint: GET /groups/:id/custom_attributes/:key</code></pre>
1776+
*
1777+
* @param groupIdOrPath the group in the form of an Integer(ID), String(path), or Group instance
1778+
* @param key the key for the custom attribute
1779+
* @return a CustomAttribute instance for the specified key
1780+
* @throws GitLabApiException if any exception occurs
1781+
*/
1782+
public CustomAttribute getCustomAttribute(final Object groupIdOrPath, final String key) throws GitLabApiException {
1783+
Response response = get(Response.Status.OK, null,
1784+
"groups", getGroupIdOrPath(groupIdOrPath), "custom_attributes", key);
1785+
return (response.readEntity(CustomAttribute.class));
1786+
}
1787+
1788+
/**
1789+
* Get an Optional instance with the value for a single custom attribute for the specified group.
1790+
*
1791+
* <pre><code>GitLab Endpoint: GET /groups/:id/custom_attributes/:key</code></pre>
1792+
*
1793+
* @param groupIdOrPath the group in the form of an Integer(ID), String(path), or Group instance, required
1794+
* @param key the key for the custom attribute, required
1795+
* @return an Optional instance with the value for a single custom attribute for the specified group
1796+
*/
1797+
public Optional<CustomAttribute> geOptionalCustomAttribute(final Object groupIdOrPath, final String key) {
1798+
try {
1799+
return (Optional.ofNullable(getCustomAttribute(groupIdOrPath, key)));
1800+
} catch (GitLabApiException glae) {
1801+
return (GitLabApi.createOptionalFromException(glae));
1802+
}
1803+
}
1804+
1805+
/**
1806+
* Set a custom attribute for the specified group. The attribute will be updated if it already exists,
1807+
* or newly created otherwise.
1808+
*
1809+
* <pre><code>GitLab Endpoint: PUT /groups/:id/custom_attributes/:key</code></pre>
1810+
*
1811+
* @param groupIdOrPath the group in the form of an Integer(ID), String(path), or Group instance
1812+
* @param key the key for the custom attribute
1813+
* @param value the value for the customAttribute
1814+
* @return a CustomAttribute instance for the updated or created custom attribute
1815+
* @throws GitLabApiException if any exception occurs
1816+
*/
1817+
public CustomAttribute setCustomAttribute(final Object groupIdOrPath, final String key, final String value) throws GitLabApiException {
1818+
1819+
if (Objects.isNull(key) || key.trim().isEmpty()) {
1820+
throw new IllegalArgumentException("Key cannot be null or empty");
1821+
}
1822+
if (Objects.isNull(value) || value.trim().isEmpty()) {
1823+
throw new IllegalArgumentException("Value cannot be null or empty");
1824+
}
1825+
1826+
GitLabApiForm formData = new GitLabApiForm().withParam("value", value);
1827+
Response response = putWithFormData(Response.Status.OK, formData,
1828+
"groups", getGroupIdOrPath(groupIdOrPath), "custom_attributes", key);
1829+
return (response.readEntity(CustomAttribute.class));
1830+
}
1831+
1832+
/**
1833+
* Delete a custom attribute for the specified group.
1834+
*
1835+
* <pre><code>GitLab Endpoint: DELETE /groups/:id/custom_attributes/:key</code></pre>
1836+
*
1837+
* @param groupIdOrPath the group in the form of an Integer(ID), String(path), or Group instance
1838+
* @param key the key of the custom attribute to delete
1839+
* @throws GitLabApiException if any exception occurs
1840+
*/
1841+
public void deleteCustomAttribute(final Object groupIdOrPath, final String key) throws GitLabApiException {
1842+
1843+
if (Objects.isNull(key) || key.trim().isEmpty()) {
1844+
throw new IllegalArgumentException("Key can't be null or empty");
1845+
}
1846+
1847+
delete(Response.Status.OK, null, "groups", getGroupIdOrPath(groupIdOrPath), "custom_attributes", key);
1848+
}
17281849
}

src/test/java/org/gitlab4j/api/AbstractIntegrationTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.WeakHashMap;
66

77
import org.gitlab4j.api.GitLabApi.ApiVersion;
8+
import org.gitlab4j.api.models.Group;
89
import org.gitlab4j.api.models.Project;
910
import org.gitlab4j.api.models.User;
1011

@@ -16,6 +17,7 @@
1617
* TEST_PRIVATE_TOKEN
1718
* TEST_NAMESPACE
1819
* TEST_PROJECT_NAME
20+
* TEST_GROUP
1921
*/
2022
public class AbstractIntegrationTest implements PropertyConstants {
2123

@@ -33,6 +35,7 @@ public class AbstractIntegrationTest implements PropertyConstants {
3335
protected static class BaseTestResources {
3436
protected GitLabApi gitLabApi;
3537
protected Project testProject;
38+
protected Group testGroup;
3639

3740
protected BaseTestResources(GitLabApi gitLabApi) {
3841
this.gitLabApi = gitLabApi;
@@ -80,6 +83,10 @@ protected static GitLabApi baseTestSetup() {
8083
problems += "TEST_PROJECT_NAME cannot be empty\n";
8184
}
8285

86+
if (TEST_GROUP == null || TEST_GROUP.trim().isEmpty()) {
87+
problems += "TEST_GROUP cannot be empty\n";
88+
}
89+
8390
if (problems.isEmpty()) {
8491
try {
8592
GitLabApi gitLabApi = new GitLabApi(ApiVersion.V4, TEST_HOST_URL, TEST_PRIVATE_TOKEN);
@@ -123,6 +130,34 @@ protected static Project getTestProject() {
123130
}
124131
}
125132

133+
/**
134+
* Get the test Group instance for the calling test class.
135+
*
136+
* @return the test Group instance for the calling test class
137+
*/
138+
protected static Group getTestGroup() {
139+
140+
Throwable t = new Throwable();
141+
StackTraceElement directCaller = t.getStackTrace()[1];
142+
String callingClassName = directCaller.getClassName();
143+
BaseTestResources baseResources = baseTestResourcesMap.get(callingClassName);
144+
if (baseResources == null || baseResources.gitLabApi == null) {
145+
System.err.println("Problems fetching test Project instance: GitLabApi instance is null");
146+
return (null);
147+
} else if (baseResources.testGroup != null) {
148+
return (baseResources.testGroup);
149+
}
150+
151+
try {
152+
Group testGroup = (baseResources.gitLabApi.getGroupApi().getGroup(TEST_GROUP));
153+
baseResources.testGroup = testGroup;
154+
return (testGroup);
155+
} catch (Exception e) {
156+
System.err.println("Problems fetching test Project instance: " + e.getMessage());
157+
return (null);
158+
}
159+
}
160+
126161
/**
127162
* Get the current user (the testing user).
128163
*

0 commit comments

Comments
 (0)