Skip to content
Open
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
32 changes: 27 additions & 5 deletions src/lib/server/models/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,33 @@ export namespace Build {
Aborted = 'ABORTED'
}

export enum Channel {
Unpublished = 'unpublished',
Alpha = 'alpha',
Beta = 'beta',
Production = 'production'
export const Channels = {
Unpublished: 'unpublished',
Alpha: 'alpha',
Beta: 'beta',
Production: 'production'
} as const;
export type Channel = (typeof Channels)[keyof typeof Channels];

const transitions = {
[Channels.Unpublished]: [Channels.Alpha, Channels.Beta, Channels.Production],
[Channels.Alpha]: [Channels.Alpha, Channels.Beta, Channels.Production],
[Channels.Beta]: [Channels.Beta, Channels.Production],
[Channels.Production]: [Channels.Production]
} as const as Readonly<Record<Channel, Channel[]>>;
Comment thread
FyreByrd marked this conversation as resolved.

type BuildForChannel = Prisma.buildGetPayload<{
select: { channel: true };
}>;

export function verifyChannel(target: Channel, build: BuildForChannel): boolean {
return (
// if unpublished OK
!build.channel ||
build.channel === Channels.Unpublished ||
// check if transition valid
transitions[build.channel as Channel]?.includes(target)
);
}

export enum Artifact {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ export const PUT: RequestHandler = async ({ request, params }) => {
const parsed = v.safeParse(releaseSchema, await request.json());
if (!parsed.success) return ErrorResponse(400, JSON.stringify(v.flatten(parsed.issues)));
const runningRelease = await prisma.release.findFirst({
where: { build_id: Number(params.buildId), status: { in: ['accepted', 'active'] } }
where: {
build_id: Number(params.buildId),
status: { in: [Release.Status.Initialized, Release.Status.Accepted, Release.Status.Active] }
}
Comment thread
FyreByrd marked this conversation as resolved.
});
if (runningRelease) {
return ErrorResponse(500, 'Release already in progress');
Expand All @@ -77,15 +80,23 @@ export const PUT: RequestHandler = async ({ request, params }) => {
id: true,
build: {
where: { id: Number(params.buildId) },
select: { id: true, version_code: true }
select: { id: true, channel: true, status: true, result: true }
}
}
});
if (!job) return ErrorResponse(404, 'Job not found');
const build = job.build.at(0);
if (!build) return ErrorResponse(404, 'Build not found');

// TODO verify channel
if (build.status !== Build.Status.Completed)
return ErrorResponse(409, `Build is incomplete. Current Status: ${build.status}`);

if (build.result !== Build.Result.Success)
return ErrorResponse(403, `Build was unsuccessful. Result: ${build.result}`);

if (!Build.verifyChannel(parsed.output.channel as Build.Channel, build))
return ErrorResponse(400, `Cannot promote from ${build.channel} to ${parsed.output.channel}`);

const release = await prisma.release.create({
data: {
...parsed.output,
Expand Down
Loading