import { batch, first, stmt } from "./db";
import { randomId } from "./crypto";
import { badRequest, conflict, forbidden } from "./errors";
import { emitNotification, emitWebhook, recordActivity } from "./events/emit";
import { compareCommits } from "./git/history";
import { branchSha, listRefs } from "./git/refs";
import { withStore } from "./git/store";
import { deleteRef } from "./git/writer";
import { allocateIssueNumber, indexIssue } from "./issues";
import { pullUrl } from "./pull-access";
import { afterPullChange } from "./pull-events";
import { addIssueEvent, MergeError, mergeGate, performMerge } from "./pulls";
import type { PullRow } from "./pulls";
import type { MergeMethod, Permission, RepoRow, Viewer } from "~/lib/types";
/** The repo owned by `ownerName` within `repo`'s fork network (itself, a fork of it, or its parent). */
export async function findForkHead(repo: RepoRow, ownerName: string): Promise<RepoRow | null> {
return first<RepoRow>(
`SELECT r.*, a.name AS owner_name FROM repos r JOIN accounts a ON a.id = r.owner_id
WHERE a.name = ? AND (r.fork_of_id = ? OR r.id = ? OR r.id = ?)`,
ownerName,
repo.id,
repo.id,
repo.fork_of_id ?? "",
);
}
/** Opens a pull request once the caller has resolved and authorized the head repository. */
export async function openPull(input: {
repo: RepoRow;
headRepo: RepoRow;
headRef: string;
base: string;
title: string;
body: string;
draft: boolean;
viewer: Viewer;
}): Promise<number> {
const { repo, headRepo, headRef, viewer } = input;
const [baseRefs, headRefs] = await Promise.all([listRefs(repo), listRefs(headRepo)]);