476 lines · 19.2 KB
import { env, waitUntil } from "cloudflare:workers";
import { dropPullRefsForIssue } from "./git/pull-bridge";
import { repoDb, stmt } from "./scoped-db";
import { locate } from "./scoped-db/locators";
import { pullBaseRepoIds } from "./pull-scope";
import { randomId } from "./crypto";
import { adjustOpenCounter } from "./issue-counters";
import { hasPermission, loadRepoById, repoPermission } from "./repos";
import { loadProtections, protectionFor, bypassesProtection } from "./protection";
import { listCommitStatuses, requiredCheckResults } from "./statuses";
import { authenticatedRemote, mintRepoToken, revokeRepoToken, revokeRepoTokens } from "./git/artifacts";
import { ZERO_SHA, withStore } from "./git/store";
import { isAncestor } from "./git/history";
import { triggerPush } from "./events/push-trigger";
import { inWorkerMergeability, landInWorker, loadPullRepos } from "./pull-merge";
import type { Landed } from "./pull-merge";
import { RebaseConflictError, rebaseInWorker } from "./pull-rebase";
import { actorViewer, afterMerged, codeOwnerBlockers, kickMergeAutomation } from "./merge-automation";
import { ensurePullHeadAccessible } from "./pull-guard";
import type { MergeMethod, Permission, RepoRow } from "~/lib/types";
import { issuesChanged } from "./repo-state";

const MERGE_TOKEN_TTL_SECONDS = 1800;
const MAX_EXTERNAL_MERGE_CHECKS = 10;
const EXIT_CONFLICT = 2;
const EXIT_HEAD_MOVED = 3;
const EXIT_BASE_MOVED = 6;

export type PullRow = {
  issue_id: string;
  repo_id: string;
  number: number;
  title: string;
  body: string;
  state: "open" | "closed";
  author_id: string;
  locked: number;
  head_repo_id: string;
  head_ref: string;
  head_sha: string;