Search in sources :

Example 36 with LegendSDLCServerException

use of org.finos.legend.sdlc.server.error.LegendSDLCServerException in project legend-sdlc by finos.

the class GitLabProjectApi method createProject.

@Override
public Project createProject(String name, String description, ProjectType type, String groupId, String artifactId, Iterable<String> tags) {
    LegendSDLCServerException.validate(name, n -> (n != null) && !n.isEmpty(), "name may not be null or empty");
    LegendSDLCServerException.validateNonNull(description, "description may not be null");
    LegendSDLCServerException.validateNonNull(type, "type may not be null");
    LegendSDLCServerException.validate(groupId, ProjectStructure::isValidGroupId, g -> "Invalid groupId: " + g);
    LegendSDLCServerException.validate(artifactId, ProjectStructure::isValidArtifactId, a -> "Invalid artifactId: " + a);
    validateProjectCreation(name, description, type, groupId, artifactId);
    try {
        GitLabMode mode = getGitLabModeFromProjectType(type);
        GitLabApi gitLabApi = getGitLabApi(mode);
        List<String> tagList = Lists.mutable.empty();
        tagList.add(getLegendSDLCProjectTag());
        if (tags != null) {
            tagList.addAll(toLegendSDLCTagSet(tags));
        }
        org.gitlab4j.api.models.Project gitLabProjectSpec = new org.gitlab4j.api.models.Project().withName(name).withDescription(description).withTagList(tagList).withVisibility(getNewProjectVisibility()).withMergeRequestsEnabled(true).withIssuesEnabled(true).withWikiEnabled(false).withSnippetsEnabled(false);
        org.gitlab4j.api.models.Project gitLabProject = gitLabApi.getProjectApi().createProject(gitLabProjectSpec);
        if (gitLabProject == null) {
            throw new LegendSDLCServerException("Failed to create project: " + name);
        }
        // protect from commits on master
        gitLabApi.getProtectedBranchesApi().protectBranch(gitLabProject.getId(), MASTER_BRANCH, AccessLevel.NONE, AccessLevel.MAINTAINER);
        // build project structure
        ProjectConfigurationUpdateBuilder.newBuilder(getProjectFileAccessProvider(), type, GitLabProjectId.getProjectIdString(mode, gitLabProject)).withMessage("Build project structure").withGroupId(groupId).withArtifactId(artifactId).withProjectStructureVersion(getDefaultProjectStructureVersion()).withProjectStructureExtensionProvider(this.projectStructureExtensionProvider).withProjectStructurePlatformExtensions(this.projectStructurePlatformExtensions).buildProjectStructure();
        return fromGitLabProject(gitLabProject, mode);
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to create project " + name, () -> "Failed to create project: " + name, () -> "Failed to create project: " + name);
    }
}
Also used : GitLabApi(org.gitlab4j.api.GitLabApi) ProjectStructure(org.finos.legend.sdlc.server.project.ProjectStructure) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) Project(org.finos.legend.sdlc.domain.model.project.Project) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabMode(org.finos.legend.sdlc.server.gitlab.mode.GitLabMode)

Example 37 with LegendSDLCServerException

use of org.finos.legend.sdlc.server.error.LegendSDLCServerException in project legend-sdlc by finos.

the class GitLabProjectApi method getProject.

@Override
public Project getProject(String id) {
    LegendSDLCServerException.validateNonNull(id, "id may not be null");
    try {
        GitLabProjectId projectId = parseProjectId(id);
        org.gitlab4j.api.models.Project gitLabProject = withRetries(() -> getGitLabApi(projectId.getGitLabMode()).getProjectApi().getProject(projectId.getGitLabId()));
        if (!isLegendSDLCProject(gitLabProject)) {
            throw new LegendSDLCServerException("Failed to get project " + id);
        }
        return fromGitLabProject(gitLabProject, projectId.getGitLabMode());
    } catch (Exception e) {
        throw buildException(e, () -> "Failed to get project " + id);
    }
}
Also used : LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException)

Example 38 with LegendSDLCServerException

use of org.finos.legend.sdlc.server.error.LegendSDLCServerException in project legend-sdlc by finos.

the class GitLabProjectApi method changeProjectDescription.

@Override
public void changeProjectDescription(String id, String newDescription) {
    LegendSDLCServerException.validateNonNull(id, "id may not be null");
    LegendSDLCServerException.validateNonNull(newDescription, "newDescription may not be null");
    try {
        GitLabProjectId projectId = parseProjectId(id);
        org.gitlab4j.api.models.Project currentProject = getPureGitLabProjectById(projectId);
        org.gitlab4j.api.models.Project updatedProject = new org.gitlab4j.api.models.Project().withId(currentProject.getId()).withDescription(newDescription);
        withRetries(() -> getGitLabApi(projectId.getGitLabMode()).getProjectApi().updateProject(updatedProject));
    } catch (LegendSDLCServerException e) {
        throw e;
    } catch (Exception e) {
        throw buildException(e, () -> "User " + getCurrentUser() + " is not allowed to change the description of project " + id + " to \"" + newDescription + "\"", () -> "Unknown project: " + id, () -> "Failed to change the description of project " + id + " to \"" + newDescription + "\"");
    }
}
Also used : Project(org.finos.legend.sdlc.domain.model.project.Project) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabProjectId(org.finos.legend.sdlc.server.gitlab.GitLabProjectId) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException)

Example 39 with LegendSDLCServerException

use of org.finos.legend.sdlc.server.error.LegendSDLCServerException in project legend-sdlc by finos.

the class GitLabAuthResource method processAuthCallback.

private Object processAuthCallback(String code, String state) {
    TokenReader reader = Token.newReader(state);
    String gitLabModeName = reader.getString();
    String originalRequestMethod = reader.getString();
    String originalRequestURL = reader.getString();
    GitLabMode mode;
    try {
        mode = GitLabMode.valueOf(gitLabModeName);
    } catch (IllegalArgumentException e) {
        throw new LegendSDLCServerException("Unknown GitLab mode: " + gitLabModeName, Status.INTERNAL_SERVER_ERROR);
    }
    try {
        this.userContext.gitLabAuthCallback(mode, code);
    } catch (LegendSDLCServerException e) {
        throw e;
    } catch (Exception e) {
        StringBuilder message = new StringBuilder("Error processing auth callback");
        String eMessage = e.getMessage();
        if (eMessage != null) {
            message.append(": ").append(eMessage);
        }
        throw new LegendSDLCServerException(message.toString(), Status.INTERNAL_SERVER_ERROR, e);
    }
    if (!"GET".equalsIgnoreCase(originalRequestMethod)) {
        // TODO consider whether 503 is the right status code
        throw new LegendSDLCServerException("Please retry request: " + originalRequestMethod + " " + originalRequestURL, Status.SERVICE_UNAVAILABLE);
    }
    // Redirect to original request URL
    throw new LegendSDLCServerException(originalRequestURL, Status.FOUND);
}
Also used : LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) TokenReader(org.finos.legend.sdlc.server.auth.Token.TokenReader) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) GitLabAuthAccessException(org.finos.legend.sdlc.server.gitlab.auth.GitLabAuthAccessException) GitLabApiException(org.gitlab4j.api.GitLabApiException) GitLabMode(org.finos.legend.sdlc.server.gitlab.mode.GitLabMode)

Example 40 with LegendSDLCServerException

use of org.finos.legend.sdlc.server.error.LegendSDLCServerException in project legend-sdlc by finos.

the class GitLabApiTestSetupUtil method prepareGitLabUserContextHelper.

/**
 * Authenticates to GitLab and creates a test GitLabUserContext.
 *
 * @param username   the name of user for whom we create this context.
 * @param password   the password of user for whom we create this context.
 * @param hostUrl    the url of the test host.
 * @param hostScheme the scheme of the test host.
 * @param hostHost   the test host.
 * @param hostPort   the port (if necessary) of the test host.
 */
public static GitLabUserContext prepareGitLabUserContextHelper(String username, String password, String hostUrl, String hostScheme, String hostHost, Integer hostPort) throws LegendSDLCServerException {
    GitLabMode gitLabMode = GitLabMode.PROD;
    TestHttpServletRequest httpServletRequest = new TestHttpServletRequest();
    TestGitLabSession session = new TestGitLabSession(username);
    GitLabApi oauthGitLabApi;
    Version version;
    try {
        oauthGitLabApi = GitLabApi.oauth2Login(hostUrl, username, password, null, null, true);
        Assert.assertNotNull(oauthGitLabApi);
        version = oauthGitLabApi.getVersion();
    } catch (GitLabApiException e) {
        StringBuilder builder = new StringBuilder("Error instantiating GitLabApi via OAuth2; response status: ").append(e.getHttpStatus());
        StringTools.appendThrowableMessageIfPresent(builder, e, "; error message: ");
        if (e.hasValidationErrors()) {
            builder.append("; validation error(s): ").append(e.getValidationErrors());
        }
        throw new LegendSDLCServerException(builder.toString(), e);
    }
    String oauthToken = oauthGitLabApi.getAuthToken();
    LOGGER.info("Retrieved access token: {}", oauthToken);
    Assert.assertNotNull(version);
    GitLabServerInfo gitLabServerInfo = GitLabServerInfo.newServerInfo(hostScheme, hostHost, hostPort);
    GitLabAppInfo gitLabAppInfo = GitLabAppInfo.newAppInfo(gitLabServerInfo, null, null, null);
    GitLabModeInfo gitLabModeInfo = GitLabModeInfo.newModeInfo(gitLabMode, gitLabAppInfo);
    session.setGitLabToken(gitLabMode, oauthToken, TokenType.OAUTH2_ACCESS);
    session.setModeInfo(gitLabModeInfo);
    LegendSDLCWebFilter.setSessionAttributeOnServletRequest(httpServletRequest, session);
    GitLabAuthorizerManager authorizerManager = GitLabAuthorizerManager.newManager(Collections.emptyList());
    return new GitLabUserContext(httpServletRequest, null, authorizerManager);
}
Also used : GitLabServerInfo(org.finos.legend.sdlc.server.gitlab.GitLabServerInfo) GitLabApi(org.gitlab4j.api.GitLabApi) GitLabModeInfo(org.finos.legend.sdlc.server.gitlab.mode.GitLabModeInfo) GitLabAuthorizerManager(org.finos.legend.sdlc.server.gitlab.auth.GitLabAuthorizerManager) GitLabApiException(org.gitlab4j.api.GitLabApiException) TestGitLabSession(org.finos.legend.sdlc.server.gitlab.auth.TestGitLabSession) GitLabAppInfo(org.finos.legend.sdlc.server.gitlab.GitLabAppInfo) LegendSDLCServerException(org.finos.legend.sdlc.server.error.LegendSDLCServerException) Version(org.gitlab4j.api.models.Version) GitLabUserContext(org.finos.legend.sdlc.server.gitlab.auth.GitLabUserContext) GitLabMode(org.finos.legend.sdlc.server.gitlab.mode.GitLabMode)

Aggregations

LegendSDLCServerException (org.finos.legend.sdlc.server.error.LegendSDLCServerException)64 GitLabProjectId (org.finos.legend.sdlc.server.gitlab.GitLabProjectId)34 GitLabApiException (org.gitlab4j.api.GitLabApiException)23 ProjectFileAccessProvider (org.finos.legend.sdlc.server.project.ProjectFileAccessProvider)20 RepositoryApi (org.gitlab4j.api.RepositoryApi)17 MergeRequest (org.gitlab4j.api.models.MergeRequest)16 GitLabApi (org.gitlab4j.api.GitLabApi)14 Branch (org.gitlab4j.api.models.Branch)11 ProjectStructure (org.finos.legend.sdlc.server.project.ProjectStructure)10 ProjectConfiguration (org.finos.legend.sdlc.domain.model.project.configuration.ProjectConfiguration)7 GitLabMode (org.finos.legend.sdlc.server.gitlab.mode.GitLabMode)7 DiffRef (org.gitlab4j.api.models.DiffRef)7 Revision (org.finos.legend.sdlc.domain.model.revision.Revision)6 VersionId (org.finos.legend.sdlc.domain.model.version.VersionId)6 MergeRequestApi (org.gitlab4j.api.MergeRequestApi)6 List (java.util.List)5 CommitsApi (org.gitlab4j.api.CommitsApi)5 Commit (org.gitlab4j.api.models.Commit)5 Comparator (java.util.Comparator)4 Pattern (java.util.regex.Pattern)4