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);
}
}
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);
}
}
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 + "\"");
}
}
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);
}
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);
}
Aggregations