Skip to content

Commit

Permalink
Validation and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
bakerkretzmar committed Dec 12, 2024
1 parent 15f6c8b commit 54c9643
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 22 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,9 +233,9 @@ Example:
https://webhooks.example.com/2
```

#### `deployment-failure-email`
#### `deployment-failure-emails`

The `deployment-failure-email` input parameter allows you to define a custom email address that Forge will notify of all deployment failures.
The `deployment-failure-emails` input parameter allows you to define up to 3 custom email addresses that Forge will notify of all deployment failures. To notify multiple email addresses, enter them on separate lines.

Example:

Expand Down
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ inputs:
deployment-webhooks:
description: URLs to send webhooks to after Forge deployments. Enter multiple URLs on separate lines.
required: false
deployment-failure-email:
description: Email address to send a notification to if the Forge deployment fails.
deployment-failure-emails:
description: Email addresses to notify if the Forge deployment fails. Enter multiple emails on separate lines.
required: false
outputs:
site-url:
Expand Down
15 changes: 11 additions & 4 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export async function createPreview({
certificate,
name,
webhooks,
failureEmail,
failureEmails,
aliases,
isolated,
username,
Expand All @@ -25,7 +25,7 @@ export async function createPreview({
certificate?: { type: 'clone'; certificate: number } | { type: 'existing'; certificate: string; key: string } | false;
name?: string;
webhooks: string[];
failureEmail?: string;
failureEmails?: string[];
aliases: string[];
isolated: boolean;
username?: string;
Expand Down Expand Up @@ -119,8 +119,15 @@ export async function createPreview({
core.info('Setting up webhooks.');
await Promise.all(webhooks.map((url) => site.createWebhook(url)));

core.info('Setting up failure email.');
await Promise.all(failureEmail.map((email) => site.createFailureEmail(email)));
if (failureEmails?.length) {
core.info('Setting up deployment failure notifications.');
if (failureEmails.length > 3) {
core.warning(
`Only 3 emails can be notified of deployment failures, found ${failureEmails.length}. Only the first 3 will be used.`,
);
}
await site.setDeploymentFailureEmails(failureEmails.slice(0, 3));
}

core.info('Deploying site.');
await site.deploy();
Expand Down
28 changes: 16 additions & 12 deletions src/forge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ type WebhookPayload = {
url: string;
};

type FailureEmailPayload = {
id: number;
email: string;
}

export class ForgeError extends Error {
axiosError: AxiosError;
data?: unknown;
Expand Down Expand Up @@ -248,9 +243,8 @@ export class Forge {
.webhook;
}

static async createFailureEmail(server: number, site: number, email: string) {
return (await this.post<{ email: FailureEmailPayload }>(`servers/${server}/sites/${site}/deployment-failure-emails`, { email })).data
.email;
static async setDeploymentFailureEmails(server: number, site: number, emails: string[]) {
await this.post(`servers/${server}/sites/${site}/deployment-failure-emails`, { emails });
}

static token(token: string) {
Expand All @@ -274,7 +268,7 @@ export class Forge {
if (this.#debug > 0) {
console.log(`> ${config.method?.toUpperCase()} /${config.url}`);
if (this.#debug > 1 && config.data) {
console.log(JSON.stringify(config.data, null, 2));
console.log(config.data);
}
}
return config;
Expand All @@ -288,7 +282,7 @@ export class Forge {
}`,
);
if (this.#debug > 1 && response.data) {
console.log(JSON.stringify(response.data, null, 2));
console.log(response.data);
}
}
return response;
Expand All @@ -309,6 +303,16 @@ export class Forge {
const [, server, site] = error.response.config.url.match(/servers\/(\d+)\/sites\/(\d+)/);
detail = `A previously requested SSL certificate was not found. This may mean that automatically obtaining and installing a Let’s Encrypt certificate failed. Please review any error output in your Forge dashboard and then try again: https://forge.laravel.com/servers/${server}/sites/${site}.`;
}
if (this.#debug > 0) {
console.log(
`< ${error.response?.config.method.toUpperCase()} /${error.response?.config.url} ${error.response?.status} ${
error.response?.statusText
}`,
);
if ((this.#debug > 1 || error.response?.status === 422) && error.response?.data) {
console.log(error.response.data);
}
}
return Promise.reject(new ForgeError(error, detail));
},
);
Expand Down Expand Up @@ -458,8 +462,8 @@ export class Site {
await Forge.createWebhook(this.server_id, this.id, url);
}

async createFailureEmail(email: string) {
await Forge.createFailureEmail(this.server_id, this.id, email);
async setDeploymentFailureEmails(emails: string[]) {
await Forge.setDeploymentFailureEmails(this.server_id, this.id, emails);
}

// TODO figure out a way to safely+reliably figure the name out internally so it doesn't need to be passed in
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export async function run() {
const noCertificate = core.getBooleanInput('no-certificate', { required: false });

const webhooks = core.getMultilineInput('deployment-webhooks', { required: false });
const failureEmail = core.getInput('deployment-failure-email', { required: false });
const failureEmails = core.getMultilineInput('deployment-failure-emails', { required: false });

let certificate:
| { type: 'clone'; certificate: number }
Expand Down Expand Up @@ -108,7 +108,7 @@ export async function run() {
environment,
certificate,
webhooks,
failureEmail,
failureEmails,
aliases,
isolated,
username,
Expand Down

0 comments on commit 54c9643

Please sign in to comment.