UNPKG

zcatalyst-cli

Version:

Command Line Tool for CATALYST

108 lines (98 loc) 2.95 kB
/* eslint-disable no-console */ /* eslint-disable @typescript-eslint/no-var-requires */ const PACKAGE_JSON = require('../package.json'); const HTTPS = require('https'); const VERSION = PACKAGE_JSON.version; const NPM_REGISTRY_LINK = process.env.NPM_REGISTRY_LINK; const RELEASE_NOTES_LINK = process.env.RELEASE_NOTES_LINK; const API_KEY = process.env.API_KEY; const MERGE_REQUEST_ID = process.env.CI_MERGE_REQUEST_IID; const INSTALL_COMMAND = `npm install -g zcatalyst-cli@${VERSION}`; const CHANNEL_NAMES = (process.env.CHANNEL_NAMES || '').split(','); const notify = async () => { let text = `Hey Team, we've released a new version of [ZCatalyst-CLI](${NPM_REGISTRY_LINK}) :fireworks: \n\n *Version:* \`v${VERSION}\``; if (MERGE_REQUEST_ID) { text = text + `\n* [Pipeline status](https://git.csez.zohocorpin.com/BaaS/ZCatalyst-CLI/-/merge_requests/${MERGE_REQUEST_ID}/pipelines)`; } const message = { text, bot: { name: 'Dx Tools Update', image: 'https://www.zoho.com/sites/zweb/images/producticon/catalyst.svg' }, card: { title: 'Announcement', thumbnail: 'https://public-catlab-development.zohostratus.in/megaphone.gif', theme: 'modern-inline' }, slides: [ { type: 'text', title: 'Installation Command', data: '```' + INSTALL_COMMAND + '```' }, { type: 'text', title: 'Release Notes', data: `${RELEASE_NOTES_LINK}` } ] }; if (!CHANNEL_NAMES || !Array.isArray(CHANNEL_NAMES) || CHANNEL_NAMES.length === 0) { console.error('Invalid channels: ' + process.env.CHANNEL_NAMES); process.exit(1); } await Promise.all( CHANNEL_NAMES.map((channel) => { return new Promise((resolve, reject) => { try { const reqOpts = { hostname: 'cliq.zoho.com', path: `/company/64396901/api/v2/channelsbyname/${channel}/message?zapikey=${API_KEY}`, qs: {}, method: 'POST', headers: { 'content-type': 'application/json' } }; const req = HTTPS.request(reqOpts, async (res) => { if (res.statusCode === 204) { console.log('Notification successful: ' + channel); return resolve(); } const data = []; res.on('data', (chunk) => { data.push(chunk); }); res.on('end', () => { const resStr = Buffer.concat(data).toString(); reject( new Error( `API ERROR ::: ${req.method} ::: https://${req.host}${req.path} ::: ${res.statusCode} ::: ${resStr}` ) ); }); }); req.on('error', reject); req.write(JSON.stringify(message)); req.end(); } catch (er) { if (er instanceof Error) { return reject(er); } const error = new Error(); error.cause = er; return reject(error); } }); }) ).catch((er) => { console.error('Error sending notifications: ', er); process.exit(1); }); console.log('All Notifications sent successfully'); process.exit(0); }; notify();