UNPKG

@atomist/sdm-core

Version:

Atomist Software Delivery Machine - Implementation

194 lines 11.4 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 sdm_1 = require("@atomist/sdm"); const slack_messages_1 = require("@atomist/slack-messages"); const _ = require("lodash"); const queryCommits_1 = require("../../util/graph/queryCommits"); var SdmGoalStateOrder; (function (SdmGoalStateOrder) { SdmGoalStateOrder[SdmGoalStateOrder["planned"] = 0] = "planned"; SdmGoalStateOrder[SdmGoalStateOrder["requested"] = 1] = "requested"; SdmGoalStateOrder[SdmGoalStateOrder["waiting_for_pre_approval"] = 2] = "waiting_for_pre_approval"; SdmGoalStateOrder[SdmGoalStateOrder["pre_approved"] = 3] = "pre_approved"; SdmGoalStateOrder[SdmGoalStateOrder["in_process"] = 4] = "in_process"; SdmGoalStateOrder[SdmGoalStateOrder["waiting_for_approval"] = 5] = "waiting_for_approval"; SdmGoalStateOrder[SdmGoalStateOrder["approved"] = 6] = "approved"; SdmGoalStateOrder[SdmGoalStateOrder["success"] = 7] = "success"; SdmGoalStateOrder[SdmGoalStateOrder["failure"] = 8] = "failure"; SdmGoalStateOrder[SdmGoalStateOrder["stopped"] = 9] = "stopped"; SdmGoalStateOrder[SdmGoalStateOrder["skipped"] = 10] = "skipped"; SdmGoalStateOrder[SdmGoalStateOrder["canceled"] = 11] = "canceled"; })(SdmGoalStateOrder || (SdmGoalStateOrder = {})); let SetGoalStateParameters = class SetGoalStateParameters { }; __decorate([ automation_client_1.MappedParameter(automation_client_1.MappedParameters.GitHubRepositoryProvider), __metadata("design:type", String) ], SetGoalStateParameters.prototype, "providerId", void 0); __decorate([ automation_client_1.Parameter({ required: false }), __metadata("design:type", String) ], SetGoalStateParameters.prototype, "goal", void 0); __decorate([ automation_client_1.Parameter({ required: false }), __metadata("design:type", String) ], SetGoalStateParameters.prototype, "state", void 0); __decorate([ automation_client_1.Parameter({ required: false }), __metadata("design:type", String) ], SetGoalStateParameters.prototype, "msgId", void 0); __decorate([ automation_client_1.Parameter({ required: false, type: "boolean" }), __metadata("design:type", Boolean) ], SetGoalStateParameters.prototype, "cancel", void 0); SetGoalStateParameters = __decorate([ automation_client_1.Parameters() ], SetGoalStateParameters); function setGoalStateCommand(sdm, repoTargets = sdm_1.GitHubRepoTargets) { return { name: "SetGoalState", description: "Set state of a particular goal", intent: [`set goal state ${sdm.configuration.name.replace("@", "")}`], paramsMaker: sdm_1.toRepoTargetingParametersMaker(SetGoalStateParameters, repoTargets), listener: (chi) => __awaiter(this, void 0, void 0, function* () { if (!chi.parameters.msgId) { chi.parameters.msgId = automation_client_1.guid(); } let repoData; try { repoData = yield queryCommits_1.fetchBranchTips(chi.context, { providerId: chi.parameters.providerId, owner: chi.parameters.targets.repoRef.owner, repo: chi.parameters.targets.repoRef.repo, }); } catch (e) { return chi.context.messageClient.respond(sdm_1.slackWarningMessage("Set Goal State", `Repository ${slack_messages_1.bold(`${chi.parameters.targets.repoRef.owner}/${chi.parameters.targets.repoRef.repo}`)} not found`, chi.context), { id: chi.parameters.msgId }); } const branch = chi.parameters.targets.repoRef.branch || repoData.defaultBranch; let sha; try { sha = chi.parameters.targets.repoRef.sha || queryCommits_1.tipOfBranch(repoData, branch); } catch (e) { return chi.context.messageClient.respond(sdm_1.slackWarningMessage("Set Goal State", `Branch ${slack_messages_1.bold(branch)} not found on ${slack_messages_1.bold(`${chi.parameters.targets.repoRef.owner}/${chi.parameters.targets.repoRef.repo}`)}`, chi.context), { id: chi.parameters.msgId }); } const id = automation_client_1.GitHubRepoRef.from({ owner: chi.parameters.targets.repoRef.owner, repo: chi.parameters.targets.repoRef.repo, sha, branch, }); const newParams = Object.assign(Object.assign({}, chi.parameters), { targets: { owner: id.owner, repo: id.repo, branch: id.branch, sha: id.sha, } }); if (chi.parameters.cancel) { return chi.context.messageClient.respond(sdm_1.slackSuccessMessage("Set Goal State", "Successfully canceled setting goal state"), { id: chi.parameters.msgId }); } else if (!chi.parameters.goal) { const goals = yield sdm_1.fetchGoalsForCommit(chi.context, id, chi.parameters.providerId); const goalSets = _.groupBy(goals, "goalSetId"); const optionsGroups = _.map(goalSets, (v, k) => { return { text: k.slice(0, 7), options: v.map(g => ({ text: `${g.name} - ${g.state}`, value: JSON.stringify({ id: g.id, name: g.name, state: g.state }), })).sort((o1, o2) => o1.text.localeCompare(o2.text)), }; }); const msg = { attachments: [{ title: "Select Goal", text: `Please select one of the following goals on ${slack_messages_1.codeLine(sha.slice(0, 7))} of ${slack_messages_1.bold(`${id.owner}/${id.repo}/${branch}`)}:`, actions: [ automation_client_1.menuForCommand({ text: "Goals", options: optionsGroups, }, "SetGoalState", "goal", newParams), automation_client_1.buttonForCommand({ text: "Cancel" }, "SetGoalState", Object.assign(Object.assign({}, newParams), { cancel: true })), ], fallback: "Select Goal", footer: sdm_1.slackFooter(), }], }; return chi.context.messageClient.respond(msg, { id: chi.parameters.msgId }); } else if (!chi.parameters.state) { const goal = JSON.parse(chi.parameters.goal); const states = _.map(sdm_1.SdmGoalState, v => ({ text: v, value: v })) .sort((s1, s2) => { return SdmGoalStateOrder[s1.value] - SdmGoalStateOrder[s2.value]; }); const msg = { attachments: [{ title: "Select Goal State", text: `Please select the desired state of goal ${slack_messages_1.italic(goal.name)} on ${slack_messages_1.codeLine(sha.slice(0, 7))} of ${slack_messages_1.bold(`${id.owner}/${id.repo}/${branch}`)}:`, actions: [ automation_client_1.menuForCommand({ text: "Goal States", options: [ { text: "current state", options: states.filter(s => s.value === goal.state) }, { text: "available states", options: states.filter(s => s.value !== goal.state) }, ], }, "SetGoalState", "state", newParams), automation_client_1.buttonForCommand({ text: "Cancel" }, "SetGoalState", Object.assign(Object.assign({}, newParams), { cancel: true })), ], fallback: "Select Goal", footer: sdm_1.slackFooter(), }], }; return chi.context.messageClient.respond(msg, { id: chi.parameters.msgId }); } else { const goal = JSON.parse(chi.parameters.goal); const goals = yield sdm_1.fetchGoalsForCommit(chi.context, id, chi.parameters.providerId); const sdmGoal = goals.find(g => g.id === goal.id); yield sdm_1.updateGoal(chi.context, sdmGoal, { state: chi.parameters.state, description: sdmGoal.description, }); return chi.context.messageClient.respond(sdm_1.slackSuccessMessage("Set Goal State", `Successfully set state of ${slack_messages_1.italic(goal.name)} on ${slack_messages_1.codeLine(sha.slice(0, 7))} of ${slack_messages_1.bold(`${id.owner}/${id.repo}/${branch}`)} to ${slack_messages_1.italic(chi.parameters.state)}`), { id: chi.parameters.msgId }); } }), }; } exports.setGoalStateCommand = setGoalStateCommand; //# sourceMappingURL=setGoalState.js.map