Skip to content

Commit 7a33bec

Browse files
improve type safety for repo metadata json
1 parent 20ab688 commit 7a33bec

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

packages/backend/src/repoManager.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Redis } from 'ioredis';
33
import { createLogger } from "./logger.js";
44
import { Connection, PrismaClient, Repo, RepoToConnection, RepoIndexingStatus, StripeSubscriptionStatus } from "@sourcebot/db";
55
import { GithubConnectionConfig, GitlabConnectionConfig, GiteaConnectionConfig } from '@sourcebot/schemas/v3/connection.type';
6-
import { AppContext, Settings, RepoMetadata } from "./types.js";
6+
import { AppContext, Settings, repoMetadataSchema } from "./types.js";
77
import { getRepoPath, getTokenFromConfig, measure, getShardPrefix } from "./utils.js";
88
import { cloneRepository, fetchRepository, upsertGitConfig } from "./git.js";
99
import { existsSync, readdirSync, promises } from 'fs';
@@ -200,8 +200,7 @@ export class RepoManager implements IRepoManager {
200200
let cloneDuration_s: number | undefined = undefined;
201201

202202
const repoPath = getRepoPath(repo, this.ctx);
203-
const metadata = repo.metadata as RepoMetadata;
204-
203+
const metadata = repoMetadataSchema.parse(repo.metadata);
205204

206205
// If the repo was already in the indexing state, this job was likely killed and picked up again. As a result,
207206
// to ensure the repo state is valid, we delete the repo if it exists so we get a fresh clone

packages/backend/src/types.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Settings as SettingsSchema } from "@sourcebot/schemas/v3/index.type";
2+
import { z } from "zod";
23

34
export type AppContext = {
45
/**
@@ -16,28 +17,32 @@ export type AppContext = {
1617

1718
export type Settings = Required<SettingsSchema>;
1819

19-
/**
20-
* Structure of the `metadata` field in the `Repo` table.
21-
*/
22-
export type RepoMetadata = {
20+
// Structure of the `metadata` field in the `Repo` table.
21+
//
22+
// @WARNING: If you modify this schema, please make sure it is backwards
23+
// compatible with any prior versions of the schema!!
24+
// @NOTE: If you move this schema, please update the comment in schema.prisma
25+
// to point to the new location.
26+
export const repoMetadataSchema = z.object({
2327
/**
2428
* A set of key-value pairs that will be used as git config
2529
* variables when cloning the repo.
2630
* @see: https://git-scm.com/docs/git-clone#Documentation/git-clone.txt-code--configcodecodeltkeygtltvaluegtcode
2731
*/
28-
gitConfig?: Record<string, string>;
32+
gitConfig: z.record(z.string(), z.string()).optional(),
2933

3034
/**
3135
* A list of branches to index. Glob patterns are supported.
3236
*/
33-
branches?: string[];
37+
branches: z.array(z.string()).optional(),
3438

3539
/**
3640
* A list of tags to index. Glob patterns are supported.
3741
*/
38-
tags?: string[];
39-
}
42+
tags: z.array(z.string()).optional(),
43+
});
4044

45+
export type RepoMetadata = z.infer<typeof repoMetadataSchema>;
4146

4247
// @see : https://stackoverflow.com/a/61132308
4348
export type DeepPartial<T> = T extends object ? {

packages/backend/src/zoekt.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { exec } from "child_process";
2-
import { AppContext, RepoMetadata, Settings } from "./types.js";
2+
import { AppContext, repoMetadataSchema, Settings } from "./types.js";
33
import { Repo } from "@sourcebot/db";
44
import { getRepoPath } from "./utils.js";
55
import { getShardPrefix } from "./utils.js";
@@ -17,7 +17,7 @@ export const indexGitRepository = async (repo: Repo, settings: Settings, ctx: Ap
1717

1818
const repoPath = getRepoPath(repo, ctx);
1919
const shardPrefix = getShardPrefix(repo.orgId, repo.id);
20-
const metadata = repo.metadata as RepoMetadata;
20+
const metadata = repoMetadataSchema.parse(repo.metadata);
2121

2222
if (metadata.branches) {
2323
const branchGlobs = metadata.branches

packages/db/prisma/schema.prisma

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ model Repo {
4343
indexedAt DateTime?
4444
isFork Boolean
4545
isArchived Boolean
46-
metadata Json
46+
metadata Json // For schema see repoMetadataSchema in packages/backend/src/types.ts
4747
cloneUrl String
4848
webUrl String?
4949
connections RepoToConnection[]

0 commit comments

Comments
 (0)