UNPKG

@atomist/sdm-core

Version:

Atomist Software Delivery Machine - Implementation

97 lines 5.03 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 sdm_1 = require("@atomist/sdm"); const slack_messages_1 = require("@atomist/slack-messages"); const _ = require("lodash"); function updateGoalStateCommand() { return { name: "UpdateGoalStateCommand", description: "Update goal state", parameters: { goalSetId: {}, uniqueName: {}, state: {}, msgId: {}, slackRequester: { uri: automation_client_1.MappedParameters.SlackUserName, required: false, declarationType: sdm_1.DeclarationType.Mapped, }, githubRequester: { uri: automation_client_1.MappedParameters.GitHubUserLogin, required: false, declarationType: sdm_1.DeclarationType.Mapped, }, teamId: { uri: automation_client_1.MappedParameters.SlackTeam, required: false, declarationType: sdm_1.DeclarationType.Mapped }, channelId: { uri: automation_client_1.MappedParameters.SlackChannel, required: false, declarationType: sdm_1.DeclarationType.Mapped }, }, listener: (ci) => __awaiter(this, void 0, void 0, function* () { const goalResult = yield ci.context.graphClient.query({ name: "SdmGoalsByGoalSetIdAndUniqueName", variables: { goalSetId: [ci.parameters.goalSetId], uniqueName: [ci.parameters.uniqueName], }, options: automation_client_1.QueryNoCacheOptions, }); if (!goalResult || !goalResult.SdmGoal[0]) { yield ci.context.messageClient.respond(sdm_1.slackErrorMessage(`Update Goal State`, "Provided goal does not exist", ci.context)); return automation_client_1.Success; } const goal = _.cloneDeep(goalResult.SdmGoal[0]); const actx = ci.context; const prov = { name: actx.context.operation, registration: actx.context.name, version: actx.context.version, correlationId: actx.context.correlationId, ts: Date.now(), channelId: ci.parameters.channelId, userId: ci.parameters.slackRequester ? ci.parameters.slackRequester : ci.parameters.githubRequester, }; goal.provenance.push(prov); // Don't set approval for restart updates if (ci.parameters.state === sdm_1.SdmGoalState.approved) { goal.approval = prov; goal.approvalRequired = false; } else if (ci.parameters.state === sdm_1.SdmGoalState.pre_approved) { goal.preApproval = prov; goal.preApprovalRequired = false; } goal.state = ci.parameters.state; goal.ts = Date.now(); goal.version = (goal.version || 0) + 1; delete goal.id; yield ci.context.messageClient.send(goal, automation_client_1.addressEvent("SdmGoal")); return ci.context.messageClient.respond(sdm_1.slackSuccessMessage("Set Goal State", `Successfully set state of ${slack_messages_1.italic(goal.name)} on ${slack_messages_1.codeLine(goal.sha.slice(0, 7))} of ${slack_messages_1.bold(`${goal.repo.owner}/${goal.repo.name}/${goal.branch}`)} to ${slack_messages_1.italic(ci.parameters.state)}`), { id: ci.parameters.msgId || automation_client_1.guid() }); }), }; } exports.updateGoalStateCommand = updateGoalStateCommand; //# sourceMappingURL=updateGoal.js.map