Search in sources :

Example 6 with GitLabApiException

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

the class GitlabServices method init.

@PostConstruct
private void init() throws MalformedURLException {
    // Adding webHooks
    initWebHookListenerURL();
    gitLabApi = gitLabApiWrapper.getGitLabApi();
    try {
        List<Project> projects = isCurrentUserAdmin() ? gitLabApi.getProjectApi().getProjects() : gitLabApi.getProjectApi().getMemberProjects();
        User currentUser = gitLabApi.getUserApi().getCurrentUser();
        if (cleanupOnly) {
            log.info("start with cleanup process");
            for (Project project : projects) {
                if (gitLabApiWrapper.isUserHasPermissionForProject(project, currentUser)) {
                    deleteWebHooks(project.getId());
                }
            }
        } else {
            for (Project project : projects) {
                if (gitLabApiWrapper.isUserHasPermissionForProject(project, currentUser)) {
                    addWebHookToProject(project.getId(), true);
                }
            }
        }
    } catch (GitLabApiException e) {
        log.warn("Failed to create GitLab web hooks", e);
        throw new RuntimeException(e);
    }
    // start cleanUp thread
    executor = Executors.newSingleThreadScheduledExecutor();
    executor.scheduleAtFixedRate(new TestResultsCleanUpRunnable(applicationSettings.getConfig().getTestResultsOutputFolderPath()), TestResultsCleanUpRunnable.INTERVAL, TestResultsCleanUpRunnable.INTERVAL, TimeUnit.MINUTES);
}
Also used : Project(org.gitlab4j.api.models.Project) User(org.gitlab4j.api.models.User) GitLabApiException(org.gitlab4j.api.GitLabApiException) TestResultsCleanUpRunnable(com.microfocus.octane.gitlab.testresults.TestResultsCleanUpRunnable) PostConstruct(javax.annotation.PostConstruct)

Example 7 with GitLabApiException

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

the class EventListener method sendCodeCoverage.

private void sendCodeCoverage(Integer projectId, Project project, Job job) throws GitLabApiException, IOException {
    Optional<Variable> coverageReportFilePathVar = VariablesHelper.getProjectVariable(gitLabApi, project.getId(), applicationSettings.getConfig().getGeneratedCoverageReportFilePathVariableName());
    Map<String, String> projectGroupVariables = VariablesHelper.getProjectGroupVariables(gitLabApi, project);
    if (coverageReportFilePathVar.isEmpty() && !projectGroupVariables.containsKey(applicationSettings.getConfig().getGeneratedCoverageReportFilePathVariableName())) {
        log.info("Variable for JaCoCo coverage report path not set. No coverage injection for this pipeline.");
    } else {
        String coverageReportFilePath = coverageReportFilePathVar.isPresent() ? coverageReportFilePathVar.get().getValue() : projectGroupVariables.get(applicationSettings.getConfig().getGeneratedCoverageReportFilePathVariableName());
        String octaneJobId = project.getPathWithNamespace().toLowerCase() + "/" + job.getName();
        String octaneBuildId = job.getId().toString();
        OctaneSDK.getClients().forEach(client -> {
            try (InputStream codeCoverageReportFile = gitLabApi.getJobApi().downloadSingleArtifactsFile(projectId, job.getId(), Paths.get(coverageReportFilePath))) {
                client.getCoverageService().pushCoverage(octaneJobId, octaneBuildId, CoverageReportType.JACOCOXML, codeCoverageReportFile);
            } catch (GitLabApiException | IOException exception) {
                log.error(exception.getMessage());
            }
        });
    }
}
Also used : InputStream(java.io.InputStream) GitLabApiException(org.gitlab4j.api.GitLabApiException) IOException(java.io.IOException)

Example 8 with GitLabApiException

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

the class EventListener method getScmData.

private SCMData getScmData(JSONObject event) {
    try {
        Integer projectId = event.getJSONObject("project").getInt("id");
        String sha = event.getJSONObject("object_attributes").getString("sha");
        String beforeSha = event.getJSONObject("object_attributes").getString("before_sha");
        CompareResults results = gitLabApi.getRepositoryApi().compare(projectId, beforeSha, sha);
        List<SCMCommit> commits = new ArrayList<>();
        results.getCommits().forEach(c -> {
            SCMCommit commit = dtoFactory.newDTO(SCMCommit.class);
            commit.setTime(c.getTimestamp() != null ? c.getTimestamp().getTime() : new Date().getTime());
            commit.setUser(c.getCommitterName());
            commit.setUserEmail(c.getCommitterEmail());
            commit.setRevId(c.getId());
            commit.setParentRevId(sha);
            commit.setComment(c.getMessage());
            try {
                List<Diff> diffs = gitLabApi.getCommitsApi().getDiff(projectId, c.getId());
                List<SCMChange> changes = new ArrayList<>();
                diffs.forEach(d -> {
                    SCMChange change = dtoFactory.newDTO(SCMChange.class);
                    change.setFile(d.getNewPath());
                    change.setType(d.getNewFile() ? "add" : d.getDeletedFile() ? "delete" : "edit");
                    changes.add(change);
                });
                commit.setChanges(changes);
            } catch (GitLabApiException e) {
                log.warn("Failed to add a commit to the SCM data", e);
            }
            commits.add(commit);
        });
        SCMRepository repo = dtoFactory.newDTO(SCMRepository.class);
        repo.setType(SCMType.GIT);
        repo.setUrl(event.getJSONObject("project").getString("git_http_url"));
        repo.setBranch(getBranchName(event));
        SCMData data = dtoFactory.newDTO(SCMData.class);
        data.setRepository(repo);
        data.setBuiltRevId(sha);
        data.setCommits(commits);
        return data;
    } catch (GitLabApiException e) {
        log.warn("Failed to return the SCM data. Returning null.");
        return null;
    }
}
Also used : GitLabApiException(org.gitlab4j.api.GitLabApiException) SCMData(com.hp.octane.integrations.dto.scm.SCMData) SCMCommit(com.hp.octane.integrations.dto.scm.SCMCommit) SCMChange(com.hp.octane.integrations.dto.scm.SCMChange) SCMRepository(com.hp.octane.integrations.dto.scm.SCMRepository)

Example 9 with GitLabApiException

use of org.gitlab4j.api.GitLabApiException 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 10 with GitLabApiException

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

the class GitLabEntityApiTestResource method runEntitiesInNormalUserWorkspaceWorkflowTest.

public void runEntitiesInNormalUserWorkspaceWorkflowTest() throws GitLabApiException {
    String projectName = "CommitFlowTestProject";
    String description = "A test project.";
    ProjectType projectType = ProjectType.PRODUCTION;
    String groupId = "org.finos.sdlc.test";
    String artifactId = "entitytestproj";
    List<String> tags = Lists.mutable.with("doe", "moffitt", AbstractGitLabServerApiTest.INTEGRATION_TEST_PROJECT_TAG);
    String workspaceName = "entitytestworkspace";
    Project createdProject = gitLabProjectApi.createProject(projectName, description, projectType, groupId, artifactId, tags);
    String projectId = createdProject.getProjectId();
    Workspace createdWorkspace = gitLabWorkspaceApi.newUserWorkspace(projectId, workspaceName);
    String workspaceId = createdWorkspace.getWorkspaceId();
    List<Entity> initialWorkspaceEntities = gitLabEntityApi.getUserWorkspaceEntityAccessContext(projectId, workspaceId).getEntities(null, null, null);
    List<Entity> initialProjectEntities = gitLabEntityApi.getProjectEntityAccessContext(projectId).getEntities(null, null, null);
    Assert.assertEquals(Collections.emptyList(), initialWorkspaceEntities);
    Assert.assertEquals(Collections.emptyList(), initialProjectEntities);
    String entityPath = "test::entity";
    String classifierPath = "meta::test::mathematicsDepartment";
    Map<String, String> entityContentMap = Maps.mutable.with("package", "test", "name", "entity", "math-113", "abstract-algebra", "math-185", "complex-analysis");
    gitLabEntityApi.getUserWorkspaceEntityModificationContext(projectId, workspaceId).createEntity(entityPath, classifierPath, entityContentMap, "initial entity");
    List<Entity> modifiedWorkspaceEntities = gitLabEntityApi.getUserWorkspaceEntityAccessContext(projectId, workspaceId).getEntities(null, null, null);
    List<Entity> modifiedProjectEntities = gitLabEntityApi.getProjectEntityAccessContext(projectId).getEntities(null, null, null);
    Assert.assertNotNull(modifiedWorkspaceEntities);
    Assert.assertEquals(Collections.emptyList(), modifiedProjectEntities);
    Assert.assertEquals(1, modifiedWorkspaceEntities.size());
    Entity initalEntity = modifiedWorkspaceEntities.get(0);
    Assert.assertEquals(initalEntity.getPath(), entityPath);
    Assert.assertEquals(initalEntity.getClassifierPath(), classifierPath);
    Assert.assertEquals(initalEntity.getContent(), entityContentMap);
    Map<String, String> newEntityContentMap = Maps.mutable.with("package", "test", "name", "entity", "math-128", "numerical-analysis", "math-110", "linear-algebra");
    gitLabEntityApi.getUserWorkspaceEntityModificationContext(projectId, workspaceId).updateEntity(entityPath, classifierPath, newEntityContentMap, "update entity");
    List<Entity> updatedWorkspaceEntities = gitLabEntityApi.getUserWorkspaceEntityAccessContext(projectId, workspaceId).getEntities(null, null, null);
    Assert.assertNotNull(updatedWorkspaceEntities);
    Assert.assertEquals(1, updatedWorkspaceEntities.size());
    Entity updatedEntity = updatedWorkspaceEntities.get(0);
    Assert.assertEquals(updatedEntity.getPath(), entityPath);
    Assert.assertEquals(updatedEntity.getClassifierPath(), classifierPath);
    Assert.assertEquals(updatedEntity.getContent(), newEntityContentMap);
    String entityPathTwo = "testtwo::entitytwo";
    String classifierPathTwo = "meta::test::csDepartment";
    Map<String, String> newEntityContentMapTwo = Maps.mutable.with("package", "testtwo", "name", "entitytwo", "cs-194", "computational-imaging", "cs-189", "machine-learning");
    gitLabEntityApi.getUserWorkspaceEntityModificationContext(projectId, workspaceId).createEntity(entityPathTwo, classifierPathTwo, newEntityContentMapTwo, "second entity");
    List<Entity> postAddWorkspaceEntities = gitLabEntityApi.getUserWorkspaceEntityAccessContext(projectId, workspaceId).getEntities(null, null, null);
    Assert.assertNotNull(postAddWorkspaceEntities);
    Assert.assertEquals(2, postAddWorkspaceEntities.size());
    gitLabEntityApi.getUserWorkspaceEntityModificationContext(projectId, workspaceId).deleteEntity(entityPath, classifierPath);
    List<Entity> postDeleteWorkspaceEntities = gitLabEntityApi.getUserWorkspaceEntityAccessContext(projectId, workspaceId).getEntities(null, null, null);
    Assert.assertNotNull(postDeleteWorkspaceEntities);
    Assert.assertEquals(1, postDeleteWorkspaceEntities.size());
    Entity remainedEntity = postDeleteWorkspaceEntities.get(0);
    Assert.assertEquals(remainedEntity.getPath(), entityPathTwo);
    Assert.assertEquals(remainedEntity.getClassifierPath(), classifierPathTwo);
    Assert.assertEquals(remainedEntity.getContent(), newEntityContentMapTwo);
    List<String> paths = gitLabEntityApi.getUserWorkspaceEntityAccessContext(projectId, workspaceId).getEntityPaths(null, null, null);
    Assert.assertNotNull(paths);
    Assert.assertEquals(1, paths.size());
    Assert.assertEquals(entityPathTwo, paths.get(0));
    List<String> labels = Collections.singletonList("default");
    Review testReview = gitLabCommitterReviewApi.createReview(projectId, workspaceId, WorkspaceType.USER, "Add Courses.", "add two courses", labels);
    String reviewId = testReview.getId();
    Review approvedReview = gitLabApproverReviewApi.approveReview(projectId, reviewId);
    Assert.assertNotNull(approvedReview);
    Assert.assertEquals(reviewId, approvedReview.getId());
    Assert.assertEquals(ReviewState.OPEN, approvedReview.getState());
    Assert.assertEquals(labels, approvedReview.getLabels());
    GitLabProjectId sdlcGitLabProjectId = GitLabProjectId.parseProjectId(projectId);
    MergeRequestApi mergeRequestApi = gitLabMemberUserContext.getGitLabAPI(sdlcGitLabProjectId.getGitLabMode()).getMergeRequestApi();
    int parsedMergeRequestId = Integer.parseInt(reviewId);
    int gitlabProjectId = sdlcGitLabProjectId.getGitLabId();
    String requiredStatus = "can_be_merged";
    CallUntil<MergeRequest, GitLabApiException> callUntil = CallUntil.callUntil(() -> mergeRequestApi.getMergeRequest(gitlabProjectId, parsedMergeRequestId), mr -> requiredStatus.equals(mr.getMergeStatus()), 20, 1000);
    if (!callUntil.succeeded()) {
        throw new RuntimeException("Merge request " + approvedReview.getId() + " still does not have status \"" + requiredStatus + "\" after " + callUntil.getTryCount() + " tries");
    }
    LOGGER.info("Waited {} times for merge to have status \"{}\"", callUntil.getTryCount(), requiredStatus);
    gitLabCommitterReviewApi.commitReview(projectId, reviewId, "add two math courses");
    String requiredMergedStatus = "merged";
    CallUntil<MergeRequest, GitLabApiException> callUntilMerged = CallUntil.callUntil(() -> mergeRequestApi.getMergeRequest(gitlabProjectId, parsedMergeRequestId), mr -> requiredMergedStatus.equals(mr.getState()), 10, 500);
    if (!callUntilMerged.succeeded()) {
        throw new RuntimeException("Merge request " + reviewId + " still does not have state \"" + requiredMergedStatus + "\" after " + callUntilMerged.getTryCount() + " tries");
    }
    LOGGER.info("Waited {} times for merge request to have state \"{}\"", callUntilMerged.getTryCount(), requiredMergedStatus);
    RepositoryApi repositoryApi = gitLabMemberUserContext.getGitLabAPI(sdlcGitLabProjectId.getGitLabMode()).getRepositoryApi();
    CallUntil<List<Branch>, GitLabApiException> callUntilBranchDeleted = CallUntil.callUntil(() -> repositoryApi.getBranches(sdlcGitLabProjectId.getGitLabId()), GitLabEntityApiTestResource::hasOnlyMasterBranch, 15, 1000);
    if (!callUntilBranchDeleted.succeeded()) {
        // Warn instead of throwing exception since we cannot manage time expectation on GitLab to reflect branch deletion.
        LOGGER.warn("Branch is still not deleted post merge after {} tries", callUntilBranchDeleted.getTryCount());
    }
    LOGGER.info("Waited {} times for branch to be deleted post merge", callUntilBranchDeleted.getTryCount());
    List<Entity> postCommitProjectEntities = gitLabEntityApi.getProjectEntityAccessContext(projectId).getEntities(null, null, null);
    Assert.assertNotNull(postCommitProjectEntities);
    Assert.assertEquals(1, postCommitProjectEntities.size());
    Entity projectEntity = postCommitProjectEntities.get(0);
    Assert.assertEquals(projectEntity.getPath(), entityPathTwo);
    Assert.assertEquals(projectEntity.getClassifierPath(), classifierPathTwo);
    Assert.assertEquals(projectEntity.getContent(), newEntityContentMapTwo);
}
Also used : Entity(org.finos.legend.sdlc.domain.model.entity.Entity) GitLabApiException(org.gitlab4j.api.GitLabApiException) Review(org.finos.legend.sdlc.domain.model.review.Review) Project(org.finos.legend.sdlc.domain.model.project.Project) MergeRequest(org.gitlab4j.api.models.MergeRequest) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) ProjectType(org.finos.legend.sdlc.domain.model.project.ProjectType) MergeRequestApi(org.gitlab4j.api.MergeRequestApi) RepositoryApi(org.gitlab4j.api.RepositoryApi) List(java.util.List) Workspace(org.finos.legend.sdlc.domain.model.project.workspace.Workspace)

Aggregations

GitLabApiException (org.gitlab4j.api.GitLabApiException)77 IOException (java.io.IOException)35 GitLabApi (org.gitlab4j.api.GitLabApi)18 Project (org.gitlab4j.api.models.Project)18 Group (org.gitlab4j.api.models.Group)14 List (java.util.List)12 GitLabProjectId (org.finos.legend.sdlc.server.gitlab.GitLabProjectId)9 MergeRequest (org.gitlab4j.api.models.MergeRequest)9 GitMember (de.catma.repository.git.GitMember)8 ArrayList (java.util.ArrayList)8 LegendSDLCServerException (org.finos.legend.sdlc.server.error.LegendSDLCServerException)8 MergeRequestApi (org.gitlab4j.api.MergeRequestApi)8 UserApi (org.gitlab4j.api.UserApi)8 ProvisioningException (com.tremolosecurity.provisioning.core.ProvisioningException)7 Workflow (com.tremolosecurity.provisioning.core.Workflow)7 GroupApi (org.gitlab4j.api.GroupApi)7 IssuesApi (org.gitlab4j.api.IssuesApi)7 AccessLevel (org.gitlab4j.api.models.AccessLevel)7 Issue (org.gitlab4j.api.models.Issue)7 Comment (de.catma.document.comment.Comment)6