diff --git a/.changeset/tame-mails-own.md b/.changeset/tame-mails-own.md new file mode 100644 index 00000000..005c8bb4 --- /dev/null +++ b/.changeset/tame-mails-own.md @@ -0,0 +1,5 @@ +--- +"@pantheon-systems/pcc-cli": patch +--- + +Added support for setting preferred webhook events. Webhook notifications will only be sent on events matching preferred events diff --git a/packages/cli/src/cli/commands/sites/webhooks.ts b/packages/cli/src/cli/commands/sites/webhooks.ts new file mode 100644 index 00000000..e3f5a1a2 --- /dev/null +++ b/packages/cli/src/cli/commands/sites/webhooks.ts @@ -0,0 +1,38 @@ +import inquirer from "inquirer"; +import ora from "ora"; +import AddOnApiHelper from "../../../lib/addonApiHelper"; +import { errorHandler } from "../../exceptions"; + +async function configurePreferredWebhookEvents(siteId: string) { + // Fetch available events + const availableEventsSpinner = ora("Fetching available events...").start(); + const availableEvents = + await AddOnApiHelper.fetchAvailableWebhookEvents(siteId); + availableEventsSpinner.succeed("Fetched available events"); + + // Prompt user to select events + const { selectedEvents } = await inquirer.prompt([ + { + type: "checkbox", + name: "selectedEvents", + message: + "Select events to receive notifications for. Select none to receive notifications for all events.", + choices: availableEvents, + }, + ]); + + if (selectedEvents.length === 0) { + console.info( + "No events selected. Your webhook will receive notifications for all events.", + ); + } + + // Update events for the site + const updateEventsSpinner = ora("Updating preferred events...").start(); + await AddOnApiHelper.updateSiteConfig(siteId, { + preferredEvents: selectedEvents, + }); + updateEventsSpinner.succeed("Updated preferred events"); +} + +export default errorHandler(configurePreferredWebhookEvents); diff --git a/packages/cli/src/cli/exceptions.ts b/packages/cli/src/cli/exceptions.ts index 4a82ce08..1a295934 100644 --- a/packages/cli/src/cli/exceptions.ts +++ b/packages/cli/src/cli/exceptions.ts @@ -32,8 +32,8 @@ export function errorHandler( if (cleanup) cleanup(arg); if (e instanceof UserNotLoggedIn) { - console.log(chalk.red("Error: User is not logged in.")); - console.log(chalk.yellow('Please run "pcc login" to login.')); + console.log(chalk.red("\nError: User is not logged in.")); + console.log(chalk.yellow('\nPlease run "pcc login" to login.')); } else { if ( axios.isAxiosError(e) && @@ -41,7 +41,7 @@ export function errorHandler( e.response?.data?.message ) { // Operational error - console.log(chalk.red(`Error: ${e.response.data.message}`)); + console.log(chalk.red(`\nError: ${e.response.data.message}`)); } else { // Unhandled error console.log( @@ -49,7 +49,7 @@ export function errorHandler( ); console.log( chalk.red( - "Error: Something went wrong. Please contact Pantheon support team.", + "\nError: Something went wrong. Please contact Pantheon support team.", ), ); } diff --git a/packages/cli/src/cli/index.ts b/packages/cli/src/cli/index.ts index e5b5a416..dfaab14b 100755 --- a/packages/cli/src/cli/index.ts +++ b/packages/cli/src/cli/index.ts @@ -34,6 +34,7 @@ import { SITE_EXAMPLES, updateSiteConfig, } from "./commands/sites/site"; +import configurePreferredWebhookEvents from "./commands/sites/webhooks"; import { createToken, listTokens, @@ -552,6 +553,19 @@ yargs(hideBin(process.argv)) id: args.id as string, limit: args.limit as number, }), + ) + .command( + "preferred-events ", + "Set preferred webhook events for a given site. Your webhook will only receive notifications for events that you specify.", + (yargs) => { + yargs.strictCommands().positional("", { + describe: "ID of the site for which you want to configure.", + demandOption: true, + type: "string", + }); + }, + async (args) => + configurePreferredWebhookEvents(String(args.id)), ); }, ) diff --git a/packages/cli/src/lib/addonApiHelper.ts b/packages/cli/src/lib/addonApiHelper.ts index 65e60d62..eb839db1 100644 --- a/packages/cli/src/lib/addonApiHelper.ts +++ b/packages/cli/src/lib/addonApiHelper.ts @@ -430,15 +430,17 @@ class AddOnApiHelper { url, webhookUrl, webhookSecret, + preferredEvents, }: { url?: string; webhookUrl?: string; webhookSecret?: string; + preferredEvents?: string[]; }, ): Promise { const idToken = await this.getIdToken(); - const configuredWebhook = webhookUrl || webhookSecret; + const configuredWebhook = webhookUrl || webhookSecret || preferredEvents; await axios.patch( `${(await getApiConfig()).SITE_ENDPOINT}/${id}`, @@ -448,6 +450,7 @@ class AddOnApiHelper { webhookConfig: { ...(webhookUrl && { webhookUrl: webhookUrl }), ...(webhookSecret && { webhookSecret: webhookSecret }), + ...(preferredEvents && { preferredEvents }), }, }), }, @@ -486,6 +489,21 @@ class AddOnApiHelper { return resp.data as WebhookDeliveryLog[]; } + + static async fetchAvailableWebhookEvents(siteId: string) { + const idToken = await this.getIdToken(); + + const resp = await axios.get( + `${(await getApiConfig()).SITE_ENDPOINT}/${siteId}/availableWebhookEvents`, + { + headers: { + Authorization: `Bearer ${idToken}`, + }, + }, + ); + + return resp.data as string[]; + } } export default AddOnApiHelper;