@atomist/sdm
Version:
Atomist Software Delivery Machine SDK
194 lines • 11.4 kB
JavaScript
;
/*
* Copyright © 2020 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);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.setGoalStateCommand = void 0;
const decorators_1 = require("@atomist/automation-client/lib/decorators");
const string_1 = require("@atomist/automation-client/lib/internal/util/string");
const GitHubRepoRef_1 = require("@atomist/automation-client/lib/operations/common/GitHubRepoRef");
const MessageClient_1 = require("@atomist/automation-client/lib/spi/message/MessageClient");
const slack_messages_1 = require("@atomist/slack-messages");
const _ = require("lodash");
const fetchGoalsOnCommit_1 = require("../../api-helper/goal/fetchGoalsOnCommit");
const storeGoals_1 = require("../../api-helper/goal/storeGoals");
const handlerRegistrations_1 = require("../../api-helper/machine/handlerRegistrations");
const messages_1 = require("../../api-helper/misc/slack/messages");
const GitHubRepoTargets_1 = require("../../api/command/target/GitHubRepoTargets");
const queryCommits_1 = require("../../core/util/graph/queryCommits");
const types_1 = require("../../typings/types");
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([
decorators_1.MappedParameter(decorators_1.MappedParameters.GitHubRepositoryProvider),
__metadata("design:type", String)
], SetGoalStateParameters.prototype, "providerId", void 0);
__decorate([
decorators_1.Parameter({ required: false }),
__metadata("design:type", String)
], SetGoalStateParameters.prototype, "goal", void 0);
__decorate([
decorators_1.Parameter({ required: false }),
__metadata("design:type", String)
], SetGoalStateParameters.prototype, "state", void 0);
__decorate([
decorators_1.Parameter({ required: false }),
__metadata("design:type", String)
], SetGoalStateParameters.prototype, "msgId", void 0);
__decorate([
decorators_1.Parameter({ required: false, type: "boolean" }),
__metadata("design:type", Boolean)
], SetGoalStateParameters.prototype, "cancel", void 0);
SetGoalStateParameters = __decorate([
decorators_1.Parameters()
], SetGoalStateParameters);
function setGoalStateCommand(sdm, repoTargets = GitHubRepoTargets_1.GitHubRepoTargets) {
return {
name: "SetGoalState",
description: "Set state of a particular goal",
intent: [`set goal state ${sdm.configuration.name.replace("@", "")}`],
paramsMaker: handlerRegistrations_1.toRepoTargetingParametersMaker(SetGoalStateParameters, repoTargets),
listener: async (chi) => {
if (!chi.parameters.msgId) {
chi.parameters.msgId = string_1.guid();
}
let repoData;
try {
repoData = await 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(messages_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(messages_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 = GitHubRepoRef_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(messages_1.slackSuccessMessage("Set Goal State", "Successfully canceled setting goal state"), { id: chi.parameters.msgId });
}
else if (!chi.parameters.goal) {
const goals = await fetchGoalsOnCommit_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: [
MessageClient_1.menuForCommand({
text: "Goals",
options: optionsGroups,
}, "SetGoalState", "goal", newParams),
MessageClient_1.buttonForCommand({ text: "Cancel" }, "SetGoalState", Object.assign(Object.assign({}, newParams), { cancel: true })),
],
fallback: "Select Goal",
footer: messages_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(types_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: [
MessageClient_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),
MessageClient_1.buttonForCommand({ text: "Cancel" }, "SetGoalState", Object.assign(Object.assign({}, newParams), { cancel: true })),
],
fallback: "Select Goal",
footer: messages_1.slackFooter(),
}],
};
return chi.context.messageClient.respond(msg, { id: chi.parameters.msgId });
}
else {
const goal = JSON.parse(chi.parameters.goal);
const goals = await fetchGoalsOnCommit_1.fetchGoalsForCommit(chi.context, id, chi.parameters.providerId);
const sdmGoal = goals.find(g => g.id === goal.id);
await storeGoals_1.updateGoal(chi.context, sdmGoal, {
state: chi.parameters.state,
description: sdmGoal.description,
});
return chi.context.messageClient.respond(messages_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