repometry 0.8.1 → 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.
Files changed (52) hide show
  1. package/README.md +2 -1
  2. package/package.json +1 -1
  3. package/target/main/resources/languages/go/parser.go +25 -5
  4. package/target/main/resources/languages/python/parser.py +12 -1
  5. package/target/main/resources/report/json/config-v1.schema.json +0 -1
  6. package/target/main/resources/report/json/report-v2.schema.json +12 -0
  7. package/target/main/ts/api/gh/github.d.ts +3 -1
  8. package/target/main/ts/api/gh/github.js +39 -54
  9. package/target/main/ts/api/git/changes.d.ts +6 -1
  10. package/target/main/ts/api/git/changes.js +19 -10
  11. package/target/main/ts/api/git/line-changes.d.ts +6 -0
  12. package/target/main/ts/api/git/line-changes.js +57 -0
  13. package/target/main/ts/cli/config.d.ts +2 -1
  14. package/target/main/ts/cli/config.js +12 -5
  15. package/target/main/ts/cli/main.js +33 -38
  16. package/target/main/ts/compose/review.d.ts +24 -13
  17. package/target/main/ts/compose/review.js +44 -32
  18. package/target/main/ts/core/model/changes.d.ts +29 -0
  19. package/target/main/ts/core/model/code-structure.d.ts +2 -0
  20. package/target/main/ts/core/model/code-structure.js +8 -2
  21. package/target/main/ts/core/model/finding.d.ts +6 -0
  22. package/target/main/ts/core/model/finding.js +23 -8
  23. package/target/main/ts/core/model/pull-review.d.ts +52 -30
  24. package/target/main/ts/core/model/pull-review.js +4 -1
  25. package/target/main/ts/core/usecases/review-effect.d.ts +18 -0
  26. package/target/main/ts/core/usecases/review-effect.js +37 -0
  27. package/target/main/ts/core/usecases/review-match.d.ts +14 -0
  28. package/target/main/ts/core/usecases/review-match.js +72 -0
  29. package/target/main/ts/core/usecases/review-position.d.ts +14 -0
  30. package/target/main/ts/core/usecases/review-position.js +53 -0
  31. package/target/main/ts/core/usecases/review-scope.d.ts +21 -0
  32. package/target/main/ts/core/usecases/review-scope.js +41 -0
  33. package/target/main/ts/core/usecases/review.d.ts +32 -21
  34. package/target/main/ts/core/usecases/review.js +89 -114
  35. package/target/main/ts/index.d.ts +4 -3
  36. package/target/main/ts/index.js +1 -1
  37. package/target/main/ts/plugins/duplication/duplication.js +1 -1
  38. package/target/main/ts/plugins/language-js/babel-provider.js +4 -2
  39. package/target/main/ts/plugins/language-js/function-facts.d.ts +3 -3
  40. package/target/main/ts/plugins/language-js/function-facts.js +3 -15
  41. package/target/main/ts/plugins/language-js/qualified-names.d.ts +11 -0
  42. package/target/main/ts/plugins/language-js/qualified-names.js +63 -0
  43. package/target/main/ts/plugins/language-js/structural-observations.d.ts +2 -1
  44. package/target/main/ts/plugins/language-js/structural-observations.js +18 -14
  45. package/target/main/ts/plugins/maintainability/structural-smells.js +3 -2
  46. package/target/main/ts/report/html/app/viewer-chart-bars.js +9 -5
  47. package/target/main/ts/report/json/store/pull-review-store.d.ts +0 -1
  48. package/target/main/ts/report/json/store/pull-review-store.js +2 -20
  49. package/target/main/ts/report/json/wire/pull-review-model.d.ts +49 -2
  50. package/target/main/ts/report/json/wire/pull-review-model.js +2 -0
  51. package/target/main/ts/report/markdown/pull-review.d.ts +5 -0
  52. package/target/main/ts/report/markdown/pull-review.js +122 -0
@@ -1,3 +1,2 @@
1
1
  import type { PullReview } from '../wire/pull-review-model.js';
2
- export declare function savePullReview(output: string, review: PullReview): Promise<void>;
3
2
  export declare function listPullReviews(output: string): Promise<readonly PullReview[]>;
@@ -1,5 +1,4 @@
1
- import { randomUUID } from 'node:crypto';
2
- import { lstat, mkdir, readFile, readdir, rename, rm, writeFile } from 'node:fs/promises';
1
+ import { lstat, readFile, readdir } from 'node:fs/promises';
3
2
  import { join } from 'node:path';
4
3
  import { Ajv2020 } from 'ajv/dist/2020.js';
5
4
  import metricCatalog from '../../../../resources/report/json/metric-catalog-v1.schema.json' with { type: 'json' };
@@ -8,25 +7,8 @@ import diffCoverage from '../../../../resources/report/json/diff-coverage-v1.sch
8
7
  import currentInspection from '../../../../resources/report/json/report-v2.schema.json' with { type: 'json' };
9
8
  import schema from '../../../../resources/report/json/pull-review-v1.schema.json' with { type: 'json' };
10
9
  const ajv = new Ajv2020({ schemas: [metricCatalog, currentInspection, comparison, diffCoverage] });
10
+ /** Reviews saved before 0.9 stay readable for the viewer; review no longer saves them. */
11
11
  const validate = ajv.compile(schema);
12
- export async function savePullReview(output, review) {
13
- if (!validate(review))
14
- throw new Error(`Invalid pull request review: ${ajv.errorsText(validate.errors)}`);
15
- if (review.number === null)
16
- throw new Error('Saving a pull request review requires its number');
17
- const directory = join(output, 'reviews');
18
- await mkdir(directory, { recursive: true, mode: 0o700 });
19
- if (!(await lstat(directory)).isDirectory())
20
- throw new Error('Expected a directory for PR reviews');
21
- const temporary = join(directory, `.review-${randomUUID()}.tmp`);
22
- try {
23
- await writeFile(temporary, `${JSON.stringify(review, null, 2)}\n`, { flag: 'wx', mode: 0o600 });
24
- await rename(temporary, join(directory, `${review.number}.json`));
25
- }
26
- finally {
27
- await rm(temporary, { force: true });
28
- }
29
- }
30
12
  export async function listPullReviews(output) {
31
13
  const directory = join(output, 'reviews');
32
14
  try {
@@ -1,7 +1,54 @@
1
- import type { PullReview } from '../../../core/model/pull-review.js';
2
- export type { PullReview } from '../../../core/model/pull-review.js';
1
+ import type { DiffCoverage } from '../../../core/model/diff-coverage.js';
2
+ import type { InspectionComparison } from '../../../core/model/comparison.js';
3
+ import type { Finding } from '../../../core/model/finding.js';
4
+ /** Reviews saved before 0.9 under `reviews/<number>.json`; the viewer lists them. */
5
+ export declare const storedPullReviewSchema = "urn:repometry:schema:pull-review:1";
6
+ type LineCoverage = InspectionComparison['metrics']['lineCoverage'];
7
+ type GateStatus = 'passed' | 'failed' | 'not-applicable' | 'indeterminate';
8
+ export interface PullReview {
9
+ readonly schema: typeof storedPullReviewSchema;
10
+ readonly number: number | null;
11
+ readonly generatedAt: string;
12
+ readonly base: string;
13
+ readonly mergeBase: string;
14
+ readonly head: string;
15
+ readonly coverage: {
16
+ readonly base: LineCoverage['baseline'] | null;
17
+ readonly head: LineCoverage['current'] | null;
18
+ readonly delta: number | null;
19
+ readonly reason: LineCoverage['reason'] | 'reports-unavailable' | 'head-coverage-changed';
20
+ readonly gate: {
21
+ readonly status: GateStatus;
22
+ readonly maxDrop: number;
23
+ } | null;
24
+ };
25
+ readonly diff: Pick<DiffCoverage, 'covered' | 'total' | 'percent' | 'files' | 'gaps'> & {
26
+ readonly status: DiffCoverage['status'] | 'unavailable';
27
+ readonly excludedPaths: readonly string[] | null;
28
+ readonly coverageInputDigest: string | null;
29
+ readonly uncovered: readonly {
30
+ readonly path: string;
31
+ readonly lines: readonly number[];
32
+ }[];
33
+ readonly gate: {
34
+ readonly status: GateStatus;
35
+ readonly minimumPercent: number;
36
+ readonly minimumLines: number;
37
+ } | null;
38
+ };
39
+ readonly findings: {
40
+ readonly added: readonly Finding[] | null;
41
+ readonly qualityCount: number | null;
42
+ readonly reason: 'reports-unavailable' | 'findings-unavailable' | 'partial-inspection' | 'analysis-changed' | null;
43
+ readonly gate: {
44
+ readonly status: GateStatus;
45
+ readonly maximum: number;
46
+ } | null;
47
+ };
48
+ }
3
49
  export declare const pullReviewsSchema = "urn:repometry:schema:pull-reviews:1";
4
50
  export interface PullReviews {
5
51
  readonly schema: typeof pullReviewsSchema;
6
52
  readonly reviews: readonly PullReview[];
7
53
  }
54
+ export {};
@@ -1 +1,3 @@
1
+ /** Reviews saved before 0.9 under `reviews/<number>.json`; the viewer lists them. */
2
+ export const storedPullReviewSchema = 'urn:repometry:schema:pull-review:1';
1
3
  export const pullReviewsSchema = 'urn:repometry:schema:pull-reviews:1';
@@ -0,0 +1,5 @@
1
+ import type { PullReview } from '../../core/model/pull-review.js';
2
+ /** The first line of every review comment, which finds the comment to update. */
3
+ export declare const reviewMarker = "<!-- repometry-review -->";
4
+ /** The review as Markdown: the run summary and the pull request comment carry the same text. */
5
+ export declare function pullReviewMarkdown(review: PullReview): string;
@@ -0,0 +1,122 @@
1
+ import { annotationLimit } from '../../core/model/pull-review.js';
2
+ /** The first line of every review comment, which finds the comment to update. */
3
+ export const reviewMarker = '<!-- repometry-review -->';
4
+ const maxLength = 60_000;
5
+ const groups = [
6
+ { effect: 'new', title: 'New', folded: false }, { effect: 'worsened', title: 'Worsened', folded: false },
7
+ { effect: 'improved', title: 'Improved', folded: true }, { effect: 'touched', title: 'Touched', folded: true },
8
+ ];
9
+ const verdicts = { passed: 'Passed', failed: 'Failed', unmeasured: 'Not measured' };
10
+ /** The review as Markdown: the run summary and the pull request comment carry the same text. */
11
+ export function pullReviewMarkdown(review) {
12
+ const head = [reviewMarker, `## Repometry${review.number === null ? ' review' : ` · PR #${review.number}`}`, '', headline(review), '',
13
+ ...facts(review).map(line => `- ${line}`), ''];
14
+ const annotated = review.issues.filter(issue => issue.effect === 'new' || issue.effect === 'worsened').length;
15
+ const ranges = review.coverage.uncovered.length;
16
+ const tail = [...annotated > annotationLimit ? [`Annotated on the diff: ${annotationLimit} of ${annotated}.`, ''] : [],
17
+ ...ranges > annotationLimit ? [`Annotated on the diff: ${annotationLimit} of ${ranges} uncovered ranges.`, ''] : []];
18
+ const { lines, cut } = issueTables(review, maxLength - [...head, ...tail].join('\n').length - 200);
19
+ return [...head, ...lines, ...tail, ...cut ? [`${cut} more issues not shown.`, ''] : []].join('\n');
20
+ }
21
+ /**
22
+ * One table per group, then the uncovered changed lines, with as many rows as the budget allows. Rows keep their order
23
+ * of importance: once one doesn't fit, every later row is counted as cut.
24
+ */
25
+ function issueTables(review, budget) {
26
+ const tables = [...groups.map(({ effect, title, folded }) => ({ title, folded, heads: ['File', 'Issue'],
27
+ rows: review.issues.filter(issue => issue.effect === effect).map(issue => `| ${place(issue.finding)} | ${describe(issue)} |`) })),
28
+ { title: 'Changed lines without coverage', folded: true, heads: ['Changed lines'],
29
+ rows: review.coverage.uncovered.map(({ path, first, last }) => `| ${location({ path, line: first, endLine: last })} |`) }];
30
+ const lines = [];
31
+ let left = budget;
32
+ let cut = 0;
33
+ for (const table of tables) {
34
+ const rendered = cut ? { lines: [], length: 0, cut: table.rows.length } : fitTable(table, left);
35
+ lines.push(...rendered.lines);
36
+ left -= rendered.length;
37
+ cut += rendered.cut;
38
+ }
39
+ return { lines, cut };
40
+ }
41
+ function fitTable({ title, folded, heads, rows }, budget) {
42
+ const header = [...folded ? [`<details><summary>${title} (${rows.length})</summary>`, ''] : [`### ${title}`, ''],
43
+ `| ${heads.join(' | ')} |`, `| ${heads.map(() => '---').join(' | ')} |`];
44
+ const closing = folded ? ['', '</details>', ''] : [''];
45
+ const kept = [];
46
+ let length = [...header, ...closing].join('\n').length + 1;
47
+ for (const row of rows) {
48
+ if (length + row.length + 1 > budget)
49
+ break;
50
+ length += row.length + 1;
51
+ kept.push(row);
52
+ }
53
+ const cut = rows.length - kept.length;
54
+ return kept.length ? { lines: [...header, ...kept, ...closing], length, cut } : { lines: [], length: 0, cut };
55
+ }
56
+ function headline({ verdict, issues, base, mergeBase, head, changedFiles }) {
57
+ const counts = groups.map(({ effect }) => [effect, issues.filter(issue => issue.effect === effect).length])
58
+ .filter(([, count]) => count > 0).map(([effect, count]) => `${count} ${effect}`);
59
+ const what = changedFiles === 0 ? `no changes since \`${base}\``
60
+ : `${counts.length ? counts.join(' · ') : 'no issues'} on the lines changed since \`${base}\``;
61
+ return `**${verdicts[verdict]}** · ${what} (\`${mergeBase.slice(0, 7)}\` → \`${head.slice(0, 7)}\`)`;
62
+ }
63
+ function facts({ coverage, debt, unmeasured, notes }) {
64
+ const lines = [coverageLine(coverage)];
65
+ if (debt.added || debt.removed)
66
+ lines.push(`Debt: +${debt.added} min added, −${debt.removed} min removed`);
67
+ lines.push(...few(unmeasured).map(reason => `Not measured: ${typeof reason === 'string' ? reason : reasonText(reason)}`));
68
+ const unmapped = coverage.unmapped.map(({ path }) => code(path));
69
+ if (unmapped.length) {
70
+ const named = unmapped.length > 3 ? `${unmapped.slice(0, 3).join(', ')} and ${unmapped.length - 3} more` : unmapped.join(', ');
71
+ lines.push(`Coverage has no record for ${unmapped.length} changed ${unmapped.length === 1 ? 'file' : 'files'}: ${named}`);
72
+ }
73
+ lines.push(...few(notes).map(reason => `Note: ${typeof reason === 'string' ? reason : reasonText(reason)}`));
74
+ return lines;
75
+ }
76
+ /** The first reasons, and a count of the rest. */
77
+ function few(reasons, shown = 5) {
78
+ return reasons.length > shown ? [...reasons.slice(0, shown), `${reasons.length - shown} more`] : [...reasons];
79
+ }
80
+ function coverageLine({ imported, covered, total, percent, gate }) {
81
+ if (!imported)
82
+ return 'Coverage not imported';
83
+ const measured = percent === null ? 'no measured lines' : `${percent.toFixed(2)}% (${covered} of ${total} lines)`;
84
+ if (gate === null)
85
+ return `Changed-line coverage: ${measured}`;
86
+ const status = gate.status === 'not-applicable' ? `applies from ${gate.minimumLines} measured lines` : gate.status;
87
+ return `Changed-line coverage: ${measured}, minimum ${gate.minimumPercent}%, ${status}`;
88
+ }
89
+ function reasonText({ operation, path, message }) {
90
+ return [operation, path === undefined ? undefined : `on ${code(path)}`].filter(Boolean).join(' ') + (operation || path ? ': ' : '')
91
+ + cell(message);
92
+ }
93
+ function describe({ effect, finding, before, fixed, context }) {
94
+ const parts = [clip(cell(finding.message))];
95
+ if (fixed)
96
+ parts.push('fixed');
97
+ else if ((effect === 'worsened' || effect === 'improved') && before !== null)
98
+ parts.push(`was ${before}`);
99
+ if (context.length)
100
+ parts.push(`same code as ${context.map(location).join(', ')}`);
101
+ return parts.join(', ');
102
+ }
103
+ function place({ path, line, endLine }) {
104
+ if (path === undefined)
105
+ return '—';
106
+ return line === undefined ? code(path) : location({ path, line, endLine: endLine ?? line });
107
+ }
108
+ function location({ path, line, endLine }) {
109
+ return code(`${path}:${line}${endLine > line ? `–${endLine}` : ''}`);
110
+ }
111
+ function code(value) {
112
+ return `<code>${cell(value)}</code>`;
113
+ }
114
+ /** A long message is shortened, so one issue can't push the others out of the comment. */
115
+ function clip(value, length = 500) {
116
+ return value.length > length ? `${value.slice(0, length - 1)}…` : value;
117
+ }
118
+ function cell(value) {
119
+ return value.replaceAll('&', '&amp;').replaceAll('<', '&lt;').replaceAll('>', '&gt;').replaceAll('|', '&#124;')
120
+ .replace(/[\r\n]+/g, ' ');
121
+ }
122
+ //# sourceMappingURL=pull-review.js.map