diff --git a/src/builder/builder.controller.ts b/src/builder/builder.controller.ts index 450cce6..273b41f 100644 --- a/src/builder/builder.controller.ts +++ b/src/builder/builder.controller.ts @@ -2,6 +2,7 @@ import { BadRequestException, Controller, Get, + NotFoundException, Param, Post, Req, @@ -13,7 +14,7 @@ import { import { Request, Response } from "express"; import { createReadStream, pathExists } from "fs-extra"; -import { APK_DIR } from "./constants"; +import { getApkDir } from "./constants"; import { AdminGuard } from "../admin.guard"; import { BuilderService } from "./builder.service"; import { FileInterceptor } from "@nestjs/platform-express"; @@ -31,21 +32,18 @@ export class BuilderController { @UploadedFile() file: Express.Multer.File, @Req() req: Request, ) { - if (!file) { + if (!file) throw new BadRequestException("You must include a file to upload."); - } return await this.builderService.handleFileUpload(file, req); } @Get("download/:file") async getAPKFile(@Res() res: Response, @Param() params) { - const path = join(process.env.APK_DIR ?? APK_DIR, params.file); + const path = join(getApkDir(), params.file); - if (!(await pathExists(path))) { - res.status(404).end("File not found"); - return; - } + if (!(await pathExists(path))) + throw new NotFoundException("File not found"); const file = createReadStream(path); diff --git a/src/builder/builder.service.ts b/src/builder/builder.service.ts index b2ec962..40c953b 100644 --- a/src/builder/builder.service.ts +++ b/src/builder/builder.service.ts @@ -1,4 +1,9 @@ -import { APK_DIR, GITHUB_EVENT_HEADER, MAX_FILE_SIZE } from "./constants"; +import { + createApkFileName, + getApkDir, + GITHUB_EVENT_HEADER, + MAX_FILE_SIZE, +} from "./constants"; import { BadRequestException, Injectable, Logger } from "@nestjs/common"; import { ensureDir, writeFile } from "fs-extra"; @@ -44,11 +49,11 @@ export class BuilderService { this.logger.log(`Got ${file.originalname} from ${request.ip}`); - const apkDir = process.env.APK_DIR ?? APK_DIR; + const apkDir = getApkDir(); await ensureDir(apkDir); - const fileName = `Argo-${Date.now()}.${fileExtension}`; + const fileName = createApkFileName(fileExtension); await writeFile(join(apkDir, fileName), file.buffer); @@ -64,35 +69,42 @@ export class BuilderService { async handleWebhook(req: Request) { const payload = JSON.parse(req.body.payload); - if (req.get(GITHUB_EVENT_HEADER) == GITHUB_EVENT_TYPE.PUSH) { - const commit = payload.head_commit; - - await this.commitsService.insert({ - author: { - username: payload.sender.login, - avatar: payload.sender.avatar_url, - }, - id: commit.id, - message: commit.message, - pending: true, - timestamp: commit.timestamp, - }); - - return { success: "Inserted commit into database" }; - } - - if (req.get(GITHUB_EVENT_HEADER) == GITHUB_EVENT_TYPE.WORKFLOW) { - if ( - payload?.action == "completed" && - payload?.workflow_job?.conclusion == "failure" - ) { - await this.commitsService.update(payload.head_sha, { - success: false, - pending: false, + switch (req.get(GITHUB_EVENT_HEADER)) { + case GITHUB_EVENT_TYPE.PUSH: + const commit = payload.head_commit; + + await this.commitsService.insert({ + author: { + username: payload.sender.login, + avatar: payload.sender.avatar_url, + }, + id: commit.id, + message: commit.message, + pending: true, + timestamp: commit.timestamp, }); - return { success: "Updated commit in database" }; - } + return { success: "Inserted commit into database" }; + + case GITHUB_EVENT_TYPE.WORKFLOW: + if (payload?.action == "completed") { + if (payload?.workflow_job?.conclusion == "failure") { + await this.commitsService.update(payload.head_sha, { + success: false, + pending: false, + }); + + return { success: "Updated commit in database" }; + } else + throw new BadRequestException( + "Workflow conclusion is not failure, no new info", + ); + } + + throw new BadRequestException("Workflow is not yet completed"); + + default: + throw new BadRequestException("Unknown Github event type"); } } } diff --git a/src/builder/constants.ts b/src/builder/constants.ts index 02ec555..3df6ad0 100644 --- a/src/builder/constants.ts +++ b/src/builder/constants.ts @@ -2,6 +2,9 @@ const MEGABYTE = 1024 * 1024; export const MAX_FILE_SIZE = MEGABYTE * 50; // Max file size in bytes -export const APK_DIR = "/apk/"; +export const getApkDir = () => process.env.APK_DIR ?? "/apk/"; + +export const createApkFileName = (fileExtension: string) => + `Argo-${Date.now()}.${fileExtension}`; export const GITHUB_EVENT_HEADER = "X-Github-Event";