import type { GiteaConfig, GiteaMirrorRepository } from "./platform-infra-gitea-config"; export interface PacBootstrapRepositoryIdentity { readonly id: string; readonly owner: string; readonly repo: string; readonly cloneUrl: string; } export function resolvePacBootstrapMirrorRepository( gitea: GiteaConfig, targetId: string, repository: PacBootstrapRepositoryIdentity, ): GiteaMirrorRepository { const candidates = gitea.sourceAuthority.repositories.filter((item) => ( item.targetId.toLowerCase() === targetId.toLowerCase() && item.gitea.owner === repository.owner && item.gitea.name === repository.repo && repositoryUrlIdentity(item.gitea.readUrl) === repositoryUrlIdentity(repository.cloneUrl) )); if (candidates.length !== 1) { const disposition = candidates.length === 0 ? "no exact match" : `${candidates.length} exact matches`; throw new Error( `cannot resolve the YAML-owned Gitea bootstrap repository for PaC repository ${repository.id} on ${targetId}: ${disposition}; ` + "match targetId, gitea owner/name, and read URL in config/platform-infra/gitea.yaml", ); } return candidates[0]; } function repositoryUrlIdentity(value: string): string { const parsed = new URL(value); const path = parsed.pathname.replace(/\/+$/u, ""); return `${parsed.protocol}//${parsed.host}${path}`; }