Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions backend/functions/src/models/autograder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { GradingJobPublicBaseSchema } from "@app-portal/shared/types";
import { Timestamp } from "firebase-admin/firestore";
import { z } from "zod";

// ⚠️ do not edit this type!!!
// edit the GradingJobPublicBaseSchema instead!!!
const GradingJobPublicSchema = GradingJobPublicBaseSchema.extend({
started: z.custom<Timestamp>((d) => d instanceof Timestamp),
completed: z.custom<Timestamp>((d) => d instanceof Timestamp).optional(),
updated: z.custom<Timestamp>((d) => d instanceof Timestamp),
});

export type GradingJobPublic = z.infer<typeof GradingJobPublicSchema>;
10 changes: 7 additions & 3 deletions backend/functions/src/routes/grading.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import {
FirestoreCollection,
GradingJobStatus,
PermissionRole,
} from "@app-portal/shared/constants";
import type { GradingJobDataInternal } from "@app-portal/shared/types";
import { submitGradingJobSchema } from "@app-portal/shared/types";
import type { Response, Request } from "express";
import { Router } from "express";
import { Timestamp } from "firebase-admin/firestore";
Expand All @@ -16,7 +19,7 @@ import {
} from "../middleware/authentication";
import { validateSchema } from "../middleware/validation";
import type { ApplicationResponse } from "../models/appResponse";
import { submitGradingJobSchema, GradingJobStatus } from "../types/grading";
import type { GradingJobPublic } from "../models/autograder";
import { publishGradingTask } from "../utils/cloudTasks";
import { appCollection } from "../utils/firestore";

Expand Down Expand Up @@ -113,7 +116,7 @@ router.post(
}

// create: job docs and cloud tasks job
const publicJob = {
const publicJob: GradingJobPublic = {
id: jobId,
responseId,
repoURL,
Expand All @@ -123,10 +126,11 @@ router.post(
completedTests: 0,
started: now,
updated: now,
suiteResults: {},
publicTests: {},
};

const internalJob = {
const internalJob: GradingJobDataInternal = {
id: jobId,
testRepo,
buildLog: "",
Expand Down
6 changes: 4 additions & 2 deletions backend/functions/src/scheduled/cleanupStaleJobs.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { FirestoreCollection } from "@app-portal/shared/constants";
import {
FirestoreCollection,
GradingJobStatus,
} from "@app-portal/shared/constants";
import { Timestamp } from "firebase-admin/firestore";
import { logger } from "firebase-functions";
import { onSchedule } from "firebase-functions/v2/scheduler";

import { db } from "../index";
import { GradingJobStatus } from "../types/grading";
import { appCollection } from "../utils/firestore";

const STALE_TIMEOUT_MINUTES = 30;
Expand Down
84 changes: 0 additions & 84 deletions backend/functions/src/types/grading.ts

This file was deleted.

6 changes: 2 additions & 4 deletions backend/functions/src/utils/firestore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
ApplicationReviewData,
AppReviewAssignment,
DecisionConfirmation,
GradingJobDataInternal,
InternalApplicationStatus,
InterviewAssignment,
RoleReviewRubric,
Expand All @@ -13,11 +14,8 @@ import type { CollectionReference } from "firebase-admin/firestore";
import { db } from "..";
import type { ApplicationForm } from "../models/appForm";
import type { ApplicationResponse } from "../models/appResponse";
import type { GradingJobPublic } from "../models/autograder";
import type { UserProfile } from "../models/user";
import type {
GradingJobDataInternal,
GradingJobPublic,
} from "../types/grading";

type ServerCollectionData = {
[FirestoreCollection.Users]: UserProfile;
Expand Down
12 changes: 12 additions & 0 deletions shared/src/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,15 @@ export enum ReviewStatus {
Denied = "denied",
Waitlisted = "waitlist",
}

export enum GradingJobStatus {
Queued = "queued",
Pending = "pending",
Cloning = "cloning",
Installing = "installing",
Building = "building",
Serving = "serving",
Testing = "testing",
Completed = "completed",
Failed = "failed",
}
84 changes: 84 additions & 0 deletions shared/src/types/autograder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { z } from "zod";

import { GradingJobStatus } from "../constants/index.js";

export const TestResultSchema = z.object({
suite: z.string().nonempty(),
testName: z.string().nonempty(),
passed: z.boolean(),
pending: z.boolean(),
stdout: z.string(),
stderr: z.string(),
errors: z.array(z.string()),
durationMs: z.int(),
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
points: z.int(),
});

export const SuiteResultSchema = z.object({
suiteName: z.string().nonempty(),
passed: z.int(),
failed: z.int(),
total: z.int(),
durationMs: z.int(),
points: z.int(),
totalPoints: z.int(),
});

// ⚠️ should not be used on its own!!!!
// use the per-package extension schema with Timestamp!!!!
export const GradingJobPublicBaseSchema = z.object({
id: z.string().nonempty(),
responseId: z.string().nonempty(),
repoURL: z.string().nonempty(),
status: z.enum(GradingJobStatus),
score: z.float64(),
totalTests: z.int(),
completedTests: z.int(),
error: z.string().optional(),
cloneDurationMs: z.int().optional(),
installDurationMs: z.int().optional(),
buildDurationMs: z.int().optional(),
testingDurationMs: z.int().optional(),
suiteResults: z.record(z.string(), SuiteResultSchema),
publicTests: z.record(z.string(), z.record(z.string(), TestResultSchema)),
});

export const GradingJobDataInternalSchema = z.object({
id: z.string().nonempty(),
testRepo: z.string().nonempty(),
buildLog: z.string(),
installLog: z.string(),
playwrightLog: z.string(),
error: z.string().optional(),
tests: z.record(z.string(), z.record(z.string(), TestResultSchema)),
});

export const submitGradingJobSchema = GradingJobPublicBaseSchema.pick({
responseId: true,
repoURL: true,
}).extend({
repoURL: z.url().refine((val) => {
try {
const url = new URL(val);
const allowedHosts = ["github.com", "www.github.com"];
const path = url.pathname.split("/").filter(Boolean);

return (
url.protocol === "https:" &&
allowedHosts.includes(url.hostname) &&
path.length === 2 &&
url.search === "" &&
url.hash === ""
);
} catch {
return false;
}
}, "Repo URL must follow the format: https://github.com/USER/REPO"),
});

export type TestResult = z.infer<typeof TestResultSchema>;
export type SuiteResult = z.infer<typeof SuiteResultSchema>;
export type GradingJobPublicBase = z.infer<typeof GradingJobPublicBaseSchema>;
export type GradingJobDataInternal = z.infer<
typeof GradingJobDataInternalSchema
>;
1 change: 1 addition & 0 deletions shared/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./form.js";
export * from "./autograder.js";
export * from "./review.js";
export * from "./response.js";
export * from "./status.js";
Expand Down
Loading