Search in sources :

Example 1 with GitLabApi

use of org.gitlab4j.api.GitLabApi in project OpenUnison by TremoloSecurity.

the class GitlabUserProvider method init.

@Override
public void init(Map<String, Attribute> cfg, ConfigManager cfgMgr, String name) throws ProvisioningException {
    this.token = cfg.get("token").getValues().get(0);
    this.url = cfg.get("url").getValues().get(0);
    this.name = name;
    this.gitLabApi = new GitLabApi(this.url, this.token);
    this.userApi = new UserApi(this.gitLabApi);
    this.groupApi = new GroupApi(this.gitLabApi);
    this.cfgMgr = cfgMgr;
}
Also used : GitLabApi(org.gitlab4j.api.GitLabApi) GroupApi(org.gitlab4j.api.GroupApi) UserApi(org.gitlab4j.api.UserApi)

Example 2 with GitLabApi

use of org.gitlab4j.api.GitLabApi in project octane-gitlab-service by MicroFocus.

the class MergeRequestHistoryHandler method executeFirstScan.

public void executeFirstScan() {
    try {
        List<Project> gitLabProjects = gitLabApi.getProjectApi().getProjects().stream().filter(project -> {
            Map<String, String> projectGroupVariables = VariablesHelper.getProjectGroupVariables(gitLabApi, project);
            Optional<Variable> shouldPublishToOctane = VariablesHelper.getProjectVariable(gitLabApi, project.getId(), applicationSettings.getConfig().getPublishMergeRequestsVariableName());
            return (shouldPublishToOctane.isPresent() && Boolean.parseBoolean(shouldPublishToOctane.get().getValue())) || (projectGroupVariables.containsKey(applicationSettings.getConfig().getPublishMergeRequestsVariableName()) && Boolean.parseBoolean(projectGroupVariables.get(applicationSettings.getConfig().getPublishMergeRequestsVariableName())));
        }).collect(Collectors.toList());
        gitLabProjects.forEach(project -> {
            try {
                Path pathToFile = Paths.get(watchPath.toString() + "/" + project.getId());
                if (!Files.exists(pathToFile)) {
                    sendMergeRequestsToOctane(project);
                    Files.createFile(pathToFile);
                }
            } catch (GitLabApiException | IOException e) {
                log.warn(e.getMessage(), e);
            }
        });
    } catch (GitLabApiException e) {
        log.error(e.getMessage(), e);
    }
}
Also used : Autowired(org.springframework.beans.factory.annotation.Autowired) DependsOn(org.springframework.context.annotation.DependsOn) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) WatchKey(java.nio.file.WatchKey) Variable(org.gitlab4j.api.models.Variable) StandardWatchEventKinds(java.nio.file.StandardWatchEventKinds) PullRequestHelper(com.microfocus.octane.gitlab.helpers.PullRequestHelper) Map(java.util.Map) MergeRequest(org.gitlab4j.api.models.MergeRequest) TaskExecutor(org.springframework.core.task.TaskExecutor) Path(java.nio.file.Path) Commit(org.gitlab4j.api.models.Commit) Files(java.nio.file.Files) VariablesHelper(com.microfocus.octane.gitlab.helpers.VariablesHelper) WatchEvent(java.nio.file.WatchEvent) IOException(java.io.IOException) ApplicationSettings(com.microfocus.octane.gitlab.app.ApplicationSettings) GitLabApiWrapper(com.microfocus.octane.gitlab.helpers.GitLabApiWrapper) Project(org.gitlab4j.api.models.Project) Collectors(java.util.stream.Collectors) Component(org.springframework.stereotype.Component) WatchService(java.nio.file.WatchService) List(java.util.List) Logger(org.apache.logging.log4j.Logger) Diff(org.gitlab4j.api.models.Diff) Paths(java.nio.file.Paths) Optional(java.util.Optional) GitLabApiException(org.gitlab4j.api.GitLabApiException) LogManager(org.apache.logging.log4j.LogManager) GitLabApi(org.gitlab4j.api.GitLabApi) FileSystems(java.nio.file.FileSystems) Path(java.nio.file.Path) Project(org.gitlab4j.api.models.Project) Optional(java.util.Optional) GitLabApiException(org.gitlab4j.api.GitLabApiException) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with GitLabApi

use of org.gitlab4j.api.GitLabApi in project legend-sdlc by finos.

the class AbstractGitLabApiTest method prepareGitLabUser.

/**
 * Create the proper users for authenticating the GitLab operations.
 */
protected static void prepareGitLabUser() throws LegendSDLCServerException {
    try {
        GitLabApi rootGitLabApi = GitLabApi.oauth2Login(TEST_HOST_URL, TEST_ADMIN_USERNAME, TEST_ADMIN_PASSWORD, null, null, true);
        Optional<User> testUser = rootGitLabApi.getUserApi().getOptionalUser(TEST_OWNER_USERNAME);
        if (!testUser.isPresent()) {
            User userSettings = new User().withUsername(TEST_OWNER_USERNAME).withEmail(TEST_OWNER_USERNAME + "@testUser.org").withName("Owner User").withSkipConfirmation(true).withIsAdmin(true);
            rootGitLabApi.getUserApi().createUser(userSettings, TEST_OWNER_PASSWORD, false);
            LOGGER.info("Created user with name {} and username {}", userSettings.getName(), userSettings.getUsername());
        }
        Optional<User> testMember = rootGitLabApi.getUserApi().getOptionalUser(TEST_MEMBER_USERNAME);
        if (!testMember.isPresent()) {
            User userSettings = new User().withUsername(TEST_MEMBER_USERNAME).withEmail(TEST_MEMBER_PASSWORD + "@testUser.org").withName("Member User").withSkipConfirmation(true).withIsAdmin(true);
            rootGitLabApi.getUserApi().createUser(userSettings, TEST_MEMBER_PASSWORD, false);
            LOGGER.info("Created user with name {} and username {}", userSettings.getName(), userSettings.getUsername());
        }
    } catch (GitLabApiException e) {
        StringBuilder builder = new StringBuilder("Error creating user for authentication; response status: ").append(e.getHttpStatus());
        String eMessage = e.getMessage();
        if (eMessage != null) {
            builder.append("; error message: ").append(eMessage);
        }
        if (e.hasValidationErrors()) {
            builder.append("; validation error(s): ").append(e.getValidationErrors());
        }
        throw new LegendSDLCServerException(builder.toString(), e);
    }
}
Also used : GitLabApi(org.gitlab4j.api.GitLabApi) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) User(org.gitlab4j.api.models.User) GitLabApiException(org.gitlab4j.api.GitLabApiException)

Example 4 with GitLabApi

use of org.gitlab4j.api.GitLabApi in project legend-sdlc by finos.

the class GitLabProjectApi method getPureGitLabProjectById.

private org.gitlab4j.api.models.Project getPureGitLabProjectById(GitLabProjectId projectId) {
    try {
        GitLabApi gitLabApi = getGitLabApi(projectId.getGitLabMode());
        org.gitlab4j.api.models.Project project = withRetries(() -> gitLabApi.getProjectApi().getProject(projectId.getGitLabId()));
        if (!isLegendSDLCProject(project)) {
            throw new LegendSDLCServerException("Unknown project: " + projectId, Status.NOT_FOUND);
        }
        return project;
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to get project " + projectId, () -> "Unknown project: " + projectId, () -> "Failed to get project " + projectId);
    }
}
Also used : GitLabApi(org.gitlab4j.api.GitLabApi) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException)

Example 5 with GitLabApi

use of org.gitlab4j.api.GitLabApi in project legend-sdlc by finos.

the class GitLabProjectApi method importProject.

@Override
public ImportReport importProject(String id, ProjectType type, String groupId, String artifactId) {
    LegendSDLCServerException.validateNonNull(id, "id may not be null");
    LegendSDLCServerException.validateNonNull(type, "type may not be null");
    LegendSDLCServerException.validate(groupId, ProjectStructure::isValidGroupId, g -> "Invalid groupId: " + g);
    LegendSDLCServerException.validate(artifactId, ProjectStructure::isValidArtifactId, a -> "Invalid artifactId: " + a);
    // Get project id
    GitLabProjectId projectId;
    if (id.chars().allMatch(Character::isDigit)) {
        projectId = GitLabProjectId.newProjectId(getGitLabModeFromProjectType(type), Integer.parseInt(id));
    } else {
        projectId = parseProjectId(id);
        if (projectId.getGitLabMode() != getGitLabModeFromProjectType(type)) {
            throw new LegendSDLCServerException("Invalid project id \"" + id + "\" for project type " + type, Status.BAD_REQUEST);
        }
    }
    // Find project
    GitLabApi gitLabApi = getGitLabApi(projectId.getGitLabMode());
    org.gitlab4j.api.ProjectApi gitLabProjectApi = gitLabApi.getProjectApi();
    org.gitlab4j.api.models.Project currentProject;
    try {
        currentProject = withRetries(() -> gitLabProjectApi.getProject(projectId.getGitLabId()));
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to access project " + id + " of type " + type, () -> "Could not find project " + id + " of type " + type, () -> "Failed to access project " + id + " of type " + type);
    }
    // Create a workspace for project configuration
    RepositoryApi repositoryApi = gitLabApi.getRepositoryApi();
    String workspaceId = "ProjectConfiguration_" + getRandomIdString();
    Branch workspaceBranch;
    try {
        workspaceBranch = GitLabApiTools.createBranchFromSourceBranchAndVerify(repositoryApi, projectId.getGitLabId(), getWorkspaceBranchName(workspaceId, WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE), MASTER_BRANCH, 30, 1_000);
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to create a workspace for initial configuration of project " + id + " of type " + type, () -> "Could not find project " + id + " of type " + type, () -> "Failed to create workspace for initial configuration of project " + id + " of type " + type);
    }
    if (workspaceBranch == null) {
        throw new LegendSDLCServerException("Failed to create workspace " + workspaceId + " in project " + projectId);
    }
    // Configure project in workspace
    ProjectFileAccessProvider projectFileAccessProvider = getProjectFileAccessProvider();
    Revision configRevision;
    try {
        ProjectConfiguration currentConfig = ProjectStructure.getProjectConfiguration(projectId.toString(), null, null, projectFileAccessProvider, WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE);
        ProjectConfigurationUpdateBuilder builder = ProjectConfigurationUpdateBuilder.newBuilder(projectFileAccessProvider, type, projectId.toString()).withWorkspace(workspaceId, WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE).withGroupId(groupId).withArtifactId(artifactId).withProjectStructureExtensionProvider(this.projectStructureExtensionProvider).withProjectStructurePlatformExtensions(this.projectStructurePlatformExtensions);
        int defaultProjectStructureVersion = getDefaultProjectStructureVersion();
        if (currentConfig == null) {
            // No current project structure: build a new one
            configRevision = builder.withProjectStructureVersion(defaultProjectStructureVersion).withMessage("Build project structure").buildProjectStructure();
        } else {
            // Existing project structure: update
            if (currentConfig.getProjectType() != type) {
                throw new LegendSDLCServerException("Mismatch between requested project type (" + type + ") and found project type (" + currentConfig.getProjectType() + ")", Status.BAD_REQUEST);
            }
            ProjectStructureVersion currentVersion = currentConfig.getProjectStructureVersion();
            if ((currentVersion == null) || (currentVersion.getVersion() < defaultProjectStructureVersion)) {
                builder.withProjectStructureVersion(defaultProjectStructureVersion).withProjectStructureExtensionVersion(null);
            }
            configRevision = builder.withMessage("Update project structure").updateProjectConfiguration();
        }
    } catch (Exception e) {
        // Try to delete the branch in case of exception
        deleteWorkspace(projectId, repositoryApi, workspaceId);
        throw e;
    }
    // Submit workspace changes, if any, for review
    String reviewId;
    if (configRevision == null) {
        // No changes: nothing to submit
        reviewId = null;
        // Try to delete the branch
        deleteWorkspace(projectId, repositoryApi, workspaceId);
    } else {
        MergeRequest mergeRequest;
        try {
            mergeRequest = gitLabApi.getMergeRequestApi().createMergeRequest(projectId.getGitLabId(), getWorkspaceBranchName(workspaceId, WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE), MASTER_BRANCH, "Project structure", "Set up project structure", null, null, null, null, true, false);
        } catch (Exception e) {
            // Try to delete the branch in case of exception
            deleteWorkspace(projectId, repositoryApi, workspaceId);
            throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to submit project configuration changes create a workspace for initial configuration of project " + id + " of type " + type, () -> "Could not find workspace " + workspaceId + " project " + id + " of type " + type, () -> "Failed to create a review for configuration of project " + id + " of type " + type);
        }
        reviewId = toStringIfNotNull(mergeRequest.getIid());
    }
    // Add tags
    Project finalProject;
    List<String> currentTags = currentProject.getTagList();
    if ((currentTags != null) && currentTags.stream().anyMatch(this::isLegendSDLCProjectTag)) {
        // already has the necessary tag
        finalProject = fromGitLabProject(currentProject, projectId.getGitLabMode());
    } else {
        List<String> updatedTags = Lists.mutable.ofInitialCapacity((currentTags == null) ? 1 : (currentTags.size() + 1));
        if (currentTags != null) {
            updatedTags.addAll(currentTags);
        }
        updatedTags.add(getLegendSDLCProjectTag());
        org.gitlab4j.api.models.Project updatedProject;
        try {
            updatedProject = gitLabProjectApi.updateProject(new org.gitlab4j.api.models.Project().withId(currentProject.getId()).withTagList(updatedTags));
        } catch (Exception e) {
            // Try to delete the branch in case of exception
            deleteWorkspace(projectId, repositoryApi, workspaceId);
            throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to import project " + id + " of type " + type, () -> "Could not find project " + id + " of type " + type, () -> "Failed to import project " + id + " of type " + type);
        }
        finalProject = fromGitLabProject(updatedProject, projectId.getGitLabMode());
    }
    return new ImportReport() {

        @Override
        public Project getProject() {
            return finalProject;
        }

        @Override
        public String getReviewId() {
            return reviewId;
        }
    };
}
Also used : GitLabApi(org.gitlab4j.api.GitLabApi) ProjectStructureVersion(org.finos.legend.sdlc.domain.model.project.configuration.ProjectStructureVersion) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) MergeRequest(org.gitlab4j.api.models.MergeRequest) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) Branch(org.gitlab4j.api.models.Branch) RepositoryApi(org.gitlab4j.api.RepositoryApi) ProjectStructure(org.finos.legend.sdlc.server.project.ProjectStructure) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) ProjectFileAccessProvider(org.finos.legend.sdlc.server.project.ProjectFileAccessProvider) Project(org.finos.legend.sdlc.domain.model.project.Project) Revision(org.finos.legend.sdlc.domain.model.revision.Revision) ProjectConfigurationUpdateBuilder(org.finos.legend.sdlc.server.project.ProjectConfigurationUpdateBuilder) ProjectConfiguration(org.finos.legend.sdlc.domain.model.project.configuration.ProjectConfiguration)

Aggregations

GitLabApi (org.gitlab4j.api.GitLabApi)27 GitLabApiException (org.gitlab4j.api.GitLabApiException)16 LegendSDLCServerException (org.finos.legend.sdlc.server.error.LegendSDLCServerException)14 GitLabProjectId (org.finos.legend.sdlc.server.gitlab.GitLabProjectId)8 ProvisioningException (com.tremolosecurity.provisioning.core.ProvisioningException)5 Workflow (com.tremolosecurity.provisioning.core.Workflow)5 GitlabUserProvider (com.tremolosecurity.unison.gitlab.provisioning.targets.GitlabUserProvider)5 Branch (org.gitlab4j.api.models.Branch)5 MergeRequest (org.gitlab4j.api.models.MergeRequest)5 Project (org.gitlab4j.api.models.Project)5 CommitsApi (org.gitlab4j.api.CommitsApi)4 CommitRef (org.gitlab4j.api.models.CommitRef)4 IOException (java.io.IOException)3 Collectors (java.util.stream.Collectors)3 ProjectFileAccessProvider (org.finos.legend.sdlc.server.project.ProjectFileAccessProvider)3 RepositoryApi (org.gitlab4j.api.RepositoryApi)3 ConfigStructure (com.microfocus.octane.gitlab.model.ConfigStructure)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2