@atomist/rugs
Version:
Helper functions for Rugs
89 lines (88 loc) • 4.03 kB
JavaScript
;
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 });
var Decorators_1 = require("@atomist/rug/operations/Decorators");
var RugMessages_1 = require("@atomist/slack-messages/RugMessages");
/*
* Copyright © 2017 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 Handlers_1 = require("@atomist/rug/operations/Handlers");
var GenericErrorHandler = (function () {
function GenericErrorHandler() {
}
GenericErrorHandler.prototype.handle = function (response) {
var body = response.body != null ? "(" + response.body + ")" : "";
var msg = this.msg === undefined ? "" : this.msg;
return new Handlers_1.CommandPlan().add(RugMessages_1.renderError("" + msg + response.msg + body, this.corrid));
};
return GenericErrorHandler;
}());
__decorate([
Decorators_1.Parameter({ description: "Error prefix", pattern: "@any", required: false }),
__metadata("design:type", String)
], GenericErrorHandler.prototype, "msg", void 0);
__decorate([
Decorators_1.Parameter({ description: "Correlation ID", pattern: "@any", required: false }),
__metadata("design:type", String)
], GenericErrorHandler.prototype, "corrid", void 0);
GenericErrorHandler = __decorate([
Decorators_1.ResponseHandler("GenericErrorHandler", "Displays an error in chat"),
Decorators_1.Tags("errors")
], GenericErrorHandler);
exports.GenericErrorHandler = GenericErrorHandler;
function handleErrors(res, params) {
res.onError = { kind: "respond", name: "GenericErrorHandler", parameters: params };
return res;
}
exports.handleErrors = handleErrors;
var GenericSuccessHandler = (function () {
function GenericSuccessHandler() {
}
GenericSuccessHandler.prototype.handle = function (response) {
// TODO - render the body?
return new Handlers_1.CommandPlan().add(RugMessages_1.renderSuccess("" + this.msg));
};
return GenericSuccessHandler;
}());
__decorate([
Decorators_1.Parameter({ description: "Success msg", pattern: "@any" }),
__metadata("design:type", String)
], GenericSuccessHandler.prototype, "msg", void 0);
GenericSuccessHandler = __decorate([
Decorators_1.ResponseHandler("GenericSuccessHandler", "Displays a success message in chat"),
Decorators_1.Tags("success")
], GenericSuccessHandler);
exports.GenericSuccessHandler = GenericSuccessHandler;
function handleSuccess(res, msg) {
res.onSuccess = { kind: "respond", name: "GenericSuccessHandler", parameters: { msg: msg } };
return res;
}
exports.handleSuccess = handleSuccess;
/**
* Wrap with error and/or success handlers.
*/
function wrap(res, success, params) {
var withErrors = handleErrors(res, params);
return handleSuccess(withErrors, success);
}
exports.wrap = wrap;