Skip to content

Preview branch alerts #2136

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

Merged
merged 2 commits into from
Jun 2, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ const FormSchema = z
.min(1)
.or(z.enum(["TASK_RUN", "DEPLOYMENT_FAILURE", "DEPLOYMENT_SUCCESS"])),
environmentTypes: z
.array(z.enum(["STAGING", "PRODUCTION"]))
.array(z.enum(["STAGING", "PRODUCTION", "PREVIEW"]))
.min(1)
.or(z.enum(["STAGING", "PRODUCTION"])),
.or(z.enum(["STAGING", "PRODUCTION", "PREVIEW"])),
type: z.enum(["WEBHOOK", "SLACK", "EMAIL"]).default("EMAIL"),
channelValue: z.string().nonempty(),
integrationId: z.string().optional(),
Expand Down Expand Up @@ -441,7 +441,7 @@ export default function Page() {
<InputGroup>
<Label>Environment</Label>
<input type="hidden" name={environmentTypes.name} value={environment.type} />
<EnvironmentCombo environment={environment} />
<EnvironmentCombo environment={{ type: environment.type }} />
<FormError id={environmentTypes.errorId}>{environmentTypes.error}</FormError>
</InputGroup>
<FormError>{form.error}</FormError>
Expand Down
39 changes: 32 additions & 7 deletions apps/webapp/app/v3/services/alerts/deliverAlert.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { type ProjectAlertChannelType, type ProjectAlertType } from "@trigger.de
import { alertsRateLimiter } from "~/v3/alertsRateLimiter.server";
import { v3RunPath } from "~/utils/pathBuilder";
import { ApiRetrieveRunPresenter } from "~/presenters/v3/ApiRetrieveRunPresenter.server";
import { environmentTitle } from "~/components/environments/EnvironmentLabel";

type FoundAlert = Prisma.Result<
typeof prisma.projectAlert,
Expand All @@ -56,6 +57,12 @@ type FoundAlert = Prisma.Result<
include: {
lockedBy: true;
lockedToVersion: true;
runtimeEnvironment: {
select: {
type: true;
branchName: true;
};
};
};
};
workerDeployment: {
Expand All @@ -65,6 +72,12 @@ type FoundAlert = Prisma.Result<
tasks: true;
};
};
environment: {
select: {
type: true;
branchName: true;
};
};
};
};
};
Expand All @@ -90,6 +103,12 @@ export class DeliverAlertService extends BaseService {
include: {
lockedBy: true,
lockedToVersion: true,
runtimeEnvironment: {
select: {
type: true,
branchName: true,
},
},
},
},
workerDeployment: {
Expand All @@ -99,6 +118,12 @@ export class DeliverAlertService extends BaseService {
tasks: true,
},
},
environment: {
select: {
type: true,
branchName: true,
},
},
},
},
},
Expand Down Expand Up @@ -177,10 +202,9 @@ export class DeliverAlertService extends BaseService {
runId: alert.taskRun.friendlyId,
taskIdentifier: alert.taskRun.taskIdentifier,
fileName: alert.taskRun.lockedBy?.filePath ?? "Unknown",
exportName: alert.taskRun.lockedBy?.exportName ?? "Unknown",
version: alert.taskRun.lockedToVersion?.version ?? "Unknown",
project: alert.project.name,
environment: alert.environment.slug,
environment: environmentTitle(alert.taskRun.runtimeEnvironment),
error: createJsonErrorObject(taskRunError),
runLink: `${env.APP_ORIGIN}/projects/v3/${alert.project.externalRef}/runs/${alert.taskRun.friendlyId}`,
organization: alert.project.organization.title,
Expand Down Expand Up @@ -211,7 +235,7 @@ export class DeliverAlertService extends BaseService {
email: "alert-deployment-failure",
to: emailProperties.data.email,
version: alert.workerDeployment.version,
environment: alert.environment.slug,
environment: environmentTitle(alert.workerDeployment.environment),
shortCode: alert.workerDeployment.shortCode,
failedAt: alert.workerDeployment.failedAt ?? new Date(),
error: preparedError,
Expand All @@ -232,7 +256,7 @@ export class DeliverAlertService extends BaseService {
email: "alert-deployment-success",
to: emailProperties.data.email,
version: alert.workerDeployment.version,
environment: alert.environment.slug,
environment: environmentTitle(alert.workerDeployment.environment),
shortCode: alert.workerDeployment.shortCode,
deployedAt: alert.workerDeployment.deployedAt ?? new Date(),
deploymentLink: `${env.APP_ORIGIN}/projects/v3/${alert.project.externalRef}/deployments/${alert.workerDeployment.shortCode}`,
Expand Down Expand Up @@ -292,6 +316,7 @@ export class DeliverAlertService extends BaseService {
id: alert.environment.id,
type: alert.environment.type,
slug: alert.environment.slug,
branchName: alert.environment.branchName ?? undefined,
},
organization: {
id: alert.project.organizationId,
Expand Down Expand Up @@ -349,6 +374,7 @@ export class DeliverAlertService extends BaseService {
id: alert.environment.id,
type: alert.environment.type,
slug: alert.environment.slug,
branchName: alert.environment.branchName ?? undefined,
},
organization: {
id: alert.project.organizationId,
Expand Down Expand Up @@ -648,9 +674,8 @@ export class DeliverAlertService extends BaseService {
const taskRunError = this.#getRunError(alert);
const error = createJsonErrorObject(taskRunError);

const exportName = alert.taskRun.lockedBy?.exportName ?? "Unknown";
const version = alert.taskRun.lockedToVersion?.version ?? "Unknown";
const environment = alert.environment.slug;
const environment = environmentTitle(alert.taskRun.runtimeEnvironment);
const taskIdentifier = alert.taskRun.taskIdentifier;
const timestamp = alert.taskRun.completedAt ?? new Date();
const runId = alert.taskRun.friendlyId;
Expand All @@ -664,7 +689,7 @@ export class DeliverAlertService extends BaseService {
type: "section",
text: {
type: "mrkdwn",
text: `:rotating_light: Error in *${exportName}* _<!date^${Math.round(
text: `:rotating_light: Error in *${taskIdentifier}* _<!date^${Math.round(
timestamp.getTime() / 1000
)}^at {date_num} {time_secs}|${timestamp.toLocaleString()}>_`,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ export class PerformTaskRunAlertsService extends BaseService {
where: { id: runId },
include: {
lockedBy: true,
runtimeEnvironment: true,
runtimeEnvironment: {
include: {
parentEnvironment: true,
},
},
},
});

Expand All @@ -32,7 +36,7 @@ export class PerformTaskRunAlertsService extends BaseService {
has: "TASK_RUN",
},
environmentTypes: {
has: run.runtimeEnvironment.type,
has: run.runtimeEnvironment.parentEnvironment?.type ?? run.runtimeEnvironment.type,
},
enabled: true,
},
Expand Down
4 changes: 0 additions & 4 deletions internal-packages/emails/emails/alert-run-failure.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export const AlertRunEmailSchema = z.object({
project: z.string(),
taskIdentifier: z.string(),
fileName: z.string(),
exportName: z.string(),
version: z.string(),
environment: z.string(),
error: z.object({
Expand All @@ -41,7 +40,6 @@ const previewDefaults: AlertRunEmailProps = {
project: "my-project",
taskIdentifier: "my-task",
fileName: "other.ts",
exportName: "myTask",
version: "20240101.1",
environment: "prod",
error: {
Expand All @@ -59,7 +57,6 @@ export default function Email(props: AlertRunEmailProps) {
project,
taskIdentifier,
fileName,
exportName,
version,
environment,
error,
Expand All @@ -81,7 +78,6 @@ export default function Email(props: AlertRunEmailProps) {
<Text style={paragraphTight}>Project: {project}</Text>
<Text style={paragraphTight}>Task ID: {taskIdentifier}</Text>
<Text style={paragraphTight}>Filename: {fileName}</Text>
<Text style={paragraphTight}>Function: {exportName}()</Text>
<Text style={paragraphTight}>Version: {version}</Text>
<Text style={paragraphTight}>Environment: {environment}</Text>

Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/v3/schemas/webhooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ const AlertWebhookRunFailedObject = z.object({
type: RuntimeEnvironmentTypeSchema,
/** Environment slug */
slug: z.string(),
/** Environment branch name */
branchName: z.string().optional(),
}),
/** Organization information */
organization: z.object({
Expand Down Expand Up @@ -99,6 +101,8 @@ const deploymentCommonProperties = {
id: z.string(),
type: RuntimeEnvironmentTypeSchema,
slug: z.string(),
/** Environment branch name */
branchName: z.string().optional(),
}),
/** Organization information */
organization: z.object({
Expand Down