use of com.hp.octane.integrations.exceptions.ErrorCodeBasedException in project octane-ci-java-sdk by MicroFocus.
the class TasksProcessorImpl method execute.
@Override
public OctaneResultAbridged execute(OctaneTaskAbridged task) {
if (task == null) {
throw new IllegalArgumentException("task MUST NOT be null");
}
if (task.getUrl() == null || task.getUrl().isEmpty()) {
throw new IllegalArgumentException("task 'URL' MUST NOT be null nor empty");
}
if (!task.getUrl().contains(NGA_API)) {
throw new IllegalArgumentException("task 'URL' expected to contain '" + NGA_API + "'; wrong handler call?");
}
long startTime = System.currentTimeMillis();
logger.info(configurer.octaneConfiguration.getLocationForLog() + "processing task '" + task.getId() + "': " + task.getMethod() + " " + task.getUrl());
OctaneResultAbridged result = DTOFactory.getInstance().newDTO(OctaneResultAbridged.class);
result.setId(task.getId());
result.setStatus(HttpStatus.SC_OK);
result.setHeaders(new HashMap<>());
result.setServiceId(configurer.octaneConfiguration.getInstanceId());
String[] path = pathTokenizer(task.getUrl());
try {
if (path.length == 1 && STATUS.equals(path[0])) {
executeStatusRequest(result);
} else if (path.length == 1 && SUSPEND_STATUS.equals(path[0])) {
suspendCiEvents(result, task.getBody());
} else if (path[0].startsWith(JOBS)) {
if (path.length == 1) {
Map<String, String> queryParams = getQueryParamsMap(path[0]);
boolean includingParameters = !"false".equals(queryParams.get("parameters"));
Long workspaceId = queryParams.containsKey("workspaceId") ? Long.parseLong(queryParams.get("workspaceId")) : null;
executeJobsListRequest(result, includingParameters, workspaceId);
} else if (path.length == 2) {
executePipelineRequest(result, path[1]);
} else if (path.length == 3 && RUN.equals(path[2])) {
executePipelineRunExecuteRequest(result, path[1], task.getBody());
} else if (path.length == 3 && STOP.equals(path[2])) {
executePipelineRunStopRequest(result, path[1], task.getBody());
} else {
result.setStatus(HttpStatus.SC_NOT_FOUND);
}
} else if (BUILD_STATUS.equalsIgnoreCase(path[0])) {
executeGetBulkBuildStatusRequest(result, task.getBody());
} else if (path.length == 2 && path[0].startsWith(BRANCHES)) {
Map<String, String> queryParams = getQueryParamsMap(path[1]);
String jobCiId = path[1].substring(0, path[1].indexOf("?"));
String filterBranchName = queryParams.getOrDefault("filterBranchName", null);
executeBranchesListRequest(result, jobCiId, filterBranchName);
} else if (EXECUTOR.equalsIgnoreCase(path[0])) {
if (HttpMethod.POST.equals(task.getMethod()) && path.length == 2) {
if (INIT.equalsIgnoreCase(path[1])) {
DiscoveryInfo discoveryInfo = dtoFactory.dtoFromJson(task.getBody(), DiscoveryInfo.class);
discoveryInfo.setConfigurationId(configurer.octaneConfiguration.getInstanceId());
configurer.pluginServices.runTestDiscovery(discoveryInfo);
PipelineNode node = configurer.pluginServices.createExecutor(discoveryInfo);
if (node != null) {
result.setBody(dtoFactory.dtoToJson(node));
result.getHeaders().put(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.getMimeType());
}
result.setStatus(HttpStatus.SC_OK);
} else if (TEST_CONN.equalsIgnoreCase(path[1])) {
TestConnectivityInfo testConnectivityInfo = dtoFactory.dtoFromJson(task.getBody(), TestConnectivityInfo.class);
OctaneResponse connTestResult = configurer.pluginServices.checkRepositoryConnectivity(testConnectivityInfo);
result.setStatus(connTestResult.getStatus());
result.setBody(connTestResult.getBody());
} else if (CREDENTIALS_UPSERT.equalsIgnoreCase(path[1])) {
CredentialsInfo credentialsInfo = dtoFactory.dtoFromJson(task.getBody(), CredentialsInfo.class);
executeUpsertCredentials(result, credentialsInfo);
} else {
result.setStatus(HttpStatus.SC_NOT_FOUND);
}
} else if (HttpMethod.DELETE.equals(task.getMethod()) && path.length == 2) {
String id = path[1];
configurer.pluginServices.deleteExecutor(id);
} else if (HttpMethod.GET.equals(task.getMethod()) && path.length == 2) {
if (CREDENTIALS.equalsIgnoreCase(path[1])) {
List<CredentialsInfo> credentials = configurer.pluginServices.getCredentials();
String json = dtoFactory.dtoCollectionToJson(credentials);
result.setBody(json);
}
}
} else {
result.setStatus(HttpStatus.SC_NOT_FOUND);
}
} catch (ErrorCodeBasedException pe) {
logger.warn(configurer.octaneConfiguration.getLocationForLog() + "task execution failed; error: " + pe.getErrorCode() + ", message : " + pe.getMessage());
result.setStatus(pe.getErrorCode());
result.setBody(String.valueOf(pe.getErrorCode()));
} catch (SPIMethodNotImplementedException spimnie) {
result.setStatus(HttpStatus.SC_NOT_IMPLEMENTED);
} catch (Throwable e) {
logger.error(configurer.octaneConfiguration.getLocationForLog() + "task execution failed", e);
result.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
TaskProcessingErrorBody errorBody = dtoFactory.newDTO(TaskProcessingErrorBody.class).setErrorMessage("Task " + task.getUrl() + " is failed. Server error message: " + e.getMessage());
result.setBody(dtoFactory.dtoToJson(errorBody));
result.getHeaders().put(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_JSON.getMimeType());
logger.warn(configurer.octaneConfiguration.getLocationForLog() + "OctaneResultAbridged.execute failed : " + e.getMessage());
}
logger.info(configurer.octaneConfiguration.getLocationForLog() + "result for task '" + task.getId() + "' available with status " + result.getStatus() + ", processing time is " + ((System.currentTimeMillis() - startTime) / 1000) + " seconds");
return result;
}
Aggregations