use of org.finos.legend.sdlc.server.gitlab.GitLabProjectId in project legend-sdlc by finos.
the class GitLabReviewApi method rejectReview.
@Override
public Review rejectReview(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 rejectMergeRequest = updateMergeRequestState(mergeRequestApi, gitLabProjectId, mergeRequest, StateEvent.CLOSE);
return fromGitLabMergeRequest(projectId, rejectMergeRequest);
} catch (Exception e) {
throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to reject review " + reviewId + " in project " + projectId, () -> "Unknown review in project " + projectId + ": " + reviewId, () -> "Error rejecting review " + reviewId + " in project " + projectId);
}
}
use of org.finos.legend.sdlc.server.gitlab.GitLabProjectId 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.gitlab.GitLabProjectId 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.gitlab.GitLabProjectId 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.gitlab.GitLabProjectId in project legend-sdlc by finos.
the class GitLabVersionApi method getVersions.
private Stream<Version> getVersions(GitLabProjectId projectId, Integer minMajorVersion, Integer maxMajorVersion, Integer minMinorVersion, Integer maxMinorVersion, Integer minPatchVersion, Integer maxPatchVersion) {
switch(getProjectTypeFromMode(projectId.getGitLabMode())) {
case PROTOTYPE:
{
return Stream.empty();
}
case PRODUCTION:
{
try {
Stream<Version> stream = PagerTools.stream(getGitLabApi(projectId.getGitLabMode()).getTagsApi().getTags(projectId.getGitLabId(), ITEMS_PER_PAGE)).filter(GitLabVersionApi::isVersionTag).map(tag -> fromGitLabTag(projectId.toString(), tag));
// major version constraint
if ((minMajorVersion != null) && (maxMajorVersion != null)) {
int minMajorVersionInt = minMajorVersion;
int maxMajorVersionInt = maxMajorVersion;
if (minMajorVersionInt == maxMajorVersionInt) {
stream = stream.filter(v -> v.getId().getMajorVersion() == minMajorVersionInt);
} else {
stream = stream.filter(v -> {
int majorVersion = v.getId().getMajorVersion();
return (minMajorVersionInt <= majorVersion) && (majorVersion <= maxMajorVersionInt);
});
}
} else if (minMajorVersion != null) {
int minMajorVersionInt = minMajorVersion;
stream = stream.filter(v -> v.getId().getMajorVersion() >= minMajorVersionInt);
} else if (maxMajorVersion != null) {
int maxMajorVersionInt = maxMajorVersion;
stream = stream.filter(v -> v.getId().getMajorVersion() <= maxMajorVersionInt);
}
// minor version constraint
if ((minMinorVersion != null) && (maxMinorVersion != null)) {
int minMinorVersionInt = minMinorVersion;
int maxMinorVersionInt = maxMinorVersion;
if (minMinorVersionInt == maxMinorVersionInt) {
stream = stream.filter(v -> v.getId().getMinorVersion() == minMinorVersionInt);
} else {
stream = stream.filter(v -> {
int minorVersion = v.getId().getMinorVersion();
return (minMinorVersionInt <= minorVersion) && (minorVersion <= maxMinorVersionInt);
});
}
} else if (minMinorVersion != null) {
int minMinorVersionInt = minMinorVersion;
stream = stream.filter(v -> v.getId().getMinorVersion() >= minMinorVersionInt);
} else if (maxMinorVersion != null) {
int maxMinorVersionInt = maxMinorVersion;
stream = stream.filter(v -> v.getId().getMinorVersion() <= maxMinorVersionInt);
}
// patch version constraint
if ((minPatchVersion != null) && (maxPatchVersion != null)) {
int minPatchVersionInt = minPatchVersion;
int maxPatchVersionInt = maxPatchVersion;
if (minPatchVersionInt == maxPatchVersionInt) {
stream = stream.filter(v -> v.getId().getPatchVersion() == minPatchVersionInt);
} else {
stream = stream.filter(v -> {
int patchVersion = v.getId().getPatchVersion();
return (minPatchVersionInt <= patchVersion) && (patchVersion <= maxPatchVersionInt);
});
}
} else if (minPatchVersion != null) {
int minPatchVersionInt = minPatchVersion;
stream = stream.filter(v -> v.getId().getPatchVersion() >= minPatchVersionInt);
} else if (maxPatchVersion != null) {
int maxPatchVersionInt = maxPatchVersion;
stream = stream.filter(v -> v.getId().getPatchVersion() <= maxPatchVersionInt);
}
return stream;
} catch (Exception e) {
throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to get versions for project " + projectId, () -> "Unknown project: " + projectId, () -> "Error getting versions for project " + projectId);
}
}
default:
{
throw new LegendSDLCServerException("Unknown project: " + projectId, Status.BAD_REQUEST);
}
}
}
Aggregations