Skip to content

Commit

Permalink
builder: improve errors, clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
Guusvanmeerveld committed Nov 9, 2024
1 parent 9efa4e7 commit d2b3212
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 39 deletions.
14 changes: 6 additions & 8 deletions src/builder/builder.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
BadRequestException,
Controller,
Get,
NotFoundException,
Param,
Post,
Req,
Expand All @@ -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";
Expand All @@ -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);

Expand Down
72 changes: 42 additions & 30 deletions src/builder/builder.service.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -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);

Expand All @@ -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");
}
}
}
5 changes: 4 additions & 1 deletion src/builder/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

0 comments on commit d2b3212

Please sign in to comment.