Search in sources :

Example 6 with Project

use of org.gitlab4j.api.models.Project in project catma by forTEXT.

the class GitlabManagerCommon method assignDefaultAccessToRootProject.

private RBACSubject assignDefaultAccessToRootProject(RBACSubject subject, Integer groupId) throws IOException {
    try {
        Group group = getGitLabApi().getGroupApi().getGroup(groupId);
        Project rootProject = getGitLabApi().getProjectApi().getProject(group.getName(), GitProjectManager.getProjectRootRepositoryName(group.getName()));
        try {
            Member member = getGitLabApi().getProjectApi().getMember(rootProject.getId(), subject.getUserId());
            if (member.getAccessLevel().value < RBACRole.ASSISTANT.getAccessLevel()) {
                return new GitMember(getGitLabApi().getProjectApi().updateMember(rootProject.getId(), subject.getUserId(), AccessLevel.forValue(RBACRole.ASSISTANT.getAccessLevel())));
            } else {
                // In both cases we refuse to update the role, and simply do nothing.
                return subject;
            }
        } catch (GitLabApiException e) {
            return new GitMember(getGitLabApi().getProjectApi().addMember(rootProject.getId(), subject.getUserId(), AccessLevel.forValue(RBACRole.ASSISTANT.getAccessLevel())));
        }
    } catch (GitLabApiException e) {
        throw new IOException("error assigning default access to root project in group #" + groupId, e);
    }
}
Also used : Group(org.gitlab4j.api.models.Group) Project(org.gitlab4j.api.models.Project) GitLabApiException(org.gitlab4j.api.GitLabApiException) IOException(java.io.IOException) GitMember(de.catma.repository.git.GitMember) GitMember(de.catma.repository.git.GitMember) Member(org.gitlab4j.api.models.Member)

Example 7 with Project

use of org.gitlab4j.api.models.Project in project catma by forTEXT.

the class GitlabManagerCommon method unassignFromResource.

@Override
public final void unassignFromResource(RBACSubject subject, String projectId, String resourceId) throws IOException {
    try {
        Project project = getGitLabApi().getProjectApi().getProject(projectId, resourceId);
        if (project == null) {
            throw new IOException("Resource unkown " + resourceId);
        }
        getGitLabApi().getProjectApi().removeMember(project.getId(), subject.getUserId());
    } catch (GitLabApiException e) {
        throw new IOException("error unassigning from resource #" + resourceId + ", subject " + subject, e);
    }
}
Also used : Project(org.gitlab4j.api.models.Project) GitLabApiException(org.gitlab4j.api.GitLabApiException) IOException(java.io.IOException)

Example 8 with Project

use of org.gitlab4j.api.models.Project in project catma by forTEXT.

the class GitLabServerManagerTest method createRepositoryInGroup.

@Test
public void createRepositoryInGroup() throws Exception {
    String randomGroupNameAndPath = Randomizer.getGroupName();
    String createdGroupPath = this.serverManager.createGroup(randomGroupNameAndPath, randomGroupNameAndPath, null);
    this.groupsToDeleteOnTearDown.add(createdGroupPath);
    assertNotNull(createdGroupPath);
    Group group = this.serverManager.getAdminGitLabApi().getGroupApi().getGroup(createdGroupPath);
    assertNotNull(group);
    assertEquals(randomGroupNameAndPath, group.getName());
    assertEquals(randomGroupNameAndPath, group.getPath());
    // to assert that the user is the owner of the new group, get the groups for the user using
    // the *user-specific* GitLabApi instance
    List<Group> groups = this.serverManager.getUserGitLabApi().getGroupApi().getGroups();
    assertEquals(1, groups.size());
    assertEquals(group.getId(), groups.get(0).getId());
    String randomRepoName = Randomizer.getRepoName();
    IRemoteGitServerManager.CreateRepositoryResponse createRepositoryResponse = this.serverManager.createRepository(randomRepoName, null, createdGroupPath);
    // we don't add the repositoryId to this.repositoriesToDeleteOnTearDown as deletion of the group will take care
    // of that for us
    assertNotNull(createRepositoryResponse);
    assert createRepositoryResponse.repositoryId > 0;
    Project project = this.serverManager.getAdminGitLabApi().getProjectApi().getProject(createRepositoryResponse.repositoryId);
    assertNotNull(project);
    assertEquals(randomRepoName, project.getName());
    assertEquals(this.serverManager.getGitLabUser().getId(), project.getCreatorId());
    List<Project> repositoriesInGroup = this.serverManager.getAdminGitLabApi().getGroupApi().getProjects(group.getId());
    assertEquals(1, repositoriesInGroup.size());
    assertEquals(createRepositoryResponse.repositoryId, (int) repositoriesInGroup.get(0).getId());
}
Also used : Group(org.gitlab4j.api.models.Group) Project(org.gitlab4j.api.models.Project) IRemoteGitServerManager(de.catma.repository.git.interfaces.IRemoteGitServerManager) Test(org.junit.Test)

Example 9 with Project

use of org.gitlab4j.api.models.Project in project catma by forTEXT.

the class GitMarkupCollectionHandlerTest method tearDown.

@After
public void tearDown() throws Exception {
    if (this.directoriesToDeleteOnTearDown.size() > 0) {
        for (File dir : this.directoriesToDeleteOnTearDown) {
            FileUtils.deleteDirectory(dir);
        }
        this.directoriesToDeleteOnTearDown.clear();
    }
    if (this.markupCollectionReposToDeleteOnTearDown.size() > 0) {
        for (String markupCollectionId : this.markupCollectionReposToDeleteOnTearDown) {
            List<Project> projects = this.gitLabServerManager.getAdminGitLabApi().getProjectApi().getProjects(markupCollectionId);
            // this getProjects overload does a search
            for (Project project : projects) {
                this.gitLabServerManager.deleteRepository(project.getId());
            }
            await().until(() -> this.gitLabServerManager.getAdminGitLabApi().getProjectApi().getProjects().isEmpty());
        }
        this.markupCollectionReposToDeleteOnTearDown.clear();
    }
    if (this.projectsToDeleteOnTearDown.size() > 0) {
        GitProjectManager gitProjectHandler = new GitProjectManager(RepositoryPropertyKey.GitBasedRepositoryBasePath.getValue(), UserIdentification.userToMap(this.catmaUser.getIdentifier()));
        for (String projectId : this.projectsToDeleteOnTearDown) {
            gitProjectHandler.delete(projectId);
        }
        this.projectsToDeleteOnTearDown.clear();
    }
    // delete the GitLab user that the GitLabServerManager constructor in setUp would have
    // created - see GitLabServerManagerTest tearDown() for more info
    User user = this.gitLabServerManager.getGitLabUser();
    this.gitLabServerManager.getAdminGitLabApi().getUserApi().deleteUser(user.getId());
    GitLabServerManagerTest.awaitUserDeleted(this.gitLabServerManager.getAdminGitLabApi().getUserApi(), user.getId());
}
Also used : Project(org.gitlab4j.api.models.Project) User(org.gitlab4j.api.models.User) File(java.io.File) After(org.junit.After)

Example 10 with Project

use of org.gitlab4j.api.models.Project in project nivio by dedica-team.

the class GitLabRepoHandler method resolve.

/**
 * Loads the open issues from the repo.
 */
@Override
public CompletableFuture<ComponentDescription> resolve(Link link) {
    if (api == null) {
        return CompletableFuture.failedFuture(new RuntimeException("The GitLab API is not configured properly."));
    }
    String repoName = getRepoName(link);
    ItemDescription itemDescription = new ItemDescription();
    try {
        Project project = api.getProjectApi().getProject(repoName);
        String description = project.getDescription();
        itemDescription.setLabel(RepositoryLinkHandler.DESCRIPTION, description);
        itemDescription.setDescription(description);
        itemDescription.setLabel(RepositoryLinkHandler.OPEN_ISSUES, project.getOpenIssuesCount());
        String avatarUrl = project.getAvatarUrl();
        if (!StringUtils.isEmpty(avatarUrl)) {
            itemDescription.setLabel(RepositoryLinkHandler.ICON, avatarUrl);
            itemDescription.setIcon(avatarUrl);
        }
        int size = api.getMergeRequestApi().getMergeRequests(repoName).size();
        itemDescription.setLabel(RepositoryLinkHandler.OPEN_PRS, size);
    // TODO link collaborators
    } catch (Exception e) {
        LOGGER.warn("Failed to grab GitLab data from repo:" + e.getMessage(), e);
        return CompletableFuture.failedFuture(e);
    }
    return CompletableFuture.completedFuture(itemDescription);
}
Also used : Project(org.gitlab4j.api.models.Project) ItemDescription(de.bonndan.nivio.input.dto.ItemDescription)

Aggregations

Project (org.gitlab4j.api.models.Project)27 GitLabApiException (org.gitlab4j.api.GitLabApiException)18 IOException (java.io.IOException)12 GitLabApi (org.gitlab4j.api.GitLabApi)8 Group (org.gitlab4j.api.models.Group)6 ProvisioningException (com.tremolosecurity.provisioning.core.ProvisioningException)5 Workflow (com.tremolosecurity.provisioning.core.Workflow)5 GitlabUserProvider (com.tremolosecurity.unison.gitlab.provisioning.targets.GitlabUserProvider)5 GitMember (de.catma.repository.git.GitMember)4 ArrayList (java.util.ArrayList)4 EventBus (com.google.common.eventbus.EventBus)3 ItemDescription (de.bonndan.nivio.input.dto.ItemDescription)3 BackgroundService (de.catma.backgroundservice.BackgroundService)3 ForkStatus (de.catma.project.ForkStatus)3 CreateRepositoryResponse (de.catma.repository.git.CreateRepositoryResponse)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 Optional (java.util.Optional)3 Collectors (java.util.stream.Collectors)3