@atomist/atomist-sdm
Version:
Atomist SDM to deliver our own projects
138 lines • 7.14 kB
JavaScript
;
/*
* 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) {
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) : new P(function (resolve) { resolve(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 types_1 = require("../typings/types");
let ApprovalParameters = class ApprovalParameters {
};
__decorate([
automation_client_1.Parameter({ displayable: false, required: true }),
__metadata("design:type", String)
], ApprovalParameters.prototype, "goalSetId", void 0);
__decorate([
automation_client_1.Parameter({ displayable: false, required: true }),
__metadata("design:type", String)
], ApprovalParameters.prototype, "goalUniqueName", void 0);
__decorate([
automation_client_1.Parameter({ displayable: false, required: true }),
__metadata("design:type", String)
], ApprovalParameters.prototype, "goalState", void 0);
__decorate([
automation_client_1.Parameter({ required: false }),
__metadata("design:type", String)
], ApprovalParameters.prototype, "msgId", void 0);
ApprovalParameters = __decorate([
automation_client_1.Parameters()
], ApprovalParameters);
exports.ApprovalCommand = {
name: "ApproveSdmGoalCommand",
intent: [],
paramsMaker: ApprovalParameters,
listener: (ci) => __awaiter(this, void 0, void 0, function* () {
const goal = (yield ci.context.graphClient.query({
name: "SdmGoal",
variables: {
goalSetId: [ci.parameters.goalSetId],
state: [ci.parameters.goalState],
uniqueName: [ci.parameters.goalUniqueName],
},
options: automation_client_1.QueryNoCacheOptions,
})).SdmGoal[0];
const updatedGoal = _.cloneDeep(goal);
updatedGoal.ts = Date.now();
updatedGoal.version = updatedGoal.version + 1;
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(),
};
updatedGoal.provenance.push(prov);
updatedGoal.data = JSON.stringify({ approved: true });
yield ci.context.messageClient.send(updatedGoal, automation_client_1.addressEvent(sdm_1.GoalRootType));
yield ci.context.messageClient.respond(sdm_1.slackSuccessMessage("Approve Goal", `Successfully approved goal ${slack_messages_1.italic(goal.url ? slack_messages_1.url(goal.url, goal.name) : 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}`)}`, {
footer: `${sdm_1.slackFooter()} \u00B7 ${goal.goalSet} \u00B7 ${goal.goalSetId.slice(0, 7)} \u00B7 ${slack_messages_1.channel(goal.approval.channelId)}`,
}), {
id: ci.parameters.msgId,
});
}),
};
exports.CancelApprovalCommand = {
name: "CancelApproveSdmGoalCommand",
intent: [],
paramsMaker: ApprovalParameters,
listener: (ci) => __awaiter(this, void 0, void 0, function* () {
const goal = (yield ci.context.graphClient.query({
name: "SdmGoal",
variables: {
goalSetId: [ci.parameters.goalSetId],
state: [ci.parameters.goalState],
uniqueName: [ci.parameters.goalUniqueName],
},
options: automation_client_1.QueryNoCacheOptions,
})).SdmGoal[0];
const updatedGoal = _.cloneDeep(goal);
updatedGoal.ts = Date.now();
updatedGoal.version = updatedGoal.version + 1;
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(),
};
updatedGoal.provenance.push(prov);
if (ci.parameters.goalState === types_1.SdmGoalState.approved) {
updatedGoal.state = types_1.SdmGoalState.waiting_for_approval;
updatedGoal.approval = undefined;
}
else if (ci.parameters.goalState === types_1.SdmGoalState.pre_approved) {
updatedGoal.state = types_1.SdmGoalState.waiting_for_pre_approval;
updatedGoal.preApproval = undefined;
}
yield ci.context.messageClient.send(updatedGoal, automation_client_1.addressEvent(sdm_1.GoalRootType));
yield ci.context.messageClient.respond(sdm_1.slackWarningMessage("Approve Goal", `Successfully canceled approval of goal ${slack_messages_1.italic(goal.url ? slack_messages_1.url(goal.url, goal.name) : 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}`)} \u00B7 ${slack_messages_1.channel(goal.approval.channelId)}`, ci.context, {
footer: `${sdm_1.slackFooter()} \u00B7 ${goal.goalSet} \u00B7 ${goal.goalSetId.slice(0, 7)}`,
}), {
id: ci.parameters.msgId,
});
}),
};
//# sourceMappingURL=approval.js.map