Search in sources :

Example 6 with GitLabMode

use of org.finos.legend.sdlc.server.gitlab.mode.GitLabMode 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 7 with GitLabMode

use of org.finos.legend.sdlc.server.gitlab.mode.GitLabMode in project legend-sdlc by finos.

the class GitLabTokenManager method encode.

Token.TokenBuilder encode(Token.TokenBuilder builder) {
    synchronized (this.tokens) {
        builder.putInt(this.tokens.size());
        for (GitLabMode mode : GitLabMode.values()) {
            GitLabToken token = this.tokens.get(mode);
            if (token != null) {
                builder.putString(mode.name());
                builder.putString(this.modeInfos.getModeInfo(mode).getAppInfo().getAppId());
                builder.putString(token.getTokenType().toString());
                builder.putString(token.getToken());
            }
        }
        return builder;
    }
}
Also used : GitLabMode(org.finos.legend.sdlc.server.gitlab.mode.GitLabMode)

Example 8 with GitLabMode

use of org.finos.legend.sdlc.server.gitlab.mode.GitLabMode in project legend-sdlc by finos.

the class GitLabTokenManager method appendTokenInfo.

StringBuilder appendTokenInfo(StringBuilder builder) {
    synchronized (this.tokens) {
        builder.append("tokens=[");
        int startLen = builder.length();
        for (GitLabMode mode : GitLabMode.values()) {
            GitLabToken token = this.tokens.get(mode);
            if (token != null) {
                ((builder.length() == startLen) ? builder : builder.append(", ")).append(mode.name()).append('=').append(token.toString());
            }
        }
        builder.append(']');
        return builder;
    }
}
Also used : GitLabMode(org.finos.legend.sdlc.server.gitlab.mode.GitLabMode)

Example 9 with GitLabMode

use of org.finos.legend.sdlc.server.gitlab.mode.GitLabMode 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 10 with GitLabMode

use of org.finos.legend.sdlc.server.gitlab.mode.GitLabMode in project legend-sdlc by finos.

the class TestGitLabProjectId method testNewProjectId.

@Test
public void testNewProjectId() {
    for (GitLabMode mode : GitLabMode.values()) {
        for (int i = 0; i < 1024; i++) {
            GitLabProjectId projectId = GitLabProjectId.newProjectId(mode, i);
            Assert.assertEquals(mode, projectId.getGitLabMode());
            Assert.assertEquals(i, projectId.getGitLabId());
            Assert.assertEquals(mode.name() + "-" + i, projectId.toString());
        }
    }
}
Also used : GitLabMode(org.finos.legend.sdlc.server.gitlab.mode.GitLabMode) Test(org.junit.Test)

Aggregations

GitLabMode (org.finos.legend.sdlc.server.gitlab.mode.GitLabMode)15 LegendSDLCServerException (org.finos.legend.sdlc.server.error.LegendSDLCServerException)7 Test (org.junit.Test)4 GitLabApi (org.gitlab4j.api.GitLabApi)3 Project (org.finos.legend.sdlc.domain.model.project.Project)2 User (org.finos.legend.sdlc.domain.model.user.User)2 GitLabUserContext (org.finos.legend.sdlc.server.gitlab.auth.GitLabUserContext)2 GitLabModeInfo (org.finos.legend.sdlc.server.gitlab.mode.GitLabModeInfo)2 ProjectStructure (org.finos.legend.sdlc.server.project.ProjectStructure)2 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 EnumSet (java.util.EnumSet)1 List (java.util.List)1 Objects (java.util.Objects)1 Set (java.util.Set)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 StreamSupport (java.util.stream.StreamSupport)1 Inject (javax.inject.Inject)1