Skip to content

Commit 856d7e3

Browse files
committed
Merge remote-tracking branch 'origin/main' into 6.x
2 parents da358ec + 0028d7e commit 856d7e3

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

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

+29
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.gitlab4j.api.GitLabApi.ApiVersion;
1414
import org.gitlab4j.api.models.Group;
1515
import org.gitlab4j.api.models.Label;
16+
import org.gitlab4j.api.models.Namespace;
1617
import org.gitlab4j.api.models.Project;
1718
import org.gitlab4j.api.models.User;
1819
import org.gitlab4j.api.utils.UrlEncoder;
@@ -169,6 +170,34 @@ public Object getLabelIdOrName(Object obj) throws GitLabApiException {
169170
}
170171
}
171172

173+
public Object getNamespaceIdOrPath(Object obj) throws GitLabApiException {
174+
175+
if (obj == null) {
176+
throw (new RuntimeException("Cannot determine ID or path from null object"));
177+
} else if (obj instanceof Long) {
178+
return (obj);
179+
} else if (obj instanceof String) {
180+
return (urlEncode(((String) obj).trim()));
181+
} else if (obj instanceof Namespace) {
182+
183+
Long id = ((Namespace) obj).getId();
184+
if (id != null && id.longValue() > 0) {
185+
return (id);
186+
}
187+
188+
String path = ((Namespace) obj).getFullPath();
189+
if (path != null && path.trim().length() > 0) {
190+
return (urlEncode(path.trim()));
191+
}
192+
193+
throw (new RuntimeException("Cannot determine ID or path from provided Namespace instance"));
194+
195+
} else {
196+
throw (new RuntimeException("Cannot determine ID or path from provided " + obj.getClass().getSimpleName() +
197+
" instance, must be Long, String, or a Namespace instance"));
198+
}
199+
}
200+
172201
protected ApiVersion getApiVersion() {
173202
return (gitLabApi.getApiVersion());
174203
}

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

+32
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.gitlab4j.api;
22

33
import java.util.List;
4+
import java.util.Optional;
45
import java.util.stream.Stream;
56

67
import jakarta.ws.rs.core.GenericType;
@@ -130,4 +131,35 @@ public Pager<Namespace> findNamespaces(String query, int itemsPerPage) throws Gi
130131
public Stream<Namespace> findNamespacesStream(String query) throws GitLabApiException {
131132
return (findNamespaces(query, getDefaultPerPage()).stream());
132133
}
134+
135+
/**
136+
* Get all details of a namespace.
137+
*
138+
* <pre><code>GitLab Endpoint: GET /namespaces/:id</code></pre>
139+
*
140+
* @param namespaceIdOrPath the namespace ID, path of the namespace, or a Namespace instance holding the namespace ID or path
141+
* @return the Namespace instance for the specified path
142+
* @throws GitLabApiException if any exception occurs
143+
*/
144+
145+
public Namespace getNamespace(Object namespaceIdOrPath) throws GitLabApiException {
146+
Response response = get(Response.Status.OK, null, "namespaces", getNamespaceIdOrPath(namespaceIdOrPath));
147+
return (response.readEntity(Namespace .class));
148+
}
149+
150+
/**
151+
* Get all details of a namespace as an Optional instance.
152+
*
153+
* <pre><code>GitLab Endpoint: GET /namespaces/:id</code></pre>
154+
*
155+
* @param namespaceIdOrPath the namespace ID, path of the namespace, or a Namespace instance holding the namespace ID or path
156+
* @return the Group for the specified group path as an Optional instance
157+
*/
158+
public Optional<Namespace> getOptionalNamespace(Object namespaceIdOrPath) {
159+
try {
160+
return (Optional.ofNullable(getNamespace(namespaceIdOrPath)));
161+
} catch (GitLabApiException glae) {
162+
return (GitLabApi.createOptionalFromException(glae));
163+
}
164+
}
133165
}

0 commit comments

Comments
 (0)