Skip to content

Commit 4ed3fe9

Browse files
fcaylusgmessner
authored andcommitted
Add method to get user by external uid #427 (#428)
This only works for admin users
1 parent d798c90 commit 4ed3fe9

File tree

5 files changed

+77
-11
lines changed

5 files changed

+77
-11
lines changed

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,48 @@ public Optional<User> getOptionalUserByEmail(String email) {
371371
}
372372
}
373373

374+
/**
375+
* Lookup a user by external UID. Returns null if not found.
376+
*
377+
* <p>NOTE: This is for admin users only.</p>
378+
*
379+
* <pre><code>GitLab Endpoint: GET /users?extern_uid=:externalUid&provider=:provider</code></pre>
380+
*
381+
* @param provider the provider of the external uid
382+
* @param externalUid the external UID of the user
383+
* @return the User instance for the specified external UID, or null if not found
384+
* @throws GitLabApiException if any exception occurs
385+
*/
386+
public User getUserByExternalUid(String provider, String externalUid) throws GitLabApiException {
387+
GitLabApiForm formData = createGitLabApiForm()
388+
.withParam("provider", provider, true)
389+
.withParam("extern_uid", externalUid, true)
390+
.withParam(PAGE_PARAM, 1)
391+
.withParam(PER_PAGE_PARAM, 1);
392+
Response response = get(Response.Status.OK, formData.asMap(), "users");
393+
List<User> users = response.readEntity(new GenericType<List<User>>() {});
394+
return (users.isEmpty() ? null : users.get(0));
395+
}
396+
397+
/**
398+
* Lookup a user by external UID and return an Optional instance.
399+
*
400+
* <p>NOTE: This is for admin users only.</p>
401+
*
402+
* <pre><code>GitLab Endpoint: GET /users?extern_uid=:externUid&provider=:provider</code></pre>
403+
*
404+
* @param provider the provider of the external uid
405+
* @param externalUid the external UID of the user
406+
* @return the User for the specified external UID as an Optional instance
407+
*/
408+
public Optional<User> getOptionalUserByExternalUid(String provider, String externalUid) {
409+
try {
410+
return (Optional.ofNullable(getUserByExternalUid(provider, externalUid)));
411+
} catch (GitLabApiException glae) {
412+
return (GitLabApi.createOptionalFromException(glae));
413+
}
414+
}
415+
374416
/**
375417
* Search users by Email or username
376418
*

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,9 @@
44
import static org.junit.Assert.assertNotNull;
55
import static org.junit.Assert.fail;
66

7-
import java.util.Arrays;
8-
import java.util.List;
9-
import java.util.Optional;
10-
11-
import org.gitlab4j.api.models.Group;
12-
import org.gitlab4j.api.models.Project;
13-
import org.gitlab4j.api.models.RepositoryFile;
14-
import org.gitlab4j.api.models.User;
15-
import org.gitlab4j.api.models.Visibility;
7+
import java.util.*;
8+
9+
import org.gitlab4j.api.models.*;
1610
import org.gitlab4j.api.utils.AccessTokenUtils;
1711
import org.gitlab4j.api.utils.AccessTokenUtils.Scope;
1812
import org.junit.AfterClass;
@@ -44,6 +38,8 @@ public class IntegrationTestSuite implements PropertyConstants {
4438
private static final String TEST_GROUP = HelperUtils.getProperty(GROUP_KEY);
4539
private static final String TEST_GROUP_PROJECT_NAME = HelperUtils.getProperty(GROUP_PROJECT_KEY);
4640
private static final String TEST_SUB_GROUP = HelperUtils.getProperty(SUB_GROUP_KEY);
41+
private static final String TEST_EXTERNAL_PROVIDER = HelperUtils.getProperty(EXTERNAL_PROVIDER_KEY);
42+
private static final String TEST_EXTERNAL_UID = HelperUtils.getProperty(EXTERNAL_UID_KEY);
4743

4844
protected static final String TEST_PRIVATE_TOKEN_NAME = "GitLab4J Test Private Token - " + HelperUtils.getRandomInt(1000);
4945
protected static String TEST_PRIVATE_TOKEN = HelperUtils.getProperty(PRIVATE_TOKEN_KEY);
@@ -168,12 +164,18 @@ private static void seedData() throws GitLabApiException {
168164
// If the tester user doen't exists, create it
169165
Optional<User> optionalUser = gitLabApi.getUserApi().getOptionalUser(TEST_LOGIN_USERNAME);
170166
if (!optionalUser.isPresent()) {
167+
Identity identity = new Identity();
168+
identity.setExternUid(TEST_EXTERNAL_UID);
169+
identity.setProvider(TEST_EXTERNAL_PROVIDER);
170+
171171
User userSettings = new User()
172172
.withUsername(TEST_LOGIN_USERNAME)
173173
.withEmail(TEST_LOGIN_USERNAME + "@gitlab4j.org")
174174
.withName("GitLab4J Tester")
175175
.withSkipConfirmation(true)
176-
.withIsAdmin(true);
176+
.withIsAdmin(true)
177+
.withIdentities(Collections.singletonList(identity));
178+
177179
gitLabApi.getUserApi().createUser(userSettings, TEST_LOGIN_PASSWORD, false);
178180
System.out.format("Created %s user (%s)%n", userSettings.getName(), userSettings.getUsername());
179181
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,6 @@ public interface PropertyConstants {
2828
public static final String TEST_REQUEST_ACCESS_USERNAME_KEY = "TEST_REQUEST_ACCESS_USERNAME";
2929
public static final String USERNAME_KEY = "TEST_USERNAME";
3030
public static final String XFER_NAMESPACE_KEY = "TEST_XFER_NAMESPACE";
31+
public static final String EXTERNAL_PROVIDER_KEY = "TEST_EXTERNAL_PROVIDER";
32+
public static final String EXTERNAL_UID_KEY = "TEST_EXTERNAL_UID";
3133
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ public class TestUserApi extends AbstractIntegrationTest {
6464
"IW/2DIlUts7gcB2hzXtt7r7+6DLx82Vb+S2jPZu2JQaB4zfgS7LQgzHUy1aAAgUUpuAbvWzuGHKO0p551Ru4qi" +
6565
"tyXN2+OUVXcYAsuIIdGGB0wLvTDgiOOSZWnSE+sg6XX user@example.com";
6666
private static final String TEST_USER_EMAIL = "test-user-email123@gitlab4j.org";
67-
67+
68+
private static final String TEST_EXTERNAL_PROVIDER = HelperUtils.getProperty(EXTERNAL_PROVIDER_KEY);
69+
private static final String TEST_EXTERNAL_UID = HelperUtils.getProperty(EXTERNAL_UID_KEY);
6870

6971
private static GitLabApi gitLabApi;
7072
private static User blockUser;
@@ -180,6 +182,18 @@ public void testGetOptionalUser() throws GitLabApiException {
180182
assertFalse(optional.isPresent());
181183
}
182184

185+
@Test
186+
public void testGetOptionalUserByExternalUid() throws GitLabApiException {
187+
188+
Optional<User> optional = gitLabApi.getUserApi().getOptionalUserByExternalUid(TEST_EXTERNAL_PROVIDER, TEST_EXTERNAL_UID);
189+
assertNotNull(optional);
190+
assertTrue(optional.isPresent());
191+
192+
optional = gitLabApi.getUserApi().getOptionalUserByExternalUid("unknown-provider", "unknown-uid");
193+
assertNotNull(optional);
194+
assertFalse(optional.isPresent());
195+
}
196+
183197
@Test
184198
public void testSudoAsUser() throws GitLabApiException {
185199

src/test/resources/test-gitlab4j.properties

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ TEST_LOGIN_PASSWORD=ChangeMeNow
2929
TEST_PROJECT_NAME=test-project
3030
TEST_USERNAME=gitlab4j
3131

32+
# This specifies the default external provider to test against and the user served by this provider, change
33+
# this if you'd like to test against a different project
34+
TEST_EXTERNAL_PROVIDER=github
35+
TEST_EXTERNAL_UID=2435223452345
36+
37+
3238
# This is the user to test sudo, block, and project transfer. If the user does not exist
3339
# it will be created during integration testing
3440
TEST_SUDO_AS_USERNAME=user1

0 commit comments

Comments
 (0)