Search in sources :

Example 26 with MergeRequest

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

the class GitLabReviewApi method editReview.

@Override
public Review editReview(String projectId, String reviewId, String title, String description, List<String> labels) {
    LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
    LegendSDLCServerException.validateNonNull(reviewId, "reviewId may not be null");
    LegendSDLCServerException.validateNonNull(title, "title may not be null");
    LegendSDLCServerException.validateNonNull(description, "description may not be null");
    GitLabProjectId gitLabProjectId = parseProjectId(projectId);
    GitLabApi gitLabApi = getGitLabApi(gitLabProjectId.getGitLabMode());
    MergeRequestApi mergeRequestApi = gitLabApi.getMergeRequestApi();
    MergeRequest mergeRequest = getReviewMergeRequest(mergeRequestApi, gitLabProjectId, reviewId);
    if (!isOpen(mergeRequest)) {
        throw new LegendSDLCServerException("Only open reviews can be edited: state of review " + mergeRequest.getIid() + " in project " + gitLabProjectId.toString() + " is " + getReviewState(mergeRequest));
    }
    try {
        MergeRequestParams mergeRequestParams = new MergeRequestParams().withTitle(title).withDescription(description);
        if (labels != null) {
            mergeRequestParams.withLabels(labels);
        }
        MergeRequest editedRequest = mergeRequestApi.updateMergeRequest(gitLabProjectId.getGitLabId(), mergeRequest.getIid(), mergeRequestParams);
        return fromGitLabMergeRequest(projectId, editedRequest);
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to edit review " + reviewId + " in project " + projectId, () -> "Unknown review in project " + projectId + ": " + reviewId, () -> "Error editing review " + reviewId + " in project " + projectId);
    }
}
Also used : GitLabApi(org.gitlab4j.api.GitLabApi) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) MergeRequest(org.gitlab4j.api.models.MergeRequest) MergeRequestParams(org.gitlab4j.api.models.MergeRequestParams) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) MergeRequestApi(org.gitlab4j.api.MergeRequestApi) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabApiException(org.gitlab4j.api.GitLabApiException)

Example 27 with MergeRequest

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

the class GitLabReviewApi method closeReview.

@Override
public Review closeReview(String projectId, String reviewId) {
    LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
    LegendSDLCServerException.validateNonNull(reviewId, "reviewId may not be null");
    GitLabProjectId gitLabProjectId = parseProjectId(projectId);
    MergeRequestApi mergeRequestApi = getGitLabApi(gitLabProjectId.getGitLabMode()).getMergeRequestApi();
    MergeRequest mergeRequest = getReviewMergeRequest(mergeRequestApi, gitLabProjectId, reviewId);
    validateMergeRequestReviewState(mergeRequest, ReviewState.OPEN);
    try {
        MergeRequest closeMergeRequest = updateMergeRequestState(mergeRequestApi, gitLabProjectId, mergeRequest, StateEvent.CLOSE);
        return fromGitLabMergeRequest(projectId, closeMergeRequest);
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to close review " + reviewId + " in project " + projectId, () -> "Unknown review in project " + projectId + ": " + reviewId, () -> "Error closing review " + reviewId + " in project " + projectId);
    }
}
Also used : MergeRequest(org.gitlab4j.api.models.MergeRequest) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) MergeRequestApi(org.gitlab4j.api.MergeRequestApi) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabApiException(org.gitlab4j.api.GitLabApiException)

Example 28 with MergeRequest

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

the class GitLabReviewApi method createReview.

@Override
public Review createReview(String projectId, String workspaceId, WorkspaceType workspaceType, String title, String description, List<String> labels) {
    LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
    LegendSDLCServerException.validateNonNull(workspaceId, "workspaceId may not be null");
    LegendSDLCServerException.validateNonNull(workspaceType, "workspaceType may not be null");
    LegendSDLCServerException.validateNonNull(title, "title may not be null");
    LegendSDLCServerException.validateNonNull(description, "description may not be null");
    ProjectFileAccessProvider.WorkspaceAccessType workspaceAccessType = ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE;
    try {
        GitLabProjectId gitLabProjectId = parseProjectId(projectId);
        String workspaceBranchName = getWorkspaceBranchName(workspaceId, workspaceType, workspaceAccessType);
        // TODO should we check for other merge requests for this workspace?
        MergeRequest mergeRequest = getGitLabApi(gitLabProjectId.getGitLabMode()).getMergeRequestApi().createMergeRequest(gitLabProjectId.getGitLabId(), workspaceBranchName, MASTER_BRANCH, title, description, null, null, (labels == null || labels.isEmpty()) ? null : labels.toArray(new String[0]), null, true);
        return fromGitLabMergeRequest(projectId, mergeRequest);
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to submit changes from " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " in project " + projectId + " for review", () -> "Unknown " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " (" + workspaceId + ") or project (" + projectId + ")", () -> "Error submitting changes from " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " in project " + projectId + " for review");
    }
}
Also used : ProjectFileAccessProvider(org.finos.legend.sdlc.server.project.ProjectFileAccessProvider) MergeRequest(org.gitlab4j.api.models.MergeRequest) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabApiException(org.gitlab4j.api.GitLabApiException)

Example 29 with MergeRequest

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

the class GitlabWorkflowApi method getReviewWorkflowAccessContext.

@Override
public WorkflowAccessContext getReviewWorkflowAccessContext(String projectId, String reviewId) {
    LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
    LegendSDLCServerException.validateNonNull(reviewId, "reviewId may not be null");
    GitLabProjectId gitLabProjectId = parseProjectId(projectId);
    MergeRequestApi mergeRequestApi = getGitLabApi(gitLabProjectId.getGitLabMode()).getMergeRequestApi();
    MergeRequest mergeRequest = getReviewMergeRequest(mergeRequestApi, gitLabProjectId, reviewId, true);
    WorkspaceInfo workspaceInfo = parseWorkspaceBranchName(mergeRequest.getSourceBranch());
    if (workspaceInfo == null) {
        throw new LegendSDLCServerException("Unknown review in project " + projectId + ": " + reviewId, Response.Status.NOT_FOUND);
    }
    return new GitLabWorkflowAccessContext(gitLabProjectId) {

        @Override
        protected String getRef() {
            return "refs/merge-requests/" + reviewId + "/head";
        }

        @Override
        protected String getRefInfoForException() {
            return "review " + reviewId + " of project " + projectId;
        }

        @Override
        protected ProjectFileAccessProvider.RevisionAccessContext getRevisionAccessContext() {
            return getProjectFileAccessProvider().getWorkspaceRevisionAccessContext(projectId, workspaceInfo.getWorkspaceId(), workspaceInfo.getWorkspaceType(), workspaceInfo.getWorkspaceAccessType());
        }
    };
}
Also used : ProjectFileAccessProvider(org.finos.legend.sdlc.server.project.ProjectFileAccessProvider) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) MergeRequest(org.gitlab4j.api.models.MergeRequest) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) MergeRequestApi(org.gitlab4j.api.MergeRequestApi)

Example 30 with MergeRequest

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

the class GitLabComparisonApi method getReviewComparison.

@Override
public Comparison getReviewComparison(String projectId, String reviewId) {
    LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
    LegendSDLCServerException.validateNonNull(reviewId, "reviewId may not be null");
    GitLabProjectId gitLabProjectId = parseProjectId(projectId);
    RepositoryApi repositoryApi = getGitLabApi(gitLabProjectId.getGitLabMode()).getRepositoryApi();
    MergeRequest mergeRequest = getReviewMergeRequest(getGitLabApi(gitLabProjectId.getGitLabMode()).getMergeRequestApi(), gitLabProjectId, reviewId);
    WorkspaceInfo workspaceInfo = parseWorkspaceBranchName(mergeRequest.getSourceBranch());
    if (workspaceInfo == null) {
        throw new LegendSDLCServerException("Unknown review in project " + projectId + ": " + reviewId, Response.Status.NOT_FOUND);
    }
    DiffRef diffRef = mergeRequest.getDiffRefs();
    if ((diffRef == null) || (diffRef.getStartSha() == null) || (diffRef.getHeadSha() == null)) {
        throw new LegendSDLCServerException("Unable to get revision info for review " + reviewId + " in project " + projectId);
    }
    String fromRevisionId = diffRef.getStartSha();
    String toRevisionId = diffRef.getHeadSha();
    ProjectStructure fromProjectStructure = getProjectStructure(projectId, workspaceInfo.getWorkspaceId(), fromRevisionId, workspaceInfo.getWorkspaceType(), workspaceInfo.getWorkspaceAccessType());
    ProjectStructure toProjectStructure = getProjectStructure(gitLabProjectId.toString(), null, toRevisionId, null, null);
    return getComparisonResult(gitLabProjectId, repositoryApi, fromRevisionId, toRevisionId, fromProjectStructure, toProjectStructure);
}
Also used : LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) MergeRequest(org.gitlab4j.api.models.MergeRequest) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) RepositoryApi(org.gitlab4j.api.RepositoryApi) ProjectStructure(org.finos.legend.sdlc.server.project.ProjectStructure) DiffRef(org.gitlab4j.api.models.DiffRef)

Aggregations

MergeRequest (org.gitlab4j.api.models.MergeRequest)32 GitLabProjectId (org.finos.legend.sdlc.server.gitlab.GitLabProjectId)25 LegendSDLCServerException (org.finos.legend.sdlc.server.error.LegendSDLCServerException)22 GitLabApiException (org.gitlab4j.api.GitLabApiException)17 MergeRequestApi (org.gitlab4j.api.MergeRequestApi)15 RepositoryApi (org.gitlab4j.api.RepositoryApi)9 ProjectFileAccessProvider (org.finos.legend.sdlc.server.project.ProjectFileAccessProvider)7 DiffRef (org.gitlab4j.api.models.DiffRef)7 Response (javax.ws.rs.core.Response)6 GitLabApi (org.gitlab4j.api.GitLabApi)6 List (java.util.List)5 Form (javax.ws.rs.core.Form)5 Project (org.finos.legend.sdlc.domain.model.project.Project)5 ProjectType (org.finos.legend.sdlc.domain.model.project.ProjectType)5 Review (org.finos.legend.sdlc.domain.model.review.Review)5 Entity (org.finos.legend.sdlc.domain.model.entity.Entity)4 Workspace (org.finos.legend.sdlc.domain.model.project.workspace.Workspace)4 ProjectStructure (org.finos.legend.sdlc.server.project.ProjectStructure)2 Branch (org.gitlab4j.api.models.Branch)2 MergeRequestParams (org.gitlab4j.api.models.MergeRequestParams)2