Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NamespaceApi.getNamespace(idOrPath) using /namespaces/:id #1079

Merged
merged 2 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/main/java/org/gitlab4j/api/AbstractApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.gitlab4j.api.GitLabApi.ApiVersion;
import org.gitlab4j.api.models.Group;
import org.gitlab4j.api.models.Label;
import org.gitlab4j.api.models.Namespace;
import org.gitlab4j.api.models.Project;
import org.gitlab4j.api.models.User;
import org.gitlab4j.api.utils.UrlEncoder;
Expand Down Expand Up @@ -169,6 +170,34 @@ public Object getLabelIdOrName(Object obj) throws GitLabApiException {
}
}

public Object getNamespaceIdOrPath(Object obj) throws GitLabApiException {

if (obj == null) {
throw (new RuntimeException("Cannot determine ID or path from null object"));
} else if (obj instanceof Long) {
return (obj);
} else if (obj instanceof String) {
return (urlEncode(((String) obj).trim()));
} else if (obj instanceof Namespace) {

Long id = ((Namespace) obj).getId();
if (id != null && id.longValue() > 0) {
return (id);
}

String path = ((Namespace) obj).getFullPath();
if (path != null && path.trim().length() > 0) {
return (urlEncode(path.trim()));
}

throw (new RuntimeException("Cannot determine ID or path from provided Namespace instance"));

} else {
throw (new RuntimeException("Cannot determine ID or path from provided " + obj.getClass().getSimpleName() +
" instance, must be Long, String, or a Namespace instance"));
}
}

protected ApiVersion getApiVersion() {
return (gitLabApi.getApiVersion());
}
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/org/gitlab4j/api/NamespaceApi.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.gitlab4j.api;

import java.util.List;
import java.util.Optional;
import java.util.stream.Stream;

import javax.ws.rs.core.GenericType;
Expand Down Expand Up @@ -130,4 +131,35 @@ public Pager<Namespace> findNamespaces(String query, int itemsPerPage) throws Gi
public Stream<Namespace> findNamespacesStream(String query) throws GitLabApiException {
return (findNamespaces(query, getDefaultPerPage()).stream());
}

/**
* Get all details of a namespace.
*
* <pre><code>GitLab Endpoint: GET /namespaces/:id</code></pre>
*
* @param namespaceIdOrPath the namespace ID, path of the namespace, or a Namespace instance holding the namespace ID or path
* @return the Namespace instance for the specified path
* @throws GitLabApiException if any exception occurs
*/

public Namespace getNamespace(Object namespaceIdOrPath) throws GitLabApiException {
Response response = get(Response.Status.OK, null, "namespaces", getNamespaceIdOrPath(namespaceIdOrPath));
return (response.readEntity(Namespace .class));
}

/**
* Get all details of a namespace as an Optional instance.
*
* <pre><code>GitLab Endpoint: GET /namespaces/:id</code></pre>
*
* @param namespaceIdOrPath the namespace ID, path of the namespace, or a Namespace instance holding the namespace ID or path
* @return the Group for the specified group path as an Optional instance
*/
public Optional<Namespace> getOptionalNamespace(Object namespaceIdOrPath) {
try {
return (Optional.ofNullable(getNamespace(namespaceIdOrPath)));
} catch (GitLabApiException glae) {
return (GitLabApi.createOptionalFromException(glae));
}
}
}
Loading