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);
}
}
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;
}
}
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;
}
}
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);
}
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());
}
}
}
Aggregations