Skip to content

Commit 1b3763e

Browse files
Add SAML group sync to GroupApi (gitlab4j#1038)
1 parent bcc9a16 commit 1b3763e

File tree

4 files changed

+115
-3
lines changed

4 files changed

+115
-3
lines changed

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

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.gitlab4j.api.models.LdapGroupLink;
2929
import org.gitlab4j.api.models.Member;
3030
import org.gitlab4j.api.models.Project;
31+
import org.gitlab4j.api.models.SamlGroupLink;
3132
import org.gitlab4j.api.models.Variable;
3233
import org.gitlab4j.api.models.Visibility;
3334
import org.gitlab4j.api.utils.ISO8601;
@@ -1183,9 +1184,9 @@ public void ldapSync(Object groupIdOrPath) throws GitLabApiException {
11831184

11841185
/**
11851186
* Get the list of LDAP group links.
1186-
*
1187+
*
11871188
* <pre><code>GitLab Endpoint: GET /groups/:id/ldap_group_links</code></pre>
1188-
*
1189+
*
11891190
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
11901191
* @return a list of LDAP group links
11911192
* @throws GitLabApiException if any exception occurs
@@ -1275,6 +1276,74 @@ public void deleteLdapGroupLink(Object groupIdOrPath, String cn, String provider
12751276
delete(Response.Status.OK, null, "groups", getGroupIdOrPath(groupIdOrPath), "ldap_group_links", provider, cn);
12761277
}
12771278

1279+
/**
1280+
* Get the list of SAML group links.
1281+
*
1282+
* <pre><code>GitLab Endpoint: GET /groups/:id/saml_group_links</code></pre>
1283+
*
1284+
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
1285+
* @return a list of SAML group links
1286+
* @throws GitLabApiException if any exception occurs
1287+
*/
1288+
public List<SamlGroupLink> getSamlGroupLinks(Object groupIdOrPath) throws GitLabApiException {
1289+
Response response = get(Response.Status.OK, null, "groups", getGroupIdOrPath(groupIdOrPath), "saml_group_links");
1290+
return (response.readEntity(new GenericType<List<SamlGroupLink>>() {}));
1291+
}
1292+
1293+
/**
1294+
* Adds an SAML group link.
1295+
*
1296+
* <pre><code>GitLab Endpoint: POST /groups/:id/saml_group_links</code></pre>
1297+
*
1298+
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
1299+
* @param samlGroupName the name of the SAML group
1300+
* @param groupAccess the minimum access level for members of the SAML group
1301+
* @throws GitLabApiException if any exception occurs
1302+
*/
1303+
public void addSamlGroupLink(Object groupIdOrPath, String samlGroupName, AccessLevel groupAccess) throws GitLabApiException {
1304+
1305+
if (groupAccess == null) {
1306+
throw new RuntimeException("groupAccess cannot be null or empty");
1307+
}
1308+
1309+
addSamlGroupLink(groupIdOrPath, samlGroupName, groupAccess.toValue());
1310+
}
1311+
1312+
/**
1313+
* Adds an SAML group link.
1314+
*
1315+
* <pre><code>GitLab Endpoint: POST /groups/:id/saml_group_links</code></pre>
1316+
*
1317+
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
1318+
* @param samlGroupName the name of the SAML group
1319+
* @param groupAccess the minimum access level for members of the SAML group
1320+
* @throws GitLabApiException if any exception occurs
1321+
*/
1322+
public void addSamlGroupLink(Object groupIdOrPath, String samlGroupName, Integer groupAccess) throws GitLabApiException {
1323+
GitLabApiForm formData = new GitLabApiForm()
1324+
.withParam("saml_group_name", samlGroupName, true)
1325+
.withParam("access_level", groupAccess, true);
1326+
post(Response.Status.CREATED, formData, "groups", getGroupIdOrPath(groupIdOrPath), "saml_group_links");
1327+
}
1328+
1329+
/**
1330+
* Deletes an SAML group link.
1331+
*
1332+
* <pre><code>GitLab Endpoint: DELETE /groups/:id/saml_group_links/:cn</code></pre>
1333+
*
1334+
* @param groupIdOrPath the group ID, path of the group, or a Group instance holding the group ID or path
1335+
* @param samlGroupName the name of the SAML group to delete
1336+
* @throws GitLabApiException if any exception occurs
1337+
*/
1338+
public void deleteSamlGroupLink(Object groupIdOrPath, String samlGroupName) throws GitLabApiException {
1339+
1340+
if (samlGroupName == null || samlGroupName.trim().isEmpty()) {
1341+
throw new RuntimeException("samlGroupName cannot be null or empty");
1342+
}
1343+
1344+
delete(Response.Status.OK, null, "groups", getGroupIdOrPath(groupIdOrPath), "saml_group_links", samlGroupName);
1345+
}
1346+
12781347
/**
12791348
* Get list of a group’s variables.
12801349
*
@@ -1961,7 +2030,7 @@ public void deleteCustomAttribute(final Object groupIdOrPath, final String key)
19612030

19622031
delete(Response.Status.OK, null, "groups", getGroupIdOrPath(groupIdOrPath), "custom_attributes", key);
19632032
}
1964-
2033+
19652034
/**
19662035
* Lists group iterations.
19672036
*
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
package org.gitlab4j.api.models;
3+
4+
import org.gitlab4j.api.utils.JacksonJson;
5+
6+
public class SamlGroupLink {
7+
8+
private String name;
9+
10+
private AccessLevel accessLevel;
11+
12+
public String getName() {
13+
return name;
14+
}
15+
16+
public void setName(String aName) {
17+
this.name = aName;
18+
}
19+
20+
public AccessLevel getAccessLevel() {
21+
return accessLevel;
22+
}
23+
24+
public void setAccessLevel(AccessLevel aAccessLevel) {
25+
accessLevel = aAccessLevel;
26+
}
27+
28+
@Override
29+
public String toString() {
30+
return (JacksonJson.toJsonString(this));
31+
}
32+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
import org.gitlab4j.api.models.RepositoryFile;
118118
import org.gitlab4j.api.models.Runner;
119119
import org.gitlab4j.api.models.RunnerDetail;
120+
import org.gitlab4j.api.models.SamlGroupLink;
120121
import org.gitlab4j.api.models.SearchBlob;
121122
import org.gitlab4j.api.models.Snippet;
122123
import org.gitlab4j.api.models.SshKey;
@@ -808,6 +809,12 @@ public void testLdapGroupLink() throws Exception {
808809
assertTrue(compareJson(link, "ldap-group-link.json"));
809810
}
810811

812+
@Test
813+
public void testSamlGroupLink() throws Exception {
814+
SamlGroupLink link = unmarshalResource(SamlGroupLink.class, "saml-group-link.json");
815+
assertTrue(compareJson(link, "saml-group-link.json"));
816+
}
817+
811818
@Test
812819
public void testSearchBlobs() throws Exception {
813820
List<SearchBlob> searchResults = unmarshalResourceList(SearchBlob.class, "wiki-blobs.json");
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"access_level": 30,
3+
"name": "A_GITLAB_DEVELOPER"
4+
}

0 commit comments

Comments
 (0)