Skip to content

Commit cabccb4

Browse files
lamdavgmessner
authored andcommitted
add extern uid field for create (#182)
* add extern uid field for create * added fluent builder pattern to User
1 parent b61f3f9 commit cabccb4

File tree

2 files changed

+188
-20
lines changed

2 files changed

+188
-20
lines changed

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

+89-20
Original file line numberDiff line numberDiff line change
@@ -311,64 +311,123 @@ public Pager<User> findUsers(String emailOrUsername, int itemsPerPage) throws Gi
311311

312312
/**
313313
* Creates a new user. Note only administrators can create new users.
314+
* Either password or reset_password should be specified (reset_password takes priority).
315+
*
316+
* If both the User object's projectsLimit and the parameter projectsLimit is specified
317+
* the parameter will take precedence.
314318
*
315319
* POST /users
316320
*
317321
* email (required) - Email
318-
* password (required) - Password
322+
* password (optional) - Password
323+
* reset_password (optional) - Send user password reset link - true or false(default)
319324
* username (required) - Username
320325
* name (required) - Name
321326
* skype (optional) - Skype ID
322-
* linkedin (optional) - Linkedin
327+
* linkedin (optional) - LinkedIn
323328
* twitter (optional) - Twitter account
324-
* website_url (optional) - Website url
329+
* website_url (optional) - Website URL
330+
* organization (optional) - Organization name
325331
* projects_limit (optional) - Number of projects user can create
326332
* extern_uid (optional) - External UID
327333
* provider (optional) - External provider name
328-
* bio (optional) - User's bio
334+
* bio (optional) - User's biography
335+
* location (optional) - User's location
329336
* admin (optional) - User is admin - true or false (default)
330337
* can_create_group (optional) - User can create groups - true or false
338+
* skip_confirmation (optional) - Skip confirmation - true or false (default)
339+
* external (optional) - Flags the user as external - true or false(default)
340+
* avatar (optional) - Image file for user's avatar
341+
* shared_runners_minutes_limit (optional) - Pipeline minutes quota for this user
331342
*
332343
* @param user the User instance with the user info to create
333344
* @param password the password for the new user
334345
* @param projectsLimit the maximum number of project
335346
* @return created User instance
336347
* @throws GitLabApiException if any exception occurs
337348
*/
338-
public User createUser(User user, String password, Integer projectsLimit) throws GitLabApiException {
339-
Form formData = userToForm(user, projectsLimit, password, true);
349+
public User createUser(User user, CharSequence password, Integer projectsLimit) throws GitLabApiException {
350+
Form formData = userToForm(user, projectsLimit, password, null, true);
340351
Response response = post(Response.Status.CREATED, formData, "users");
341352
return (response.readEntity(User.class));
342353
}
343354

344355
/**
345-
* Modifies an existing user. Only administrators can change attributes of a user.
356+
* Creates a new user. Note only administrators can create new users.
357+
* Either password or reset_password should be specified (reset_password takes priority).
346358
*
347-
* PUT /users/:id
359+
* Creates a user with reset_password being <code>true</code>
360+
*
361+
* POST /users
348362
*
349363
* email (required) - Email
350-
* password (required) - Password
364+
* password (optional) - Password
365+
* reset_password (optional) - Send user password reset link - true or false(default)
351366
* username (required) - Username
352367
* name (required) - Name
353368
* skype (optional) - Skype ID
354-
* linkedin (optional) - Linkedin
369+
* linkedin (optional) - LinkedIn
355370
* twitter (optional) - Twitter account
356-
* website_url (optional) - Website url
371+
* website_url (optional) - Website URL
372+
* organization (optional) - Organization name
357373
* projects_limit (optional) - Number of projects user can create
358374
* extern_uid (optional) - External UID
359375
* provider (optional) - External provider name
360-
* bio (optional) - User's bio
376+
* bio (optional) - User's biography
377+
* location (optional) - User's location
361378
* admin (optional) - User is admin - true or false (default)
362379
* can_create_group (optional) - User can create groups - true or false
380+
* skip_confirmation (optional) - Skip confirmation - true or false (default)
381+
* external (optional) - Flags the user as external - true or false(default)
382+
* avatar (optional) - Image file for user's avatar
383+
* shared_runners_minutes_limit (optional) - Pipeline minutes quota for this user
384+
*
385+
* @param user the User instance with the user info to create
386+
* @param password the password for the new user
387+
* @param resetPassword whether to send a password reset link
388+
* @return created User instance
389+
* @throws GitLabApiException if any exception occurs
390+
*/
391+
public User createUser(User user, CharSequence password, boolean resetPassword) throws GitLabApiException {
392+
Form formData = userToForm(user, null, password, resetPassword, true);
393+
Response response = post(Response.Status.CREATED, formData, "users");
394+
return (response.readEntity(User.class));
395+
}
396+
397+
/**
398+
* Modifies an existing user. Only administrators can change attributes of a user.
399+
*
400+
* PUT /users/:id
401+
*
402+
* email - Email
403+
* username - Username
404+
* name - Name
405+
* password - Password
406+
* skype - Skype ID
407+
* linkedin - LinkedIn
408+
* twitter - Twitter account
409+
* website_url - Website URL
410+
* organization - Organization name
411+
* projects_limit - Limit projects each user can create
412+
* extern_uid - External UID
413+
* provider - External provider name
414+
* bio - User's biography
415+
* location (optional) - User's location
416+
* admin (optional) - User is admin - true or false (default)
417+
* can_create_group (optional) - User can create groups - true or false
418+
* skip_reconfirmation (optional) - Skip reconfirmation - true or false (default)
419+
* external (optional) - Flags the user as external - true or false(default)
420+
* shared_runners_minutes_limit (optional) - Pipeline minutes quota for this user
421+
* avatar (optional) - Image file for user's avatar
363422
*
364423
* @param user the User instance with the user info to modify
365424
* @param password the new password for the user
366425
* @param projectsLimit the maximum number of project
367426
* @return the modified User instance
368427
* @throws GitLabApiException if any exception occurs
369428
*/
370-
public User modifyUser(User user, String password, Integer projectsLimit) throws GitLabApiException {
371-
Form form = userToForm(user, projectsLimit, password, false);
429+
public User modifyUser(User user, CharSequence password, Integer projectsLimit) throws GitLabApiException {
430+
Form form = userToForm(user, projectsLimit, password, false, false);
372431
Response response = put(Response.Status.OK, form.asMap(), "users", user.getId());
373432
return (response.readEntity(User.class));
374433
}
@@ -560,7 +619,7 @@ public SshKey addSshKey(Integer userId, String title, String key) throws GitLabA
560619
}
561620

562621
/**
563-
* Deletes key owned by currently authenticated user. This is an idempotent function and calling it
622+
* Deletes key owned by currently authenticated user. This is an idempotent function and calling it
564623
* on a key that is already deleted or not available results in success.
565624
*
566625
* DELETE /user/keys/:key_id
@@ -744,24 +803,34 @@ public void revokeImpersonationToken(Integer userId, Integer tokenId) throws Git
744803
* @param create whether the form is being populated to create a new user
745804
* @return the populated Form instance
746805
*/
747-
Form userToForm(User user, Integer projectsLimit, String password, boolean create) {
806+
Form userToForm(User user, Integer projectsLimit, CharSequence password, Boolean resetPassword, boolean create) {
807+
if (create) {
808+
if ((password == null || password.toString().trim().isEmpty()) && !resetPassword) {
809+
throw new IllegalArgumentException("either password or reset_password must be set");
810+
}
811+
}
812+
projectsLimit = (projectsLimit == null) ? user.getProjectsLimit() : projectsLimit;
813+
748814
return (new GitLabApiForm()
749815
.withParam("email", user.getEmail(), create)
750-
.withParam("password", password, create)
816+
.withParam("password", password, false)
817+
.withParam("reset_password", resetPassword, false)
751818
.withParam("username", user.getUsername(), create)
752819
.withParam("name", user.getName(), create)
753820
.withParam("skype", user.getSkype(), false)
754821
.withParam("linkedin", user.getLinkedin(), false)
755822
.withParam("twitter", user.getTwitter(), false)
756823
.withParam("website_url", user.getWebsiteUrl(), false)
757-
.withParam("projects_limit", projectsLimit, false)
758824
.withParam("organization", user.getOrganization(), false)
825+
.withParam("projects_limit", projectsLimit, false)
826+
.withParam("extern_uid", user.getExternUid(), false)
759827
.withParam("provider", user.getProvider(), false)
760828
.withParam("bio", user.getBio(), false)
761829
.withParam("location", user.getLocation(), false)
762830
.withParam("admin", user.getIsAdmin(), false)
763831
.withParam("can_create_group", user.getCanCreateGroup(), false)
764-
.withParam("external", user.getExternal(), false))
765-
.withParam("skip_confirmation",user.getSkipConfirmation(),false);
832+
.withParam("skip_confirmation", user.getSkipConfirmation(), false)
833+
.withParam("external", user.getExternal(), false)
834+
.withParam("shared_runners_minutes_limit", user.getSharedRunnersMinutesLimit(),false));
766835
}
767836
}

src/main/java/org/gitlab4j/api/models/User.java

+99
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,103 @@
44

55
@XmlRootElement
66
public class User extends AbstractUser {
7+
private String externUid;
8+
9+
public void setExternUid(String externUid) {
10+
this.externUid = externUid;
11+
}
12+
13+
public String getExternUid() {
14+
return this.externUid;
15+
}
16+
17+
public User withEmail(String email) {
18+
setEmail(email);
19+
return this;
20+
}
21+
22+
public User withName(String name) {
23+
setName(name);
24+
return this;
25+
}
26+
27+
public User withUsername(String username) {
28+
setUsername(username);
29+
return this;
30+
}
31+
32+
public User withSkype(String skype) {
33+
setSkype(skype);
34+
return this;
35+
}
36+
37+
public User withLinkedin(String linkedIn) {
38+
setLinkedin(linkedIn);
39+
return this;
40+
}
41+
42+
public User withTwitter(String twitter) {
43+
setTwitter(twitter);
44+
return this;
45+
}
46+
47+
public User withWebsiteUrl(String websiteUrl) {
48+
setWebsiteUrl(websiteUrl);
49+
return this;
50+
}
51+
52+
public User withOrganization(String organization) {
53+
setOrganization(organization);
54+
return this;
55+
}
56+
57+
public User withProjectLimit(Integer projectsLimit) {
58+
setProjectsLimit(projectsLimit);
59+
return this;
60+
}
61+
62+
public User withExternUid(String externUid) {
63+
setExternUid(externUid);
64+
return this;
65+
}
66+
67+
public User withProvider(String provider) {
68+
setProvider(provider);
69+
return this;
70+
}
71+
72+
public User withBio(String bio) {
73+
setBio(bio);
74+
return this;
75+
}
76+
77+
public User withLocation(String location) {
78+
setLocation(location);
79+
return this;
80+
}
81+
82+
public User withIsAdmin(Boolean isAdmin) {
83+
setIsAdmin(isAdmin);
84+
return this;
85+
}
86+
87+
public User withCanCreateGroup(Boolean canCreateGroup) {
88+
setCanCreateGroup(canCreateGroup);
89+
return this;
90+
}
91+
92+
public User withSkipConfirmation(Boolean skipConfirmation) {
93+
setSkipConfirmation(skipConfirmation);
94+
return this;
95+
}
96+
97+
public User withExternal(Boolean external) {
98+
setExternal(external);
99+
return this;
100+
}
101+
102+
public User withSharedRunnersMinuteLimit(Integer sharedRunnersMinuteLimit) {
103+
setSharedRunnersMinutesLimit(sharedRunnersMinuteLimit);
104+
return this;
105+
}
7106
}

0 commit comments

Comments
 (0)