import { Option, Schema } from "effect" import { createStore } from "solid-js/store" export type PullRequestStatus = "open" | "draft" | "merged" | "closed" export type PullRequestInfo = { readonly status: PullRequestStatus readonly title?: string } export type PullRequestRef = { readonly owner: string readonly repo: string readonly number: number readonly key: string } type CacheEntry = { readonly fetchedAt: number readonly info?: PullRequestInfo } const TTL_MS = 5 * 60 * 1000 const OWNER_PATTERN = /^[A-Za-z0-9](?:[A-Za-z0-9-]{0,37}[A-Za-z0-9])?$/ const REPO_PATTERN = /^[A-Za-z0-9._-]{1,100}$/ const NUMBER_PATTERN = /^[1-9]\d{0,9}$/ const RESERVED_OWNERS = [ "about", "codespaces", "collections", "customer-stories", "discussions", "enterprise", "events", "explore", "features", "issues", "login", "marketplace", "new", "notifications", "orgs", "organizations", "pricing", "pulls", "readme", "search", "security", "settings", "signup", "site", "sponsors", "team", "topics", "trending", "users", ] const PullRequestPayload = Schema.Struct({ title: Schema.optional(Schema.String), state: Schema.optional(Schema.String), merged: Schema.optional(Schema.Boolean), draft: Schema.optional(Schema.Boolean), }) const decodePayload = Schema.decodeUnknownOption(Schema.fromJsonString(PullRequestPayload)) /** Strictly parses a GitHub PR URL into owner, repo, and number; never forwards query strings or credentials. */ export function parseGitHubPR(url: string): PullRequestRef | undefined { if (!URL.canParse(url)) return undefined const parsed = new URL(url) if ((parsed.protocol !== "http:" && parsed.protocol !== "https:") || parsed.username || parsed.password) { return undefined } const host = parsed.hostname.toLowerCase() if (host !== "github.com" && host !== "www.github.com") return undefined const segments = parsed.pathname.split("/").filter(Boolean) if (segments.length < 4 || segments[2] !== "pull") return undefined const owner = segments[0] const repo = segments[1] const rawNumber = segments[3] if ( !OWNER_PATTERN.test(owner) || RESERVED_OWNERS.includes(owner.toLowerCase()) || !REPO_PATTERN.test(repo) || repo === "." || repo === ".." || !NUMBER_PATTERN.test(rawNumber) ) { return undefined } const number = Number(rawNumber) return { owner, repo, number, key: `${owner.toLowerCase()}/${repo.toLowerCase()}#${number}`, } } /** Decodes GitHub's `/repos/{owner}/{repo}/pulls/{number}` JSON response body into status and title. */ export function decodeGitHubPR(body: string): PullRequestInfo | undefined { const payload = Option.getOrUndefined(decodePayload(body)) if (!payload) return undefined const status: PullRequestStatus | undefined = payload.merged ? "merged" : payload.state === "closed" ? "closed" : payload.draft ? "draft" : payload.state === "open" ? "open" : undefined if (!status) return undefined const title = payload.title?.trim() return title ? { status, title } : { status } } export type GitHubPRFetcher = (ref: PullRequestRef) => Promise const defaultFetcher: GitHubPRFetcher = async (ref) => { const response = await fetch(`https://api.github.com/repos/${ref.owner}/${ref.repo}/pulls/${ref.number}`, { headers: { Accept: "application/vnd.github+json" }, credentials: "omit", }).catch(() => undefined) if (!response?.ok) return undefined const body = await response.text().catch(() => undefined) return body ? decodeGitHubPR(body) : undefined } /** Reactive, TTL-cached GitHub PR metadata lookup with in-flight deduplication and negative caching for private repos. */ export function createGitHubPRCache(fetcher: GitHubPRFetcher = defaultFetcher, now: () => number = () => Date.now()) { const [entries, setEntries] = createStore>({}) const inFlight = new Map>() const read = (url: string): PullRequestInfo | undefined => { const ref = parseGitHubPR(url) return ref ? entries[ref.key]?.info : undefined } const load = (url: string) => { const ref = parseGitHubPR(url) if (!ref) return const existing = entries[ref.key] if (existing && now() - existing.fetchedAt < TTL_MS) return if (inFlight.has(ref.key)) return const task = fetcher(ref) .catch(() => undefined) .then((info) => { inFlight.delete(ref.key) setEntries(ref.key, { fetchedAt: now(), info }) }) inFlight.set(ref.key, task) } return { read, load } }