use of io.jenkins.blueocean.commons.ErrorMessage in project blueocean-plugin by jenkinsci.
the class GithubPipelineCreateRequest method create.
@SuppressWarnings("unchecked")
@Override
public BluePipeline create(Reachable parent) throws IOException {
String apiUrl = null;
//default
String orgName = getName();
String credentialId = null;
StringBuilder sb = new StringBuilder();
List<String> repos = new ArrayList<>();
if (scmConfig != null) {
apiUrl = StringUtils.defaultIfBlank(scmConfig.getUri(), GithubScm.DEFAULT_API_URI);
if (scmConfig.getConfig().get("orgName") instanceof String) {
orgName = (String) scmConfig.getConfig().get("orgName");
}
credentialId = scmConfig.getCredentialId();
if (scmConfig != null && scmConfig.getConfig().get("repos") instanceof List) {
for (String r : (List<String>) scmConfig.getConfig().get("repos")) {
sb.append(String.format("(%s\\b)?", r));
repos.add(r);
}
}
}
User authenticatedUser = User.current();
if (authenticatedUser == null) {
throw new ServiceException.UnauthorizedException("Must login to create a pipeline");
}
TopLevelItem item = null;
try {
if (credentialId != null) {
validateCredentialId(credentialId, apiUrl);
}
item = create(Jenkins.getInstance(), getName(), DESCRIPTOR, CustomOrganizationFolderDescriptor.class);
if (item instanceof OrganizationFolder) {
if (credentialId != null) {
//Find domain attached to this credentialId, if present check if it's BlueOcean specific domain then
//add the properties otherwise simply use it
Domain domain = CredentialsUtils.findDomain(credentialId, authenticatedUser);
if (domain == null) {
//this should not happen since validateCredentialId found the credential
throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to create pipeline").add(new ErrorMessage.Error("scm.credentialId", ErrorMessage.Error.ErrorCodes.INVALID.toString(), "No domain in user credentials found for credentialId: " + scmConfig.getCredentialId())));
}
if (domain.test(new BlueOceanDomainRequirement())) {
((OrganizationFolder) item).addProperty(new BlueOceanCredentialsProvider.FolderPropertyImpl(authenticatedUser.getId(), credentialId, BlueOceanCredentialsProvider.createDomain(apiUrl)));
}
}
GitHubSCMNavigator gitHubSCMNavigator = new GitHubSCMNavigator(apiUrl, orgName, credentialId, credentialId);
if (sb.length() > 0) {
gitHubSCMNavigator.setPattern(sb.toString());
}
// cick of github scan build
OrganizationFolder organizationFolder = (OrganizationFolder) item;
organizationFolder.getNavigators().replace(gitHubSCMNavigator);
if (repos.size() == 1) {
SCMSourceEvent.fireNow(new SCMSourceEventImpl(repos.get(0), item, apiUrl, gitHubSCMNavigator));
} else {
organizationFolder.scheduleBuild(new Cause.UserIdCause());
}
return new GithubOrganizationFolder(organizationFolder, parent.getLink());
}
} catch (Exception e) {
String msg = String.format("Error creating pipeline %s: %s", getName(), e.getMessage());
logger.error(msg, e);
if (item != null) {
try {
item.delete();
} catch (InterruptedException e1) {
logger.error(String.format("Error creating pipeline %s: %s", getName(), e1.getMessage()), e1);
throw new ServiceException.UnexpectedErrorException("Error cleaning up pipeline " + getName() + " due to error: " + e.getMessage(), e);
}
}
if (e instanceof ServiceException) {
throw e;
}
throw new ServiceException.UnexpectedErrorException(msg, e);
}
return null;
}
use of io.jenkins.blueocean.commons.ErrorMessage in project blueocean-plugin by jenkinsci.
the class GithubScm method getCredentialsDomainName.
private String getCredentialsDomainName(String apiUri) {
java.net.URI uri;
try {
uri = new URI(apiUri);
} catch (URISyntaxException e) {
throw new ServiceException.UnexpectedErrorException(new ErrorMessage(400, "Invalid URI: " + apiUri));
}
String domainName = getCredentialDomainName();
if (this instanceof GithubEnterpriseScm) {
return domainName + "-" + uri.getHost();
}
return domainName;
}
use of io.jenkins.blueocean.commons.ErrorMessage in project blueocean-plugin by jenkinsci.
the class GithubScmContentProvider method saveContent.
@Override
public Object saveContent(@Nonnull StaplerRequest staplerRequest, @Nonnull Item item) {
JSONObject body;
try {
body = JSONObject.fromObject(IOUtils.toString(staplerRequest.getReader()));
} catch (IOException e) {
throw new ServiceException.UnexpectedErrorException("Failed to read request body");
}
body.put("$class", "io.jenkins.blueocean.blueocean_github_pipeline.GithubScmSaveFileRequest");
GithubScmSaveFileRequest request = staplerRequest.bindJSON(GithubScmSaveFileRequest.class, body);
if (request == null) {
throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to bind request"));
}
ScmContentProvider scmContentProvider = ScmContentProvider.resolve(item);
if (scmContentProvider != null) {
return saveContent(request, item);
}
throw new ServiceException.BadRequestExpception("No save scm content provider found for pipeline: " + item.getFullName());
}
use of io.jenkins.blueocean.commons.ErrorMessage in project blueocean-plugin by jenkinsci.
the class GithubScmContentProvider method getContent.
@Override
public Object getContent(@Nonnull StaplerRequest request, @Nonnull Item item) {
String path = StringUtils.defaultIfEmpty(request.getParameter("path"), null);
String type = StringUtils.defaultIfEmpty(request.getParameter("type"), null);
String repo = StringUtils.defaultIfEmpty(request.getParameter("repo"), null);
String branch = StringUtils.defaultIfEmpty(request.getParameter("branch"), null);
List<ErrorMessage.Error> errors = new ArrayList<>();
if (!(item instanceof MultiBranchProject) && repo == null) {
errors.add(new ErrorMessage.Error("repo", ErrorMessage.Error.ErrorCodes.MISSING.toString(), String.format("repo and branch parameters are required because pipeline %s is not a multi-branch project ", item.getFullName())));
}
if (type != null && !type.equals("file")) {
errors.add(new ErrorMessage.Error("file", ErrorMessage.Error.ErrorCodes.INVALID.toString(), String.format("type %s not supported. Only 'file' type supported.", type)));
}
if (path == null) {
errors.add(new ErrorMessage.Error("path", ErrorMessage.Error.ErrorCodes.MISSING.toString(), "path is required query parameter"));
}
if (!errors.isEmpty()) {
throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to load scm file").addAll(errors));
}
ScmParamsFromItem scmParamsFromItem = new ScmParamsFromItem(item);
//if no repo param, then see if its there in given Item.
if (repo == null && scmParamsFromItem.repo == null) {
throw new ServiceException.BadRequestExpception("github repo could not be determine from pipeline: " + item.getFullName());
}
// If both, repo param and repo in pipeline scm configuration present, they better match
if (repo != null && scmParamsFromItem.repo != null && !repo.equals(scmParamsFromItem.repo)) {
throw new ServiceException.BadRequestExpception(String.format("repo parameter %s doesn't match with repo in pipeline %s github configuration repo: %s ", repo, item.getFullName(), scmParamsFromItem.repo));
}
if (repo == null) {
repo = scmParamsFromItem.repo;
}
String url = String.format("%s/repos/%s/%s/contents/%s", scmParamsFromItem.apiUrl, scmParamsFromItem.owner, repo, path);
if (branch != null) {
//if branch is present fetch this file from branch
url += "?ref=" + branch;
}
try {
Map ghContent = HttpRequest.get(url).withAuthorization("token " + scmParamsFromItem.accessToken).to(Map.class);
if (ghContent == null) {
throw new ServiceException.UnexpectedErrorException("Failed to load file: " + path);
}
return new GithubFile(new GithubContent.Builder().sha((String) ghContent.get("sha")).name((String) ghContent.get("name")).repo(repo).owner(scmParamsFromItem.owner).path(path).base64Data((String) ghContent.get("content")).build());
} catch (IOException e) {
throw new ServiceException.UnexpectedErrorException(String.format("Failed to load file %s: %s", path, e.getMessage()), e);
}
}
use of io.jenkins.blueocean.commons.ErrorMessage in project blueocean-plugin by jenkinsci.
the class GitPipelineCreateRequest method create.
@Override
public BluePipeline create(Reachable parent) throws IOException {
User authenticatedUser = User.current();
if (authenticatedUser == null) {
throw new ServiceException.UnauthorizedException("Must login to create a pipeline");
}
String sourceUri = scmConfig.getUri();
if (sourceUri == null) {
throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to create Git pipeline:" + getName()).add(new ErrorMessage.Error("scmConfig.uri", ErrorMessage.Error.ErrorCodes.MISSING.toString(), "uri is required")));
}
TopLevelItem item = create(Jenkins.getInstance(), getName(), MODE, MultiBranchProjectDescriptor.class);
if (item instanceof WorkflowMultiBranchProject) {
WorkflowMultiBranchProject project = (WorkflowMultiBranchProject) item;
if (StringUtils.isNotBlank(scmConfig.getCredentialId())) {
Domain domain = CredentialsUtils.findDomain(scmConfig.getCredentialId(), authenticatedUser);
if (domain == null) {
throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to create pipeline").add(new ErrorMessage.Error("scm.credentialId", ErrorMessage.Error.ErrorCodes.INVALID.toString(), "No domain in user credentials found for credentialId: " + scmConfig.getCredentialId())));
}
if (domain.test(new BlueOceanDomainRequirement())) {
//this is blueocean specific domain
project.addProperty(new BlueOceanCredentialsProvider.FolderPropertyImpl(authenticatedUser.getId(), scmConfig.getCredentialId(), BlueOceanCredentialsProvider.createDomain(sourceUri)));
}
}
String credentialId = StringUtils.defaultString(scmConfig.getCredentialId());
project.getSourcesList().add(new BranchSource(new GitSCMSource(null, sourceUri, credentialId, "*", "", false)));
project.scheduleBuild(new Cause.UserIdCause());
return new MultiBranchPipelineImpl(project);
} else {
try {
// we don't know about this project type
item.delete();
} catch (InterruptedException e) {
throw new ServiceException.UnexpectedErrorException("Failed to delete pipeline: " + getName());
}
}
return null;
}
Aggregations