From 9461bd04f1c1bb30bd8f29915ea76e1f092f707b Mon Sep 17 00:00:00 2001 From: IshaVenikar Date: Fri, 18 Oct 2024 10:06:27 +0530 Subject: [PATCH] Pass env variables when creating project --- packages/backend/src/resolvers.ts | 34 +++++++++++++++---- packages/backend/src/schema.gql | 2 ++ packages/backend/src/service.ts | 15 +++++--- .../components/projects/create/Configure.tsx | 31 +++++++++-------- packages/gql-client/src/client.ts | 8 +++-- packages/gql-client/src/mutations.ts | 8 ++--- 6 files changed, 67 insertions(+), 31 deletions(-) diff --git a/packages/backend/src/resolvers.ts b/packages/backend/src/resolvers.ts index 5e640749..dbe65f3b 100644 --- a/packages/backend/src/resolvers.ts +++ b/packages/backend/src/resolvers.ts @@ -211,8 +211,15 @@ export const createResolvers = async (service: Service): Promise => { organizationSlug, data, lrn, - auctionParams - }: { organizationSlug: string; data: AddProjectFromTemplateInput; lrn: string; auctionParams: AuctionParams }, + auctionParams, + environmentVariables + }: { + organizationSlug: string; + data: AddProjectFromTemplateInput; + lrn: string; + auctionParams: AuctionParams, + environmentVariables: { environments: string[]; key: string; value: string }[]; + }, context: any, ) => { try { @@ -221,7 +228,8 @@ export const createResolvers = async (service: Service): Promise => { organizationSlug, data, lrn, - auctionParams + auctionParams, + environmentVariables ); } catch (err) { log(err); @@ -235,12 +243,26 @@ export const createResolvers = async (service: Service): Promise => { organizationSlug, data, lrn, - auctionParams - }: { organizationSlug: string; data: DeepPartial; lrn: string; auctionParams: AuctionParams }, + auctionParams, + environmentVariables + }: { + organizationSlug: string; + data: DeepPartial; + lrn: string; + auctionParams: AuctionParams, + environmentVariables: { environments: string[]; key: string; value: string }[]; + }, context: any, ) => { try { - return await service.addProject(context.user, organizationSlug, data, lrn, auctionParams); + return await service.addProject( + context.user, + organizationSlug, + data, + lrn, + auctionParams, + environmentVariables + ); } catch (err) { log(err); throw err; diff --git a/packages/backend/src/schema.gql b/packages/backend/src/schema.gql index df8bd03a..b4f74d72 100644 --- a/packages/backend/src/schema.gql +++ b/packages/backend/src/schema.gql @@ -271,12 +271,14 @@ type Mutation { data: AddProjectFromTemplateInput lrn: String auctionParams: AuctionParams + environmentVariables: [AddEnvironmentVariableInput!] ): Project! addProject( organizationSlug: String! data: AddProjectInput! lrn: String auctionParams: AuctionParams + environmentVariables: [AddEnvironmentVariableInput!] ): Project! updateProject(projectId: String!, data: UpdateProjectInput): Boolean! redeployToProd(deploymentId: String!): Boolean! diff --git a/packages/backend/src/service.ts b/packages/backend/src/service.ts index e52595fa..5b127712 100644 --- a/packages/backend/src/service.ts +++ b/packages/backend/src/service.ts @@ -784,7 +784,8 @@ export class Service { organizationSlug: string, data: AddProjectFromTemplateInput, lrn?: string, - auctionParams?: AuctionParams + auctionParams?: AuctionParams, + environmentVariables?: { environments: string[]; key: string; value: string }[], ): Promise { try { const octokit = await this.getOctokit(user.id); @@ -815,7 +816,7 @@ export class Service { repository: gitRepo.data.full_name, // TODO: Set selected template template: 'webapp', - }, lrn, auctionParams); + }, lrn, auctionParams, environmentVariables); if (!project || !project.id) { throw new Error('Failed to create project from template'); @@ -833,7 +834,8 @@ export class Service { organizationSlug: string, data: DeepPartial, lrn?: string, - auctionParams?: AuctionParams + auctionParams?: AuctionParams, + environmentVariables?: { environments: string[]; key: string; value: string }[], ): Promise { const organization = await this.db.getOrganization({ where: { @@ -844,8 +846,11 @@ export class Service { throw new Error('Organization does not exist'); } - const project = await this.db.addProject(user, organization.id, data); - log(`Project created ${project.id}`); + const project = await this.db.addProject(user, organization.id, data);4 + + if(environmentVariables) { + await this.addEnvironmentVariables(project.id, environmentVariables); + } const octokit = await this.getOctokit(user.id); const [owner, repo] = project.repository.split('/'); diff --git a/packages/frontend/src/components/projects/create/Configure.tsx b/packages/frontend/src/components/projects/create/Configure.tsx index 75061055..7f920c0a 100644 --- a/packages/frontend/src/components/projects/create/Configure.tsx +++ b/packages/frontend/src/components/projects/create/Configure.tsx @@ -3,7 +3,7 @@ import { useForm, Controller } from 'react-hook-form'; import { FormProvider, FieldValues } from 'react-hook-form'; import { useNavigate, useSearchParams } from 'react-router-dom'; import { useMediaQuery } from 'usehooks-ts'; -import { AuctionParams } from 'gql-client'; +import { AddEnvironmentVariableInput, AuctionParams } from 'gql-client'; import { ArrowRightCircleFilledIcon, @@ -55,7 +55,7 @@ const Configure = () => { const isTabletView = useMediaQuery('(min-width: 720px)'); // md: const buttonSize = isTabletView ? { size: 'lg' as const } : {}; - const createProject = async (data: FieldValues): Promise => { + const createProject = async (data: FieldValues, envVariables: AddEnvironmentVariableInput[]): Promise => { setIsLoading(true); let projectId: string | null = null; @@ -81,13 +81,15 @@ const Configure = () => { isPrivate, }; - const { addProjectFromTemplate } = await client.addProjectFromTemplate( - orgSlug!, - projectData, - lrn, - auctionParams - ); - projectId = addProjectFromTemplate.id; + const { addProjectFromTemplate } = await client.addProjectFromTemplate( + orgSlug!, + projectData, + lrn, + auctionParams, + envVariables + ); + + projectId = addProjectFromTemplate.id; } else { const { addProject } = await client.addProject( orgSlug!, @@ -98,7 +100,8 @@ const Configure = () => { template: 'webapp', }, lrn, - auctionParams + auctionParams, + envVariables ); projectId = addProject.id; @@ -134,12 +137,12 @@ const Configure = () => { }; }); - const projectId = await createProject(createFormData); + const projectId = await createProject(createFormData, environmentVariables); - const { addEnvironmentVariables: isEnvironmentVariablesAdded } = - await client.addEnvironmentVariables(projectId, environmentVariables); + const { environmentVariables: isEnvironmentVariablesAdded } = + await client.getEnvironmentVariables(projectId); - if (isEnvironmentVariablesAdded) { + if (isEnvironmentVariablesAdded.length > 0) { toast({ id: createFormData.variables.length > 1 diff --git a/packages/gql-client/src/client.ts b/packages/gql-client/src/client.ts index f7099de5..0e0eff9f 100644 --- a/packages/gql-client/src/client.ts +++ b/packages/gql-client/src/client.ts @@ -233,6 +233,7 @@ export class GQLClient { data: types.AddProjectFromTemplateInput, lrn?: string, auctionParams?: types.AuctionParams, + environmentVariables?: types.AddEnvironmentVariableInput[] ): Promise { const result = await this.client.mutate({ mutation: mutations.addProjectFromTemplate, @@ -240,7 +241,8 @@ export class GQLClient { organizationSlug, data, lrn, - auctionParams + auctionParams, + environmentVariables }, }); @@ -252,6 +254,7 @@ export class GQLClient { data: types.AddProjectInput, lrn?: string, auctionParams?: types.AuctionParams, + environmentVariables?: types.AddEnvironmentVariableInput[] ): Promise { const result = await this.client.mutate({ mutation: mutations.addProject, @@ -259,7 +262,8 @@ export class GQLClient { organizationSlug, data, lrn, - auctionParams + auctionParams, + environmentVariables }, }); diff --git a/packages/gql-client/src/mutations.ts b/packages/gql-client/src/mutations.ts index 1ad04e22..876e5611 100644 --- a/packages/gql-client/src/mutations.ts +++ b/packages/gql-client/src/mutations.ts @@ -49,16 +49,16 @@ export const updateDeploymentToProd = gql` `; export const addProjectFromTemplate = gql` - mutation ($organizationSlug: String!, $data: AddProjectFromTemplateInput, $lrn: String, $auctionParams: AuctionParams) { - addProjectFromTemplate(organizationSlug: $organizationSlug, data: $data, lrn: $lrn, auctionParams: $auctionParams) { + mutation ($organizationSlug: String!, $data: AddProjectFromTemplateInput, $lrn: String, $auctionParams: AuctionParams, $environmentVariables: [AddEnvironmentVariableInput!]) { + addProjectFromTemplate(organizationSlug: $organizationSlug, data: $data, lrn: $lrn, auctionParams: $auctionParams, environmentVariables: $environmentVariables) { id } } `; export const addProject = gql` - mutation ($organizationSlug: String!, $data: AddProjectInput!, $lrn: String, $auctionParams: AuctionParams) { - addProject(organizationSlug: $organizationSlug, data: $data, lrn: $lrn, auctionParams: $auctionParams) { + mutation ($organizationSlug: String!, $data: AddProjectInput!, $lrn: String, $auctionParams: AuctionParams, $environmentVariables: [AddEnvironmentVariableInput!]) { + addProject(organizationSlug: $organizationSlug, data: $data, lrn: $lrn, auctionParams: $auctionParams, environmentVariables: $environmentVariables) { id } }