/** * Issue numbers a pull request's title or body points at: `#123`, * `owner/repo#123`, and `https://github.com/owner/repo/issues/123` forms for * the same repo. Pure; used to keep issues an open PR already covers out of a * sweep, so two agents do not work the same issue. */ export function issueRefsInText(text: string, repoSlug: string) { const refs = new Set() const source = String(text ?? '') const repo = repoSlug.toLowerCase() const escaped = repo.replace(/[.*+?^${}()|[\]\\]/g, '\\$&') const patterns = [ /(?:^|[^\w/#&])#(\d{1,7})\b/g, new RegExp(`(?:^|[^\\w/])${escaped}#(\\d{1,7})\\b`, 'gi'), new RegExp(`github\\.com/${escaped}/(?:issues|pull)/(\\d{1,7})\\b`, 'gi'), ] for (const pattern of patterns) { for (const match of source.matchAll(pattern)) { const number = Number(match[1]) if (Number.isInteger(number) && number > 0) refs.add(number) } } return refs }