repometry 0.8.0 → 0.9.0
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.
- package/README.md +24 -23
- package/package.json +1 -1
- package/target/main/resources/languages/go/parser.go +25 -5
- package/target/main/resources/languages/python/parser.py +12 -1
- package/target/main/resources/report/json/config-v1.schema.json +0 -1
- package/target/main/resources/report/json/report-v2.schema.json +12 -0
- package/target/main/ts/api/gh/github.d.ts +3 -1
- package/target/main/ts/api/gh/github.js +39 -54
- package/target/main/ts/api/git/changes.d.ts +6 -1
- package/target/main/ts/api/git/changes.js +19 -10
- package/target/main/ts/api/git/line-changes.d.ts +6 -0
- package/target/main/ts/api/git/line-changes.js +57 -0
- package/target/main/ts/cli/config.d.ts +2 -1
- package/target/main/ts/cli/config.js +12 -5
- package/target/main/ts/cli/main.js +33 -38
- package/target/main/ts/compose/review.d.ts +24 -13
- package/target/main/ts/compose/review.js +44 -32
- package/target/main/ts/core/model/changes.d.ts +29 -0
- package/target/main/ts/core/model/code-structure.d.ts +2 -0
- package/target/main/ts/core/model/code-structure.js +8 -2
- package/target/main/ts/core/model/finding.d.ts +6 -0
- package/target/main/ts/core/model/finding.js +23 -8
- package/target/main/ts/core/model/pull-review.d.ts +52 -30
- package/target/main/ts/core/model/pull-review.js +4 -1
- package/target/main/ts/core/usecases/review-effect.d.ts +18 -0
- package/target/main/ts/core/usecases/review-effect.js +37 -0
- package/target/main/ts/core/usecases/review-match.d.ts +14 -0
- package/target/main/ts/core/usecases/review-match.js +72 -0
- package/target/main/ts/core/usecases/review-position.d.ts +14 -0
- package/target/main/ts/core/usecases/review-position.js +53 -0
- package/target/main/ts/core/usecases/review-scope.d.ts +21 -0
- package/target/main/ts/core/usecases/review-scope.js +41 -0
- package/target/main/ts/core/usecases/review.d.ts +32 -21
- package/target/main/ts/core/usecases/review.js +89 -114
- package/target/main/ts/index.d.ts +4 -3
- package/target/main/ts/index.js +1 -1
- package/target/main/ts/plugins/duplication/duplication.js +1 -1
- package/target/main/ts/plugins/language-js/babel-provider.js +4 -2
- package/target/main/ts/plugins/language-js/function-facts.d.ts +3 -3
- package/target/main/ts/plugins/language-js/function-facts.js +3 -15
- package/target/main/ts/plugins/language-js/qualified-names.d.ts +11 -0
- package/target/main/ts/plugins/language-js/qualified-names.js +63 -0
- package/target/main/ts/plugins/language-js/structural-observations.d.ts +2 -1
- package/target/main/ts/plugins/language-js/structural-observations.js +18 -14
- package/target/main/ts/plugins/maintainability/structural-smells.js +3 -2
- package/target/main/ts/report/html/app/viewer-chart-bars.js +9 -5
- package/target/main/ts/report/json/store/pull-review-store.d.ts +0 -1
- package/target/main/ts/report/json/store/pull-review-store.js +2 -20
- package/target/main/ts/report/json/wire/pull-review-model.d.ts +49 -2
- package/target/main/ts/report/json/wire/pull-review-model.js +2 -0
- package/target/main/ts/report/markdown/pull-review.d.ts +5 -0
- package/target/main/ts/report/markdown/pull-review.js +122 -0
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { lstat, readFile, realpath } from 'node:fs/promises';
|
|
3
|
-
import { join, resolve } from 'node:path';
|
|
2
|
+
import { lstat, readFile, realpath, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { join, relative, resolve, sep } from 'node:path';
|
|
4
4
|
import { firstParentRange, gitRoot, resolveGitCommit } from '../api/git/git.js';
|
|
5
5
|
import { gitReportObjects } from '../api/git/git-reader.js';
|
|
6
6
|
import { resolveGitBranch } from '../api/git/commit-metadata.js';
|
|
7
7
|
import { clearSourceCache, sourceCacheRoot } from '../api/git/source-cache.js';
|
|
8
|
-
import { writeGitHubDiffCoverage, writeGitHubResult, writeGitHubReview } from '../api/gh/github.js';
|
|
8
|
+
import { gitHubPullRequest, writeGitHubDiffCoverage, writeGitHubResult, writeGitHubReview } from '../api/gh/github.js';
|
|
9
9
|
import { remoteExecutor } from '../api/worker/remote-executor.js';
|
|
10
10
|
import { serveWorker } from '../api/worker/worker.js';
|
|
11
11
|
import { workerToken } from '../api/worker/worker-protocol.js';
|
|
@@ -14,20 +14,19 @@ import { defaultLanguages, languageRegistry } from '../compose/languages.js';
|
|
|
14
14
|
import { analyzeSnapshot, attachFindings, inspect } from '../compose/inspect.js';
|
|
15
15
|
import { measureDiffCoverage } from '../compose/diff-coverage.js';
|
|
16
16
|
import { prepareMetabranch } from '../compose/prepare-metabranch.js';
|
|
17
|
-
import { reviewExitCode,
|
|
17
|
+
import { review as reviewChange, reviewExitCode, reviewReason } from '../compose/review.js';
|
|
18
18
|
import { effectivePlugins } from '../compose/plugin-list.js';
|
|
19
|
-
import { isRecord } from '../core/engine/plugin.js';
|
|
20
19
|
import { validSubjectId } from '../core/model/report.js';
|
|
21
20
|
import { sourceExclusionMatcher } from '../core/profile/exclusions.js';
|
|
22
21
|
import { compareInspections } from '../core/usecases/compare.js';
|
|
23
22
|
import { exportProjects } from '../report/html/site/projects.js';
|
|
24
23
|
import { fileMetrics } from '../report/json/projections/file-metrics.js';
|
|
25
24
|
import { LocalReportStore } from '../report/json/store/local-store.js';
|
|
26
|
-
import {
|
|
25
|
+
import { pullReviewMarkdown } from '../report/markdown/pull-review.js';
|
|
27
26
|
import { LocalObjectStore } from '../report/json/store/object-store.js';
|
|
28
27
|
import { checkStore } from '../report/json/store/store-check.js';
|
|
29
28
|
import { dispatch, flag, option } from './arguments.js';
|
|
30
|
-
import { readConfig } from './config.js';
|
|
29
|
+
import { configFile, readConfig } from './config.js';
|
|
31
30
|
import { importerIds, readInputs } from './imports.js';
|
|
32
31
|
import { outputReports, reporterIds, viewTarget } from './reporting.js';
|
|
33
32
|
import { parseRange } from './revisions.js';
|
|
@@ -73,14 +72,13 @@ const commands = {
|
|
|
73
72
|
'min-coverage': option('PERCENT', 'Minimum acceptable changed-line coverage.') }, run: diffCoverage,
|
|
74
73
|
},
|
|
75
74
|
review: {
|
|
76
|
-
description: '
|
|
75
|
+
description: 'Report the issues a change adds or makes worse on the lines it changes.', parameters: 'BASE [HEAD]', min: 1, max: 2,
|
|
77
76
|
options: { ...configOptions, import: importOption, exclude: excludeOption, ci: ciOption,
|
|
78
|
-
|
|
79
|
-
'min-coverage': option('PERCENT', 'Minimum
|
|
80
|
-
'min-lines': option('COUNT', '
|
|
81
|
-
'max-
|
|
82
|
-
|
|
83
|
-
annotations: flag('Write GitHub line annotations.') }, run: review,
|
|
77
|
+
pull: option('NUMBER', 'Name the pull request in the result.'),
|
|
78
|
+
'min-coverage': option('PERCENT', 'Minimum changed-line coverage. Default 80.'),
|
|
79
|
+
'min-lines': option('COUNT', 'Measured changed lines from which the coverage minimum applies. Default 20.'),
|
|
80
|
+
'max-new-issues': option('COUNT', 'Allowed new and worsened issues. Default 0.'),
|
|
81
|
+
markdown: option('PATH', 'Also write the result as Markdown, for a pull request comment.') }, run: review,
|
|
84
82
|
},
|
|
85
83
|
report: {
|
|
86
84
|
description: 'Render HTML or JSON from saved reports.', parameters: '[FORMAT[=PATH]]...', max: Infinity,
|
|
@@ -342,38 +340,35 @@ async function diffCoverage(input) {
|
|
|
342
340
|
return report.status === 'partial' ? 2 : report.decision?.status === 'failed' ? 3 : 0;
|
|
343
341
|
}
|
|
344
342
|
async function review(input) {
|
|
345
|
-
const { directory, root
|
|
343
|
+
const { directory, root } = await context(input);
|
|
346
344
|
const github = useGitHub(input);
|
|
347
|
-
if (input.flag('annotations') && !github)
|
|
348
|
-
throw new Error('--annotations requires --ci github');
|
|
349
345
|
const config = await readConfig(root, input);
|
|
350
346
|
const imports = input.many('import');
|
|
351
347
|
const { lcov, goCover, findings } = imports.length ? readInputs(directory, imports) : readInputs(root, config.imports);
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
return attachFindings(report, findings, root, root, sourceExclusionMatcher(exclude, generated));
|
|
363
|
-
},
|
|
364
|
-
} : store.reader;
|
|
365
|
-
const number = count(input.text('pull'), '--pull', 1);
|
|
366
|
-
const report = await reviewPull({ directory: root, base: input.args[0], revision: input.args[1] ?? 'HEAD', lcov, goCover, exclude, number, plugins: config.plugins,
|
|
348
|
+
if (findings.length && imports.length)
|
|
349
|
+
throw new Error('review compares both sides of a change; scanner imports apply to full inspections only');
|
|
350
|
+
if (findings.length)
|
|
351
|
+
process.stderr.write('Scanner imports from the config are skipped: review compares both sides of a change.\n');
|
|
352
|
+
const settingsPath = configFile(root, input);
|
|
353
|
+
const { plugins, ...settings } = config;
|
|
354
|
+
const report = await reviewChange({ directory: root, base: input.args[0], revision: input.args[1] ?? 'HEAD', lcov, goCover,
|
|
355
|
+
exclude: [...config.exclude, ...input.many('exclude')], ignore: config.ignore, plugins, settings,
|
|
356
|
+
settingsPath: settingsPath === undefined ? undefined : relative(root, settingsPath).split(sep).join('/'),
|
|
357
|
+
number: count(input.text('pull'), '--pull', 1) ?? (github ? await gitHubPullRequest() : undefined),
|
|
367
358
|
minimumPercent: percent(input.text('min-coverage') ?? config.minCoverage, '--min-coverage'),
|
|
368
359
|
minimumLines: count(input.text('min-lines') ?? config.minLines, '--min-lines'),
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
if (number !== undefined)
|
|
372
|
-
await savePullReview(writableStore(store), report);
|
|
360
|
+
maxNewIssues: count(input.text('max-new-issues') ?? config.maxNewIssues, '--max-new-issues') });
|
|
361
|
+
const markdown = pullReviewMarkdown(report);
|
|
373
362
|
if (github)
|
|
374
|
-
await writeGitHubReview(report,
|
|
363
|
+
await writeGitHubReview(report, markdown, await gitRoot(root));
|
|
364
|
+
const markdownPath = input.text('markdown');
|
|
365
|
+
if (markdownPath !== undefined)
|
|
366
|
+
await writeFile(resolve(directory, markdownPath), markdown);
|
|
375
367
|
print(report);
|
|
376
|
-
|
|
368
|
+
const code = reviewExitCode(report);
|
|
369
|
+
if (code !== 0)
|
|
370
|
+
process.stderr.write(`${reviewReason(report)}\n`);
|
|
371
|
+
return code;
|
|
377
372
|
}
|
|
378
373
|
function commitId(root, revision) {
|
|
379
374
|
return validSubjectId('commit', revision) ? Promise.resolve(revision) : resolveGitCommit(root, revision);
|
|
@@ -1,15 +1,26 @@
|
|
|
1
|
+
import type { LcovFiles } from '../api/import/lcov-files.js';
|
|
2
|
+
import type { Plugin } from '../core/engine/plugin.js';
|
|
3
|
+
import type { FindingIgnoreRule } from '../core/model/ignore-rule.js';
|
|
1
4
|
import type { PullReview } from '../core/model/pull-review.js';
|
|
2
|
-
import type {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
readonly
|
|
8
|
-
readonly
|
|
9
|
-
readonly
|
|
10
|
-
readonly
|
|
5
|
+
import type { ReviewOptions } from '../core/usecases/review.js';
|
|
6
|
+
export { projectReview, reviewExitCode, reviewReason } from '../core/usecases/review.js';
|
|
7
|
+
export interface ReviewRequest extends ReviewOptions {
|
|
8
|
+
/** The repository root. */
|
|
9
|
+
readonly directory: string;
|
|
10
|
+
readonly base: string;
|
|
11
|
+
readonly revision?: string;
|
|
12
|
+
readonly lcov?: LcovFiles;
|
|
13
|
+
readonly goCover?: LcovFiles;
|
|
14
|
+
readonly exclude?: readonly string[];
|
|
15
|
+
readonly ignore?: readonly FindingIgnoreRule[];
|
|
16
|
+
readonly plugins?: readonly Plugin[];
|
|
17
|
+
/** The settings that shaped both inspections, which keeps the second one's reuse of unchanged files honest. */
|
|
18
|
+
readonly settings?: unknown;
|
|
19
|
+
/** The settings file's repository path; a change to it is noted. */
|
|
20
|
+
readonly settingsPath?: string;
|
|
11
21
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
22
|
+
/**
|
|
23
|
+
* Analyses the merge base and the head with the same settings, in a temporary store, and reports what the change did
|
|
24
|
+
* on the lines it changed. The repository's own store is neither read nor written.
|
|
25
|
+
*/
|
|
26
|
+
export declare function review(request: ReviewRequest): Promise<PullReview>;
|
|
@@ -1,39 +1,51 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { mkdtemp, rm } from 'node:fs/promises';
|
|
2
|
+
import { tmpdir } from 'node:os';
|
|
3
|
+
import { join } from 'node:path';
|
|
2
4
|
import { findMergeBase } from '../api/git/changes.js';
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
5
|
+
import { GitBlobReader, resolveGitCommit } from '../api/git/git.js';
|
|
6
|
+
import { readLineChanges } from '../api/git/line-changes.js';
|
|
7
|
+
import { checkOptions, projectReview } from '../core/usecases/review.js';
|
|
6
8
|
import { measureChangedLines } from './diff-coverage.js';
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
export async function review(request
|
|
15
|
-
const
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
},
|
|
25
|
-
...(lcov === undefined && goCover === undefined ? {} : { measureChanged: () => measureChangedLines(request) }),
|
|
26
|
-
...overrides,
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
async function readIfAvailable(reader, id) {
|
|
9
|
+
import { HistoryCache } from './history-cache.js';
|
|
10
|
+
import { inspect } from './inspect.js';
|
|
11
|
+
export { projectReview, reviewExitCode, reviewReason } from '../core/usecases/review.js';
|
|
12
|
+
/**
|
|
13
|
+
* Analyses the merge base and the head with the same settings, in a temporary store, and reports what the change did
|
|
14
|
+
* on the lines it changed. The repository's own store is neither read nor written.
|
|
15
|
+
*/
|
|
16
|
+
export async function review(request) {
|
|
17
|
+
const { directory, lcov, goCover, exclude, ignore, plugins } = request;
|
|
18
|
+
checkOptions(request, lcov !== undefined || goCover !== undefined);
|
|
19
|
+
const [head, requested] = await Promise.all([resolveGitCommit(directory, request.revision ?? 'HEAD'),
|
|
20
|
+
resolveGitCommit(directory, request.base).catch((error) => {
|
|
21
|
+
throw new Error(`${error instanceof Error ? error.message : String(error)}. A review needs the base's history: fetch it, `
|
|
22
|
+
+ 'for example with fetch-depth: 0 on actions/checkout.', { cause: error });
|
|
23
|
+
})]);
|
|
24
|
+
const mergeBase = await findMergeBase(directory, requested, head);
|
|
25
|
+
const output = await mkdtemp(join(tmpdir(), 'repometry-review-'));
|
|
30
26
|
try {
|
|
31
|
-
|
|
27
|
+
const history = new HistoryCache(request.settings);
|
|
28
|
+
const inspectCommit = (id, coverage) => inspect({ directory, output, source: { revision: id }, exclude, ignore, plugins,
|
|
29
|
+
...coverage ? { lcov, goCover } : {} }, undefined, { history, deferSummary: true });
|
|
30
|
+
const base = await inspectCommit(mergeBase, false);
|
|
31
|
+
const headReport = await inspectCommit(head, true);
|
|
32
|
+
const changes = await readLineChanges(directory, mergeBase, head);
|
|
33
|
+
const coverage = lcov === undefined && goCover === undefined ? null
|
|
34
|
+
: await measureChangedLines({ directory, base: mergeBase, revision: head, lcov, goCover, exclude, plugins });
|
|
35
|
+
const sources = { base: await sourceLines(directory, mergeBase, changes, 'previous'), head: await sourceLines(directory, head, changes, 'path') };
|
|
36
|
+
const settingsChanged = request.settingsPath !== undefined
|
|
37
|
+
&& changes.files.some(file => file.path === request.settingsPath || file.previous === request.settingsPath);
|
|
38
|
+
return projectReview(request, { requestedBase: request.base, base, head: headReport, changes, sources, coverage, settingsChanged });
|
|
32
39
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
return null;
|
|
36
|
-
throw error;
|
|
40
|
+
finally {
|
|
41
|
+
await rm(output, { recursive: true, force: true });
|
|
37
42
|
}
|
|
38
43
|
}
|
|
44
|
+
/** The text of one side's changed files, which scope and matching read line by line. */
|
|
45
|
+
async function sourceLines(directory, commit, changes, side) {
|
|
46
|
+
const paths = changes.files.flatMap(file => file[side] === null || file.whole ? [] : [file[side]]);
|
|
47
|
+
const blobs = await new GitBlobReader(directory).read(paths.map(path => `${commit}:${path}`));
|
|
48
|
+
const texts = new Map(paths.map((path, index) => [path, blobs[index]?.toString('utf8').split('\n').map(line => line.replace(/\r$/, ''))]));
|
|
49
|
+
return { line: (path, line) => line >= 1 ? texts.get(path)?.[line - 1] : undefined };
|
|
50
|
+
}
|
|
39
51
|
//# sourceMappingURL=review.js.map
|
|
@@ -2,6 +2,35 @@ export interface LineRange {
|
|
|
2
2
|
readonly first: number;
|
|
3
3
|
readonly last: number;
|
|
4
4
|
}
|
|
5
|
+
/** One file's change between two commits, with the changed lines on each side. */
|
|
6
|
+
export interface FileLineChange {
|
|
7
|
+
/** The head path; null when the file was deleted. */
|
|
8
|
+
readonly path: string | null;
|
|
9
|
+
/** The base path; null when the file was added. */
|
|
10
|
+
readonly previous: string | null;
|
|
11
|
+
/** Head side: added and modified lines. */
|
|
12
|
+
readonly added: readonly LineRange[];
|
|
13
|
+
/** Base side: modified and removed lines. */
|
|
14
|
+
readonly removed: readonly LineRange[];
|
|
15
|
+
/** Binary or undecodable content: the file changed, but its lines are unknown. */
|
|
16
|
+
readonly whole: boolean;
|
|
17
|
+
/** The diff's hunks, which place unchanged base lines on the head side. Empty for added, deleted and whole files. */
|
|
18
|
+
readonly hunks: readonly LineHunk[];
|
|
19
|
+
}
|
|
20
|
+
/** One diff hunk: `count` lines from `first` on each side; a count of 0 is an insertion or removal after line `first`. */
|
|
21
|
+
export interface LineHunk {
|
|
22
|
+
readonly base: {
|
|
23
|
+
readonly first: number;
|
|
24
|
+
readonly count: number;
|
|
25
|
+
};
|
|
26
|
+
readonly head: {
|
|
27
|
+
readonly first: number;
|
|
28
|
+
readonly count: number;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
export interface LineChanges {
|
|
32
|
+
readonly files: readonly FileLineChange[];
|
|
33
|
+
}
|
|
5
34
|
export interface GitChanges {
|
|
6
35
|
readonly files: ReadonlyMap<string, readonly LineRange[]>;
|
|
7
36
|
readonly gaps: ReadonlyMap<string, string>;
|
|
@@ -18,6 +18,8 @@ export interface StructuralObservation {
|
|
|
18
18
|
readonly label: string;
|
|
19
19
|
readonly line?: number;
|
|
20
20
|
readonly endLine?: number;
|
|
21
|
+
/** The qualified name of the function the observation is about or sits in. File and module observations omit it. */
|
|
22
|
+
readonly anchor?: string;
|
|
21
23
|
}
|
|
22
24
|
export interface CodeStructure {
|
|
23
25
|
readonly path: string;
|
|
@@ -19,8 +19,14 @@ function isFunctionComplexity(value) {
|
|
|
19
19
|
&& isPosition(value.line) && isPosition(value.column);
|
|
20
20
|
}
|
|
21
21
|
function isObservation(value) {
|
|
22
|
-
return isRecord(value) && typeof value.rule === 'string' && checkIds.has(value.rule)
|
|
23
|
-
&&
|
|
22
|
+
return isRecord(value) && typeof value.rule === 'string' && checkIds.has(value.rule) && typeof value.label === 'string'
|
|
23
|
+
&& observedAt(value);
|
|
24
|
+
}
|
|
25
|
+
function observedAt(value) {
|
|
26
|
+
return isCount(value.value) && isPosition(value.line) && isPosition(value.endLine) && optionalName(value.anchor);
|
|
27
|
+
}
|
|
28
|
+
function optionalName(value) {
|
|
29
|
+
return value === undefined || (typeof value === 'string' && value.length > 0);
|
|
24
30
|
}
|
|
25
31
|
export const codeStructure = {
|
|
26
32
|
id: 'repometry.code-structure/v1',
|
|
@@ -18,6 +18,12 @@ export interface Finding {
|
|
|
18
18
|
readonly endLine?: number;
|
|
19
19
|
readonly effortMinutes?: number;
|
|
20
20
|
readonly related?: readonly FindingLocation[];
|
|
21
|
+
/** The measured value the rule judged, such as a function's cognitive complexity or a duplicate's copy count. */
|
|
22
|
+
readonly value?: number;
|
|
23
|
+
/** The value the rule allows. */
|
|
24
|
+
readonly limit?: number;
|
|
25
|
+
/** What the issue is about across commits: a function's qualified name, `(file)`, or a duplicate's fingerprint. */
|
|
26
|
+
readonly anchor?: string;
|
|
21
27
|
}
|
|
22
28
|
export declare function isSensitiveFinding(category: Finding['category']): boolean;
|
|
23
29
|
export declare function isFinding(value: unknown): value is Finding;
|
|
@@ -13,14 +13,29 @@ function isLocation(value) {
|
|
|
13
13
|
&& count(value.line, 1) && count(value.endLine, value.line);
|
|
14
14
|
}
|
|
15
15
|
export function isFinding(value) {
|
|
16
|
-
return isRecord(value) &&
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
&& (
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
16
|
+
return isRecord(value) && namesIssue(value) && placesIssue(value) && measuresIssue(value);
|
|
17
|
+
}
|
|
18
|
+
function namesIssue(value) {
|
|
19
|
+
return text(value.tool) && text(value.rule) && text(value.message)
|
|
20
|
+
&& findingCategories.some(category => category === value.category) && findingLevels.some(level => level === value.level);
|
|
21
|
+
}
|
|
22
|
+
function placesIssue(value) {
|
|
23
|
+
return optional(value.path, text) && optional(value.line, line => value.path !== undefined && count(line, 1))
|
|
24
|
+
&& optional(value.endLine, endLine => typeof value.line === 'number' && count(endLine, value.line))
|
|
25
|
+
&& optional(value.related, related => Array.isArray(related) && related.every(isLocation));
|
|
26
|
+
}
|
|
27
|
+
function measuresIssue(value) {
|
|
28
|
+
return optional(value.effortMinutes, minutes => count(minutes, 0)) && optional(value.value, measure)
|
|
29
|
+
&& optional(value.limit, measure) && optional(value.anchor, text);
|
|
30
|
+
}
|
|
31
|
+
function optional(value, test) {
|
|
32
|
+
return value === undefined || test(value);
|
|
33
|
+
}
|
|
34
|
+
function text(value) {
|
|
35
|
+
return typeof value === 'string' && value.length > 0;
|
|
36
|
+
}
|
|
37
|
+
function measure(value) {
|
|
38
|
+
return typeof value === 'number' && Number.isFinite(value) && value >= 0;
|
|
24
39
|
}
|
|
25
40
|
export const findingResult = {
|
|
26
41
|
id: 'repometry.findings-and-measurements/v1',
|
|
@@ -1,48 +1,70 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export declare const
|
|
5
|
-
type
|
|
6
|
-
|
|
1
|
+
import type { Finding, FindingLocation } from './finding.js';
|
|
2
|
+
export declare const pullReviewSchema = "urn:repometry:schema:pull-review:2";
|
|
3
|
+
/** GitHub shows at most this many annotations of each kind per step. */
|
|
4
|
+
export declare const annotationLimit = 10;
|
|
5
|
+
export type IssueEffect = 'new' | 'worsened' | 'improved' | 'touched';
|
|
6
|
+
/** What a change did to one issue on the lines it changed. */
|
|
7
|
+
export interface ReviewedIssue {
|
|
8
|
+
readonly effect: IssueEffect;
|
|
9
|
+
/** The head finding, or the base finding when the change fixed it. A duplicate is placed on a changed copy. */
|
|
10
|
+
readonly finding: Finding;
|
|
11
|
+
/** The base value of a matched issue. */
|
|
12
|
+
readonly before: number | null;
|
|
13
|
+
/** The base had the issue and the head doesn't. */
|
|
14
|
+
readonly fixed: boolean;
|
|
15
|
+
/** A duplicate's other copies. */
|
|
16
|
+
readonly context: readonly FindingLocation[];
|
|
17
|
+
/** Effort minutes this change added (positive) or removed (negative). */
|
|
18
|
+
readonly debtMinutes: number;
|
|
19
|
+
}
|
|
20
|
+
export interface ReviewReason {
|
|
21
|
+
readonly message: string;
|
|
22
|
+
readonly path?: string;
|
|
23
|
+
readonly operation?: string;
|
|
24
|
+
}
|
|
25
|
+
/** What a change did on the lines it changed: issues by effect, changed-line coverage, and the two gates. */
|
|
7
26
|
export interface PullReview {
|
|
8
27
|
readonly schema: typeof pullReviewSchema;
|
|
9
28
|
readonly number: number | null;
|
|
10
29
|
readonly generatedAt: string;
|
|
30
|
+
/** The base as requested, such as `origin/main`. */
|
|
11
31
|
readonly base: string;
|
|
12
32
|
readonly mergeBase: string;
|
|
13
33
|
readonly head: string;
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
readonly
|
|
20
|
-
|
|
21
|
-
readonly maxDrop: number;
|
|
22
|
-
} | null;
|
|
34
|
+
/** Files the change added, edited, renamed or deleted. */
|
|
35
|
+
readonly changedFiles: number;
|
|
36
|
+
readonly verdict: 'passed' | 'failed' | 'unmeasured';
|
|
37
|
+
readonly issues: readonly ReviewedIssue[];
|
|
38
|
+
readonly debt: {
|
|
39
|
+
readonly added: number;
|
|
40
|
+
readonly removed: number;
|
|
23
41
|
};
|
|
24
|
-
readonly
|
|
25
|
-
readonly
|
|
26
|
-
readonly
|
|
27
|
-
readonly
|
|
42
|
+
readonly coverage: {
|
|
43
|
+
readonly imported: boolean;
|
|
44
|
+
readonly covered: number;
|
|
45
|
+
readonly total: number;
|
|
46
|
+
readonly percent: number | null;
|
|
28
47
|
readonly uncovered: readonly {
|
|
29
48
|
readonly path: string;
|
|
30
|
-
readonly
|
|
49
|
+
readonly first: number;
|
|
50
|
+
readonly last: number;
|
|
51
|
+
}[];
|
|
52
|
+
readonly unmapped: readonly {
|
|
53
|
+
readonly path: string;
|
|
54
|
+
readonly reason: string;
|
|
31
55
|
}[];
|
|
32
56
|
readonly gate: {
|
|
33
|
-
readonly status: GateStatus;
|
|
34
57
|
readonly minimumPercent: number;
|
|
35
58
|
readonly minimumLines: number;
|
|
59
|
+
readonly status: 'passed' | 'failed' | 'not-applicable';
|
|
36
60
|
} | null;
|
|
37
61
|
};
|
|
38
|
-
readonly
|
|
39
|
-
readonly
|
|
40
|
-
readonly
|
|
41
|
-
readonly
|
|
42
|
-
readonly gate: {
|
|
43
|
-
readonly status: GateStatus;
|
|
44
|
-
readonly maximum: number;
|
|
45
|
-
} | null;
|
|
62
|
+
readonly issueGate: {
|
|
63
|
+
readonly maximum: number;
|
|
64
|
+
readonly count: number;
|
|
65
|
+
readonly status: 'passed' | 'failed';
|
|
46
66
|
};
|
|
67
|
+
/** Measurements the check needs that the head lacks: the review can't vouch for the change. */
|
|
68
|
+
readonly unmeasured: readonly ReviewReason[];
|
|
69
|
+
readonly notes: readonly ReviewReason[];
|
|
47
70
|
}
|
|
48
|
-
export {};
|
|
@@ -1 +1,4 @@
|
|
|
1
|
-
export const pullReviewSchema = 'urn:repometry:schema:pull-review:
|
|
1
|
+
export const pullReviewSchema = 'urn:repometry:schema:pull-review:2';
|
|
2
|
+
/** GitHub shows at most this many annotations of each kind per step. */
|
|
3
|
+
export const annotationLimit = 10;
|
|
4
|
+
//# sourceMappingURL=pull-review.js.map
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { LineChanges } from '../model/changes.js';
|
|
2
|
+
import type { Finding } from '../model/finding.js';
|
|
3
|
+
import type { ReviewedIssue } from '../model/pull-review.js';
|
|
4
|
+
export type { IssueEffect, ReviewedIssue } from '../model/pull-review.js';
|
|
5
|
+
export type { SourceLines } from './review-scope.js';
|
|
6
|
+
import type { SourceLines } from './review-scope.js';
|
|
7
|
+
/**
|
|
8
|
+
* Pairs base and head findings that are the same issue, and keeps the pairs on the changed lines of either side.
|
|
9
|
+
* A head finding without a pair is new; a base finding without one was fixed.
|
|
10
|
+
*/
|
|
11
|
+
export declare function classifyIssues(base: readonly Finding[], head: readonly Finding[], changes: LineChanges, sources: {
|
|
12
|
+
readonly base: SourceLines;
|
|
13
|
+
readonly head: SourceLines;
|
|
14
|
+
}): readonly ReviewedIssue[];
|
|
15
|
+
export declare function debtEffect(issues: readonly ReviewedIssue[]): {
|
|
16
|
+
readonly added: number;
|
|
17
|
+
readonly removed: number;
|
|
18
|
+
};
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { isSensitiveFinding } from '../model/finding.js';
|
|
2
|
+
import { pairFindings } from './review-match.js';
|
|
3
|
+
import { compare, inScope, placed, reviewSide } from './review-scope.js';
|
|
4
|
+
const order = { new: 0, worsened: 1, improved: 2, touched: 3 };
|
|
5
|
+
/**
|
|
6
|
+
* Pairs base and head findings that are the same issue, and keeps the pairs on the changed lines of either side.
|
|
7
|
+
* A head finding without a pair is new; a base finding without one was fixed.
|
|
8
|
+
*/
|
|
9
|
+
export function classifyIssues(base, head, changes, sources) {
|
|
10
|
+
const before = reviewSide(changes, 'base', sources.base);
|
|
11
|
+
const after = reviewSide(changes, 'head', sources.head);
|
|
12
|
+
const issues = pairFindings(base, head, changes, { base: before, head: after })
|
|
13
|
+
.filter(([from, to]) => (from !== undefined && inScope(from, before)) || (to !== undefined && inScope(to, after)))
|
|
14
|
+
.map(([from, to]) => review(from, to, before, after));
|
|
15
|
+
return issues.sort((left, right) => Number(isSensitiveFinding(right.finding.category)) - Number(isSensitiveFinding(left.finding.category))
|
|
16
|
+
|| order[left.effect] - order[right.effect] || compare(left.finding.path ?? '', right.finding.path ?? '')
|
|
17
|
+
|| (left.finding.line ?? 0) - (right.finding.line ?? 0));
|
|
18
|
+
}
|
|
19
|
+
export function debtEffect(issues) {
|
|
20
|
+
return issues.reduce((total, { debtMinutes }) => debtMinutes > 0 ? { ...total, added: total.added + debtMinutes }
|
|
21
|
+
: { ...total, removed: total.removed - debtMinutes }, { added: 0, removed: 0 });
|
|
22
|
+
}
|
|
23
|
+
function review(base, head, before, after) {
|
|
24
|
+
const effort = (finding) => finding?.effortMinutes ?? 0;
|
|
25
|
+
if (!head)
|
|
26
|
+
return { effect: 'improved', ...placed(base, before), before: base.value ?? null, fixed: true, debtMinutes: -effort(base) };
|
|
27
|
+
return { effect: effectOf(base, head), ...placed(head, after), before: base?.value ?? null, fixed: false,
|
|
28
|
+
debtMinutes: effort(head) - effort(base) };
|
|
29
|
+
}
|
|
30
|
+
function effectOf(base, head) {
|
|
31
|
+
if (!base)
|
|
32
|
+
return 'new';
|
|
33
|
+
if (head.value === undefined || base.value === undefined || head.value === base.value)
|
|
34
|
+
return 'touched';
|
|
35
|
+
return head.value > base.value ? 'worsened' : 'improved';
|
|
36
|
+
}
|
|
37
|
+
//# sourceMappingURL=review-effect.js.map
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { LineChanges } from '../model/changes.js';
|
|
2
|
+
import type { Finding } from '../model/finding.js';
|
|
3
|
+
import type { ReviewSide } from './review-scope.js';
|
|
4
|
+
/** A base finding and a head finding that are the same issue; one side is absent for a new or a fixed issue. */
|
|
5
|
+
export type FindingPair = readonly [base: Finding | undefined, head: Finding | undefined];
|
|
6
|
+
/**
|
|
7
|
+
* Pairs the findings of both sides that are the same issue. First by position through the diff, which follows code that
|
|
8
|
+
* moved or was renamed and tells unnamed functions apart. Then by rule, file, anchor and the text of the first line, in
|
|
9
|
+
* line order. Last, an anchor that only one finding on each side still carries, such as a rewritten named function.
|
|
10
|
+
*/
|
|
11
|
+
export declare function pairFindings(base: readonly Finding[], head: readonly Finding[], changes: LineChanges, sides: {
|
|
12
|
+
readonly base: ReviewSide;
|
|
13
|
+
readonly head: ReviewSide;
|
|
14
|
+
}): FindingPair[];
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { digest } from '../model/snapshot.js';
|
|
2
|
+
import { positionMatches } from './review-position.js';
|
|
3
|
+
/**
|
|
4
|
+
* Pairs the findings of both sides that are the same issue. First by position through the diff, which follows code that
|
|
5
|
+
* moved or was renamed and tells unnamed functions apart. Then by rule, file, anchor and the text of the first line, in
|
|
6
|
+
* line order. Last, an anchor that only one finding on each side still carries, such as a rewritten named function.
|
|
7
|
+
*/
|
|
8
|
+
export function pairFindings(base, head, changes, sides) {
|
|
9
|
+
const byBase = new Map(changes.files.flatMap(file => file.previous === null ? [] : [[file.previous, file]]));
|
|
10
|
+
const headPath = (path) => byBase.has(path) ? byBase.get(path).path : path;
|
|
11
|
+
const baseLeft = new Set(base);
|
|
12
|
+
const headLeft = new Set(head);
|
|
13
|
+
const pairs = [];
|
|
14
|
+
const take = (from, to) => {
|
|
15
|
+
if (!baseLeft.has(from) || !headLeft.has(to))
|
|
16
|
+
return;
|
|
17
|
+
baseLeft.delete(from);
|
|
18
|
+
headLeft.delete(to);
|
|
19
|
+
pairs.push([from, to]);
|
|
20
|
+
};
|
|
21
|
+
for (const { base: from, head: to } of positionMatches(base, head, byBase, headPath))
|
|
22
|
+
take(from, to);
|
|
23
|
+
pairByKey(baseLeft, headLeft, headPath, (finding, path, side) => identity(finding, path, side === 'base' ? sides.base : sides.head), take);
|
|
24
|
+
pairByKey(baseLeft, headLeft, headPath, uniqueAnchor(baseLeft, headLeft, headPath), take);
|
|
25
|
+
return [...pairs, ...[...headLeft].map(finding => [undefined, finding]), ...[...baseLeft].map(finding => [finding, undefined])];
|
|
26
|
+
}
|
|
27
|
+
/** Pairs leftover findings with equal keys, in line order. */
|
|
28
|
+
function pairByKey(baseLeft, headLeft, headPath, keyOf, take) {
|
|
29
|
+
const waiting = new Map();
|
|
30
|
+
for (const finding of inLineOrder(baseLeft)) {
|
|
31
|
+
const path = finding.path === undefined ? undefined : headPath(finding.path);
|
|
32
|
+
const key = path === null ? undefined : keyOf(finding, path, 'base');
|
|
33
|
+
if (key !== undefined)
|
|
34
|
+
waiting.set(key, [...waiting.get(key) ?? [], finding]);
|
|
35
|
+
}
|
|
36
|
+
for (const finding of inLineOrder(headLeft)) {
|
|
37
|
+
const key = keyOf(finding, finding.path, 'head');
|
|
38
|
+
const match = key === undefined ? undefined : waiting.get(key)?.shift();
|
|
39
|
+
if (match)
|
|
40
|
+
take(match, finding);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/** Keys an anchor that exactly one leftover finding carries on each side; repeated anchors such as `(anonymous)` get none. */
|
|
44
|
+
function uniqueAnchor(baseLeft, headLeft, headPath) {
|
|
45
|
+
const anchorKey = (finding, path) => finding.anchor === undefined || finding.category === 'duplication' ? undefined : JSON.stringify([finding.rule, path ?? null, finding.anchor]);
|
|
46
|
+
const counts = new Map();
|
|
47
|
+
const count = (key, side) => {
|
|
48
|
+
if (key !== undefined)
|
|
49
|
+
counts.set(`${side}${key}`, (counts.get(`${side}${key}`) ?? 0) + 1);
|
|
50
|
+
};
|
|
51
|
+
for (const finding of baseLeft)
|
|
52
|
+
count(anchorKey(finding, finding.path === undefined ? undefined : headPath(finding.path) ?? undefined), 0);
|
|
53
|
+
for (const finding of headLeft)
|
|
54
|
+
count(anchorKey(finding, finding.path), 1);
|
|
55
|
+
return (finding, path) => {
|
|
56
|
+
const key = anchorKey(finding, path);
|
|
57
|
+
return key !== undefined && counts.get(`0${key}`) === 1 && counts.get(`1${key}`) === 1 ? key : undefined;
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function identity(finding, path, side) {
|
|
61
|
+
if (finding.category === 'duplication' && finding.anchor !== undefined)
|
|
62
|
+
return JSON.stringify(['duplication', finding.anchor]);
|
|
63
|
+
return JSON.stringify([finding.rule, path ?? null, finding.anchor ?? null, lineHash(finding, side)]);
|
|
64
|
+
}
|
|
65
|
+
function lineHash(finding, side) {
|
|
66
|
+
const text = finding.path === undefined || finding.line === undefined ? undefined : side.lines.line(finding.path, finding.line);
|
|
67
|
+
return text === undefined ? '(file)' : digest(text.replace(/\s+/g, '')).slice(0, 16);
|
|
68
|
+
}
|
|
69
|
+
function inLineOrder(findings) {
|
|
70
|
+
return [...findings].sort((left, right) => (left.line ?? 0) - (right.line ?? 0) || (left.endLine ?? 0) - (right.endLine ?? 0));
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=review-match.js.map
|