use of org.finos.legend.sdlc.server.error.LegendSDLCServerException in project legend-sdlc by finos.
the class GitLabReviewApi method getReviewUpdateStatus.
@Override
public ReviewUpdateStatus getReviewUpdateStatus(String projectId, String reviewId) {
LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
LegendSDLCServerException.validateNonNull(reviewId, "reviewId may not be null");
GitLabProjectId gitLabProjectId = parseProjectId(projectId);
GitLabApi gitLabApi = getGitLabApi(gitLabProjectId.getGitLabMode());
MergeRequest mergeRequest = getReviewMergeRequest(gitLabApi.getMergeRequestApi(), gitLabProjectId, reviewId);
if (!(isOpen(mergeRequest) || isLocked(mergeRequest))) {
throw new LegendSDLCServerException("Cannot get update status for review " + mergeRequest.getIid() + " in project " + projectId + ": state is " + getReviewState(mergeRequest), Status.CONFLICT);
}
return getReviewUpdateStatus(gitLabProjectId, gitLabApi, mergeRequest);
}
use of org.finos.legend.sdlc.server.error.LegendSDLCServerException in project legend-sdlc by finos.
the class GitLabReviewApi method approveReview.
@Override
public Review approveReview(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);
try {
MergeRequest approvalMergeRequest = mergeRequestApi.approveMergeRequest(gitLabProjectId.getGitLabId(), mergeRequest.getIid(), mergeRequest.getSha());
// The MergeRequest that comes back from the approveMergeRequest call is not adequate for
// creating a Review, as most relevant properties are null. The only useful thing we get
// from it is the last update time.
mergeRequest.setUpdatedAt(approvalMergeRequest.getUpdatedAt());
return fromGitLabMergeRequest(projectId, mergeRequest);
} catch (GitLabApiException e) {
switch(e.getHttpStatus()) {
// Status 401 (Unauthorized) can indicate either that the user is not properly authenticated or that the user is not a valid approver for the merge request.
case 401:
case 403:
{
throw new LegendSDLCServerException("User " + getCurrentUser() + " is not allowed to approve review " + reviewId + " in project " + projectId, Status.FORBIDDEN, e);
}
case 404:
{
throw new LegendSDLCServerException("Unknown review in project " + projectId + ": " + reviewId, Status.NOT_FOUND, e);
}
default:
{
StringBuilder builder = new StringBuilder("Error approving review ").append(reviewId).append(" in project ").append(projectId);
String eMessage = e.getMessage();
if (eMessage != null) {
builder.append(": ").append(eMessage);
}
throw new LegendSDLCServerException(builder.toString(), e);
}
}
} catch (LegendSDLCServerException e) {
throw e;
} catch (Exception e) {
StringBuilder builder = new StringBuilder("Error approving review ").append(reviewId).append(" in project ").append(projectId);
String eMessage = e.getMessage();
if (eMessage != null) {
builder.append(": ").append(eMessage);
}
throw new LegendSDLCServerException(builder.toString(), e);
}
}
use of org.finos.legend.sdlc.server.error.LegendSDLCServerException in project legend-sdlc by finos.
the class GitLabReviewApi method updateReview.
@Override
public ReviewUpdateStatus updateReview(String projectId, String reviewId) {
LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
LegendSDLCServerException.validateNonNull(reviewId, "reviewId may not be null");
GitLabProjectId gitLabProjectId = parseProjectId(projectId);
GitLabApi gitLabApi = getGitLabApi(gitLabProjectId.getGitLabMode());
MergeRequestApi mergeRequestApi = gitLabApi.getMergeRequestApi();
// Check the current status of the review
MergeRequest initialMergeRequest = getReviewMergeRequest(mergeRequestApi, gitLabProjectId, reviewId);
if (!isOpen(initialMergeRequest)) {
throw new LegendSDLCServerException("Only open reviews can be updated: state of review " + initialMergeRequest.getIid() + " in project " + projectId + " is " + getReviewState(initialMergeRequest), Status.CONFLICT);
}
ReviewUpdateStatus updateStatus = getReviewUpdateStatus(gitLabProjectId, gitLabApi, initialMergeRequest);
if (updateStatus.isUpdateInProgress() || ((updateStatus.getBaseRevisionId() != null) && updateStatus.getBaseRevisionId().equals(updateStatus.getTargetRevisionId()))) {
// Update in progress or already up to date: no need to update
return updateStatus;
}
// Start update attempt
MergeRequest rebaseMergeRequest;
try {
CallUntil<MergeRequest, GitLabApiException> callUntil = CallUntil.callUntil(() -> withRetries(() -> mergeRequestApi.rebaseMergeRequest(gitLabProjectId.getGitLabId(), initialMergeRequest.getIid())), MergeRequest::getRebaseInProgress, 3, 500L);
if (!callUntil.succeeded()) {
throw new LegendSDLCServerException("Failed to start update for review " + reviewId + " in project " + projectId);
}
rebaseMergeRequest = callUntil.getResult();
} catch (Exception e) {
throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to update review " + reviewId + " in project " + projectId, () -> "Unknown review in project " + projectId + ": " + reviewId, () -> "Error updating review " + reviewId + " in project " + projectId);
}
return getReviewUpdateStatus(gitLabProjectId, gitLabApi, rebaseMergeRequest);
}
use of org.finos.legend.sdlc.server.error.LegendSDLCServerException in project legend-sdlc by finos.
the class GitLabReviewApi method getMergeRequestTargetRevision.
private String getMergeRequestTargetRevision(GitLabProjectId projectId, GitLabApi gitLabApi, MergeRequest mergeRequest) {
RepositoryApi repositoryApi = gitLabApi.getRepositoryApi();
Branch targetBranch;
try {
targetBranch = withRetries(() -> repositoryApi.getBranch(projectId.getGitLabId(), mergeRequest.getTargetBranch()));
} catch (Exception e) {
LOGGER.error("Error getting target branch head for merge request {} in project {} (target branch: {})", mergeRequest.getIid(), projectId, mergeRequest.getTargetBranch(), e);
StringBuilder builder = new StringBuilder("Error getting target revision for review ").append(mergeRequest.getIid()).append(" for project ").append(projectId);
StringTools.appendThrowableMessageIfPresent(builder, e);
throw new LegendSDLCServerException(builder.toString(), e);
}
Commit targetHead = targetBranch.getCommit();
if ((targetHead == null) || (targetHead.getId() == null)) {
LOGGER.error("Error getting target branch head for merge request {} in project {} (target branch: {}): {}", mergeRequest.getIid(), projectId, mergeRequest.getTargetBranch(), targetHead);
throw new LegendSDLCServerException("Error getting target revision for review " + mergeRequest.getIid() + " for project");
}
return targetHead.getId();
}
use of org.finos.legend.sdlc.server.error.LegendSDLCServerException in project legend-sdlc by finos.
the class GitLabRevisionApi method getProjectPackageRevisionContext.
@Override
public RevisionAccessContext getProjectPackageRevisionContext(String projectId, String packagePath) {
LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
LegendSDLCServerException.validateNonNull(packagePath, "packagePath may not be null");
if (!isValidPackagePath(packagePath)) {
throw new LegendSDLCServerException("Invalid package path: " + packagePath, Status.BAD_REQUEST);
}
ProjectStructure projectStructure = getProjectStructure(projectId, null, null, null, null);
MutableList<String> directories = Iterate.collectWith(projectStructure.getEntitySourceDirectories(), ProjectStructure.EntitySourceDirectory::packagePathToFilePath, packagePath, Lists.mutable.empty());
MutableList<String> canonicalizedAndReducedDirectories = ProjectPaths.canonicalizeAndReduceDirectories(directories);
return new ProjectFileRevisionAccessContextWrapper(getProjectFileAccessProvider().getRevisionAccessContext(projectId, null, null, null, canonicalizedAndReducedDirectories), new PackageablePathExceptionProcessor(packagePath, canonicalizedAndReducedDirectories));
}
Aggregations