Something went wrong. Try again.
Monorepo for Tangled tangled.org
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970import type { BranchEntry } from "./repo";
export type PullSource = "patch" | "branch" | "fork";
const SOURCES: PullSource[] = ["patch", "branch", "fork"];
export const parseSource = (raw: string | null | undefined): PullSource | null => { const value = raw?.toLowerCase() as PullSource | undefined; return value && SOURCES.includes(value) ? value : null;};
const branchTime = (branch: BranchEntry): number => { const when = branch.commit?.Committer?.When; return when ? Date.parse(when) : NaN;};
/** newest commit first; branches with no commit sort last, order otherwise kept */export const sortBranchesByRecency = (branches: readonly BranchEntry[]): BranchEntry[] => branches .map((branch, index) => ({ branch, index, time: branchTime(branch) })) .sort((a, b) => { const aMissing = Number.isNaN(a.time); const bMissing = Number.isNaN(b.time); if (aMissing || bMissing) return aMissing === bMissing ? a.index - b.index : aMissing ? 1 : -1; return b.time - a.time || a.index - b.index; }) .map((entry) => entry.branch);
/** you can't open a PR from the default branch, so it never appears as a source */export const sourceBranchChoices = (branches: readonly BranchEntry[]): BranchEntry[] => sortBranchesByRecency(branches.filter((branch) => !branch.is_default));
const named = (branches: readonly BranchEntry[], name: string): boolean => branches.some((branch) => branch.reference.name === name);
export const defaultTargetBranch = (branches: readonly BranchEntry[], current: string): string => { if (current && named(branches, current)) return current; return branches.find((branch) => branch.is_default)?.reference.name ?? "";};
export const defaultSourceBranch = ( source: PullSource, current: string, branchChoices: readonly BranchEntry[], forkBranches: readonly BranchEntry[]): string => { if (source === "patch") return current; const candidates = source === "fork" ? forkBranches : branchChoices; if (current && named(candidates, current)) return current; return candidates[0]?.reference.name ?? "";};
export interface ComposeSelection { source: PullSource; sourceBranch: string; targetBranch: string; /** repo did of the chosen fork */ fork: string;}
export const composeQuery = (selection: ComposeSelection): string => { const query = new URLSearchParams(); if (selection.source !== "branch") query.set("source", selection.source); if (selection.sourceBranch) query.set("sourceBranch", selection.sourceBranch); if (selection.targetBranch) query.set("targetBranch", selection.targetBranch); if (selection.source === "fork" && selection.fork) query.set("fork", selection.fork); return query.toString();};