UNPKG

@atomist/sdm-core

Version:

Atomist Software Delivery Machine - Implementation

159 lines 8.74 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 SdmGoalMessage_1 = require("@atomist/sdm/lib/api/goal/SdmGoalMessage"); const validateGoal_1 = require("../../../../internal/delivery/goals/support/validateGoal"); const goalSigning_1 = require("../../../../internal/signing/goalSigning"); const types_1 = require("../../../../typings/types"); /** * Move downstream goals from 'planned' to 'requested' when preconditions are met. */ let RequestDownstreamGoalsOnGoalSuccess = class RequestDownstreamGoalsOnGoalSuccess { constructor(name, implementationMapper, repoRefResolver, credentialsResolver, preferenceStoreFactory) { this.name = name; this.implementationMapper = implementationMapper; this.repoRefResolver = repoRefResolver; this.credentialsResolver = credentialsResolver; this.preferenceStoreFactory = preferenceStoreFactory; } handle(event, context) { return __awaiter(this, void 0, void 0, function* () { const sdmGoal = event.data.SdmGoal[0]; if (!validateGoal_1.shouldHandle(sdmGoal)) { automation_client_1.logger.debug(`Goal ${sdmGoal.uniqueName} skipped because not managed by this SDM`); return automation_client_1.Success; } yield goalSigning_1.verifyGoal(sdmGoal, this.configuration.sdm.goalSigning, context); const id = this.repoRefResolver.repoRefFromPush(sdmGoal.push); const credentials = yield sdm_1.resolveCredentialsPromise(this.credentialsResolver.eventHandlerCredentials(context, id)); const preferences = this.preferenceStoreFactory(context); const goals = sdm_1.fetchGoalsFromPush(sdmGoal); const goalsToRequest = goals.filter(g => isDirectlyDependentOn(sdmGoal, g)) .filter(g => expectToBeFulfilledAfterRequest(g, this.name)) .filter(shouldBePlannedOrSkipped) .filter(g => sdm_1.preconditionsAreMet(g, { goalsForCommit: goals })); if (goalsToRequest.length > 0) { automation_client_1.logger.debug("because %s is successful, these goals are now ready: %s", sdm_1.goalKeyString(sdmGoal), goalsToRequest.map(sdm_1.goalKeyString).join(", ")); } yield Promise.all(goalsToRequest.map((sdmG) => __awaiter(this, void 0, void 0, function* () { if (sdmG.preApprovalRequired) { return sdm_1.updateGoal(context, sdmG, { state: types_1.SdmGoalState.waiting_for_pre_approval, description: !!sdmGoal.descriptions && !!sdmGoal.descriptions.waitingForPreApproval ? sdmGoal.descriptions.waitingForPreApproval : `Start required: ${sdmG.name}`, }); } else { let g = sdmG; const cbs = this.implementationMapper.findFulfillmentCallbackForGoal(sdmG); for (const cb of cbs) { g = yield cb.callback(g, { id, addressChannels: undefined, preferences, configuration: this.configuration, credentials, context, }); } return sdm_1.updateGoal(context, g, { state: types_1.SdmGoalState.requested, description: !!sdmGoal.descriptions && !!sdmGoal.descriptions.requested ? sdmGoal.descriptions.requested : `Ready: ${g.name}`, data: g.data, }); } }))); return automation_client_1.Success; }); } }; __decorate([ automation_client_1.Value(""), __metadata("design:type", Object) ], RequestDownstreamGoalsOnGoalSuccess.prototype, "configuration", void 0); RequestDownstreamGoalsOnGoalSuccess = __decorate([ decorators_1.EventHandler("Move downstream goals from 'planned' to 'requested' when preconditions are met", () => automation_client_1.GraphQL.subscription({ name: "OnAnySuccessfulSdmGoal", 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]; } }, })), __metadata("design:paramtypes", [String, Object, Object, Object, Function]) ], RequestDownstreamGoalsOnGoalSuccess); exports.RequestDownstreamGoalsOnGoalSuccess = RequestDownstreamGoalsOnGoalSuccess; function shouldBePlannedOrSkipped(dependentGoal) { if (dependentGoal.state === types_1.SdmGoalState.planned) { return true; } if (dependentGoal.state === types_1.SdmGoalState.skipped) { automation_client_1.logger.debug("Goal %s was skipped, but now maybe it can go", dependentGoal.uniqueName); return true; } if (dependentGoal.state === types_1.SdmGoalState.failure && dependentGoal.retryFeasible) { automation_client_1.logger.debug("Goal %s failed, but maybe we will retry it", dependentGoal.uniqueName); return true; } automation_client_1.logger.debug("Goal '%s' in state '%s' will not be requested", dependentGoal.uniqueName, dependentGoal.state); return false; } function expectToBeFulfilledAfterRequest(dependentGoal, name) { switch (dependentGoal.fulfillment.method) { case SdmGoalMessage_1.SdmGoalFulfillmentMethod.Sdm: return true; case SdmGoalMessage_1.SdmGoalFulfillmentMethod.SideEffect: return dependentGoal.fulfillment.name !== name; case SdmGoalMessage_1.SdmGoalFulfillmentMethod.Other: // legacy behavior return true; default: return false; } } function isDirectlyDependentOn(successfulGoal, goal) { if (!goal) { return false; } if (!goal.preConditions || goal.preConditions.length === 0) { return false; // no preconditions? not dependent } if (sdm_1.mapKeyToGoal(goal.preConditions)(successfulGoal)) { automation_client_1.logger.debug("%s depends on %s", goal.uniqueName, successfulGoal.uniqueName); return true; // the failed goal is one of my preconditions? dependent } return false; } //# sourceMappingURL=RequestDownstreamGoalsOnGoalSuccess.js.map