@atomist/automation-client
Version:
Atomist API for software low-level client
297 lines • 12.9 kB
JavaScript
;
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 slack_messages_1 = require("@atomist/slack-messages");
const _ = require("lodash");
const MessageClient_1 = require("../../../spi/message/MessageClient");
const MessageClientSupport_1 = require("../../../spi/message/MessageClientSupport");
const logger_1 = require("../../../util/logger");
const redact_1 = require("../../../util/redact");
const string_1 = require("../../util/string");
const RequestProcessor_1 = require("../RequestProcessor");
class AbstractMessageClient extends MessageClientSupport_1.MessageClientSupport {
constructor(request, correlationId, team, source, configuration) {
super();
this.request = request;
this.correlationId = correlationId;
this.team = team;
this.source = source;
this.configuration = configuration;
}
delete(destinations, options) {
return __awaiter(this, void 0, void 0, function* () {
return this.doSend(undefined, destinations, Object.assign(Object.assign({}, options), { delete: true }));
});
}
doSend(msg, destinations, options = {}) {
return __awaiter(this, void 0, void 0, function* () {
if (!!msg && msg.content_type === "application/x-atomist-continuation+json") {
return this.sendResponse(msg).then(() => msg);
}
const ts = this.ts(options);
if (!Array.isArray(destinations)) {
destinations = [destinations];
}
let destinationIdentifier;
const responseDestinations = [];
let thread_ts;
if (options.thread === true && !!this.source) {
thread_ts = _.get(this.source, "slack.message.ts");
}
else if (typeof options.thread === "string") {
thread_ts = options.thread;
}
destinations.forEach(d => {
if (d.userAgent === MessageClient_1.SlackDestination.SLACK_USER_AGENT) {
destinationIdentifier = "slack";
const sd = d;
string_1.toStringArray(sd.channels).filter(c => !!c).forEach(c => {
responseDestinations.push({
user_agent: MessageClient_1.SlackDestination.SLACK_USER_AGENT,
slack: {
team: {
id: sd.team,
},
channel: {
name: c,
},
thread_ts,
},
});
});
string_1.toStringArray(sd.users).filter(u => !!u).forEach(u => {
responseDestinations.push({
user_agent: MessageClient_1.SlackDestination.SLACK_USER_AGENT,
slack: {
team: {
id: sd.team,
},
user: {
name: u,
},
thread_ts,
},
});
});
}
else if (d.userAgent === MessageClient_1.CustomEventDestination.INGESTER_USER_AGENT) {
destinationIdentifier = "ingester";
responseDestinations.push({
user_agent: MessageClient_1.CustomEventDestination.INGESTER_USER_AGENT,
ingester: {
root_type: d.rootType,
},
});
}
else if (d.userAgent === MessageClient_1.WebDestination.WEB_USER_AGENT) {
destinationIdentifier = "web";
}
else if (d.userAgent === MessageClient_1.SourceDestination.SOURCE_USER_AGENT) {
destinationIdentifier = d.system;
responseDestinations.push(d.source);
}
});
if (responseDestinations.length === 0 && this.source) {
// TODO CD this is probably not always going to be valid
destinationIdentifier = "slack";
const responseDestination = _.cloneDeep(this.source);
if (responseDestination.slack) {
delete responseDestination.slack.user;
if (!!thread_ts) {
responseDestination.slack.thread_ts = thread_ts;
}
}
responseDestinations.push(responseDestination);
}
const response = {
api_version: "1",
correlation_id: this.correlationId,
team: this.team,
source: this.source ? this.source : undefined,
command: RequestProcessor_1.isCommandIncoming(this.request) ? this.request.command : undefined,
event: RequestProcessor_1.isEventIncoming(this.request) ? this.request.extensions.operationName : undefined,
destinations: responseDestinations,
id: options.id ? options.id : undefined,
timestamp: ts,
ttl: ts && options.ttl ? options.ttl : undefined,
post_mode: options.post === "update_only" ? "update_only" : (options.post === "always" ? "always" : "ttl"),
};
if (destinationIdentifier === "web") {
return Promise.resolve();
}
else if (destinationIdentifier === "slack") {
if (MessageClient_1.isSlackMessage(msg)) {
const msgClone = _.cloneDeep(msg);
const actions = mapActions(msgClone);
response.content_type = MessageClient_1.MessageMimeTypes.SLACK_JSON;
response.body = slack_messages_1.render(msgClone, false);
response.actions = actions;
}
else if (MessageClient_1.isFileMessage(msg)) {
response.content_type = MessageClient_1.MessageMimeTypes.SLACK_FILE_JSON;
response.body = JSON.stringify({
content: msg.content,
filename: msg.fileName,
filetype: msg.fileType,
title: msg.title,
initial_comment: msg.comment,
});
}
else if (typeof msg === "string") {
response.content_type = MessageClient_1.MessageMimeTypes.PLAIN_TEXT;
response.body = msg;
}
else if (!!options.delete) {
response.content_type = "application/x-atomist-delete";
response.body === undefined;
}
if (_.get(this.configuration, "redact.messages", true) === true) {
response.body = redact_1.redact(response.body);
}
}
else if (destinationIdentifier === "ingester") {
response.content_type = MessageClient_1.MessageMimeTypes.APPLICATION_JSON;
response.body = JSON.stringify(msg);
response.id = (options.id ? options.id : string_1.guid());
}
return this.sendResponse(response).then(() => response);
});
}
ts(options) {
if (options.id) {
if (options.ts) {
return options.ts;
}
else {
return Date.now();
}
}
else {
return undefined;
}
}
}
exports.AbstractMessageClient = AbstractMessageClient;
class AbstractWebSocketMessageClient extends AbstractMessageClient {
constructor(ws, request, correlationId, team, source, configuration) {
super(request, correlationId, team, source, configuration);
this.ws = ws;
this.request = request;
this.correlationId = correlationId;
this.team = team;
this.source = source;
this.configuration = configuration;
}
sendResponse(response) {
return __awaiter(this, void 0, void 0, function* () {
this.ws.send(response);
});
}
}
exports.AbstractWebSocketMessageClient = AbstractWebSocketMessageClient;
class WebSocketCommandMessageClient extends AbstractWebSocketMessageClient {
constructor(request, ws, configuration) {
super(ws, request, request.correlation_id, request.team, request.source, configuration);
}
doSend(msg, destinations, options = {}) {
const _super = Object.create(null, {
doSend: { get: () => super.doSend }
});
return __awaiter(this, void 0, void 0, function* () {
return _super.doSend.call(this, msg, destinations, options);
});
}
}
exports.WebSocketCommandMessageClient = WebSocketCommandMessageClient;
class WebSocketEventMessageClient extends AbstractWebSocketMessageClient {
constructor(request, ws, configuration) {
super(ws, request, request.extensions.correlation_id, { id: request.extensions.team_id, name: request.extensions.team_name }, null, configuration);
}
doSend(msg, destinations, options = {}) {
const _super = Object.create(null, {
doSend: { get: () => super.doSend }
});
return __awaiter(this, void 0, void 0, function* () {
if (!Array.isArray(destinations)) {
destinations = [destinations];
}
if (destinations.length === 0) {
throw new Error("Response messages are not supported for event handlers");
}
else {
return _super.doSend.call(this, msg, destinations, options);
}
});
}
}
exports.WebSocketEventMessageClient = WebSocketEventMessageClient;
function mapActions(msg) {
const actions = [];
let counter = 0;
if (msg.attachments) {
msg.attachments.filter(attachment => attachment.actions).forEach(attachment => {
attachment.actions.forEach(a => {
if (!!a && !!a.command) {
const cra = a;
const id = counter++;
cra.command.id = `${cra.command.id}-${id}`;
a.name = `${a.name}-${id}`;
const action = {
id: cra.command.id,
parameter_name: cra.command.parameterName,
command: cra.command.name,
parameters: mapParameters(cra.command.parameters),
};
actions.push(action);
// Lastly we need to delete our extension from the slack action
cra.command = undefined;
}
});
});
return actions;
}
}
exports.mapActions = mapActions;
function mapParameters(data) {
const parameters = [];
for (const key in data) {
if (data.hasOwnProperty(key)) {
const value = data[key];
if (value) {
parameters.push({
name: key,
value: value.toString(),
});
}
else {
// logger.debug(`Parameter value for '${key}' is null`);
}
}
}
return parameters;
}
function sendMessage(message, ws, log = true) {
if (log) {
logger_1.logger.debug(`Sending message '${JSON.stringify(message, string_1.replacer)}'`);
}
ws.send(JSON.stringify(message));
}
exports.sendMessage = sendMessage;
function clean(addresses) {
let na = string_1.toStringArray(addresses);
if (na) {
// Filter out any null addresses
na = na.filter(nad => nad !== null && nad.length > 0);
}
return na;
}
exports.clean = clean;
//# sourceMappingURL=WebSocketMessageClient.js.map