UNPKG

@atomist/sdm-core

Version:

Atomist Software Delivery Machine - Implementation

109 lines 4.82 kB
"use strict"; /* * Copyright © 2019 Atomist, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const automation_client_1 = require("@atomist/automation-client"); const redact_1 = require("@atomist/automation-client/lib/util/redact"); const sdm_1 = require("@atomist/sdm"); const types_1 = require("../../typings/types"); const helpers_1 = require("./helpers"); /** * Execute an incoming job task event */ function executeTask(sdm) { return { name: "ExecuteTask", description: "Execute a job task", subscription: automation_client_1.GraphQL.subscription({ name: "OnAnyJobTask", variables: { registration: sdm.configuration.name, }, }), listener: exports.ExecuteTaskListener, }; } exports.executeTask = executeTask; exports.ExecuteTaskListener = (e, ctx) => __awaiter(void 0, void 0, void 0, function* () { const task = e.data.AtmJobTask[0]; let jobData; let taskData; try { jobData = JSON.parse(task.job.data); taskData = JSON.parse(task.data); } catch (e) { automation_client_1.logger.warn("Parsing of job or task data failed: %s", e.message); yield updateJobTaskState(task.id, types_1.AtmJobTaskState.failed, redact_1.redact(`Task command '${task.name}' failed: ${e.message}`), ctx); } if (taskData.type === sdm_1.JobTaskType.Command) { const md = automation_client_1.automationClientInstance().automationServer.automations.commands .find(c => c.name === task.name); if (!md) { yield updateJobTaskState(task.id, types_1.AtmJobTaskState.failed, `Task command '${task.name}' could not be found`, ctx); } else { try { // Invoke the command const result = yield automation_client_1.automationClientInstance().automationServer.invokeCommand(helpers_1.prepareCommandInvocation(md, taskData.parameters), helpers_1.prepareHandlerContext(ctx, jobData)); // Handle result if (!!result && result.code !== undefined) { if (result.code === 0) { yield updateJobTaskState(task.id, types_1.AtmJobTaskState.success, `Task command '${task.name}' successfully executed`, ctx); } else { yield updateJobTaskState(task.id, types_1.AtmJobTaskState.failed, redact_1.redact(result.message || `Task command '${task.name}' failed`), ctx); } } else { yield updateJobTaskState(task.id, types_1.AtmJobTaskState.success, `Task command '${task.name}' successfully executed`, ctx); } } catch (e) { automation_client_1.logger.warn("Command execution failed: %s", e.message); yield updateJobTaskState(task.id, types_1.AtmJobTaskState.failed, redact_1.redact(`Task command '${task.name}' failed: ${e.message}`), ctx); } } } return automation_client_1.Success; }); /** * Update the job task status */ function updateJobTaskState(id, state, message, ctx) { return __awaiter(this, void 0, void 0, function* () { yield ctx.graphClient.mutate({ name: "SetJobTaskState", variables: { id, state: { state, message, }, }, }); }); } //# sourceMappingURL=executeTask.js.map