use of org.gitlab4j.api.models.CommitRef in project legend-sdlc by finos.
the class GitLabVersionApi method newVersion.
private Version newVersion(GitLabProjectId projectId, String revisionId, VersionId versionId, String notes) {
String tagName = buildVersionTagName(versionId);
String message = "Release tag for version " + versionId.toVersionIdString();
try {
GitLabApi gitLabApi = getGitLabApi(projectId.getGitLabMode());
CommitsApi commitsApi = gitLabApi.getCommitsApi();
Commit referenceCommit;
if (revisionId == null) {
referenceCommit = commitsApi.getCommit(projectId.getGitLabId(), MASTER_BRANCH);
if (referenceCommit == null) {
throw new LegendSDLCServerException("Cannot create version " + versionId.toVersionIdString() + " of project " + projectId + ": cannot find current revision (project may be corrupt)", Status.INTERNAL_SERVER_ERROR);
}
} else {
try {
referenceCommit = commitsApi.getCommit(projectId.getGitLabId(), revisionId);
} catch (GitLabApiException e) {
if (GitLabApiTools.isNotFoundGitLabApiException(e)) {
throw new LegendSDLCServerException("Revision " + revisionId + " is unknown in project " + projectId, Status.BAD_REQUEST);
}
throw e;
}
Pager<CommitRef> referenceCommitBranchPager = withRetries(() -> commitsApi.getCommitRefs(projectId.getGitLabId(), referenceCommit.getId(), RefType.BRANCH, ITEMS_PER_PAGE));
Stream<CommitRef> referenceCommitBranches = PagerTools.stream(referenceCommitBranchPager);
if (referenceCommitBranches.noneMatch(ref -> MASTER_BRANCH.equals(ref.getName()))) {
throw new LegendSDLCServerException("Revision " + revisionId + " is unknown in project " + projectId, Status.BAD_REQUEST);
}
}
String referenceRevisionId = referenceCommit.getId();
Pager<CommitRef> referenceCommitTagPager = withRetries(() -> commitsApi.getCommitRefs(projectId.getGitLabId(), referenceRevisionId, RefType.TAG, ITEMS_PER_PAGE));
List<CommitRef> referenceCommitTags = PagerTools.stream(referenceCommitTagPager).collect(Collectors.toList());
if (referenceCommitTags.stream().map(CommitRef::getName).anyMatch(GitLabVersionApi::isVersionTagName)) {
StringBuilder builder = new StringBuilder("Revision ").append(referenceRevisionId).append(" has already been released in ");
List<VersionId> revisionVersionIds = referenceCommitTags.stream().map(CommitRef::getName).filter(GitLabVersionApi::isVersionTagName).map(GitLabVersionApi::parseVersionTagName).collect(Collectors.toList());
if (revisionVersionIds.size() == 1) {
builder.append("version ");
revisionVersionIds.get(0).appendVersionIdString(builder);
} else {
builder.append("versions ");
revisionVersionIds.sort(Comparator.naturalOrder());
boolean first = true;
for (VersionId revisionVersionId : revisionVersionIds) {
if (first) {
first = false;
} else {
builder.append(", ");
}
revisionVersionId.appendVersionIdString(builder);
}
}
throw new LegendSDLCServerException(builder.toString());
}
Tag tag = getGitLabApi(projectId.getGitLabMode()).getTagsApi().createTag(projectId.getGitLabId(), tagName, referenceRevisionId, message, notes);
return fromGitLabTag(projectId.toString(), tag);
} catch (Exception e) {
throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to create version " + versionId.toVersionIdString() + " of project " + projectId, () -> "Unknown project: " + projectId, () -> "Error creating version " + versionId.toVersionIdString() + " of project " + projectId);
}
}
use of org.gitlab4j.api.models.CommitRef in project legend-sdlc by finos.
the class GitLabWorkspaceApi method updateWorkspace.
/**
* There are 4 possible outcome for this method:
* 1. NO_OP: If the workspace is already branching from the HEAD of master, nothing is needed.
* 2. UPDATED: If the workspace is not branching from the HEAD of master, and we successfully rebase the branch to master HEAD.
* 3. CONFLICT: If the workspace is not branching from the HEAD of master, and we failed to rebase the branch to master HEAD due to merge conflicts.
* 4. ERROR
* <p>
* The procedure goes like the followings:
* 1. Check if the current workspace is already up to date:
* - If yes, return NO_OP
* - If no, proceed
* 2. Create a temp branch to attempt to rebase:
* - If rebase succeeded, return UPDATED
* - If rebase failed, further check if we need to enter conflict resolution mode.
* This check makes sure the conflict that causes rebase to fail does not come from intermediate
* commits by squashing these commits and attempt to do another rebase there. If this still fails
* it means the workspace in overall truly has merge conflicts while updating, so entering conflict resolution mode
*/
@Override
public WorkspaceUpdateReport updateWorkspace(String projectId, String workspaceId, WorkspaceType workspaceType) {
LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
LegendSDLCServerException.validateNonNull(workspaceId, "workspaceId may not be null");
LOGGER.info("Updating workspace {} in project {} to latest revision", workspaceId, projectId);
GitLabProjectId gitLabProjectId = parseProjectId(projectId);
ProjectFileAccessProvider.WorkspaceAccessType workspaceAccessType = ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE;
String workspaceBranchName = getBranchName(workspaceId, workspaceType, workspaceAccessType);
GitLabApi gitLabApi = getGitLabApi(gitLabProjectId.getGitLabMode());
RepositoryApi repositoryApi = gitLabApi.getRepositoryApi();
// Get the workspace branch
Branch workspaceBranch;
try {
workspaceBranch = withRetries(() -> repositoryApi.getBranch(gitLabProjectId.getGitLabId(), workspaceBranchName));
} catch (Exception e) {
throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to access " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " in project " + projectId, () -> "Unknown " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " in project " + projectId + ": " + workspaceId, () -> "Error accessing " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " in project " + projectId);
}
String currentWorkspaceRevisionId = workspaceBranch.getCommit().getId();
LOGGER.info("Found latest revision of {} {} in project {}: {}", workspaceType.getLabel() + " " + workspaceAccessType.getLabel(), workspaceId, projectId, currentWorkspaceRevisionId);
// Determine the revision to update to
Branch masterBranch;
try {
masterBranch = withRetries(() -> repositoryApi.getBranch(gitLabProjectId.getGitLabId(), MASTER_BRANCH));
} catch (Exception e) {
throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to access the latest revision in project " + projectId, () -> "Unknown project: " + projectId, () -> "Error accessing latest revision for project " + projectId);
}
String masterRevisionId = masterBranch.getCommit().getId();
LOGGER.info("Found latest revision of project {}: {}", projectId, masterRevisionId);
CommitsApi commitsApi = gitLabApi.getCommitsApi();
// Check if the workspace already has the latest revision
try {
boolean isAlreadyLatest = false;
// This will check if the branch contains the master HEAD commit by looking up the list of references the commit is pushed to
if (masterRevisionId.equals(currentWorkspaceRevisionId)) {
isAlreadyLatest = true;
} else {
Pager<CommitRef> masterHeadCommitRefPager = withRetries(() -> commitsApi.getCommitRefs(gitLabProjectId.getGitLabId(), masterRevisionId, RefType.BRANCH, ITEMS_PER_PAGE));
Stream<CommitRef> masterHeadCommitRefs = PagerTools.stream(masterHeadCommitRefPager);
if (masterHeadCommitRefs.anyMatch(cr -> workspaceBranchName.equals(cr.getName()))) {
isAlreadyLatest = true;
}
}
if (isAlreadyLatest) {
// revision is already in the workspace, no update necessary, hence NO_OP
LOGGER.info("Workspace {} in project {} already has revision {}, no update necessary", workspaceId, projectId, masterRevisionId);
return createWorkspaceUpdateReport(WorkspaceUpdateReportStatus.NO_OP, masterRevisionId, currentWorkspaceRevisionId);
}
} catch (Exception e) {
throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to access revision " + masterRevisionId + " in project " + projectId, () -> "Unknown revision in project " + projectId + ": " + masterRevisionId, () -> "Error accessing revision " + masterRevisionId + " of project " + projectId);
}
// Temp branch for checking for merge conflicts
String tempBranchName = newUserTemporaryBranchName();
Branch tempBranch;
try {
tempBranch = GitLabApiTools.createBranchAndVerify(repositoryApi, gitLabProjectId.getGitLabId(), tempBranchName, currentWorkspaceRevisionId, 30, 1_000);
} catch (Exception e) {
throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to create temporary workspace " + tempBranchName + " in project " + projectId, () -> "Unknown project: " + projectId, () -> "Error creating temporary workspace " + tempBranchName + " in project " + projectId);
}
if (tempBranch == null) {
throw new LegendSDLCServerException("Failed to create temporary workspace " + tempBranchName + " in project " + projectId + " from revision " + currentWorkspaceRevisionId);
}
// Attempt to rebase the temporary branch on top of master
boolean rebaseSucceeded = this.attemptToRebaseWorkspaceUsingTemporaryBranch(projectId, workspaceId, workspaceType, tempBranchName, masterRevisionId);
// 2. There are merge conflicts, so we enter conflict resolution route
if (!rebaseSucceeded) {
String workspaceCreationRevisionId;
try {
workspaceCreationRevisionId = withRetries(() -> repositoryApi.getMergeBase(gitLabProjectId.getGitLabId(), Arrays.asList(MASTER_BRANCH, currentWorkspaceRevisionId)).getId());
} catch (Exception e) {
throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to get merged base revision for workspace " + workspaceId + " from project " + projectId, () -> "Could not find revision " + currentWorkspaceRevisionId + " from project " + projectId, () -> "Failed to fetch merged base revision for workspace " + workspaceId + " from project " + projectId);
}
// Small optimization step to make sure we need squashing.
// If there are less than 2 commits (not including the base commit), there is no point in squashing
List<Revision> latestTwoRevisionsOnWorkspaceBranch = this.revisionApi.getWorkspaceRevisionContext(projectId, workspaceId, workspaceType).getRevisions(null, null, null, 2);
Set<String> latestTwoRevisionOnWorkspaceBranchIds = latestTwoRevisionsOnWorkspaceBranch.stream().map(Revision::getId).collect(Collectors.toSet());
if (latestTwoRevisionOnWorkspaceBranchIds.contains(workspaceCreationRevisionId)) {
LOGGER.debug("Failed to rebase branch {}, but the branch does not have enough commits to perform squashing. Proceeding to conflict resolution...", workspaceBranchName);
return this.createConflictResolution(projectId, workspaceId, workspaceType, masterRevisionId);
} else {
LOGGER.debug("Failed to rebase branch {}. Performing squashing commits and re-attempting rebase...", workspaceBranchName);
}
WorkspaceUpdateReport rebaseUpdateAttemptReport = this.attemptToSquashAndRebaseWorkspace(projectId, workspaceId, workspaceType, masterRevisionId, currentWorkspaceRevisionId, workspaceCreationRevisionId);
return WorkspaceUpdateReportStatus.UPDATED.equals(rebaseUpdateAttemptReport.getStatus()) ? rebaseUpdateAttemptReport : this.createConflictResolution(projectId, workspaceId, workspaceType, masterRevisionId);
}
String updatedCurrentWorkspaceRevisionId = this.revisionApi.getWorkspaceRevisionContext(projectId, workspaceId, workspaceType).getCurrentRevision().getId();
return createWorkspaceUpdateReport(WorkspaceUpdateReportStatus.UPDATED, masterRevisionId, updatedCurrentWorkspaceRevisionId);
}
use of org.gitlab4j.api.models.CommitRef in project legend-sdlc by finos.
the class GitLabRevisionApi method getRevisionStatus.
@Override
public RevisionStatus getRevisionStatus(String projectId, String revisionId) {
LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
LegendSDLCServerException.validateNonNull(revisionId, "revisionId may not be null");
GitLabProjectId gitLabProjectId = parseProjectId(projectId);
try {
GitLabApi gitLabApi = getGitLabApi(gitLabProjectId.getGitLabMode());
CommitsApi commitsApi = gitLabApi.getCommitsApi();
Revision revision = getProjectRevisionContext(projectId).getRevision(revisionId);
Pager<CommitRef> commitRefPager = withRetries(() -> commitsApi.getCommitRefs(gitLabProjectId.getGitLabId(), revision.getId(), RefType.ALL, ITEMS_PER_PAGE));
List<CommitRef> commitRefs = PagerTools.stream(commitRefPager).collect(Collectors.toList());
boolean isCommitted = commitRefs.stream().anyMatch(cr -> MASTER_BRANCH.equals(cr.getName()));
List<Version> versions;
List<String> versionTagNames = commitRefs.stream().filter(cr -> (RefType.TAG == cr.getType()) && isVersionTagName(cr.getName())).map(CommitRef::getName).collect(Collectors.toList());
if (versionTagNames.isEmpty()) {
versions = Collections.emptyList();
} else {
TagsApi tagsApi = gitLabApi.getTagsApi();
versions = Lists.mutable.ofInitialCapacity(versionTagNames.size());
for (String tagName : versionTagNames) {
Tag tag = withRetries(() -> tagsApi.getTag(gitLabProjectId.getGitLabId(), tagName));
versions.add(fromGitLabTag(projectId, tag));
}
versions.sort(Comparator.comparing(Version::getId));
}
List<Workspace> workspaces;
if (isCommitted) {
workspaces = Collections.emptyList();
} else {
// Note that here we will not account for conflict resolution or backup branch because in the model those are not real workspaces.
workspaces = commitRefs.stream().filter(cr -> (RefType.BRANCH == cr.getType()) && isWorkspaceBranchName(cr.getName(), ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE)).map(cr -> fromWorkspaceBranchName(projectId, cr.getName(), WorkspaceType.USER, ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE)).collect(Collectors.toList());
}
return new RevisionStatus() {
@Override
public Revision getRevision() {
return revision;
}
@Override
public boolean isCommitted() {
return isCommitted;
}
@Override
public List<Workspace> getWorkspaces() {
return workspaces;
}
@Override
public List<Version> getVersions() {
return versions;
}
};
} catch (Exception e) {
throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to access the status for revision " + revisionId + " of project " + projectId, () -> "Unknown: revision " + revisionId + " of project " + projectId, () -> "Error getting the status for revision " + revisionId + " of project " + projectId);
}
}
use of org.gitlab4j.api.models.CommitRef in project legend-sdlc by finos.
the class GitLabWorkspaceApi method isWorkspaceOutdatedByAccessType.
private boolean isWorkspaceOutdatedByAccessType(String projectId, String workspaceId, WorkspaceType workspaceType, ProjectFileAccessProvider.WorkspaceAccessType workspaceAccessType) {
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(workspaceAccessType, "workspaceAccessType may not be null");
GitLabProjectId gitLabProjectId = parseProjectId(projectId);
String workspaceBranchName = getBranchName(workspaceId, workspaceType, workspaceAccessType);
GitLabApi gitLabApi = getGitLabApi(gitLabProjectId.getGitLabMode());
RepositoryApi repositoryApi = gitLabApi.getRepositoryApi();
// Get the workspace branch
Branch workspaceBranch;
try {
workspaceBranch = withRetries(() -> repositoryApi.getBranch(gitLabProjectId.getGitLabId(), workspaceBranchName));
} catch (Exception e) {
throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to access " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " in project " + projectId, () -> "Unknown " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " in project " + projectId + ": " + workspaceId, () -> "Error accessing " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " in project " + projectId);
}
String workspaceRevisionId = workspaceBranch.getCommit().getId();
// Get HEAD of master
Branch masterBranch;
try {
masterBranch = withRetries(() -> repositoryApi.getBranch(gitLabProjectId.getGitLabId(), MASTER_BRANCH));
} catch (Exception e) {
throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to access the latest revision in project " + projectId, () -> "Unknown project: " + projectId, () -> "Error accessing latest revision for project " + projectId);
}
String masterRevisionId = masterBranch.getCommit().getId();
CommitsApi commitsApi = gitLabApi.getCommitsApi();
// Check if the workspace does not have the latest revision of the project, i.e. it is outdated
try {
if (masterRevisionId.equals(workspaceRevisionId)) {
return false;
}
Pager<CommitRef> masterHeadCommitRefsPager = withRetries(() -> commitsApi.getCommitRefs(gitLabProjectId.getGitLabId(), masterRevisionId, RefType.BRANCH, ITEMS_PER_PAGE));
Stream<CommitRef> masterHeadCommitRefs = PagerTools.stream(masterHeadCommitRefsPager);
// This will check if the branch contains the master HEAD commit by looking up the list of references the commit is pushed to
return masterHeadCommitRefs.noneMatch(cr -> workspaceBranchName.equals(cr.getName()));
} catch (Exception e) {
throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to check if " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " in project " + projectId + " is outdated", () -> "Unknown revision (" + masterRevisionId + "), or " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " (" + workspaceId + ") or project (" + projectId + ")", () -> "Error checking if " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " of project " + projectId + " is outdated");
}
}
Aggregations