* Converts a PM2 process object to an object for WebhookServer * route. * * Example 1: * - input: { pm2_env: { env_hook: { name: 'api', type: 'bitbucket' } } } * - output: { name: 'api', type: 'bitbucket' } * Example 2: * - input: { pm2_env: { env_hook: { type: 'bi
(app)
| 81 | * @private |
| 82 | */ |
| 83 | static _parseProcess(app) { |
| 84 | // Check data |
| 85 | if (!app || !app.pm2_env) { |
| 86 | return null; |
| 87 | } |
| 88 | let config = app.pm2_env.env_hook; |
| 89 | if (!config) { |
| 90 | log(`No options found for "${app.name}" route`); |
| 91 | return null; |
| 92 | } |
| 93 | if (config === true) { |
| 94 | config = {}; |
| 95 | } |
| 96 | |
| 97 | // Config to WebhookServer route |
| 98 | let self = this; |
| 99 | let name = app.name || 'unknown'; |
| 100 | let cwd = config.cwd || app.pm_cwd || app.pm2_env.cwd || app.pm2_env.pm_cwd; |
| 101 | let commandOptions = Object.assign({}, { cwd }, config.commandOptions || {}); |
| 102 | let route = { |
| 103 | name, |
| 104 | type: config.type, |
| 105 | secret: config.secret, |
| 106 | method(payload) { |
| 107 | log(`Parsed payload: ${JSON.stringify(payload)}`); |
| 108 | try { |
| 109 | if (config.command) { |
| 110 | log(`Running command: ${config.command}`); |
| 111 | self._runCommand(config.command, commandOptions) |
| 112 | .catch(e => onError(name, e)); |
| 113 | } |
| 114 | } catch (e) { |
| 115 | onError(name, e); |
| 116 | } |
| 117 | } |
| 118 | }; |
| 119 | route = cleanObj(route); |
| 120 | |
| 121 | return route; |
| 122 | |
| 123 | function onError(routeName, e) { |
| 124 | let err = e.message || e; |
| 125 | log(`Error on "${name}" route: ${err}`, 2); |
| 126 | throw e; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Runs a line command. |
no test coverage detected