UNPKG

@atomist/sdm-core

Version:

Atomist Software Delivery Machine - Implementation

106 lines 5.84 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 __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; 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 decorators_1 = require("@atomist/automation-client/lib/decorators"); const sdm_1 = require("@atomist/sdm"); const validateGoal_1 = require("../../../../internal/delivery/goals/support/validateGoal"); const goalSigning_1 = require("../../../../internal/signing/goalSigning"); /** * Skip downstream goals on failed or stopped goal */ let SkipDownstreamGoalsOnGoalFailure = class SkipDownstreamGoalsOnGoalFailure { handle(event, context) { return __awaiter(this, void 0, void 0, function* () { const failedGoal = event.data.SdmGoal[0]; if (!validateGoal_1.shouldHandle(failedGoal)) { automation_client_1.logger.debug(`Goal ${failedGoal.uniqueName} skipped because not managed by this SDM`); return automation_client_1.Success; } yield goalSigning_1.verifyGoal(failedGoal, this.configuration.sdm.goalSigning, context); const goals = sdm_1.fetchGoalsFromPush(failedGoal); const goalsToSkip = goals.filter(g => isDependentOn(failedGoal, g, sdm_1.mapKeyToGoal(goals))) .filter(g => g.state === "planned"); let failedGoalState; let failedGoalDescription; switch (failedGoal.state) { case sdm_1.SdmGoalState.failure: failedGoalDescription = "failed"; failedGoalState = sdm_1.SdmGoalState.skipped; break; case sdm_1.SdmGoalState.stopped: failedGoalDescription = "stopped goals"; failedGoalState = sdm_1.SdmGoalState.skipped; break; case sdm_1.SdmGoalState.canceled: failedGoalDescription = "was canceled"; failedGoalState = sdm_1.SdmGoalState.canceled; break; } yield Promise.all(goalsToSkip.map(g => sdm_1.updateGoal(context, g, { state: failedGoalState, description: `${failedGoalState === sdm_1.SdmGoalState.skipped ? "Skipped" : "Canceled"} ${g.name} because ${failedGoal.name} ${failedGoalDescription}`, }))); return automation_client_1.Success; }); } }; __decorate([ automation_client_1.Value(""), __metadata("design:type", Object) ], SkipDownstreamGoalsOnGoalFailure.prototype, "configuration", void 0); SkipDownstreamGoalsOnGoalFailure = __decorate([ decorators_1.EventHandler("Skip downstream goals on failed, stopped or canceled goal", () => automation_client_1.GraphQL.subscription({ name: "OnAnyFailedSdmGoal", variables: { registration: () => { var _a, _b; return [(_b = (_a = automation_client_1.automationClientInstance()) === null || _a === void 0 ? void 0 : _a.configuration) === null || _b === void 0 ? void 0 : _b.name]; } }, })) ], SkipDownstreamGoalsOnGoalFailure); exports.SkipDownstreamGoalsOnGoalFailure = SkipDownstreamGoalsOnGoalFailure; function isDependentOn(failedGoal, goal, preconditionToGoal) { if (!goal) { return false; } if (!goal.preConditions || goal.preConditions.length === 0) { return false; // no preconditions? not dependent } if (sdm_1.mapKeyToGoal(goal.preConditions)(failedGoal)) { return true; // the failed goal is one of my preconditions? dependent } // otherwise, recurse on my preconditions return !!goal.preConditions .map(precondition => isDependentOn(failedGoal, preconditionToGoal(precondition), preconditionToGoal)) .find(a => a); // if one is true, return true } //# sourceMappingURL=SkipDownstreamGoalsOnGoalFailure.js.map