All files / src/commands/webhooks webhooks.command.js

35.71% Statements 20/56
100% Branches 1/1
0% Functions 0/1
35.71% Lines 20/56

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x 193x                                                                         193x 193x  
import { getWebhooks } from '@clevercloud/client/esm/api/v2/notification.js';
import { defineCommand } from '../../lib/define-command.js';
import { styleText } from '../../lib/style-text.js';
import { Logger } from '../../logger.js';
import { getOwnerAndApp } from '../../models/notification.js';
import { sendToApi } from '../../models/send-to-api.js';
import { humanJsonOutputFormatOption, listAllNotificationsOption, orgaIdOrNameOption } from '../global.options.js';
 
export const webhooksCommand = defineCommand({
  description: 'Manage webhooks',
  since: '0.6.0',
  options: {
    org: orgaIdOrNameOption,
    listAll: listAllNotificationsOption,
    format: humanJsonOutputFormatOption,
  },
  args: [],
  async handler(options) {
    const { org, listAll, format } = options;

    const { ownerId, appId } = await getOwnerAndApp(org, org == null && !listAll);
    const hooks = await getWebhooks({ ownerId }).then(sendToApi);

    const formattedHooks = hooks
      .filter((hook) => {
        const emptyScope = !hook.scope || hook.scope.length === 0;
        return !appId || emptyScope || hook.scope.includes(appId);
      })
      .map((hook) => ({
        id: hook.id,
        name: hook.name,
        ownerId: hook.ownerId,
        services: hook.scope ?? [hook.ownerId],
        events: hook.events ?? ['ALL'],
        urls: hook.urls,
      }));

    switch (format) {
      case 'json': {
        Logger.printJson(formattedHooks);
        break;
      }
      case 'human':
      default: {
        formattedHooks.forEach((hook) => {
          Logger.println(hook.name ? styleText('bold', hook.name) : hook.id);
          Logger.println(`  id: ${hook.id}`);
          Logger.println(`  services: ${hook.services.join(', ')}`);
          Logger.println(`  events: ${hook.events.join(', ')}`);
          Logger.println('  hooks:');
          hook.urls.forEach((url) => Logger.println(`    ${url.url} (${url.format})`));
        });
      }
    }
  },
});