/ opencodehub / src / lib / auto-merge.ts
swadhinbiswas Complete auto-merge rule introspection and PR policy UI · 2/19/2026
38b7cb9
249 lines · 7.1 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
/**
 * Auto-merge Library
 * Handles automatic merging of PRs when conditions are met
 */

import { getDatabase, schema } from "@/db";
import { eq } from "drizzle-orm";
import { logger } from "./logger";
import { addToMergeQueue } from "./merge-queue";
import {
    evaluateAutoMergeRulesDetailed,
    type AutoMergeRuleEvaluation,
} from "./auto-merge-rules";

export interface AutoMergeOptions {
    mergeMethod: "merge" | "squash" | "rebase";
    deleteHeadBranch?: boolean;
    commitTitle?: string;
    commitMessage?: string;
}

export interface AutoMergeStatus {
    enabled: boolean;
    eligibleToMerge: boolean;
    blockers: string[];
    enabledBy?: string;
    enabledAt?: Date;
    policySummary?: {
        totalRules: number;
        matchedRules: number;
        passedRules: number;
        failedRules: number;
        unmatchedRules: number;
    };
    ruleEvaluations?: AutoMergeRuleEvaluation[];
}

/**
 * Enable auto-merge for a PR
 */
export async function enableAutoMerge(
    prId: string,
    userId: string,
    options: AutoMergeOptions
): Promise<{ success: boolean; error?: string }> {
    const db = getDatabase();

    const pr = await db.query.pullRequests.findFirst({
        where: eq(schema.pullRequests.id, prId),
    });

    if (!pr) {
        return { success: false, error: "Pull request not found" };
    }

    if (pr.state !== "open" || pr.isDraft) {
        return { success: false, error: "Cannot enable auto-merge on closed or draft PRs" };
    }

    try {
        // @ts-expect-error - Drizzle multi-db union type issue
        await db.update(schema.pullRequests)
            .set({
                allowAutoMerge: true,
                autoMergeMethod: options.mergeMethod,
                autoMergeEnabledById: userId,
                autoMergeEnabledAt: new Date(),
                updatedAt: new Date(),
            })
            .where(eq(schema.pullRequests.id, prId));

        logger.info({ prId, userId, method: options.mergeMethod }, "Auto-merge enabled");

        // Check if already eligible
        await processAutoMerge(prId);

        return { success: true };
    } catch (error) {
        const message = error instanceof Error ? error.message : "Unknown error";
        return { success: false, error: message };
    }
}

/**
 * Disable auto-merge for a PR
 */
export async function disableAutoMerge(prId: string): Promise<boolean> {
    const db = getDatabase();

    try {
        // @ts-expect-error - Drizzle multi-db union type issue
        await db.update(schema.pullRequests)
            .set({
                allowAutoMerge: false,
                autoMergeMethod: null,
                autoMergeEnabledById: null,
                autoMergeEnabledAt: null,
                updatedAt: new Date(),
            })
            .where(eq(schema.pullRequests.id, prId));

        logger.info({ prId }, "Auto-merge disabled");
        return true;
    } catch (error) {
        logger.error({ prId, error }, "Failed to disable auto-merge");
        return false;
    }
}

/**
 * Check if a PR is eligible for auto-merge
 */
export async function checkAutoMergeEligibility(prId: string): Promise<AutoMergeStatus> {
    const db = getDatabase();

    const pr = await db.query.pullRequests.findFirst({
        where: eq(schema.pullRequests.id, prId),
        with: {
            checks: true,
            reviews: true,
        },
    });

    if (!pr) {
        return { enabled: false, eligibleToMerge: false, blockers: ["PR not found"] };
    }

    if (!pr.allowAutoMerge) {
        return { enabled: false, eligibleToMerge: false, blockers: [] };
    }

    const blockers: string[] = [];

    // Check PR state
    if (pr.state !== "open") {
        blockers.push("PR is not open");
    }

    if (pr.isDraft) {
        blockers.push("PR is a draft");
    }

    // Check mergeable status
    if (pr.mergeable === false) {
        blockers.push("PR has merge conflicts");
    }

    if (pr.mergeableState === "blocked") {
        blockers.push("PR is blocked by branch protection");
    }

    // Check CI status
    const checks = pr.checks || [];
    const failingChecks = checks.filter(
        c => c.status === "completed" && c.conclusion !== "success" && c.conclusion !== "neutral"
    );
    const pendingChecks = checks.filter(c => c.status !== "completed");

    if (failingChecks.length > 0) {
        blockers.push(`${failingChecks.length} failing check(s)`);
    }

    if (pendingChecks.length > 0) {
        blockers.push(`${pendingChecks.length} pending check(s)`);
    }

    // Check reviews
    const reviews = pr.reviews || [];
    const changesRequested = reviews.filter(r => r.state === "changes_requested");

    if (changesRequested.length > 0) {
        blockers.push("Changes requested by reviewer(s)");
    }

    const ruleEvaluation = await evaluateAutoMergeRulesDetailed(prId);
    blockers.push(...ruleEvaluation.blockers);

    // Get enabler info
    let enabledBy: string | undefined;
    if (pr.autoMergeEnabledById) {
        const user = await db.query.users.findFirst({
            where: eq(schema.users.id, pr.autoMergeEnabledById),
            columns: { username: true },
        });
        enabledBy = user?.username;
    }

    return {
        enabled: true,
        eligibleToMerge: blockers.length === 0,
        blockers,
        enabledBy,
        enabledAt: pr.autoMergeEnabledAt || undefined,
        policySummary: {
            totalRules: ruleEvaluation.evaluations.length,
            matchedRules: ruleEvaluation.evaluations.filter((evaluation) => evaluation.matched).length,
            passedRules: ruleEvaluation.evaluations.filter((evaluation) => evaluation.passed).length,
            failedRules: ruleEvaluation.evaluations.filter(
                (evaluation) => evaluation.matched && !evaluation.passed
            ).length,
            unmatchedRules: ruleEvaluation.evaluations.filter((evaluation) => !evaluation.matched).length,
        },
        ruleEvaluations: ruleEvaluation.evaluations,
    };
}

/**
 * Process auto-merge for a PR if eligible
 */
export async function processAutoMerge(prId: string): Promise<boolean> {
    const status = await checkAutoMergeEligibility(prId);

    if (!status.enabled || !status.eligibleToMerge) {
        return false;
    }

    const db = getDatabase();
    const pr = await db.query.pullRequests.findFirst({
        where: eq(schema.pullRequests.id, prId),
    });

    if (!pr || !pr.autoMergeEnabledById) {
        return false;
    }

    try {
        // Add to merge queue for processing
        await addToMergeQueue({
            repositoryId: pr.repositoryId,
            pullRequestId: prId,
            addedById: pr.autoMergeEnabledById,
            mergeMethod: (pr.autoMergeMethod as "merge" | "squash" | "rebase") || "merge",
        });

        logger.info({ prId }, "PR added to merge queue via auto-merge");
        return true;
    } catch (error) {
        logger.error({ prId, error }, "Failed to process auto-merge");
        return false;
    }
}

/**
 * Get auto-merge status for display
 */
export async function getAutoMergeStatus(prId: string): Promise<AutoMergeStatus> {
    return checkAutoMergeEligibility(prId);
}