Skip to content

Commit

Permalink
Merge branch 'develop' into update_analytics
Browse files Browse the repository at this point in the history
  • Loading branch information
charleslavon authored Aug 2, 2023
2 parents dd0b90e + 0dfd8e4 commit e560da7
Show file tree
Hide file tree
Showing 11 changed files with 707 additions and 144 deletions.
1 change: 1 addition & 0 deletions backend/src/modules/deploys/deploy-error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
export enum DeployError {
NAME_CONFLICT = 'NAME_CONFLICT',
BAD_REPO = 'BAD_REPO',
TRANSFER_INITIATED = 'TRANSFER_INITIATED',
}
202 changes: 152 additions & 50 deletions backend/src/modules/deploys/deploys.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
UseInterceptors,
UsePipes,
ConflictException,
NotFoundException,
} from '@nestjs/common';
import { DeploysService } from './deploys.service';
import { ZodValidationPipe } from 'src/pipes/ZodValidationPipe';
Expand All @@ -30,19 +31,21 @@ import { Request as ExpressRequest } from 'express';
export class DeploysController {
constructor(private readonly deploysService: DeploysService) {}

@Post('addDeploy')
@Post('addConsoleDeployProject')
@UseGuards(BearerAuthGuard)
@UsePipes(new ZodValidationPipe(Deploys.mutation.inputs.addDeploy))
async addDeploy(
@UsePipes(
new ZodValidationPipe(Deploys.mutation.inputs.addConsoleDeployProject),
)
async addConsoleDeployProject(
@Req() req: ExpressRequest,
@Body()
{
githubRepoFullName,
projectName,
}: z.infer<typeof Deploys.mutation.inputs.addDeploy>,
): Promise<Api.Mutation.Output<'/deploys/addDeploy'>> {
}: z.infer<typeof Deploys.mutation.inputs.addConsoleDeployProject>,
): Promise<Api.Mutation.Output<'/deploys/addConsoleDeployProject'>> {
// called from Console frontend to initialize a new repo for deployment
try {
// called from Console frontend to initialize a new repo for deployment
const repository = await this.deploysService.createDeployProject({
user: req.user as User, // TODO change to UserDetails from auth service
githubRepoFullName,
Expand All @@ -56,6 +59,25 @@ export class DeploysController {
throw mapError(e);
}
}

@Post('contractDeployConfigs')
@UseGuards(GithubBasicAuthGuard)
async contractDeployConfigs(
@Req() req: ExpressRequest,
@Body()
{
repoDeploymentSlug,
}: z.infer<typeof Deploys.mutation.inputs.contractDeployConfigs>,
): Promise<Api.Mutation.Output<'/deploys/contractDeployConfigs'>> {
try {
return await this.deploysService.getContractDeployConfigs(
repoDeploymentSlug,
);
} catch (e: any) {
throw mapError(e);
}
}

@Post('isRepositoryTransferred')
@UseGuards(BearerAuthGuard)
@UsePipes(new ZodValidationPipe(Deploys.query.inputs.isRepositoryTransferred))
Expand Down Expand Up @@ -90,74 +112,148 @@ export class DeploysController {
}: z.infer<typeof Deploys.mutation.inputs.transferGithubRepository>,
): Promise<Api.Mutation.Output<'/deploys/transferGithubRepository'>> {
// called from Console frontend to initialize a new repo for deployment
const repository = await this.deploysService.transferGithubRepository({
user: req.user as User, // TODO change to UserDetails from auth service
newGithubUsername,
repositorySlug,
});
return {
repositorySlug: repository.slug,
githubRepoFullName: repository.githubRepoFullName,
};
try {
const repository = await this.deploysService.transferGithubRepository({
user: req.user as User, // TODO change to UserDetails from auth service
newGithubUsername,
repositorySlug,
});
return {
repositorySlug: repository.slug,
githubRepoFullName: repository.githubRepoFullName,
};
} catch (e: any) {
throw mapError(e);
}
}

@Post('deployWasm')
@Post('addRepoDeployment')
@UseGuards(GithubBasicAuthGuard) // Currently used only by github - can be extended to authorize other clients with Bearer tokens
@UsePipes(new ZodValidationPipe(Deploys.mutation.inputs.addRepoDeployment))
async addRepoDeployment(
@Req() req: Request,
@Body()
{
githubRepoFullName,
commitHash,
commitMessage,
}: z.infer<typeof Deploys.mutation.inputs.addRepoDeployment>,
): Promise<Api.Mutation.Output<'/deploys/addRepoDeployment'>> {
try {
const repoDeployment = await this.deploysService.addRepoDeployment({
githubRepoFullName,
commitHash,
commitMessage,
});

return { repoDeploymentSlug: repoDeployment.slug };
} catch (e: any) {
throw mapError(e);
}
}

@Post('addContractDeployment')
@UseInterceptors(AnyFilesInterceptor())
@UseGuards(GithubBasicAuthGuard) // Currently used only by github - can be extended to authorize other clients with Bearer tokens
async addContractDeployment(
@Req() req: ExpressRequest,
@UploadedFiles() files: Array<Express.Multer.File>,
@Body() body: z.infer<typeof Deploys.mutation.inputs.addContractDeployment>,
): Promise<Api.Mutation.Output<'/deploys/addContractDeployment'>> {
const parsedValue =
Deploys.mutation.inputs.addContractDeployment.safeParse(body);
if (parsedValue.success === false) {
throw new BadRequestException(fromZodError(parsedValue.error).toString());
}

const parsedFiles = Deploys.mutation.inputs.wasmFiles.safeParse(files);
if (parsedFiles.success === false) {
throw new BadRequestException(fromZodError(parsedFiles.error).toString());
}
const { repoDeploymentSlug } = body;

try {
// called from Console frontend to initialize a new repo for deployment
await this.deploysService.addContractDeployment({
repoDeploymentSlug,
files,
});

return await this.deploysService.getContractDeployConfigs(
repoDeploymentSlug,
);
} catch (e: any) {
throw mapError(e);
}
}

@Post('addNearSocialComponentDeployment')
@UseInterceptors(AnyFilesInterceptor())
@UseGuards(GithubBasicAuthGuard) // Currently used only by github - can be extended to authorize other clients
async deployWasm(
async addNearSocialComponentDeployment(
@Req() req: Request,
@UploadedFiles() files: Array<Express.Multer.File>,
@Body() body: z.infer<typeof Deploys.mutation.inputs.deployWasm>,
@Body()
body: z.infer<typeof Deploys.mutation.inputs.deployNearSocialComponent>,
) {
const parsedValue = Deploys.mutation.inputs.deployWasm.safeParse(body);
const parsedValue =
Deploys.mutation.inputs.deployNearSocialComponent.safeParse(body);
if (parsedValue.success === false) {
throw new BadRequestException(fromZodError(parsedValue.error).toString());
}

const parsedFiles = Deploys.mutation.inputs.wasmFiles.safeParse(files);
const parsedFiles =
Deploys.mutation.inputs.nearSocialFiles.safeParse(files);
if (parsedFiles.success === false) {
throw new BadRequestException(fromZodError(parsedFiles.error).toString());
}
const { githubRepoFullName, commitHash, commitMessage } = body;

// called from Console frontend to initialize a new repo for deployment
return this.deploysService.deployRepository({
githubRepoFullName,
commitHash,
commitMessage,
files,
});
const {
componentName,
componentDescription,
componentIconIpfsCid,
componentTags,
} = body;
try {
return await this.deploysService.addNearSocialComponentDeployment({
repoDeploymentSlug: body.repoDeploymentSlug,
metadata: {
componentName,
componentDescription,
componentIconIpfsCid,
componentTags,
},
file: files[0],
});
} catch (e: any) {
throw mapError(e);
}
}

@Post('addFrontend')
@UsePipes(new ZodValidationPipe(Deploys.mutation.inputs.addFrontend))
@Post('addFrontendDeployment')
@UsePipes(
new ZodValidationPipe(Deploys.mutation.inputs.addFrontendDeployment),
)
@UseGuards(GithubBasicAuthGuard) // Currently used only by github - can be extended to authorize other clients
async addFrontend(
async addFrontendDeployment(
@Req() req: Request,
@Body()
{
repoDeploymentSlug,
frontendDeployUrl,
cid,
packageName,
}: z.infer<typeof Deploys.mutation.inputs.addFrontend>,
) {
const repoDeployment = await this.deploysService.getRepoDeploymentBySlug(
repoDeploymentSlug,
);
if (!repoDeployment) {
throw new BadRequestException(
`RepoDeployment slug ${repoDeploymentSlug} not found`,
);
}: z.infer<typeof Deploys.mutation.inputs.addFrontendDeployment>,
): Promise<Api.Mutation.Output<'/deploys/addFrontendDeployment'>> {
try {
return await this.deploysService.addFrontendDeployment({
frontendDeployUrl,
cid,
packageName,
repoDeploymentSlug,
});
} catch (e: any) {
throw mapError(e);
}

return this.deploysService.addFrontend({
repositorySlug: repoDeployment.repositorySlug,
frontendDeployUrl,
cid,
packageName,
repoDeploymentSlug,
});
}

@Post('listRepositories')
Expand Down Expand Up @@ -198,13 +294,19 @@ function mapError(e: Error) {
const code = errorInfo?.code;
switch (code) {
case 'PERMISSION_DENIED':
return new ForbiddenException();
return new ForbiddenException(e.message);
case 'TOO_MANY_REQUESTS':
return new TooManyRequestsException();
case 'NAME_CONFLICT':
return new ConflictException(code);
case 'CONFLICT':
return new ConflictException(code);
case 'BAD_REQUEST':
return new BadRequestException(e.message);
case 'TRANSFER_INITIATED':
return new BadRequestException(e.message);
case 'NOT_FOUND':
return new NotFoundException(e.message);
default:
return e;
}
Expand Down
Loading

0 comments on commit e560da7

Please sign in to comment.