@atomist/automation-client
Version:
Atomist API for software low-level client
84 lines • 3.73 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 appRoot = require("app-root-path");
const fs = require("fs");
const os = require("os");
const path = require("path");
const httpClient_1 = require("../../spi/http/httpClient");
const logger_1 = require("../../util/logger");
const shutdown_1 = require("../util/shutdown");
const string_1 = require("../util/string");
const Url = "https://webhook.atomist.com/atomist/application/teams";
function started(teamId, event, configuration) {
return sendEvent("started", teamId, event, configuration);
}
function stopping(teamId, event, configuration) {
return sendEvent("stopping", teamId, event, configuration);
}
function sendEvent(state, teamId, event, configuration) {
return __awaiter(this, void 0, void 0, function* () {
event.state = state;
event.ts = Date.now();
logger_1.logger.debug("Sending application event: %j", event);
try {
yield configuration.http.client.factory.create(Url).exchange(`${Url}/${teamId}`, {
method: httpClient_1.HttpMethod.Post,
body: event,
});
}
catch (e) {
logger_1.logger.error("Failed to send application event: %s", e.message);
logger_1.logger.debug(e);
}
});
}
/**
* Register the automation client to send application events to Atomist.
* This is useful to show starting and stopping automation clients as part of their general lifecycle in eg Slack.
*/
function registerApplicationEvents(workspaceId, configuration) {
return __awaiter(this, void 0, void 0, function* () {
const gitInfo = path.join(appRoot.path, "git-info.json");
if (!fs.existsSync(gitInfo)) {
return;
}
// tslint:disable-next-line:no-var-requires
const git = require(`${appRoot.path}/git-info.json`);
const sha = git.sha;
const branch = git.branch;
const repo = git.repository;
const env = process.env.VCAP_APPLICATION ? JSON.parse(process.env.VCAP_APPLICATION) : undefined;
const event = {
git: {
sha,
branch,
repo,
},
domain: configuration.environment,
pod: env ? env.instance_id : os.hostname(),
host: env ? env.instance_id : os.hostname(),
id: env ? env.instance_id : string_1.guid(),
};
if (env) {
event.data = JSON.stringify({
cloudfoundry: process.env.VCAP_APPLICATION,
});
}
// register shutdown hook
shutdown_1.registerShutdownHook(() => stopping(workspaceId, event, configuration)
.then(() => Promise.resolve(0), () => Promise.resolve(1)), 2000, "application stopping event");
// trigger application started event
yield started(workspaceId, event, configuration);
});
}
exports.registerApplicationEvents = registerApplicationEvents;
//# sourceMappingURL=applicationEvent.js.map