UNPKG

@atomist/automation-client

Version:
66 lines 2.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const appRoot = require("app-root-path"); const axios_1 = require("axios"); const stringify = require("json-stringify-safe"); const os = require("os"); const globals_1 = require("../../globals"); 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) { return sendEvent("started", teamId, event); } function stopping(teamId, event) { return sendEvent("stopping", teamId, event); } function sendEvent(state, teamId, event) { event.state = state; event.ts = Date.now(); logger_1.logger.debug("Sending application event:", stringify(event)); return axios_1.default.post(`${Url}/${teamId}`, event) .catch(err => { logger_1.logger.error(err); }); } /** * 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. * @param {string} workspaceId * @returns {Promise<any>} */ function registerApplicationEvents(workspaceId) { // 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: globals_1.automationClientInstance().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(() => { return stopping(workspaceId, event) .then(() => Promise.resolve(0)) .catch(() => Promise.resolve(1)); }); // trigger application started event return started(workspaceId, event); } exports.registerApplicationEvents = registerApplicationEvents; //# sourceMappingURL=applicationEvent.js.map