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);
}
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());
}
});
}
}
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;
}
}
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);
}
}
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);
}
Aggregations