Skip to content
← Public packages

@kody/kody-factory

Forkable multi-project software factory: signals, priority queue, project UI, sweep job. Agent runners and Sentry reach it by events.

FollowFork

Files

pr-refs.ts

24 lines · 915 B · TypeScript
/**
 * 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<number>()
	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
}