Skip to content

Commit 74a144b

Browse files
committed
Added getGroupIdOrPath().
1 parent 4b18991 commit 74a144b

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

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

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import javax.ws.rs.core.StreamingOutput;
1212

1313
import org.gitlab4j.api.GitLabApi.ApiVersion;
14+
import org.gitlab4j.api.models.Group;
1415
import org.gitlab4j.api.models.Project;
1516

1617
/**
@@ -56,8 +57,44 @@ public Object getProjectIdOrPath(Object obj) throws GitLabApiException {
5657

5758
} else {
5859

59-
throw (new RuntimeException("Cannot determine ID or path from provided " + obj.getClass().getSimpleName() +
60-
" instance, must be Integer, String, or Project instance"));
60+
throw (new RuntimeException("Cannot determine ID or path from provided " + obj.getClass().getSimpleName() +
61+
" instance, must be Integer, String, or a Project instance"));
62+
}
63+
}
64+
65+
/**
66+
* Returns the group ID or path from the provided Integer, String, or Group instance.
67+
*
68+
* @param obj the object to determine the ID or path from
69+
* @return the group ID or path from the provided Integer, String, or Group instance
70+
* @throws GitLabApiException if any exception occurs during execution
71+
*/
72+
public Object getGroupIdOrPath(Object obj) throws GitLabApiException {
73+
74+
if (obj == null) {
75+
throw (new RuntimeException("Cannot determine ID or path from null object"));
76+
} else if (obj instanceof Integer) {
77+
return (obj);
78+
} else if (obj instanceof String) {
79+
return (urlEncode(((String) obj).trim()));
80+
} else if (obj instanceof Group) {
81+
82+
Integer id = ((Group) obj).getId();
83+
if (id != null && id.intValue() > 0) {
84+
return (id);
85+
}
86+
87+
String path = ((Group) obj).getFullPath();
88+
if (path != null && path.trim().length() > 0) {
89+
return (urlEncode(path.trim()));
90+
}
91+
92+
throw (new RuntimeException("Cannot determine ID or path from provided Group instance"));
93+
94+
} else {
95+
96+
throw (new RuntimeException("Cannot determine ID or path from provided " + obj.getClass().getSimpleName() +
97+
" instance, must be Integer, String, or a Group instance"));
6198
}
6299
}
63100

0 commit comments

Comments
 (0)