Skip to content

Commit f6965d3

Browse files
committed
Added updateMember() methods (#103).
1 parent 2aa68d2 commit f6965d3

File tree

2 files changed

+252
-12
lines changed

2 files changed

+252
-12
lines changed

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

Lines changed: 126 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package org.gitlab4j.api;
22

3+
import java.util.Date;
34
import java.util.List;
45

56
import javax.ws.rs.core.Form;
67
import javax.ws.rs.core.GenericType;
78
import javax.ws.rs.core.Response;
89

910
import org.gitlab4j.api.GitLabApi.ApiVersion;
11+
import org.gitlab4j.api.models.AccessLevel;
1012
import org.gitlab4j.api.models.Group;
1113
import org.gitlab4j.api.models.Member;
1214
import org.gitlab4j.api.models.Project;
@@ -359,32 +361,146 @@ public Member getMember(int groupId, int userId) throws GitLabApiException {
359361
*
360362
* POST /groups/:id/members
361363
*
362-
* @param groupId the project ID to add the member to
363-
* @param userId the user ID of the member to add
364-
* @param accessLevel the access level for the new member
364+
* @param groupId the project ID to add the member to, required
365+
* @param userId the user ID of the member to add, required
366+
* @param accessLevel the access level for the new member, required
365367
* @return a Member instance for the added user
366368
* @throws GitLabApiException if any exception occurs
367369
*/
368370
public Member addMember(Integer groupId, Integer userId, Integer accessLevel) throws GitLabApiException {
371+
return (addMember(groupId, userId, accessLevel, null));
372+
}
369373

370-
Form formData = new Form();
371-
formData.param("user_id", userId.toString());
372-
formData.param("access_level", accessLevel.toString());
374+
/**
375+
* Adds a user to the list of group members.
376+
*
377+
* POST /groups/:id/members
378+
*
379+
* @param groupId the project ID to add the member to, required
380+
* @param userId the user ID of the member to add, required
381+
* @param accessLevel the access level for the new member, required
382+
* @return a Member instance for the added user
383+
* @throws GitLabApiException if any exception occurs
384+
*/
385+
public Member addMember(Integer groupId, Integer userId, AccessLevel accessLevel) throws GitLabApiException {
386+
return (addMember(groupId, userId, accessLevel.toValue(), null));
387+
}
388+
389+
/**
390+
* Adds a user to the list of group members.
391+
*
392+
* POST /groups/:id/members
393+
*
394+
* @param groupId the project ID to add the member to, required
395+
* @param userId the user ID of the member to add, required
396+
* @param accessLevel the access level for the new member, required
397+
* @param expiresAt the date the membership in the group will expire, optional
398+
* @return a Member instance for the added user
399+
* @throws GitLabApiException if any exception occurs
400+
*/
401+
public Member addMember(Integer groupId, Integer userId, AccessLevel accessLevel, Date expiresAt) throws GitLabApiException {
402+
return (addMember(groupId, userId, accessLevel.toValue(), expiresAt));
403+
}
404+
405+
/**
406+
* Adds a user to the list of group members.
407+
*
408+
* POST /groups/:id/members
409+
*
410+
* @param groupId the project ID to add the member to, required
411+
* @param userId the user ID of the member to add, required
412+
* @param accessLevel the access level for the new member, required
413+
* @param expiresAt the date the membership in the group will expire, optional
414+
* @return a Member instance for the added user
415+
* @throws GitLabApiException if any exception occurs
416+
*/
417+
public Member addMember(Integer groupId, Integer userId, Integer accessLevel, Date expiresAt) throws GitLabApiException {
418+
419+
GitLabApiForm formData = new GitLabApiForm()
420+
.withParam("user_id", userId, true)
421+
.withParam("access_level", accessLevel, true)
422+
.withParam("expires_at", expiresAt, false);
373423
Response response = post(Response.Status.CREATED, formData, "groups", groupId, "members");
374424
return (response.readEntity(Member.class));
375425
}
376426

377427
/**
378-
* Removes member from the group team.
428+
* Updates a member of a group.
429+
*
430+
* PUT /groups/:groupId/members/:userId
431+
*
432+
* @param groupId the group ID the member belongs to, required
433+
* @param userId the user ID of the member to update, required
434+
* @param accessLevel the new access level for the member, required
435+
* @return the updated member
436+
* @throws GitLabApiException if any exception occurs
437+
*/
438+
public Member updateMember(Integer groupId, Integer userId, Integer accessLevel) throws GitLabApiException {
439+
return (updateMember(groupId, userId, accessLevel, null));
440+
}
441+
442+
/**
443+
* Updates a member of a group.
444+
*
445+
* PUT /groups/:groupId/members/:userId
446+
*
447+
* @param groupId the group ID the member belongs to, required
448+
* @param userId the user ID of the member to update, required
449+
* @param accessLevel the new access level for the member, required
450+
* @return the updated member
451+
* @throws GitLabApiException if any exception occurs
452+
*/
453+
public Member updateMember(Integer groupId, Integer userId, AccessLevel accessLevel) throws GitLabApiException {
454+
return (updateMember(groupId, userId, accessLevel.toValue(), null));
455+
}
456+
457+
/**
458+
* Updates a member of a group.
459+
*
460+
* PUT /groups/:groupId/members/:userId
461+
*
462+
* @param groupId the group ID the member belongs to, required
463+
* @param userId the user ID of the member to update, required
464+
* @param accessLevel the new access level for the member, required
465+
* @param expiresAt the date the membership in the group will expire, optional
466+
* @return the updated member
467+
* @throws GitLabApiException if any exception occurs
468+
*/
469+
public Member updateMember(Integer groupId, Integer userId, AccessLevel accessLevel, Date expiresAt) throws GitLabApiException {
470+
return (updateMember(groupId, userId, accessLevel.toValue(), expiresAt));
471+
}
472+
473+
/**
474+
* Updates a member of a group.
475+
*
476+
* PUT /groups/:groupId/members/:userId
477+
*
478+
* @param groupId the group ID the member belongs to, required
479+
* @param userId the user ID of the member to update, required
480+
* @param accessLevel the new access level for the member, required
481+
* @param expiresAt the date the membership in the group will expire, optional
482+
* @return the updated member
483+
* @throws GitLabApiException if any exception occurs
484+
*/
485+
public Member updateMember(Integer groupId, Integer userId, Integer accessLevel, Date expiresAt) throws GitLabApiException {
486+
GitLabApiForm formData = new GitLabApiForm()
487+
.withParam("access_level", accessLevel, true)
488+
.withParam("expires_at", expiresAt, false);
489+
Response response = put(Response.Status.OK, formData.asMap(), "groups", groupId, "members", userId);
490+
return (response.readEntity(Member.class));
491+
}
492+
493+
/**
494+
* Removes member from the group.
379495
*
380496
* DELETE /groups/:id/members/:user_id
381497
*
382-
* @param projectId the project ID to remove the member from
498+
* @param groupId the group ID to remove the member from
383499
* @param userId the user ID of the member to remove
384500
* @throws GitLabApiException if any exception occurs
385501
*/
386-
public void removeMember(Integer projectId, Integer userId) throws GitLabApiException {
502+
public void removeMember(Integer groupId, Integer userId) throws GitLabApiException {
387503
Response.Status expectedStatus = (isApiVersion(ApiVersion.V3) ? Response.Status.OK : Response.Status.NO_CONTENT);
388-
delete(expectedStatus, null, "groups", projectId, "members", userId);
504+
delete(expectedStatus, null, "groups", groupId, "members", userId);
389505
}
390506
}

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

Lines changed: 126 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@
2525

2626
import java.io.UnsupportedEncodingException;
2727
import java.net.URLEncoder;
28+
import java.util.Date;
2829
import java.util.List;
2930

3031
import javax.ws.rs.core.Form;
3132
import javax.ws.rs.core.GenericType;
3233
import javax.ws.rs.core.Response;
3334

3435
import org.gitlab4j.api.GitLabApi.ApiVersion;
36+
import org.gitlab4j.api.models.AccessLevel;
3537
import org.gitlab4j.api.models.Event;
3638
import org.gitlab4j.api.models.Issue;
3739
import org.gitlab4j.api.models.Member;
@@ -959,6 +961,40 @@ public Member getMember(Integer projectId, Integer userId) throws GitLabApiExcep
959961
return (response.readEntity(Member.class));
960962
}
961963

964+
/**
965+
* Adds a user to a project team. This is an idempotent method and can be called multiple times
966+
* with the same parameters. Adding team membership to a user that is already a member does not
967+
* affect the existing membership.
968+
*
969+
* POST /projects/:id/members
970+
*
971+
* @param projectId the project ID to add the team member to, required
972+
* @param userId the user ID of the member to add, required
973+
* @param accessLevel the access level for the new member, required
974+
* @return the added member
975+
* @throws GitLabApiException if any exception occurs
976+
*/
977+
public Member addMember(Integer projectId, Integer userId, Integer accessLevel) throws GitLabApiException {
978+
return (addMember(projectId, userId, accessLevel, null));
979+
}
980+
981+
/**
982+
* Adds a user to a project team. This is an idempotent method and can be called multiple times
983+
* with the same parameters. Adding team membership to a user that is already a member does not
984+
* affect the existing membership.
985+
*
986+
* POST /projects/:id/members
987+
*
988+
* @param projectId the project ID to add the team member to, required
989+
* @param userId the user ID of the member to add, required
990+
* @param accessLevel the access level for the new member, required
991+
* @return the added member
992+
* @throws GitLabApiException if any exception occurs
993+
*/
994+
public Member addMember(Integer projectId, Integer userId, AccessLevel accessLevel) throws GitLabApiException {
995+
return (addMember(projectId, userId, accessLevel.toValue(), null));
996+
}
997+
962998
/**
963999
* Adds a user to a project team. This is an idempotent method and can be called multiple times
9641000
* with the same parameters. Adding team membership to a user that is already a member does not
@@ -969,15 +1005,103 @@ public Member getMember(Integer projectId, Integer userId) throws GitLabApiExcep
9691005
* @param projectId the project ID to add the team member to
9701006
* @param userId the user ID of the member to add
9711007
* @param accessLevel the access level for the new member
1008+
* @param expiresAt the date the membership in the group will expire
9721009
* @return the added member
9731010
* @throws GitLabApiException if any exception occurs
9741011
*/
975-
public Member addMember(Integer projectId, Integer userId, Integer accessLevel) throws GitLabApiException {
976-
GitLabApiForm formData = new GitLabApiForm().withParam("user_id", userId, true).withParam("access_level", accessLevel, true);
1012+
public Member addMember(Integer projectId, Integer userId, AccessLevel accessLevel, Date expiresAt) throws GitLabApiException {
1013+
return (addMember(projectId, userId, accessLevel.toValue(), expiresAt));
1014+
}
1015+
1016+
/**
1017+
* Adds a user to a project team. This is an idempotent method and can be called multiple times
1018+
* with the same parameters. Adding team membership to a user that is already a member does not
1019+
* affect the existing membership.
1020+
*
1021+
* POST /projects/:id/members
1022+
*
1023+
* @param projectId the project ID to add the team member to
1024+
* @param userId the user ID of the member to add
1025+
* @param accessLevel the access level for the new member
1026+
* @param expiresAt the date the membership in the group will expire
1027+
* @return the added member
1028+
* @throws GitLabApiException if any exception occurs
1029+
*/
1030+
public Member addMember(Integer projectId, Integer userId, Integer accessLevel, Date expiresAt) throws GitLabApiException {
1031+
GitLabApiForm formData = new GitLabApiForm()
1032+
.withParam("user_id", userId, true)
1033+
.withParam("access_level", accessLevel, true)
1034+
.withParam("expires_at", expiresAt, false);
9771035
Response response = post(Response.Status.CREATED, formData, "projects", projectId, "members");
9781036
return (response.readEntity(Member.class));
9791037
}
9801038

1039+
/**
1040+
* Updates a member of a project.
1041+
*
1042+
* PUT /projects/:projectId/members/:userId
1043+
*
1044+
* @param projectId the project ID the member belongs to, required
1045+
* @param userId the user ID of the member to update, required
1046+
* @param accessLevel the new access level for the member, required
1047+
* @return the updated member
1048+
* @throws GitLabApiException if any exception occurs
1049+
*/
1050+
public Member updateMember(Integer projectId, Integer userId, Integer accessLevel) throws GitLabApiException {
1051+
return (updateMember(projectId, userId, accessLevel, null));
1052+
}
1053+
1054+
/**
1055+
* Updates a member of a project.
1056+
*
1057+
* PUT /projects/:projectId/members/:userId
1058+
*
1059+
* @param projectId the project ID the member belongs to, required
1060+
* @param userId the user ID of the member to update, required
1061+
* @param accessLevel the new access level for the member, required
1062+
* @return the updated member
1063+
* @throws GitLabApiException if any exception occurs
1064+
*/
1065+
public Member updateMember(Integer projectId, Integer userId, AccessLevel accessLevel) throws GitLabApiException {
1066+
return (updateMember(projectId, userId, accessLevel.toValue(), null));
1067+
}
1068+
1069+
/**
1070+
* Updates a member of a project.
1071+
*
1072+
* PUT /projects/:projectId/members/:userId
1073+
*
1074+
* @param projectId the project ID the member belongs to, required
1075+
* @param userId the user ID of the member to update, required
1076+
* @param accessLevel the new access level for the member, required
1077+
* @param expiresAt the date the membership in the group will expire, optional
1078+
* @return the updated member
1079+
* @throws GitLabApiException if any exception occurs
1080+
*/
1081+
public Member updateMember(Integer projectId, Integer userId, AccessLevel accessLevel, Date expiresAt) throws GitLabApiException {
1082+
return (updateMember(projectId, userId, accessLevel.toValue(), expiresAt));
1083+
}
1084+
1085+
/**
1086+
* Updates a member of a project.
1087+
*
1088+
* PUT /projects/:projectId/members/:userId
1089+
*
1090+
* @param projectId the project ID the member belongs to, required
1091+
* @param userId the user ID of the member to update, required
1092+
* @param accessLevel the new access level for the member, required
1093+
* @param expiresAt the date the membership in the group will expire, optional
1094+
* @return the updated member
1095+
* @throws GitLabApiException if any exception occurs
1096+
*/
1097+
public Member updateMember(Integer projectId, Integer userId, Integer accessLevel, Date expiresAt) throws GitLabApiException {
1098+
GitLabApiForm formData = new GitLabApiForm()
1099+
.withParam("access_level", accessLevel, true)
1100+
.withParam("expires_at", expiresAt, false);
1101+
Response response = put(Response.Status.OK, formData.asMap(), "projects", projectId, "members", userId);
1102+
return (response.readEntity(Member.class));
1103+
}
1104+
9811105
/**
9821106
* Removes user from project team.
9831107
*

0 commit comments

Comments
 (0)