use of org.kohsuke.stapler.HttpResponse in project blueocean-plugin by jenkinsci.
the class GitScm method validateAndCreate.
@Override
public HttpResponse validateAndCreate(@JsonBody JSONObject request) {
boolean requirePush = request.has("requirePush");
// --[ Grab repo url and SCMSource ]----------------------------------------------------------
final String repositoryUrl;
final AbstractGitSCMSource scmSource;
if (request.has("repositoryUrl")) {
repositoryUrl = request.getString("repositoryUrl");
scmSource = new GitSCMSource(repositoryUrl);
} else {
try {
String fullName = request.getJSONObject("pipeline").getString("fullName");
SCMSourceOwner item = Jenkins.get().getItemByFullName(fullName, SCMSourceOwner.class);
if (item != null) {
scmSource = (AbstractGitSCMSource) item.getSCMSources().iterator().next();
repositoryUrl = scmSource.getRemote();
} else {
return HttpResponses.errorJSON("No repository found for: " + fullName);
}
} catch (JSONException e) {
return HttpResponses.errorJSON("No repositoryUrl or pipeline.fullName specified in request.");
} catch (RuntimeException e) {
return HttpResponses.errorWithoutStack(ServiceException.INTERNAL_SERVER_ERROR, e.getMessage());
}
}
// --[ Grab user ]-------------------------------------------------------------------------------------
User user = User.current();
if (user == null) {
throw new ServiceException.UnauthorizedException("Not authenticated");
}
// --[ Get credential id from request or create from repo url ]----------------------------------------
String credentialId = null;
if (request.has("credentialId")) {
credentialId = request.getString("credentialId");
}
if (credentialId == null) {
credentialId = makeCredentialId(repositoryUrl);
}
if (credentialId == null) {
// Still null? Must be a bad repoURL
throw new ServiceException.BadRequestException("Invalid URL \"" + repositoryUrl + "\"");
}
// Create new is only for username + password
if (request.has("userName") || request.has("password")) {
createPWCredentials(credentialId, user, request, repositoryUrl);
}
final StandardCredentials creds = CredentialsMatchers.firstOrNull(CredentialsProvider.lookupCredentials(StandardCredentials.class, Jenkins.get(), Jenkins.getAuthentication(), (List<DomainRequirement>) null), CredentialsMatchers.allOf(CredentialsMatchers.withId(credentialId)));
if (creds == null) {
throw new ServiceException.NotFoundException("No credentials found for: " + credentialId);
}
try {
if (requirePush) {
String branch = request.getString("branch");
if (repositoryUrl != null) {
((GitSCMSource) scmSource).setCredentialsId(credentialId);
}
new GitBareRepoReadSaveRequest(scmSource, branch, null, branch, null, null).invokeOnScm((GitSCMFileSystem.FSFunction<Void>) repository -> {
GitUtils.validatePushAccess(repository, repositoryUrl, creds);
return null;
});
} else {
List<ErrorMessage.Error> errors = GitUtils.validateCredentials(repositoryUrl, creds);
if (!errors.isEmpty()) {
throw new ServiceException.UnauthorizedException(errors.get(0).getMessage());
}
}
} catch (Exception e) {
String message = e.getMessage();
if (message != null && message.contains("TransportException")) {
throw new ServiceException.PreconditionRequired("Repository URL unreachable: " + repositoryUrl);
}
return HttpResponses.errorWithoutStack(ServiceException.PRECONDITION_REQUIRED, message);
}
return HttpResponses.okJSON();
}
use of org.kohsuke.stapler.HttpResponse in project blueocean-plugin by jenkinsci.
the class PipelineNodeImpl method restart.
public HttpResponse restart(StaplerRequest request) {
try {
JSONObject body = JSONObject.fromObject(IOUtils.toString(request.getReader()));
boolean restart = body.getBoolean("restart");
if (restart && isRestartable()) {
LOGGER.debug("submitInputStep, restart: {}, step: {}", restart, this.getDisplayName());
RestartDeclarativePipelineAction restartDeclarativePipelineAction = this.run.getAction(RestartDeclarativePipelineAction.class);
Queue.Item item = restartDeclarativePipelineAction.run(this.getDisplayName());
BluePipeline bluePipeline = BluePipelineFactory.getPipelineInstance(this.run.getParent(), this.parent);
BlueQueueItem queueItem = QueueUtil.getQueuedItem(bluePipeline.getOrganization(), item, run.getParent());
if (queueItem != null) {
// If the item is still queued
return (req, rsp, node1) -> {
rsp.setStatus(HttpServletResponse.SC_OK);
rsp.getOutputStream().print(Export.toJson(queueItem.toRun()));
};
}
final WorkflowRun restartRun = getRun(run.getParent(), item.getId());
if (restartRun != null) {
return (req, rsp, node1) -> {
rsp.setStatus(HttpServletResponse.SC_OK);
rsp.getOutputStream().print(Export.toJson(new PipelineRunImpl(restartRun, parent, bluePipeline.getOrganization())));
};
} else {
// For some reason could not be added to the queue
throw new ServiceException.UnexpectedErrorException("Run was not added to queue.");
}
}
// ISE cant happen if stage not restartable or anything else :)
} catch (Exception e) {
LOGGER.warn("error restarting stage: " + e.getMessage(), e);
throw new ServiceException.UnexpectedErrorException(e.getMessage());
}
return null;
}
use of org.kohsuke.stapler.HttpResponse in project blueocean-plugin by jenkinsci.
the class PipelineStepImpl method submitInputStep.
@Override
public HttpResponse submitInputStep(StaplerRequest request) {
JSONObject body;
try {
body = JSONObject.fromObject(IOUtils.toString(request.getReader()));
} catch (IOException e) {
throw new ServiceException.UnexpectedErrorException(e.getMessage());
}
String id = body.getString(ID_ELEMENT);
if (id == null) {
throw new ServiceException.BadRequestException("id is required");
}
if (body.get(PARAMETERS_ELEMENT) == null && body.get(ABORT_ELEMENT) == null) {
throw new ServiceException.BadRequestException("parameters is required");
}
WorkflowRun run = node.getRun();
InputAction inputAction = run.getAction(InputAction.class);
if (inputAction == null) {
throw new ServiceException.BadRequestException("Error processing Input Submit request. This Run instance does not" + " have an InputAction.");
}
try {
InputStepExecution execution = inputAction.getExecution(id);
if (execution == null) {
throw new ServiceException.BadRequestException(String.format("Error processing Input Submit request. This Run instance does not" + " have an Input with an id of '%s'.", id));
}
// if abort, abort and return
if (body.get(ABORT_ELEMENT) != null && body.getBoolean(ABORT_ELEMENT)) {
return execution.doAbort();
}
try {
execution.preSubmissionCheck();
} catch (Failure f) {
throw new ServiceException.BadRequestException(f.getMessage());
}
Object o = parseValue(execution, JSONArray.fromObject(body.get(PARAMETERS_ELEMENT)), request);
HttpResponse response = execution.proceed(o);
for (PipelineInputStepListener listener : ExtensionList.lookup(PipelineInputStepListener.class)) {
listener.onStepContinue(execution.getInput(), run);
}
return response;
} catch (IOException | InterruptedException | TimeoutException e) {
throw new ServiceException.UnexpectedErrorException("Error processing Input Submit request." + e.getMessage());
}
}
Aggregations