Search in sources :

Example 11 with UserApi

use of org.gitlab4j.api.UserApi in project catma by forTEXT.

the class GitLabServerManagerTest method tearDown.

@AfterEach
public void tearDown() throws Exception {
    GitLabApi adminGitLabApi = gitlabManagerPrivileged.getGitLabApi();
    GroupApi groupApi = adminGitLabApi.getGroupApi();
    ProjectApi projectApi = adminGitLabApi.getProjectApi();
    UserApi userApi = adminGitLabApi.getUserApi();
    if (groupsToDeleteOnTearDown.size() > 0) {
        for (String groupPath : groupsToDeleteOnTearDown) {
            gitlabManagerRestricted.deleteGroup(groupPath);
            await().until(() -> groupApi.getGroups().isEmpty());
        }
    }
    if (repositoriesToDeleteOnTearDown.size() > 0) {
        for (Integer repositoryId : repositoriesToDeleteOnTearDown) {
            gitlabManagerRestricted.deleteRepository(repositoryId);
            await().until(() -> projectApi.getProjects().isEmpty());
        }
    }
    if (usersToDeleteOnTearDown.size() > 0) {
        for (Integer userId : usersToDeleteOnTearDown) {
            userApi.deleteUser(userId);
            GitLabServerManagerTest.awaitUserDeleted(userApi, userId);
        }
    }
    // delete the GitLab user that we created in setUp, including associated groups/repos
    // TODO: explicit deletion of associated groups/repos (above) is now superfluous since we are doing a hard delete
    userApi.deleteUser(gitlabManagerRestricted.getUser().getUserId(), true);
// GitLabServerManagerTest.awaitUserDeleted(userApi, gitlabManagerRestricted.getUser().getUserId());
}
Also used : GitLabApi(org.gitlab4j.api.GitLabApi) GroupApi(org.gitlab4j.api.GroupApi) ProjectApi(org.gitlab4j.api.ProjectApi) UserApi(org.gitlab4j.api.UserApi) AfterEach(org.junit.jupiter.api.AfterEach)

Example 12 with UserApi

use of org.gitlab4j.api.UserApi in project catma by forTEXT.

the class CustomUserApiTest method tearDown.

@After
public void tearDown() throws Exception {
    if (this.usersToDeleteOnTearDown.size() > 0) {
        for (Integer userId : this.usersToDeleteOnTearDown) {
            UserApi userApi = this.gitLabApi.getUserApi();
            userApi.deleteUser(userId);
            GitLabServerManagerTest.awaitUserDeleted(userApi, userId);
        }
    }
}
Also used : UserApi(org.gitlab4j.api.UserApi) After(org.junit.After)

Example 13 with UserApi

use of org.gitlab4j.api.UserApi in project catma by forTEXT.

the class GitlabManagerPrivileged method createPersonalAccessToken.

@Override
public String createPersonalAccessToken(int userId, String tokenName, LocalDate expiresAt) throws IOException {
    UserApi userApi = this.privilegedGitLabApi.getUserApi();
    try {
        PersonalAccessToken personalAccessToken = userApi.createPersonalAccessToken(userId, tokenName, Date.from(expiresAt.atStartOfDay(ZoneId.systemDefault()).toInstant()), new Scope[] { Scope.READ_API });
        logger.info(String.format("Created personal access token for user with ID %1$s.", userId));
        return personalAccessToken.getToken();
    } catch (GitLabApiException e) {
        throw new IOException("Failed to create personal access token", e);
    }
}
Also used : PersonalAccessToken(org.gitlab4j.api.models.PersonalAccessToken) GitLabApiException(org.gitlab4j.api.GitLabApiException) IOException(java.io.IOException) UserApi(org.gitlab4j.api.UserApi)

Example 14 with UserApi

use of org.gitlab4j.api.UserApi in project catma by forTEXT.

the class GitlabManagerPrivileged method acquireImpersonationToken.

@Override
public Pair<GitUser, String> acquireImpersonationToken(String identifier, String provider, String email, String name) throws IOException {
    User user = this.acquireUser(identifier, provider, email, name);
    UserApi customUserApi = this.privilegedGitLabApi.getUserApi();
    try {
        List<PersonalAccessToken> impersonationTokens = customUserApi.getImpersonationTokens(user.getId(), ImpersonationState.ACTIVE);
        // revoke the default token if it exists actively
        for (PersonalAccessToken token : impersonationTokens) {
            if (token.getName().equals(GITLAB_DEFAULT_IMPERSONATION_TOKEN_NAME)) {
                privilegedGitLabApi.getUserApi().revokeImpersonationToken(user.getId(), token.getId());
                break;
            }
        }
    } catch (GitLabApiException e) {
        throw new IOException("Failed to revoke existing impersonation token", e);
    }
    String impersonationToken = this.createImpersonationToken(user.getId(), GITLAB_DEFAULT_IMPERSONATION_TOKEN_NAME);
    if (impersonationToken == null) {
        String errorMessage = String.format("Failed to acquire impersonation token for CATMA with identifier `%s`. " + "The creation of the token the associated GitLab user ID `%s` failed, no " + "active impersonation token called `%s` can be found!", identifier, user.getId(), GITLAB_DEFAULT_IMPERSONATION_TOKEN_NAME);
        throw new IOException(errorMessage);
    }
    Pair<GitUser, String> retVal = new Pair<>(new GitUser(user), impersonationToken);
    return retVal;
}
Also used : User(org.gitlab4j.api.models.User) GitUser(de.catma.repository.git.GitUser) PersonalAccessToken(org.gitlab4j.api.models.PersonalAccessToken) GitLabApiException(org.gitlab4j.api.GitLabApiException) IOException(java.io.IOException) UserApi(org.gitlab4j.api.UserApi) GitUser(de.catma.repository.git.GitUser) Pair(de.catma.util.Pair)

Example 15 with UserApi

use of org.gitlab4j.api.UserApi in project catma by forTEXT.

the class GitlabManagerPrivileged method setTermsOfUseConsentGiven.

public void setTermsOfUseConsentGiven(de.catma.user.User catmaUser, boolean value) {
    try {
        UserApi userApi = privilegedGitLabApi.getUserApi();
        User user = userApi.getUser(catmaUser.getUserId());
        Optional<CustomAttribute> optionalAttribute = Optional.empty();
        if (user.getCustomAttributes() != null) {
            optionalAttribute = user.getCustomAttributes().stream().filter(attr -> attr.getKey().equals(CustomAttributeName.terms_of_use_consent_given.name())).findFirst();
        }
        CustomAttribute attr = optionalAttribute.orElse(new CustomAttribute().withKey(CustomAttributeName.terms_of_use_consent_given.name()).withValue(Boolean.FALSE.toString()));
        attr.setValue(Boolean.valueOf(value).toString());
        userApi.changeCustomAttribute(user.getId(), attr);
    } catch (GitLabApiException e) {
        logger.log(Level.SEVERE, "Could access custom attributes", e);
    }
}
Also used : User(org.gitlab4j.api.models.User) GitUser(de.catma.repository.git.GitUser) CustomAttribute(org.gitlab4j.api.models.CustomAttribute) GitLabApiException(org.gitlab4j.api.GitLabApiException) UserApi(org.gitlab4j.api.UserApi)

Aggregations

UserApi (org.gitlab4j.api.UserApi)17 GitLabApiException (org.gitlab4j.api.GitLabApiException)7 User (org.gitlab4j.api.models.User)5 GitUser (de.catma.repository.git.GitUser)4 IOException (java.io.IOException)3 PersonalAccessToken (org.gitlab4j.api.models.PersonalAccessToken)3 AfterEach (org.junit.jupiter.api.AfterEach)3 EventBus (com.google.common.eventbus.EventBus)2 BackgroundService (de.catma.backgroundservice.BackgroundService)2 File (java.io.File)2 GitLabApi (org.gitlab4j.api.GitLabApi)2 GroupApi (org.gitlab4j.api.GroupApi)2 CustomAttribute (org.gitlab4j.api.models.CustomAttribute)2 Pair (de.catma.util.Pair)1 SecureRandom (java.security.SecureRandom)1 ProjectApi (org.gitlab4j.api.ProjectApi)1 Identity (org.gitlab4j.api.models.Identity)1 Project (org.gitlab4j.api.models.Project)1 After (org.junit.After)1 Test (org.junit.jupiter.api.Test)1