Skip to content

Commit

Permalink
fix(cli,dashboard): promote deployments without existing production d…
Browse files Browse the repository at this point in the history
…eployments (#340)

* fix(cli,dashboard): promote deployments without existing production deployments

* chore: add changeset
  • Loading branch information
QuiiBz authored Dec 3, 2022
1 parent 2ee0221 commit 75eac5d
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .changeset/dry-mangos-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lagon/cli': patch
---

Fix some commands returning an error due to wrong parsing
5 changes: 5 additions & 0 deletions .changeset/sweet-kids-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lagon/dashboard': patch
---

Allow promoting deployments for functions without a production deployment
10 changes: 8 additions & 2 deletions packages/cli/src/commands/promote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::{anyhow, Result};
use dialoguer::Confirm;
use std::path::PathBuf;

use serde::Serialize;
use serde::{Deserialize, Serialize};

use crate::utils::{
get_function_config, info, print_progress, success, validate_code_file, Config, TrpcClient,
Expand All @@ -15,6 +15,12 @@ struct PromoteDeploymentRequest {
deployment_id: String,
}

#[derive(Deserialize, Debug)]
struct PromoteDeploymentResponse {
#[allow(dead_code)]
ok: bool,
}

pub async fn promote(file: PathBuf, deployment_id: String) -> Result<()> {
let config = Config::new()?;

Expand All @@ -37,7 +43,7 @@ pub async fn promote(file: PathBuf, deployment_id: String) -> Result<()> {
true => {
let end_progress = print_progress("Promoting Deployment...");
TrpcClient::new(&config)
.mutation::<PromoteDeploymentRequest, ()>(
.mutation::<PromoteDeploymentRequest, PromoteDeploymentResponse>(
"deploymentPromote",
PromoteDeploymentRequest {
function_id: function_config.function_id,
Expand Down
10 changes: 8 additions & 2 deletions packages/cli/src/commands/rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::{anyhow, Result};
use std::path::PathBuf;

use dialoguer::Confirm;
use serde::Serialize;
use serde::{Deserialize, Serialize};

use crate::utils::{
delete_function_config, get_function_config, info, print_progress, success, validate_code_file,
Expand All @@ -15,6 +15,12 @@ struct DeleteFunctionRequest {
function_id: String,
}

#[derive(Deserialize, Debug)]
struct DeleteFunctionResponse {
#[allow(dead_code)]
ok: bool,
}

pub async fn rm(file: PathBuf) -> Result<()> {
let config = Config::new()?;

Expand All @@ -37,7 +43,7 @@ pub async fn rm(file: PathBuf) -> Result<()> {
true => {
let end_progress = print_progress("Deleting Function...");
TrpcClient::new(&config)
.mutation::<DeleteFunctionRequest, ()>(
.mutation::<DeleteFunctionRequest, DeleteFunctionResponse>(
"functionDelete",
DeleteFunctionRequest {
function_id: function_config.function_id,
Expand Down
10 changes: 8 additions & 2 deletions packages/cli/src/commands/undeploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::{anyhow, Result};
use dialoguer::Confirm;
use std::path::PathBuf;

use serde::Serialize;
use serde::{Deserialize, Serialize};

use crate::utils::{
get_function_config, info, print_progress, success, validate_code_file, Config, TrpcClient,
Expand All @@ -15,6 +15,12 @@ struct DeleteDeploymentRequest {
deployment_id: String,
}

#[derive(Deserialize, Debug)]
struct DeleteDeploymentResponse {
#[allow(dead_code)]
ok: bool,
}

pub async fn undeploy(file: PathBuf, deployment_id: String) -> Result<()> {
let config = Config::new()?;

Expand All @@ -35,7 +41,7 @@ pub async fn undeploy(file: PathBuf, deployment_id: String) -> Result<()> {
true => {
let end_progress = print_progress("Deleting Deployment...");
TrpcClient::new(&config)
.mutation::<DeleteDeploymentRequest, ()>(
.mutation::<DeleteDeploymentRequest, DeleteDeploymentResponse>(
"deploymentDelete",
DeleteDeploymentRequest {
function_id: function_config.function_id,
Expand Down
20 changes: 10 additions & 10 deletions packages/cli/src/utils/trpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ impl<'a> TrpcClient<'a> {
Self { client, config }
}

pub async fn query<T: Serialize, R: DeserializeOwned>(
&self,
key: &str,
body: Option<T>,
) -> Result<TrpcResponse<R>> {
pub async fn query<T, R>(&self, key: &str, body: Option<T>) -> Result<TrpcResponse<R>>
where
T: Serialize,
R: DeserializeOwned,
{
let input = match body {
Some(body) => format!("?input={}", encode(&serde_json::to_string(&body)?)),
None => String::new(),
Expand Down Expand Up @@ -73,11 +73,11 @@ impl<'a> TrpcClient<'a> {
}
}

pub async fn mutation<T: Serialize, R: DeserializeOwned>(
&self,
key: &str,
body: T,
) -> Result<TrpcResponse<R>> {
pub async fn mutation<T, R>(&self, key: &str, body: T) -> Result<TrpcResponse<R>>
where
T: Serialize,
R: DeserializeOwned,
{
let body = serde_json::to_string(&body)?;

let request = Request::builder()
Expand Down
15 changes: 8 additions & 7 deletions packages/dashboard/lib/api/deployments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,12 @@ export async function removeDeployment(
);
}

export async function unpromoteProductionDeployment(functionId: string): Promise<{
id: string;
}> {
export async function unpromoteProductionDeployment(functionId: string): Promise<
| {
id: string;
}
| undefined
> {
const currentDeployment = await prisma.deployment.findFirst({
where: {
functionId,
Expand All @@ -151,9 +154,7 @@ export async function unpromoteProductionDeployment(functionId: string): Promise
});

if (!currentDeployment) {
throw new TRPCError({
code: 'NOT_FOUND',
});
return undefined;
}

return prisma.deployment.update({
Expand Down Expand Up @@ -223,7 +224,7 @@ export async function promoteProductionDeployment(functionId: string, newDeploym
await redis.publish(
'promote',
JSON.stringify({
previousDeploymentId: previousDeployment.id,
previousDeploymentId: previousDeployment?.id || '',
functionId: func.id,
functionName: func.name,
deploymentId: newDeploymentId,
Expand Down
2 changes: 2 additions & 0 deletions packages/dashboard/lib/trpc/accountsRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ export const accountsRouter = (t: T) =>
},
select: null,
});

return { ok: true };
}),
});
10 changes: 5 additions & 5 deletions packages/dashboard/lib/trpc/deploymentsRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,7 @@ export const deploymentsRouter = (t: T) =>
});

if (input.isProduction) {
try {
await unpromoteProductionDeployment(input.functionId);
} catch {
// this is the first deployment
}
await unpromoteProductionDeployment(input.functionId);
}

const [func, deployment] = await Promise.all([
Expand Down Expand Up @@ -183,6 +179,8 @@ export const deploymentsRouter = (t: T) =>
)
.mutation(async ({ input }) => {
await promoteProductionDeployment(input.functionId, input.deploymentId);

return { ok: true };
}),
deploymentDelete: t.procedure
.input(
Expand Down Expand Up @@ -231,5 +229,7 @@ export const deploymentsRouter = (t: T) =>
},
input.deploymentId,
);

return { ok: true };
}),
});
4 changes: 4 additions & 0 deletions packages/dashboard/lib/trpc/functionsRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,8 @@ export const functionsRouter = (t: T) =>
currentDomains,
);
}

return { ok: true };
}),
functionDelete: t.procedure
.input(
Expand Down Expand Up @@ -393,5 +395,7 @@ export const functionsRouter = (t: T) =>
}

await removeFunction(func);

return { ok: true };
}),
});
8 changes: 8 additions & 0 deletions packages/dashboard/lib/trpc/organizationsRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export const organizationsRouter = (t: T) =>
currentOrganizationId: organization.id,
},
});

return { ok: true };
}),
organizationUpdate: t.procedure
.input(
Expand All @@ -68,6 +70,8 @@ export const organizationsRouter = (t: T) =>
},
select: null,
});

return { ok: true };
}),
organizationsDelete: t.procedure
.input(
Expand Down Expand Up @@ -141,6 +145,8 @@ export const organizationsRouter = (t: T) =>
currentOrganizationId: leftOrganization?.id,
},
});

return { ok: true };
}),
organizationSetCurrent: t.procedure
.input(
Expand All @@ -157,5 +163,7 @@ export const organizationsRouter = (t: T) =>
currentOrganizationId: input.organizationId,
},
});

return { ok: true };
}),
});
2 changes: 2 additions & 0 deletions packages/dashboard/lib/trpc/tokensRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,7 @@ export const tokensRouter = (t: T) =>
id: input.tokenId,
},
});

return { ok: true };
}),
});

2 comments on commit 75eac5d

@vercel
Copy link

@vercel vercel bot commented on 75eac5d Dec 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

storybook – ./packages/dashboard

storybook-lagon.vercel.app
storybook-git-main-lagon.vercel.app
storybook-swart-eight.vercel.app
ui.lagon.app

@vercel
Copy link

@vercel vercel bot commented on 75eac5d Dec 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

dashboard – ./packages/dashboard

dash.lagon.app
dashboard-lagon.vercel.app
dashboard-git-main-lagon.vercel.app

Please sign in to comment.