verify-public-repository-links.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /** Reject tracked files that reference an unavailable legacy repository. */
  2. import { execFileSync } from 'node:child_process'
  3. import { existsSync, lstatSync, readFileSync, readlinkSync } from 'node:fs'
  4. import { resolve } from 'node:path'
  5. import { pathToFileURL } from 'node:url'
  6. const root = resolve(import.meta.dirname, '..')
  7. const unavailableOwner = ['deepseek', 'ai'].join('-')
  8. const unavailableRepositoryName = ['deepseek', 'harness', 'sdk'].join('-')
  9. const unavailableRepository = `${unavailableOwner}/${unavailableRepositoryName}`
  10. const archivedAgentNotePrefix = '.agents/notes/archived/'
  11. const namedReferenceCharacters: Readonly<Record<string, string>> = {
  12. hyphen: '-',
  13. sol: '/',
  14. }
  15. /** Normalize source spellings that render or decode to repository separators. */
  16. function canonicalReferenceText(source: string): string {
  17. return source
  18. .replaceAll('\\/', '/')
  19. .replace(/\\u(0023|002d|002f)/gi, (_match, code: string) => String.fromCodePoint(Number.parseInt(code, 16)))
  20. .replace(/%(23|2d|2f)/gi, (_match, code: string) => String.fromCodePoint(Number.parseInt(code, 16)))
  21. .replace(/&#(?:(\d+)|x([\da-f]+));/gi, (entity, decimal: string | undefined, hexadecimal: string | undefined) => {
  22. const code = Number.parseInt(decimal ?? hexadecimal ?? '', decimal === undefined ? 16 : 10)
  23. return code === 35 || code === 45 || code === 47 ? String.fromCodePoint(code) : entity
  24. })
  25. .replace(/&(hyphen|num|sol);/gi, (entity, name: string) => namedReferenceCharacters[name.toLowerCase()] ?? entity)
  26. .normalize('NFKC')
  27. .toLowerCase()
  28. }
  29. /** One tracked reference to the unavailable repository. */
  30. export interface UnavailableRepositoryReference {
  31. /** Repository-relative file path. */
  32. file: string
  33. /** One-based source line. */
  34. line: number
  35. }
  36. /**
  37. * Locate unavailable-repository references in one active text file.
  38. * @param file - Repository-relative path used in diagnostics.
  39. * @param source - Text to inspect.
  40. * @returns every matching source line, excluding frozen archived Agent Notes.
  41. */
  42. export function findUnavailableRepositoryReferences(file: string, source: string): UnavailableRepositoryReference[] {
  43. if (file.startsWith(archivedAgentNotePrefix)) return []
  44. const references: UnavailableRepositoryReference[] = []
  45. for (const [index, line] of source.split('\n').entries()) {
  46. const canonicalLine = canonicalReferenceText(line)
  47. if (canonicalLine.includes(unavailableRepository)) references.push({ file, line: index + 1 })
  48. }
  49. return references
  50. }
  51. function trackedFiles(repoRoot: string): string[] {
  52. return execFileSync('git', ['ls-files', '-z'], { cwd: repoRoot, encoding: 'utf8' })
  53. .split('\0')
  54. .filter(file => file !== '')
  55. }
  56. function scanRepository(repoRoot: string): UnavailableRepositoryReference[] {
  57. const references: UnavailableRepositoryReference[] = []
  58. for (const file of trackedFiles(repoRoot)) {
  59. const path = resolve(repoRoot, file)
  60. if (!existsSync(path)) continue
  61. const stat = lstatSync(path)
  62. if (!stat.isFile() && !stat.isSymbolicLink()) continue
  63. const source = stat.isSymbolicLink() ? readlinkSync(path) : readFileSync(path, 'utf8')
  64. if (source.includes('\0')) continue
  65. references.push(...findUnavailableRepositoryReferences(file, source))
  66. }
  67. return references
  68. }
  69. const invokedPath = process.argv[1]
  70. const isMain = invokedPath !== undefined && import.meta.url === pathToFileURL(resolve(invokedPath)).href
  71. if (isMain) {
  72. const references = scanRepository(root)
  73. if (references.length === 0) {
  74. console.log('verify-public-repository-links: tracked files reference no unavailable repository.')
  75. } else {
  76. console.error('verify-public-repository-links: unavailable repository references found:')
  77. for (const reference of references) console.error(` ${reference.file}:${String(reference.line)}`)
  78. process.exitCode = 1
  79. }
  80. }