Search in sources :

Example 11 with Branch

use of org.gitlab4j.api.models.Branch in project choerodon-starters by open-hand.

the class RepositoryApi method createBranch.

/**
 * Creates a branch for the project. Support as of version 6.8.x
 * <p>
 * POST /projects/:id/repository/branches
 *
 * @param projectId  the project to create the branch for
 * @param branchName the name of the branch to create
 * @param ref        Source to create the branch from, can be an existing branch, tag or commit SHA
 * @return the branch info for the created branch
 * @throws GitLabApiException if any exception occurs
 */
public Branch createBranch(Integer projectId, String branchName, String ref) throws GitLabApiException {
    Form formData = new GitLabApiForm().withParam(isApiVersion(ApiVersion.V3) ? "branch_name" : "branch", branchName, true).withParam("ref", ref, true);
    Response response = post(Response.Status.CREATED, formData.asMap(), "projects", projectId, "repository", "branches");
    return (response.readEntity(Branch.class));
}
Also used : Response(javax.ws.rs.core.Response) Form(javax.ws.rs.core.Form) Branch(org.gitlab4j.api.models.Branch)

Example 12 with Branch

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

the class GitLabApiTools method createBranchFromSourceBranchAndVerify.

public static Branch createBranchFromSourceBranchAndVerify(RepositoryApi api, Object projectIdOrPath, String branchName, String sourceBranchName, int maxVerificationTries, long verificationWaitMillis) throws GitLabApiException {
    Branch sourceBranch = null;
    CallUntil<Branch, GitLabApiException> callUntil = CallUntil.callUntil(() -> getBranch(api, projectIdOrPath, sourceBranchName), Objects::nonNull, maxVerificationTries, verificationWaitMillis);
    if (callUntil.succeeded()) {
        sourceBranch = callUntil.getResult();
    }
    if (sourceBranch == null) {
        LOGGER.warn("Failed to get source branch {} in project {}. Aborting branch creation from source branch.", sourceBranchName, projectIdOrPath);
        return null;
    }
    return createBranchAndVerify(api, projectIdOrPath, branchName, sourceBranch.getCommit().getId(), maxVerificationTries, verificationWaitMillis);
}
Also used : Branch(org.gitlab4j.api.models.Branch) Objects(java.util.Objects) GitLabApiException(org.gitlab4j.api.GitLabApiException)

Example 13 with Branch

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

the class GitLabApiTools method createBranchAndVerify.

public static Branch createBranchAndVerify(RepositoryApi api, Object projectIdOrPath, String branchName, String sourceCommitId, int maxVerificationTries, long verificationWaitMillis) throws GitLabApiException {
    LOGGER.debug("Creating branch {} in project {} from commit {}", branchName, projectIdOrPath, sourceCommitId);
    Branch branch = getBranchAtCommit(api, projectIdOrPath, branchName, sourceCommitId);
    if (branch == null) {
        // Branch does not exist in the expected form, try to create it
        api.createBranch(projectIdOrPath, branchName, sourceCommitId);
        CallUntil<Branch, GitLabApiException> callUntil = CallUntil.callUntil(() -> getBranchAtCommit(api, projectIdOrPath, branchName, sourceCommitId), Objects::nonNull, maxVerificationTries, verificationWaitMillis);
        if (callUntil.succeeded()) {
            branch = callUntil.getResult();
        }
        LOGGER.debug("Creating branch {} in project {} from commit {} {}", branchName, projectIdOrPath, sourceCommitId, (branch == null) ? "failed" : "succeeded");
    } else {
        // Branch already exists in the expected form, no need to create it
        LOGGER.debug("Branch {} already exists in project {} with commit {}", branchName, projectIdOrPath, sourceCommitId);
    }
    return branch;
}
Also used : Branch(org.gitlab4j.api.models.Branch) Objects(java.util.Objects) GitLabApiException(org.gitlab4j.api.GitLabApiException)

Example 14 with Branch

use of org.gitlab4j.api.models.Branch 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");
    }
}
Also used : GitLabApi(org.gitlab4j.api.GitLabApi) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) Branch(org.gitlab4j.api.models.Branch) RepositoryApi(org.gitlab4j.api.RepositoryApi) CommitRef(org.gitlab4j.api.models.CommitRef) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabApiException(org.gitlab4j.api.GitLabApiException) CommitsApi(org.gitlab4j.api.CommitsApi)

Example 15 with Branch

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

the class GitLabWorkspaceApi method newWorkspace.

/**
 * When we create a new workspace, we also should clean left-over backup and conflict resolution workspaces with the same name
 */
@Override
public Workspace newWorkspace(String projectId, String workspaceId, WorkspaceType workspaceType) {
    LegendSDLCServerException.validateNonNull(projectId, "projectId may not be null");
    LegendSDLCServerException.validateNonNull(workspaceId, "workspaceId may not be null");
    validateWorkspaceId(workspaceId);
    GitLabProjectId gitLabProjectId = parseProjectId(projectId);
    RepositoryApi repositoryApi = getGitLabApi(gitLabProjectId.getGitLabMode()).getRepositoryApi();
    // Delete backup workspace with the same name if exists
    Branch backupBranch = null;
    ProjectFileAccessProvider.WorkspaceAccessType backupWorkspaceType = ProjectFileAccessProvider.WorkspaceAccessType.BACKUP;
    try {
        backupBranch = withRetries(() -> repositoryApi.getBranch(gitLabProjectId.getGitLabId(), getWorkspaceBranchName(workspaceId, workspaceType, backupWorkspaceType)));
    } catch (Exception e) {
        if (!GitLabApiTools.isNotFoundGitLabApiException(e)) {
            LOGGER.error("Error accessing {} {} in project {}", workspaceType.getLabel() + " " + backupWorkspaceType.getLabel(), workspaceId, projectId, e);
        }
    }
    if (backupBranch != null) {
        LOGGER.debug("Cleaning up left-over {} {} in project {}", workspaceType.getLabel() + " " + backupWorkspaceType.getLabel(), workspaceId, projectId);
        try {
            boolean deleted = GitLabApiTools.deleteBranchAndVerify(repositoryApi, gitLabProjectId.getGitLabId(), getWorkspaceBranchName(workspaceId, workspaceType, backupWorkspaceType), 20, 1_000);
            if (!deleted) {
                LOGGER.error("Failed to delete {} {} in project {}", workspaceType.getLabel() + " " + backupWorkspaceType.getLabel(), workspaceId, projectId);
            }
        } catch (Exception e) {
            // unfortunate, but this should not throw error
            LOGGER.error("Error deleting {} {} in project {}", workspaceType.getLabel() + " " + backupWorkspaceType.getLabel(), workspaceId, projectId, e);
        }
    }
    // Delete workspace with conflict resolution with the same name if exists
    Branch conflictResolutionBranch = null;
    ProjectFileAccessProvider.WorkspaceAccessType conflictResolutionWorkspaceType = ProjectFileAccessProvider.WorkspaceAccessType.CONFLICT_RESOLUTION;
    try {
        conflictResolutionBranch = withRetries(() -> repositoryApi.getBranch(gitLabProjectId.getGitLabId(), getWorkspaceBranchName(workspaceId, workspaceType, conflictResolutionWorkspaceType)));
    } catch (Exception e) {
        if (!GitLabApiTools.isNotFoundGitLabApiException(e)) {
            LOGGER.error("Error accessing {} {} in project {}", workspaceType.getLabel() + " " + conflictResolutionWorkspaceType.getLabel(), workspaceId, projectId, e);
        }
    }
    if (conflictResolutionBranch != null) {
        LOGGER.debug("Cleaning up left-over {} {} in project {}", workspaceType.getLabel() + " " + conflictResolutionWorkspaceType.getLabel(), workspaceId, projectId);
        try {
            boolean deleted = GitLabApiTools.deleteBranchAndVerify(repositoryApi, gitLabProjectId.getGitLabId(), getWorkspaceBranchName(workspaceId, workspaceType, conflictResolutionWorkspaceType), 20, 1_000);
            if (!deleted) {
                LOGGER.error("Failed to delete {} {} in project {}", workspaceType.getLabel() + " " + conflictResolutionWorkspaceType.getLabel(), workspaceId, projectId);
            }
        } catch (Exception e) {
            // unfortunate, but this should not throw error
            LOGGER.error("Error deleting {} {} in project {}", workspaceType.getLabel() + " " + conflictResolutionWorkspaceType.getLabel(), workspaceId, projectId, e);
        }
    }
    // Create new workspace
    Branch branch;
    ProjectFileAccessProvider.WorkspaceAccessType workspaceAccessType = ProjectFileAccessProvider.WorkspaceAccessType.WORKSPACE;
    try {
        branch = GitLabApiTools.createBranchFromSourceBranchAndVerify(repositoryApi, gitLabProjectId.getGitLabId(), getWorkspaceBranchName(workspaceId, workspaceType, workspaceAccessType), MASTER_BRANCH, 30, 1_000);
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to create workspace " + workspaceId + " in project " + projectId, () -> "Unknown project: " + projectId, () -> "Error creating " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " in project " + projectId);
    }
    if (branch == null) {
        throw new LegendSDLCServerException("Failed to create " + workspaceType.getLabel() + " " + workspaceAccessType.getLabel() + " " + workspaceId + " in project " + projectId);
    }
    return workspaceBranchToWorkspace(projectId, branch, workspaceType, workspaceAccessType);
}
Also used : ProjectFileAccessProvider(org.finos.legend.sdlc.server.project.ProjectFileAccessProvider) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) Branch(org.gitlab4j.api.models.Branch) RepositoryApi(org.gitlab4j.api.RepositoryApi) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabApiException(org.gitlab4j.api.GitLabApiException)

Aggregations

Branch (org.gitlab4j.api.models.Branch)18 LegendSDLCServerException (org.finos.legend.sdlc.server.error.LegendSDLCServerException)15 GitLabApiException (org.gitlab4j.api.GitLabApiException)15 GitLabProjectId (org.finos.legend.sdlc.server.gitlab.GitLabProjectId)14 RepositoryApi (org.gitlab4j.api.RepositoryApi)14 ProjectFileAccessProvider (org.finos.legend.sdlc.server.project.ProjectFileAccessProvider)12 CommitsApi (org.gitlab4j.api.CommitsApi)6 GitLabApi (org.gitlab4j.api.GitLabApi)6 Revision (org.finos.legend.sdlc.domain.model.revision.Revision)4 Commit (org.gitlab4j.api.models.Commit)4 CommitAction (org.gitlab4j.api.models.CommitAction)4 CommitRef (org.gitlab4j.api.models.CommitRef)4 CompareResults (org.gitlab4j.api.models.CompareResults)4 MergeRequest (org.gitlab4j.api.models.MergeRequest)4 MergeRequestApi (org.gitlab4j.api.MergeRequestApi)3 Diff (org.gitlab4j.api.models.Diff)3 Arrays (java.util.Arrays)2 List (java.util.List)2 Objects (java.util.Objects)2 Set (java.util.Set)2