@ankhorage/doctor 0.14.36 → 0.15.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/README.md +18 -12
  3. package/dist/analysis.d.ts +13 -0
  4. package/dist/analysis.d.ts.map +1 -1
  5. package/dist/analysis.js.map +1 -1
  6. package/dist/commandArguments.d.ts +11 -0
  7. package/dist/commandArguments.d.ts.map +1 -0
  8. package/dist/commandArguments.js +18 -0
  9. package/dist/commandArguments.js.map +1 -0
  10. package/dist/commandContext.d.ts.map +1 -1
  11. package/dist/commandContext.js +2 -1
  12. package/dist/commandContext.js.map +1 -1
  13. package/dist/commands.d.ts +1 -0
  14. package/dist/commands.d.ts.map +1 -1
  15. package/dist/commands.js +13 -19
  16. package/dist/commands.js.map +1 -1
  17. package/dist/dependencyPolicyAnalysis.d.ts.map +1 -1
  18. package/dist/dependencyPolicyAnalysis.js +8 -4
  19. package/dist/dependencyPolicyAnalysis.js.map +1 -1
  20. package/dist/diagnostics.d.ts +2 -2
  21. package/dist/diagnostics.d.ts.map +1 -1
  22. package/dist/diagnostics.js.map +1 -1
  23. package/dist/index.d.ts +1 -1
  24. package/dist/index.d.ts.map +1 -1
  25. package/dist/index.js.map +1 -1
  26. package/dist/packageMetadata.d.ts.map +1 -1
  27. package/dist/packageMetadata.js +2 -1
  28. package/dist/packageMetadata.js.map +1 -1
  29. package/dist/repositoryMetadataAnalysis.d.ts +4 -0
  30. package/dist/repositoryMetadataAnalysis.d.ts.map +1 -0
  31. package/dist/repositoryMetadataAnalysis.js +203 -0
  32. package/dist/repositoryMetadataAnalysis.js.map +1 -0
  33. package/dist/repositoryMetadataCommandOptions.d.ts +11 -0
  34. package/dist/repositoryMetadataCommandOptions.d.ts.map +1 -0
  35. package/dist/repositoryMetadataCommandOptions.js +38 -0
  36. package/dist/repositoryMetadataCommandOptions.js.map +1 -0
  37. package/dist/utils/getRepositoryRule.d.ts.map +1 -1
  38. package/dist/utils/getRepositoryRule.js +1 -1
  39. package/dist/utils/getRepositoryRule.js.map +1 -1
  40. package/package.json +9 -9
  41. package/src/analysis.ts +14 -0
  42. package/src/commandArguments.ts +31 -0
  43. package/src/commandContext.ts +5 -1
  44. package/src/commands.ts +14 -27
  45. package/src/dependencyPolicyAnalysis.ts +8 -3
  46. package/src/diagnostics.ts +9 -0
  47. package/src/index.ts +1 -0
  48. package/src/packageMetadata.ts +7 -1
  49. package/src/repositoryMetadataAnalysis.ts +340 -0
  50. package/src/repositoryMetadataCommandOptions.ts +49 -0
  51. package/src/utils/getRepositoryRule.ts +3 -1
@@ -0,0 +1,340 @@
1
+ import { promises as fs } from 'node:fs';
2
+ import path from 'node:path';
3
+
4
+ import type { RepositoryMetadataFact } from '@ankhorage/rules-repository';
5
+ import {
6
+ decodeRepositoryMetadata,
7
+ evaluateRepositoryMetadataPreflight,
8
+ REPOSITORY_RULE_METADATA,
9
+ } from '@ankhorage/rules-repository';
10
+
11
+ import type {
12
+ DoctorAnalysisRequest,
13
+ DoctorAnalysisResult,
14
+ DoctorRepositoryMetadataOptions,
15
+ } from './analysis.js';
16
+ import type { DoctorDiagnostic, DoctorDiagnosticSeverity, DoctorRuleId } from './diagnostics.js';
17
+
18
+ /*** Acquire repository metadata at the filesystem boundary and evaluate it through published Rules. */
19
+ export async function analyzeRepositoryMetadata(
20
+ result: DoctorAnalysisResult,
21
+ request: DoctorAnalysisRequest,
22
+ ): Promise<DoctorAnalysisResult> {
23
+ const metadata = await acquireRepositoryMetadataAsync(
24
+ result.targetPath,
25
+ request.repositoryMetadata,
26
+ );
27
+ const diagnostics = [
28
+ ...result.diagnostics,
29
+ ...metadata.diagnostics,
30
+ ...(metadata.evaluate
31
+ ? evaluateMetadataPreflight(
32
+ metadata.fact,
33
+ result.targetPath,
34
+ request.repositoryMetadata?.enforce,
35
+ )
36
+ : []),
37
+ ];
38
+
39
+ return { ...result, diagnostics };
40
+ }
41
+
42
+ interface RepositoryMetadataAcquisition {
43
+ readonly diagnostics: readonly DoctorDiagnostic[];
44
+ readonly evaluate: boolean;
45
+ readonly fact: RepositoryMetadataFact;
46
+ }
47
+
48
+ /*** Acquire only canonical JSON metadata and never execute target repository source code. */
49
+ async function acquireRepositoryMetadataAsync(
50
+ targetPath: string,
51
+ options: DoctorRepositoryMetadataOptions | undefined,
52
+ ): Promise<RepositoryMetadataAcquisition> {
53
+ const metadataOptions = options ?? {};
54
+ const { applicabilityDiagnostic } = metadataOptions;
55
+ if (applicabilityDiagnostic !== undefined) {
56
+ return createUnconfirmedApplicabilityAcquisition(
57
+ targetPath,
58
+ applicabilityDiagnostic,
59
+ metadataOptions.enforce,
60
+ );
61
+ }
62
+ const managed = metadataOptions.managed === true;
63
+ const eligibility = metadataOptions.eligibility ?? (managed ? 'applicable' : 'third-party');
64
+ const profile = metadataOptions.profile ?? (await detectMetadataProfileAsync(targetPath));
65
+ const fact = { eligibility, managed, profile } as const satisfies RepositoryMetadataFact;
66
+ const canonicalPath = canonicalMetadataPath(profile);
67
+ const metadataPath = path.join(targetPath, canonicalPath);
68
+
69
+ if (eligibility !== 'applicable' || !managed) {
70
+ return { diagnostics: [], evaluate: true, fact };
71
+ }
72
+
73
+ const canonicalSource = await readMetadataSourceAsync(metadataPath);
74
+ if (canonicalSource.kind !== 'absent')
75
+ return acquireDiscoveredMetadata(
76
+ canonicalSource,
77
+ canonicalPath,
78
+ metadataPath,
79
+ fact,
80
+ metadataOptions.enforce,
81
+ );
82
+
83
+ const alternatePath = canonicalMetadataPath(oppositeProfile(profile));
84
+ const alternateMetadataPath = path.join(targetPath, alternatePath);
85
+ const alternateSource = await readMetadataSourceAsync(alternateMetadataPath);
86
+ return alternateSource.kind === 'absent'
87
+ ? { diagnostics: [], evaluate: true, fact }
88
+ : acquireDiscoveredMetadata(
89
+ alternateSource,
90
+ alternatePath,
91
+ alternateMetadataPath,
92
+ fact,
93
+ metadataOptions.enforce,
94
+ );
95
+ }
96
+
97
+ /*** Surface an incomplete managed-host identity instead of guessing target applicability. */
98
+ function createUnconfirmedApplicabilityAcquisition(
99
+ targetPath: string,
100
+ message: string,
101
+ enforce: boolean | undefined,
102
+ ): RepositoryMetadataAcquisition {
103
+ return {
104
+ diagnostics: [
105
+ createMetadataDiagnostic({
106
+ code: 'repository-metadata-applicability-unknown',
107
+ message,
108
+ path: targetPath,
109
+ ruleId: 'repository.metadata.eligibility.confirmed',
110
+ severity: metadataSeverity(enforce),
111
+ }),
112
+ ],
113
+ evaluate: false,
114
+ fact: { eligibility: 'third-party', managed: false, profile: 'non-source' },
115
+ };
116
+ }
117
+
118
+ type MetadataSource =
119
+ | { readonly kind: 'absent' }
120
+ | { readonly kind: 'present'; readonly source: string }
121
+ | { readonly error: unknown; readonly kind: 'unreadable' };
122
+
123
+ /*** Decode one discovered metadata document or surface its filesystem failure. */
124
+ function acquireDiscoveredMetadata(
125
+ source: Exclude<MetadataSource, { readonly kind: 'absent' }>,
126
+ metadataPath: string,
127
+ absoluteMetadataPath: string,
128
+ fact: RepositoryMetadataFact,
129
+ enforce: boolean | undefined,
130
+ ): RepositoryMetadataAcquisition {
131
+ return source.kind === 'present'
132
+ ? decodeMetadataSource(
133
+ source.source,
134
+ { ...fact, path: metadataPath },
135
+ absoluteMetadataPath,
136
+ enforce,
137
+ )
138
+ : unreadableMetadataAcquisition(
139
+ metadataPath,
140
+ absoluteMetadataPath,
141
+ source.error,
142
+ enforce,
143
+ fact,
144
+ );
145
+ }
146
+
147
+ /*** Read metadata text while distinguishing absence from an unreadable authored document. */
148
+ async function readMetadataSourceAsync(metadataPath: string): Promise<MetadataSource> {
149
+ try {
150
+ return { kind: 'present', source: await fs.readFile(metadataPath, 'utf8') };
151
+ } catch (error) {
152
+ if (hasErrorCode(error, 'ENOENT')) {
153
+ return { kind: 'absent' };
154
+ }
155
+
156
+ return { error, kind: 'unreadable' };
157
+ }
158
+ }
159
+
160
+ /*** Create one unreadable-document result at the authored canonical or alternate path. */
161
+ function unreadableMetadataAcquisition(
162
+ metadataPath: string,
163
+ absoluteMetadataPath: string,
164
+ error: unknown,
165
+ enforce: boolean | undefined,
166
+ fact: RepositoryMetadataFact,
167
+ ): RepositoryMetadataAcquisition {
168
+ return {
169
+ diagnostics: [
170
+ createMetadataDiagnostic({
171
+ code: 'repository-metadata-unreadable',
172
+ message: 'Repository metadata could not be read: ' + describeError(error),
173
+ path: absoluteMetadataPath,
174
+ ruleId: 'repository.metadata.document.valid',
175
+ severity: metadataSeverity(enforce),
176
+ }),
177
+ ],
178
+ evaluate: false,
179
+ fact: { ...fact, path: metadataPath },
180
+ };
181
+ }
182
+
183
+ /*** Parse and decode metadata while keeping syntax errors distinct from structural decoder findings. */
184
+ function decodeMetadataSource(
185
+ source: string,
186
+ fact: RepositoryMetadataFact,
187
+ metadataPath: string,
188
+ enforce: boolean | undefined,
189
+ ): RepositoryMetadataAcquisition {
190
+ let parsed: unknown;
191
+ try {
192
+ parsed = JSON.parse(source) as unknown;
193
+ } catch (error) {
194
+ return invalidMetadataAcquisition(
195
+ fact,
196
+ metadataPath,
197
+ 'Repository metadata contains invalid JSON: ' + describeError(error),
198
+ enforce,
199
+ );
200
+ }
201
+
202
+ const decoded = decodeRepositoryMetadata(parsed);
203
+ if (!decoded.ok) {
204
+ return {
205
+ diagnostics: decoded.diagnostics.map((diagnostic) =>
206
+ createMetadataDiagnostic({
207
+ code: 'invalid-repository-metadata',
208
+ message: diagnostic.message,
209
+ path: metadataPath + diagnostic.path,
210
+ ruleId: 'repository.metadata.document.valid',
211
+ severity: metadataSeverity(enforce),
212
+ }),
213
+ ),
214
+ evaluate: false,
215
+ fact,
216
+ };
217
+ }
218
+
219
+ return { diagnostics: [], evaluate: true, fact: { ...fact, document: decoded.document } };
220
+ }
221
+
222
+ /*** Return one invalid-document result without falsely reporting that the document is missing. */
223
+ function invalidMetadataAcquisition(
224
+ fact: RepositoryMetadataFact,
225
+ metadataPath: string,
226
+ message: string,
227
+ enforce: boolean | undefined,
228
+ ): RepositoryMetadataAcquisition {
229
+ return {
230
+ diagnostics: [
231
+ createMetadataDiagnostic({
232
+ code: 'invalid-repository-metadata',
233
+ message,
234
+ path: metadataPath,
235
+ ruleId: 'repository.metadata.document.valid',
236
+ severity: metadataSeverity(enforce),
237
+ }),
238
+ ],
239
+ evaluate: false,
240
+ fact,
241
+ };
242
+ }
243
+
244
+ /*** Derive the profile-owned location from real source ownership unless a host explicitly supplies it. */
245
+ async function detectMetadataProfileAsync(targetPath: string): Promise<'non-source' | 'source'> {
246
+ try {
247
+ return (await fs.stat(path.join(targetPath, 'src'))).isDirectory() ? 'source' : 'non-source';
248
+ } catch {
249
+ return 'non-source';
250
+ }
251
+ }
252
+
253
+ /*** Evaluate canonical metadata policy using only acquired portable facts. */
254
+ function evaluateMetadataPreflight(
255
+ metadata: RepositoryMetadataFact,
256
+ targetPath: string,
257
+ enforce: boolean | undefined,
258
+ ): readonly DoctorDiagnostic[] {
259
+ return evaluateRepositoryMetadataPreflight({
260
+ cliCommands: [],
261
+ imports: [],
262
+ metadata,
263
+ packageJson: { dependencies: [], exports: [], fields: {}, scripts: {} },
264
+ paths: [],
265
+ workflowBunVersions: {},
266
+ }).findings.map((finding) =>
267
+ createMetadataDiagnostic({
268
+ code: 'field-invalid',
269
+ message: finding.message,
270
+ path: resolveMetadataFindingPath(targetPath, metadata, finding.subjects[0]?.path),
271
+ ruleId: finding.ruleId as DoctorRuleId,
272
+ severity: metadataSeverity(enforce),
273
+ }),
274
+ );
275
+ }
276
+
277
+ /*** Resolve Rules document paths and RFC 6901 fragments without treating a pointer as a filesystem path. */
278
+ function resolveMetadataFindingPath(
279
+ targetPath: string,
280
+ metadata: RepositoryMetadataFact,
281
+ findingPath: string | undefined,
282
+ ): string {
283
+ const documentPath = path.join(
284
+ targetPath,
285
+ metadata.path ?? canonicalMetadataPath(metadata.profile),
286
+ );
287
+ if (findingPath === undefined || findingPath.startsWith('#')) {
288
+ return documentPath + (findingPath ?? '');
289
+ }
290
+
291
+ const fragmentIndex = findingPath.indexOf('#');
292
+ const relativePath = fragmentIndex === -1 ? findingPath : findingPath.slice(0, fragmentIndex);
293
+ const fragment = fragmentIndex === -1 ? '' : findingPath.slice(fragmentIndex);
294
+ return path.join(targetPath, relativePath) + fragment;
295
+ }
296
+
297
+ /*** Use published Rules path policy instead of reproducing metadata path literals in Doctor. */
298
+ function canonicalMetadataPath(profile: 'non-source' | 'source'): string {
299
+ return profile === 'source'
300
+ ? REPOSITORY_RULE_METADATA.metadata.profiles.source.canonicalPath
301
+ : REPOSITORY_RULE_METADATA.metadata.profiles.nonSource.canonicalPath;
302
+ }
303
+
304
+ /*** Select the other Rules-defined profile path when canonical metadata is absent. */
305
+ function oppositeProfile(profile: 'non-source' | 'source'): 'non-source' | 'source' {
306
+ return profile === 'source' ? 'non-source' : 'source';
307
+ }
308
+
309
+ /*** Promote preflight findings only through the explicit Doctor activation request. */
310
+ function metadataSeverity(enforce: boolean | undefined): DoctorDiagnosticSeverity {
311
+ return enforce === true ? 'error' : 'warning';
312
+ }
313
+
314
+ /*** Preserve stable Doctor diagnostic structure for Rules and decoder findings. */
315
+ function createMetadataDiagnostic(input: {
316
+ readonly code: DoctorDiagnostic['code'];
317
+ readonly message: string;
318
+ readonly path: string;
319
+ readonly ruleId: DoctorRuleId;
320
+ readonly severity: DoctorDiagnosticSeverity;
321
+ }): DoctorDiagnostic {
322
+ return {
323
+ code: input.code,
324
+ message: input.message,
325
+ path: input.path,
326
+ profile: 'unknown',
327
+ ruleId: input.ruleId,
328
+ severity: input.severity,
329
+ };
330
+ }
331
+
332
+ /*** Identify filesystem absence without treating other read failures as missing metadata. */
333
+ function hasErrorCode(error: unknown, code: string): boolean {
334
+ return error instanceof Error && 'code' in error && error.code === code;
335
+ }
336
+
337
+ /*** Render parser and filesystem failures without exposing an implementation-specific exception shape. */
338
+ function describeError(error: unknown): string {
339
+ return error instanceof Error ? error.message : String(error);
340
+ }
@@ -0,0 +1,49 @@
1
+ import { promises as fs } from 'node:fs';
2
+ import path from 'node:path';
3
+
4
+ import type { DoctorRepositoryMetadataOptions } from './analysis.js';
5
+
6
+ /*** Derive managed applicability only when GitHub's checkout and Doctor's target share one canonical identity. */
7
+ export async function createMetadataOptionsAsync(input: {
8
+ readonly context: { readonly cwd: string; readonly env: NodeJS.ProcessEnv };
9
+ readonly enforceRepositoryMetadata: boolean;
10
+ readonly inputPath?: string;
11
+ }): Promise<DoctorRepositoryMetadataOptions> {
12
+ const { env } = input.context;
13
+ const managedWorkflow =
14
+ env.GITHUB_ACTIONS === 'true' &&
15
+ env.GITHUB_REPOSITORY_OWNER === 'ankhorage' &&
16
+ env.GITHUB_REPOSITORY?.startsWith('ankhorage/') === true;
17
+
18
+ if (!managedWorkflow) {
19
+ return { enforce: input.enforceRepositoryMetadata, managed: false };
20
+ }
21
+
22
+ const workspacePath = env.GITHUB_WORKSPACE;
23
+ if (workspacePath === undefined || workspacePath.length === 0) {
24
+ return createUnconfirmedApplicabilityOptions(input.enforceRepositoryMetadata);
25
+ }
26
+
27
+ try {
28
+ const [workspaceIdentity, targetIdentity] = await Promise.all([
29
+ fs.realpath(workspacePath),
30
+ fs.realpath(path.resolve(input.context.cwd, input.inputPath ?? '.')),
31
+ ]);
32
+ return {
33
+ enforce: input.enforceRepositoryMetadata,
34
+ managed: workspaceIdentity === targetIdentity,
35
+ };
36
+ } catch {
37
+ return createUnconfirmedApplicabilityOptions(input.enforceRepositoryMetadata);
38
+ }
39
+ }
40
+
41
+ /*** Reject incomplete managed-workflow identity instead of applying managed policy to an arbitrary target. */
42
+ function createUnconfirmedApplicabilityOptions(enforce: boolean): DoctorRepositoryMetadataOptions {
43
+ return {
44
+ applicabilityDiagnostic:
45
+ 'Repository metadata applicability could not be confirmed because this managed GitHub Actions workflow has no resolvable GITHUB_WORKSPACE target identity.',
46
+ enforce,
47
+ managed: false,
48
+ };
49
+ }
@@ -9,7 +9,9 @@ export function getRepositoryRule(ruleId: DoctorRuleId): {
9
9
  readonly id: DoctorRuleId;
10
10
  readonly severity: DoctorDiagnosticSeverity;
11
11
  } {
12
- const rule = createRepositoryRuleSet().rules.find((candidate) => candidate.id === ruleId);
12
+ const rule = createRepositoryRuleSet().rules.find(
13
+ (candidate) => candidate.identity.id === ruleId,
14
+ );
13
15
  if (rule === undefined) {
14
16
  throw new Error('Unknown repository rule: ' + ruleId);
15
17
  }