Skip to content

Commit 634c368

Browse files
committed
feat: add well-known files classifier for repo enricher
Signed-off-by: Mouad BANI <mouad-mb@outlook.com>
1 parent 8d3354a commit 634c368

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
export type WellKnownFileType =
2+
| 'security'
3+
| 'contributing'
4+
| 'governance'
5+
| 'maintainers'
6+
| 'code_of_conduct'
7+
| 'readme'
8+
9+
export type WellKnownDirectory = 'root' | '.github' | 'docs'
10+
11+
export interface TreeEntryNode {
12+
name: string
13+
type: string
14+
oid: string
15+
}
16+
17+
export interface WellKnownFileEntry {
18+
fileType: WellKnownFileType
19+
directory: WellKnownDirectory
20+
path: string
21+
blobOid: string
22+
}
23+
24+
export interface RepoTrees {
25+
root: TreeEntryNode[] | null
26+
github: TreeEntryNode[] | null
27+
docs: TreeEntryNode[] | null
28+
}
29+
30+
const STEM_TO_TYPE: Record<string, WellKnownFileType> = {
31+
README: 'readme',
32+
SECURITY: 'security',
33+
CONTRIBUTING: 'contributing',
34+
GOVERNANCE: 'governance',
35+
MAINTAINERS: 'maintainers',
36+
CODEOWNERS: 'maintainers',
37+
CODE_OF_CONDUCT: 'code_of_conduct',
38+
}
39+
40+
function stemOf(filename: string): string {
41+
return filename
42+
.replace(/\.[^.]+$/, '')
43+
.toUpperCase()
44+
.replace(/-/g, '_')
45+
}
46+
47+
const DIRECTORIES: Array<{ key: keyof RepoTrees; directory: WellKnownDirectory; prefix: string }> =
48+
[
49+
{ key: 'root', directory: 'root', prefix: '' },
50+
{ key: 'github', directory: '.github', prefix: '.github/' },
51+
{ key: 'docs', directory: 'docs', prefix: 'docs/' },
52+
]
53+
54+
export function classifyWellKnownFiles(trees: RepoTrees): WellKnownFileEntry[] {
55+
const result: WellKnownFileEntry[] = []
56+
for (const { key, directory, prefix } of DIRECTORIES) {
57+
for (const entry of trees[key] ?? []) {
58+
if (entry.type !== 'blob') continue
59+
const fileType = STEM_TO_TYPE[stemOf(entry.name)]
60+
if (!fileType) continue
61+
result.push({ fileType, directory, path: prefix + entry.name, blobOid: entry.oid })
62+
}
63+
}
64+
return result
65+
}
66+
67+
// Mirrors the retired REST probe so repos.security_file_enabled semantics don't shift
68+
export function deriveSecurityFileEnabled(trees: RepoTrees): boolean {
69+
return [...(trees.root ?? []), ...(trees.github ?? [])].some(
70+
(entry) => entry.type === 'blob' && entry.name.toUpperCase() === 'SECURITY.MD',
71+
)
72+
}

0 commit comments

Comments
 (0)