Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PCC-1210] Add support for setting preferred webhook events #279

Merged
merged 4 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tame-mails-own.md
Original file line number Diff line number Diff line change
@@ -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
38 changes: 38 additions & 0 deletions packages/cli/src/cli/commands/sites/webhooks.ts
Original file line number Diff line number Diff line change
@@ -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);
8 changes: 4 additions & 4 deletions packages/cli/src/cli/exceptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,24 @@ export function errorHandler<T>(
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) &&
(e.response?.status ?? 500) < 500 && // Treat internal server errors as unhandled errors
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(
chalk.yellow("\nStack trace:", (e as { stack: string }).stack),
);
console.log(
chalk.red(
"Error: Something went wrong. Please contact Pantheon support team.",
"\nError: Something went wrong. Please contact Pantheon support team.",
),
);
}
Expand Down
14 changes: 14 additions & 0 deletions packages/cli/src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
SITE_EXAMPLES,
updateSiteConfig,
} from "./commands/sites/site";
import configurePreferredWebhookEvents from "./commands/sites/webhooks";
import {
createToken,
listTokens,
Expand Down Expand Up @@ -552,6 +553,19 @@ yargs(hideBin(process.argv))
id: args.id as string,
limit: args.limit as number,
}),
)
.command(
"preferred-events <id>",
"Set preferred webhook events for a given site. Your webhook will only receive notifications for events that you specify.",
(yargs) => {
yargs.strictCommands().positional("<id>", {
describe: "ID of the site for which you want to configure.",
demandOption: true,
type: "string",
});
},
async (args) =>
configurePreferredWebhookEvents(String(args.id)),
);
},
)
Expand Down
20 changes: 19 additions & 1 deletion packages/cli/src/lib/addonApiHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,15 +430,17 @@ class AddOnApiHelper {
url,
webhookUrl,
webhookSecret,
preferredEvents,
}: {
url?: string;
webhookUrl?: string;
webhookSecret?: string;
preferredEvents?: string[];
},
): Promise<void> {
const idToken = await this.getIdToken();

const configuredWebhook = webhookUrl || webhookSecret;
const configuredWebhook = webhookUrl || webhookSecret || preferredEvents;

await axios.patch(
`${(await getApiConfig()).SITE_ENDPOINT}/${id}`,
Expand All @@ -448,6 +450,7 @@ class AddOnApiHelper {
webhookConfig: {
...(webhookUrl && { webhookUrl: webhookUrl }),
...(webhookSecret && { webhookSecret: webhookSecret }),
...(preferredEvents && { preferredEvents }),
},
}),
},
Expand Down Expand Up @@ -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}`,
},
Dismissed Show dismissed Hide dismissed
},
);

return resp.data as string[];
}
}

export default AddOnApiHelper;
Loading