/ opencodehub / src / lib / codeowners.ts
Swadhin Biswas fix(ci): resolve test failures - add workspace, coverage deps, minimatch import · 4/30/2026
e1eb4d4
293 lines · 7.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/**
 * CODEOWNERS Parser Library
 * Parse and match CODEOWNERS file for automatic reviewer assignment
 */

import { minimatch } from "minimatch";
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
import { and, eq, or } from "drizzle-orm";
import { schema } from "@/db";

export interface CodeOwnerRule {
    pattern: string;
    owners: string[];
    lineNumber: number;
}

export interface CodeOwnersFile {
    rules: CodeOwnerRule[];
    path: string;
}

/**
 * Parse CODEOWNERS file content
 */
export function parseCodeOwners(content: string, filePath: string = "CODEOWNERS"): CodeOwnersFile {
    const rules: CodeOwnerRule[] = [];
    const lines = content.split("\n");

    for (let i = 0; i < lines.length; i++) {
        const line = lines[i].trim();

        // Skip empty lines and comments
        if (!line || line.startsWith("#")) {
            continue;
        }

        // Parse the line: pattern owner1 owner2 ...
        const parts = line.split(/\s+/).filter(Boolean);
        if (parts.length < 2) {
            continue; // Invalid line, needs pattern + at least one owner
        }

        const pattern = parts[0];
        const owners = parts.slice(1).map(normalizeOwner);

        rules.push({
            pattern,
            owners,
            lineNumber: i + 1,
        });
    }

    return { rules, path: filePath };
}

/**
 * Normalize owner format (remove @ prefix if present)
 */
function normalizeOwner(owner: string): string {
    return owner.startsWith("@") ? owner.slice(1) : owner;
}

/**
 * Find owners for a specific file path
 * Returns the owners from the LAST matching rule (CODEOWNERS uses last-match-wins)
 */
export function findOwnersForFile(
    codeOwners: CodeOwnersFile,
    filePath: string
): string[] {
    let matchedOwners: string[] = [];

    for (const rule of codeOwners.rules) {
        if (matchPattern(rule.pattern, filePath)) {
            matchedOwners = rule.owners;
        }
    }

    return matchedOwners;
}

/**
 * Find owners for multiple file paths
 * Returns unique owners from all matching rules
 */
export function findOwnersForFiles(
    codeOwners: CodeOwnersFile,
    filePaths: string[]
): string[] {
    const ownerSet = new Set<string>();

    for (const filePath of filePaths) {
        const owners = findOwnersForFile(codeOwners, filePath);
        for (const owner of owners) {
            ownerSet.add(owner);
        }
    }

    return Array.from(ownerSet);
}

/**
 * Match a file path against a CODEOWNERS pattern
 */
function matchPattern(pattern: string, filePath: string): boolean {
    // Normalize paths
    const normalizedPath = filePath.startsWith("/") ? filePath.slice(1) : filePath;
    let normalizedPattern = pattern;

    // Handle patterns starting with /
    if (normalizedPattern.startsWith("/")) {
        normalizedPattern = normalizedPattern.slice(1);
    }

    // Handle directory patterns ending with /
    if (normalizedPattern.endsWith("/")) {
        normalizedPattern = normalizedPattern + "**";
    }

    // Handle patterns without leading directory
    // e.g., "*.js" should match "src/foo.js"
    if (!normalizedPattern.includes("/") && !normalizedPattern.startsWith("**/")) {
        normalizedPattern = "**/" + normalizedPattern;
    }

    return minimatch(normalizedPath, normalizedPattern, {
        dot: true,
        matchBase: false,
    });
}

/**
 * Get suggested reviewers for a PR based on changed files
 */
export async function getSuggestedReviewers(
    codeOwnersContent: string,
    changedFiles: string[],
    excludeAuthors: string[] = []
): Promise<{
    owners: string[];
    coverage: { file: string; owners: string[] }[];
}> {
    const codeOwners = parseCodeOwners(codeOwnersContent);
    const coverage: { file: string; owners: string[] }[] = [];

    for (const file of changedFiles) {
        const owners = findOwnersForFile(codeOwners, file);
        coverage.push({ file, owners });
    }

    // Get unique owners, excluding PR authors
    const allOwners = findOwnersForFiles(codeOwners, changedFiles);
    const suggestedOwners = allOwners.filter(
        (owner) => !excludeAuthors.includes(owner)
    );

    return {
        owners: suggestedOwners,
        coverage,
    };
}

/**
 * Common CODEOWNERS locations in order of priority
 */
export const CODEOWNERS_PATHS = [
    "CODEOWNERS",
    ".github/CODEOWNERS",
    "docs/CODEOWNERS",
];

export async function expandOwnersToUsernames(options: {
    db: NodePgDatabase<typeof schema>;
    repository: typeof schema.repositories.$inferSelect;
    owners: string[];
}): Promise<Set<string>> {
    const { db, repository, owners } = options;
    const results = new Set<string>();

    let org = null;
    if (repository.ownerType === "organization") {
        org = await db.query.organizations.findFirst({
            where: eq(schema.organizations.id, repository.ownerId),
        });
    }

    for (const owner of owners) {
        const normalized = owner.startsWith("@") ? owner.slice(1) : owner;

        if (!normalized.includes("/")) {
            results.add(normalized);
            continue;
        }

        const [orgPart, teamPart] = normalized.split("/");
        if (!teamPart) continue;

        let orgId = repository.ownerType === "organization" ? repository.ownerId : null;
        if (orgPart && org && org.name !== orgPart) {
            const orgByName = await db.query.organizations.findFirst({
                where: eq(schema.organizations.name, orgPart),
            });
            orgId = orgByName?.id || orgId;
        }

        if (!orgId) continue;

        const team = await db.query.teams.findFirst({
            where: and(
                eq(schema.teams.organizationId, orgId),
                or(eq(schema.teams.slug, teamPart), eq(schema.teams.name, teamPart))
            ),
        });

        if (!team) continue;

        const members = await db.query.teamMembers.findMany({
            where: eq(schema.teamMembers.teamId, team.id),
            with: { user: true },
        });

        for (const member of members) {
            if (member.user?.username) {
                results.add(member.user.username);
            }
        }
    }

    return results;
}

/**
 * Validate CODEOWNERS content and return any errors
 */
export function validateCodeOwners(content: string): {
    valid: boolean;
    errors: { line: number; message: string }[];
    warnings: { line: number; message: string }[];
} {
    const errors: { line: number; message: string }[] = [];
    const warnings: { line: number; message: string }[] = [];
    const lines = content.split("\n");

    for (let i = 0; i < lines.length; i++) {
        const line = lines[i].trim();
        const lineNum = i + 1;

        // Skip empty lines and comments
        if (!line || line.startsWith("#")) {
            continue;
        }

        const parts = line.split(/\s+/).filter(Boolean);

        if (parts.length < 2) {
            errors.push({
                line: lineNum,
                message: "Invalid syntax: needs pattern and at least one owner",
            });
            continue;
        }

        const pattern = parts[0];
        const owners = parts.slice(1);

        // Check for invalid patterns
        try {
            minimatch.makeRe(pattern);
        } catch {
            errors.push({
                line: lineNum,
                message: `Invalid glob pattern: ${pattern}`,
            });
        }

        // Check owners format
        for (const owner of owners) {
            if (!owner.startsWith("@") && !owner.includes("/")) {
                warnings.push({
                    line: lineNum,
                    message: `Owner "${owner}" should start with @ or be a team (org/team)`,
                });
            }
        }
    }

    return {
        valid: errors.length === 0,
        errors,
        warnings,
    };
}