♻️ refactor: update merge.ts

swadhinbiswas committed 3 months ago
9693b6f
1 parent e541e04
commit 9693b6f
1 file changed
+18 -5
▼src/pages/api/repos/[owner]/[repo]/pulls/[number]/merge.ts
+18-5
8
8
9
9
import { withErrorHandler } from "@/lib/errors";
10
10
import { logger } from "@/lib/logger";
11
-import { unauthorized, badRequest, notFound, serverError, forbidden, success, conflict } from "@/lib/api";
11
+import { unauthorized, badRequest, notFound, forbidden, success, conflict } from "@/lib/api";
12
12
import { evaluateGates } from "@/lib/ci-gates";
13
13
import { closeLinkedIssuesOnMerge } from "@/lib/pr-issue-linking";
14
14
import { checkPathPermissions } from "@/lib/path-scoping";
69
69
return badRequest("Pull request is not open");
70
70
}
71
71
72
+ if (pr.stateId) {
73
+ const customState = await db.query.prStateDefinitions.findFirst({
74
+ where: eq(schema.prStateDefinitions.id, pr.stateId)
75
+ });
76
+ if (customState && !customState.allowMerge) {
77
+ return conflict(`Merge blocked: PR is in state '${customState.displayName}', which does not allow merging.`);
78
+ }
79
+ }
80
+
72
81
const repoPath = await resolveRepoPath(repo.diskPath);
73
82
const { diffs } = await compareBranches(repoPath, pr.baseBranch, pr.headBranch);
74
83
const changedFiles = diffs.map((diff) => diff.file).filter(Boolean);
81
90
}
82
91
83
92
// CodeOwner validation
84
- const { checkCodeOwnerApprovals } = await import("@/lib/codeowners-enforcement");
85
- const codeOwnerResult = await checkCodeOwnerApprovals(repo.id, pr.id, changedFiles);
86
- if (!codeOwnerResult.canMerge) {
87
- return conflict(`Merge blocked: Missing CodeOwner approvals for ${codeOwnerResult.missingApprovals.length} files.`);
93
+ const { checkCodeOwnerApprovals, isCodeOwnerEnforced } = await import("@/lib/codeowners-enforcement");
94
+ const enforced = await isCodeOwnerEnforced(repo.id, pr.baseBranch, pr.stateId);
95
+
96
+ if (enforced) {
97
+ const codeOwnerResult = await checkCodeOwnerApprovals(repo.id, pr.id, changedFiles);
98
+ if (!codeOwnerResult.canMerge) {
99
+ return conflict(`Merge blocked: Missing CodeOwner approvals for ${codeOwnerResult.missingApprovals.length} files.`);
100
+ }
88
101
}
89
102
90
103
// Verify CI Gates (Strict Merge Checks)
91
104