|
3 | 3 | import java.io.File; |
4 | 4 | import java.util.Date; |
5 | 5 | import java.util.List; |
| 6 | +import java.util.Objects; |
6 | 7 | import java.util.Optional; |
7 | 8 | import java.util.stream.Stream; |
8 | 9 |
|
|
15 | 16 | import org.gitlab4j.api.models.AccessRequest; |
16 | 17 | import org.gitlab4j.api.models.AuditEvent; |
17 | 18 | import org.gitlab4j.api.models.Badge; |
| 19 | +import org.gitlab4j.api.models.CustomAttribute; |
18 | 20 | import org.gitlab4j.api.models.Group; |
19 | 21 | import org.gitlab4j.api.models.GroupFilter; |
20 | 22 | import org.gitlab4j.api.models.GroupParams; |
@@ -1725,4 +1727,123 @@ public void unshareGroup(Object groupIdOrPath, Integer sharedWithGroupId) throws |
1725 | 1727 | delete(Response.Status.NO_CONTENT, null, |
1726 | 1728 | "groups", getGroupIdOrPath(groupIdOrPath), "share", sharedWithGroupId); |
1727 | 1729 | } |
| 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 | + } |
1728 | 1849 | } |
0 commit comments