use of io.jenkins.blueocean.commons.ErrorMessage in project blueocean-plugin by jenkinsci.
the class GithubScmSaveFileRequest method save.
public Object save(@Nonnull String apiUrl, @Nullable String owner, @Nullable String repoName, @Nullable String accessToken) {
List<ErrorMessage.Error> errors = new ArrayList<>();
if (this.content == null) {
errors.add(new ErrorMessage.Error("content", ErrorMessage.Error.ErrorCodes.MISSING.toString(), "content is required parameter"));
throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to save file to scm").addAll(errors));
} else {
errors.addAll(content.validate());
}
//if no owner given then check if its there in request
if (owner == null) {
owner = content.getOwner();
}
if (repoName == null) {
repoName = content.getRepo();
}
if (owner == null) {
errors.add(new ErrorMessage.Error("content.owner", ErrorMessage.Error.ErrorCodes.MISSING.toString(), "No scm owner found with pipeline %s, please provide content.owner parameter"));
}
if (repoName == null) {
errors.add(new ErrorMessage.Error("content.repo", ErrorMessage.Error.ErrorCodes.MISSING.toString(), "No scm repo found with pipeline %s, please provide content.repo parameter"));
}
if (errors.size() > 0) {
throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to save content").addAll(errors));
}
if (!errors.isEmpty()) {
throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to save file to scm").addAll(errors));
}
try {
String sha = createBranchIfNotPresent(apiUrl, owner, repoName, accessToken);
//sha in request overrides the one we computed
if (!StringUtils.isBlank(content.getSha())) {
sha = content.getSha();
}
Map<String, Object> body = new HashMap<>();
body.put("message", content.getMessage());
body.put("content", content.getBase64Data());
if (!StringUtils.isBlank(content.getBranch())) {
body.put("branch", content.getBranch());
}
if (!StringUtils.isBlank(sha)) {
body.put("sha", sha);
}
final Map ghResp = HttpRequest.put(String.format("%s/repos/%s/%s/contents/%s", apiUrl, owner, repoName, content.getPath())).withAuthorization("token " + accessToken).withBody(body).to(Map.class);
if (ghResp == null) {
throw new ServiceException.UnexpectedErrorException("Failed to save file to Github: " + content.getPath());
}
final Map ghContent = (Map) ghResp.get("content");
if (ghContent == null) {
throw new ServiceException.UnexpectedErrorException("Failed to save file: " + content.getPath());
}
return new GithubFile(new GithubContent.Builder().sha((String) ghContent.get("sha")).name((String) ghContent.get("name")).repo(repoName).owner(owner).path((String) ghContent.get("path")).build());
} catch (IOException e) {
throw new ServiceException.UnexpectedErrorException("Failed to save file: " + e.getMessage(), e);
}
}
use of io.jenkins.blueocean.commons.ErrorMessage in project blueocean-plugin by jenkinsci.
the class GithubPipelineCreateRequest method validateCredentialId.
static void validateCredentialId(String credentialId, String apiUrl) throws IOException {
if (credentialId != null && !credentialId.trim().isEmpty()) {
StandardUsernamePasswordCredentials credentials = CredentialsUtils.findCredential(credentialId, StandardUsernamePasswordCredentials.class, new BlueOceanDomainRequirement());
if (credentials == null) {
throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to create Github pipeline").add(new ErrorMessage.Error("scmConfig.credentialId", ErrorMessage.Error.ErrorCodes.NOT_FOUND.toString(), "No Credentials instance found for credentialId: " + credentialId)));
} else {
String accessToken = credentials.getPassword().getPlainText();
validateGithubAccessToken(accessToken, apiUrl);
}
}
}
use of io.jenkins.blueocean.commons.ErrorMessage in project blueocean-plugin by jenkinsci.
the class BluePipelineContainer method create.
/**
* Create new pipeline.
*
* @param body {@link BluePipelineCreateRequest} request object
* @return {@link CreateResponse} response
*/
@POST
@WebMethod(name = "")
public CreateResponse create(@JsonBody JSONObject body, StaplerRequest staplerRequest) throws IOException {
ErrorMessage err = new ErrorMessage(400, "Failed to create Git pipeline");
if (body.get("name") == null) {
err.add(new ErrorMessage.Error("name", ErrorMessage.Error.ErrorCodes.MISSING.toString(), "name is required"));
}
if (body.get("$class") == null) {
err.add(new ErrorMessage.Error("$class", ErrorMessage.Error.ErrorCodes.MISSING.toString(), "$class is required"));
}
if (!err.getErrors().isEmpty()) {
throw new ServiceException.BadRequestExpception(err);
}
BluePipelineCreateRequest request = staplerRequest.bindJSON(BluePipelineCreateRequest.class, body);
return create(request);
}
use of io.jenkins.blueocean.commons.ErrorMessage in project blueocean-plugin by jenkinsci.
the class GitPipelineCreateRequest method validate.
private void validate(String name, BlueScmConfig scmConfig) {
if (scmConfig == null) {
throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to create Git pipeline").add(new ErrorMessage.Error("scmConfig", ErrorMessage.Error.ErrorCodes.MISSING.toString(), "scmConfig is required")));
}
List<ErrorMessage.Error> errors = new ArrayList<>();
String sourceUri = scmConfig.getUri();
if (sourceUri == null) {
errors.add(new ErrorMessage.Error("scmConfig.uri", ErrorMessage.Error.ErrorCodes.MISSING.toString(), "uri is required"));
} else {
StandardCredentials credentials = null;
if (scmConfig.getCredentialId() != null) {
credentials = GitUtils.getCredentials(Jenkins.getInstance(), sourceUri, scmConfig.getCredentialId());
if (credentials == null) {
errors.add(new ErrorMessage.Error("scmConfig.credentialId", ErrorMessage.Error.ErrorCodes.NOT_FOUND.toString(), String.format("credentialId: %s not found", scmConfig.getCredentialId())));
}
}
//validate credentials if no credential id (perhaps git repo doesn't need auth or credentials is present)
if (scmConfig.getCredentialId() == null || credentials != null) {
errors.addAll(GitUtils.validateCredentials(sourceUri, credentials));
}
}
try {
Jenkins.getInstance().getProjectNamingStrategy().checkName(getName());
} catch (Failure f) {
errors.add(new ErrorMessage.Error("scmConfig.name", ErrorMessage.Error.ErrorCodes.INVALID.toString(), name + "in not a valid name"));
}
if (Jenkins.getInstance().getItem(name) != null) {
errors.add(new ErrorMessage.Error("name", ErrorMessage.Error.ErrorCodes.ALREADY_EXISTS.toString(), name + " already exists"));
}
if (!errors.isEmpty()) {
throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to create Git pipeline:" + name).addAll(errors));
}
}
use of io.jenkins.blueocean.commons.ErrorMessage in project blueocean-plugin by jenkinsci.
the class GitPipelineUpdateRequest method getGitScmSource.
@SuppressWarnings("unchecked")
private BranchSource getGitScmSource(MultiBranchProject mbp) {
String sourceUri = null;
String credentialId = null;
if (scmConfig != null) {
sourceUri = scmConfig.getUri();
List<ErrorMessage.Error> errors = new ArrayList<>();
StandardCredentials credentials = null;
if (scmConfig.getCredentialId() != null) {
credentials = GitUtils.getCredentials(Jenkins.getInstance(), sourceUri, scmConfig.getCredentialId());
if (credentials == null) {
errors.add(new ErrorMessage.Error("scmConfig.credentialId", ErrorMessage.Error.ErrorCodes.NOT_FOUND.toString(), String.format("credentialId: %s not found", scmConfig.getCredentialId())));
}
}
if (sourceUri != null) {
errors.addAll(GitUtils.validateCredentials(sourceUri, credentials));
}
credentialId = scmConfig.getCredentialId() == null ? "" : scmConfig.getCredentialId();
if (!errors.isEmpty()) {
throw new ServiceException.BadRequestExpception(new ErrorMessage(400, "Failed to create Git pipeline").addAll(errors));
}
}
PersistedList<BranchSource> sources = mbp.getSourcesList();
for (BranchSource source : sources) {
if (source.getSource() instanceof GitSCMSource) {
GitSCMSource gitSCMSource = (GitSCMSource) source.getSource();
String remote = gitSCMSource.getRemote();
if (sourceUri != null && !sourceUri.equals(gitSCMSource.getRemote())) {
remote = sourceUri;
}
String cred = gitSCMSource.getCredentialsId();
if (!gitSCMSource.getCredentialsId().equals(credentialId)) {
cred = credentialId;
}
GitSCMSource s = new GitSCMSource(gitSCMSource.getId(), remote, cred, gitSCMSource.getIncludes(), gitSCMSource.getExcludes(), gitSCMSource.isIgnoreOnPushNotifications());
s.setOwner(mbp);
return new BranchSource(s);
}
}
if (sourceUri != null) {
//if no scm sources in this MBP project, add a new one using passed sourceUri
return new BranchSource(new GitSCMSource(null, sourceUri, credentialId, "*", "", false));
}
return null;
}
Aggregations