use of org.gitlab4j.api.models.MergeRequest in project choerodon-starters by open-hand.
the class MergeRequestApi method createMergeRequest.
/**
* Creates a merge request.
*
* <pre><code>GitLab Endpoint: POST /projects/:id/merge_requests</code></pre>
*
* @param projectIdOrPath the project in the form of an Integer(ID), String(path), or Project instance
* @param params a MergeRequestParams instance holding the info to create the merge request
* @return the created MergeRequest instance
* @throws GitLabApiException if any exception occurs
* @since GitLab Starter 8.17, GitLab CE 11.0.
*/
public MergeRequest createMergeRequest(Object projectIdOrPath, MergeRequestParams params) throws GitLabApiException {
GitLabApiForm form = params.getForm(true);
Response response = post(Response.Status.CREATED, form, "projects", getProjectIdOrPath(projectIdOrPath), "merge_requests");
return (response.readEntity(MergeRequest.class));
}
use of org.gitlab4j.api.models.MergeRequest in project choerodon-starters by open-hand.
the class MergeRequestApi method updateMergeRequest.
/**
* Updates an existing merge request. You can change branches, title, or even close the MR.
* <p>
* PUT /projects/:id/merge_requests/:merge_request_id
*
* @param projectId the ID of a project
* @param mergeRequestId the internal ID of the merge request to update
* @param targetBranch the target branch, optional
* @param title the title for the merge request
* @param assigneeId the Assignee user ID, optional
* @param description the description of the merge request, optional
* @param stateEvent new state for the merge request, optional
* @param labels comma separated list of labels, optional
* @param milestoneId the ID of a milestone, optional
* @return the updated merge request
* @throws GitLabApiException if any exception occurs
*/
public MergeRequest updateMergeRequest(Integer projectId, Integer mergeRequestId, String targetBranch, String title, Integer assigneeId, String description, StateEvent stateEvent, String labels, Integer milestoneId) throws GitLabApiException {
if (projectId == null) {
throw new GitLabApiException("projectId cannot be null");
}
if (mergeRequestId == null) {
throw new GitLabApiException("mergeRequestId cannot be null");
}
Form formData = new GitLabApiForm().withParam("target_branch", targetBranch).withParam("title", title).withParam("assignee_id", assigneeId).withParam("description", description).withParam("state_event", stateEvent).withParam("labels", labels).withParam("milestone_id", milestoneId);
Response response = put(Response.Status.OK, formData.asMap(), "projects", projectId, "merge_requests", mergeRequestId);
return (response.readEntity(MergeRequest.class));
}
use of org.gitlab4j.api.models.MergeRequest in project legend-sdlc by finos.
the class GitLabEntityApiTestResource method runEntitiesInNormalGroupWorkspaceWorkflowTest.
public void runEntitiesInNormalGroupWorkspaceWorkflowTest() throws GitLabApiException {
String projectName = "CommitFlowTestProjectTwo";
String description = "A test project.";
ProjectType projectType = ProjectType.PRODUCTION;
String groupId = "org.finos.sdlc.test";
String artifactId = "entitytestprojtwo";
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.newGroupWorkspace(projectId, workspaceName);
String workspaceId = createdWorkspace.getWorkspaceId();
List<Entity> initialWorkspaceEntities = gitLabEntityApi.getGroupWorkspaceEntityAccessContext(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.getGroupWorkspaceEntityModificationContext(projectId, workspaceId).createEntity(entityPath, classifierPath, entityContentMap, "initial entity");
List<Entity> modifiedWorkspaceEntities = gitLabEntityApi.getGroupWorkspaceEntityAccessContext(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.getGroupWorkspaceEntityModificationContext(projectId, workspaceId).updateEntity(entityPath, classifierPath, newEntityContentMap, "update entity");
List<Entity> updatedWorkspaceEntities = gitLabEntityApi.getGroupWorkspaceEntityAccessContext(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.getGroupWorkspaceEntityModificationContext(projectId, workspaceId).createEntity(entityPathTwo, classifierPathTwo, newEntityContentMapTwo, "second entity");
List<Entity> postAddWorkspaceEntities = gitLabEntityApi.getGroupWorkspaceEntityAccessContext(projectId, workspaceId).getEntities(null, null, null);
Assert.assertNotNull(postAddWorkspaceEntities);
Assert.assertEquals(2, postAddWorkspaceEntities.size());
gitLabEntityApi.getGroupWorkspaceEntityModificationContext(projectId, workspaceId).deleteEntity(entityPath, classifierPath);
List<Entity> postDeleteWorkspaceEntities = gitLabEntityApi.getGroupWorkspaceEntityAccessContext(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.getGroupWorkspaceEntityAccessContext(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.GROUP, "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());
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);
}
use of org.gitlab4j.api.models.MergeRequest in project legend-sdlc by finos.
the class GitLabWorkspaceApiTestResource method runUpdateGroupWorkspaceWithRebaseNoConflictTest.
public void runUpdateGroupWorkspaceWithRebaseNoConflictTest() throws GitLabApiException {
// Create new workspace from previous HEAD
String projectName = "WorkspaceTestProjectThree";
String description = "A test project.";
ProjectType projectType = ProjectType.PRODUCTION;
String groupId = "org.finos.sdlc.test";
String artifactId = "testworkprojthree";
List<String> tags = Lists.mutable.with("doe", "moffitt", AbstractGitLabServerApiTest.INTEGRATION_TEST_PROJECT_TAG);
String workspaceName = "workspaceone";
Project createdProject = gitLabProjectApi.createProject(projectName, description, projectType, groupId, artifactId, tags);
String projectId = createdProject.getProjectId();
Workspace createdWorkspace = gitLabWorkspaceApi.newGroupWorkspace(projectId, workspaceName);
String workspaceId = createdWorkspace.getWorkspaceId();
List<Entity> initialWorkspaceEntities = gitLabEntityApi.getGroupWorkspaceEntityAccessContext(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);
// Create another workspace, commit, review, merge to move project HEAD forward -- use workspace two
String workspaceTwoName = "workspacetwo";
Workspace createdWorkspaceTwo = gitLabWorkspaceApi.newGroupWorkspace(projectId, workspaceTwoName);
String workspaceTwoId = createdWorkspaceTwo.getWorkspaceId();
List<Entity> initialWorkspaceTwoEntities = gitLabEntityApi.getGroupWorkspaceEntityAccessContext(projectId, workspaceTwoId).getEntities(null, null, null);
Assert.assertEquals(Collections.emptyList(), initialWorkspaceTwoEntities);
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.getGroupWorkspaceEntityModificationContext(projectId, workspaceTwoId).createEntity(entityPath, classifierPath, entityContentMap, "initial entity");
List<Entity> modifiedWorkspaceEntities = gitLabEntityApi.getGroupWorkspaceEntityAccessContext(projectId, workspaceTwoId).getEntities(null, null, null);
Assert.assertNotNull(modifiedWorkspaceEntities);
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);
List<String> labels = Collections.singletonList("default");
Review testReview = gitLabCommitterReviewApi.createReview(projectId, workspaceTwoId, WorkspaceType.GROUP, "Add Courses.", "add two math 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());
GitLabProjectId sdlcGitLabProjectId = GitLabProjectId.parseProjectId(projectId);
MergeRequestApi mergeRequestApi = gitLabMemberUserContext.getGitLabAPI(sdlcGitLabProjectId.getGitLabMode()).getMergeRequestApi();
Integer parsedMergeRequestId = Integer.parseInt(reviewId);
Integer 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()), branches -> GitLabApiTestSetupUtil.hasOnlyBranchesWithNames(branches, Lists.mutable.of(workspaceName, "master")), 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", workspaceTwoName, callUntilBranchDeleted.getTryCount());
}
LOGGER.info("Waited {} times for branch {} to be deleted post merge", callUntilBranchDeleted.getTryCount(), workspaceTwoName);
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(), entityPath);
Assert.assertEquals(projectEntity.getClassifierPath(), classifierPath);
Assert.assertEquals(projectEntity.getContent(), entityContentMap);
// Create changes and make change in workspace branch -- use workspace
Map<String, String> currentEntityContentMap = Maps.mutable.with("package", "test", "name", "entity", "math-113", "abstract-algebra", "math-185", "complex-analysis");
gitLabEntityApi.getGroupWorkspaceEntityModificationContext(projectId, workspaceId).createEntity(entityPath, classifierPath, currentEntityContentMap, "initial entity");
List<Entity> modifiedWorkspaceEntitiesNew = gitLabEntityApi.getGroupWorkspaceEntityAccessContext(projectId, workspaceId).getEntities(null, null, null);
Assert.assertNotNull(modifiedWorkspaceEntitiesNew);
Assert.assertEquals(1, modifiedWorkspaceEntities.size());
Entity initalEntityNew = modifiedWorkspaceEntitiesNew.get(0);
Assert.assertEquals(initalEntityNew.getPath(), entityPath);
Assert.assertEquals(initalEntityNew.getClassifierPath(), classifierPath);
Assert.assertEquals(initalEntityNew.getContent(), currentEntityContentMap);
// Update workspace branch and trigger rebase
gitLabWorkspaceApi.updateGroupWorkspace(projectId, workspaceId);
List<Entity> updatedWorkspaceEntities = gitLabEntityApi.getGroupWorkspaceEntityAccessContext(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(), currentEntityContentMap);
}
use of org.gitlab4j.api.models.MergeRequest in project legend-sdlc by finos.
the class GitLabProjectConfigurationApi method getReviewToProjectConfiguration.
@Override
public ProjectConfiguration getReviewToProjectConfiguration(String projectId, String reviewId) {
LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
LegendSDLCServerException.validateNonNull(reviewId, "reviewId may not be null");
GitLabProjectId gitLabProjectId = parseProjectId(projectId);
MergeRequest mergeRequest = getReviewMergeRequest(getGitLabApi(gitLabProjectId.getGitLabMode()).getMergeRequestApi(), gitLabProjectId, reviewId);
validateMergeRequestForComparison(mergeRequest);
DiffRef diffRef = mergeRequest.getDiffRefs();
if (diffRef != null && diffRef.getStartSha() != null && diffRef.getHeadSha() != null) {
String revisionId = diffRef.getHeadSha();
return getProjectRevisionProjectConfiguration(projectId, revisionId);
} else {
throw new LegendSDLCServerException("Unable to get [to] revision info in project " + projectId + " for review " + reviewId);
}
}
Aggregations