repometry 0.0.0-dev.0 → 0.1.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/LICENSE +21 -0
- package/README.md +24 -1
- package/package.json +48 -7
- package/target/main/resources/report/json/compact-json-v1.schema.json +24 -0
- package/target/main/resources/report/json/comparison-v1.schema.json +65 -0
- package/target/main/resources/report/json/config-v1.schema.json +34 -0
- package/target/main/resources/report/json/diff-coverage-v1.schema.json +55 -0
- package/target/main/resources/report/json/file-metrics-v1.schema.json +134 -0
- package/target/main/resources/report/json/inspection-context-v1.schema.json +172 -0
- package/target/main/resources/report/json/metabranch-v1.schema.json +9 -0
- package/target/main/resources/report/json/metric-catalog-v1.schema.json +31 -0
- package/target/main/resources/report/json/public-history-v1.schema.json +50 -0
- package/target/main/resources/report/json/pull-review-v1.schema.json +97 -0
- package/target/main/resources/report/json/quality-history-v1.schema.json +51 -0
- package/target/main/resources/report/json/report-v1.schema.json +65 -0
- package/target/main/resources/report/json/report-v2.schema.json +238 -0
- package/target/main/resources/report/json/repository-summary-v1.schema.json +181 -0
- package/target/main/resources/report/json/viewer-details-v1.schema.json +343 -0
- package/target/main/resources/report/json/viewer-inspection-v1.schema.json +13 -0
- package/target/main/resources/viewer/files.css +578 -0
- package/target/main/resources/viewer/index.html +130 -0
- package/target/main/resources/viewer/projects.html +52 -0
- package/target/main/resources/viewer/styles.css +586 -0
- package/target/main/resources/viewer/viewer-pulls.css +64 -0
- package/target/main/ts/api/gh/github-pr-comment.d.ts +4 -0
- package/target/main/ts/api/gh/github-pr-comment.js +107 -0
- package/target/main/ts/api/gh/github.d.ts +6 -0
- package/target/main/ts/api/gh/github.js +91 -0
- package/target/main/ts/api/git/blob-batch.d.ts +10 -0
- package/target/main/ts/api/git/blob-batch.js +105 -0
- package/target/main/ts/api/git/changes.d.ts +22 -0
- package/target/main/ts/api/git/changes.js +107 -0
- package/target/main/ts/api/git/commit-metadata.d.ts +3 -0
- package/target/main/ts/api/git/commit-metadata.js +54 -0
- package/target/main/ts/api/git/git-churn.d.ts +9 -0
- package/target/main/ts/api/git/git-churn.js +70 -0
- package/target/main/ts/api/git/git-reader.d.ts +16 -0
- package/target/main/ts/api/git/git-reader.js +73 -0
- package/target/main/ts/api/git/git.d.ts +21 -0
- package/target/main/ts/api/git/git.js +152 -0
- package/target/main/ts/api/git/inspection-source.d.ts +15 -0
- package/target/main/ts/api/git/inspection-source.js +30 -0
- package/target/main/ts/api/git/local-sources.d.ts +5 -0
- package/target/main/ts/api/git/local-sources.js +46 -0
- package/target/main/ts/api/git/local.d.ts +3 -0
- package/target/main/ts/api/git/local.js +35 -0
- package/target/main/ts/api/git/metabranch-worktree.d.ts +1 -0
- package/target/main/ts/api/git/metabranch-worktree.js +34 -0
- package/target/main/ts/api/git/source-cache.d.ts +3 -0
- package/target/main/ts/api/git/source-cache.js +56 -0
- package/target/main/ts/api/import/finding-import.d.ts +9 -0
- package/target/main/ts/api/import/finding-import.js +226 -0
- package/target/main/ts/api/import/lcov-files.d.ts +5 -0
- package/target/main/ts/api/import/lcov-files.js +13 -0
- package/target/main/ts/api/import/profile-file.d.ts +4 -0
- package/target/main/ts/api/import/profile-file.js +16 -0
- package/target/main/ts/api/worker/remote-executor.d.ts +2 -0
- package/target/main/ts/api/worker/remote-executor.js +45 -0
- package/target/main/ts/api/worker/worker-protocol.d.ts +7 -0
- package/target/main/ts/api/worker/worker-protocol.js +101 -0
- package/target/main/ts/api/worker/worker.d.ts +5 -0
- package/target/main/ts/api/worker/worker.js +85 -0
- package/target/main/ts/cli/arguments.d.ts +28 -0
- package/target/main/ts/cli/arguments.js +110 -0
- package/target/main/ts/cli/config.d.ts +14 -0
- package/target/main/ts/cli/config.js +60 -0
- package/target/main/ts/cli/github-pr-comment.d.ts +1 -0
- package/target/main/ts/cli/github-pr-comment.js +12 -0
- package/target/main/ts/cli/imports.d.ts +7 -0
- package/target/main/ts/cli/imports.js +29 -0
- package/target/main/ts/cli/main.d.ts +2 -0
- package/target/main/ts/cli/main.js +343 -284
- package/target/main/ts/cli/reporting.d.ts +18 -0
- package/target/main/ts/cli/reporting.js +108 -0
- package/target/main/ts/cli/revisions.d.ts +6 -0
- package/target/main/ts/cli/revisions.js +15 -0
- package/target/main/ts/cli/store.d.ts +13 -0
- package/target/main/ts/cli/store.js +38 -0
- package/target/main/ts/compose/analyze-snapshot.d.ts +4 -0
- package/target/main/ts/compose/analyze-snapshot.js +163 -0
- package/target/main/ts/compose/attach-churn.d.ts +2 -0
- package/target/main/ts/compose/attach-churn.js +37 -0
- package/target/main/ts/compose/diff-coverage.d.ts +20 -0
- package/target/main/ts/compose/diff-coverage.js +56 -0
- package/target/main/ts/compose/inspect.d.ts +23 -0
- package/target/main/ts/compose/inspect.js +44 -0
- package/target/main/ts/compose/plugin-list.d.ts +6 -0
- package/target/main/ts/compose/plugin-list.js +9 -0
- package/target/main/ts/compose/prepare-metabranch.d.ts +9 -0
- package/target/main/ts/compose/prepare-metabranch.js +16 -0
- package/target/main/ts/compose/review.d.ts +16 -0
- package/target/main/ts/compose/review.js +38 -0
- package/target/main/ts/compose/sources.d.ts +3 -0
- package/target/main/ts/compose/sources.js +29 -0
- package/target/main/ts/core/engine/execute.d.ts +10 -0
- package/target/main/ts/core/engine/execute.js +49 -0
- package/target/main/ts/core/engine/manifest.d.ts +4 -0
- package/target/main/ts/core/engine/manifest.js +104 -0
- package/target/main/ts/core/engine/plan.d.ts +22 -0
- package/target/main/ts/core/engine/plan.js +106 -0
- package/target/main/ts/core/engine/plugin.d.ts +59 -0
- package/target/main/ts/core/engine/plugin.js +4 -0
- package/target/main/ts/core/engine/report-products.d.ts +16 -0
- package/target/main/ts/core/engine/report-products.js +85 -0
- package/target/main/ts/core/model/changes.d.ts +8 -0
- package/target/main/ts/core/model/changes.js +1 -0
- package/target/main/ts/core/model/churn.d.ts +11 -0
- package/target/main/ts/core/model/churn.js +5 -0
- package/target/main/ts/core/model/clone-source.d.ts +16 -0
- package/target/main/ts/core/model/clone-source.js +14 -0
- package/target/main/ts/core/model/code-structure.d.ts +30 -0
- package/target/main/ts/core/model/code-structure.js +31 -0
- package/target/main/ts/core/model/comparison.d.ts +24 -0
- package/target/main/ts/core/model/comparison.js +1 -0
- package/target/main/ts/core/model/diff-coverage.d.ts +23 -0
- package/target/main/ts/core/model/diff-coverage.js +1 -0
- package/target/main/ts/core/model/finding-ignore.d.ts +8 -0
- package/target/main/ts/core/model/finding-ignore.js +35 -0
- package/target/main/ts/core/model/finding.d.ts +28 -0
- package/target/main/ts/core/model/finding.js +29 -0
- package/target/main/ts/core/model/ignore-rule.d.ts +11 -0
- package/target/main/ts/core/model/ignore-rule.js +1 -0
- package/target/main/ts/core/model/measurement.d.ts +27 -0
- package/target/main/ts/core/model/measurement.js +24 -0
- package/target/main/ts/core/model/metric-descriptor.d.ts +17 -0
- package/target/main/ts/core/model/metric-descriptor.js +1 -0
- package/target/main/ts/core/model/plugin-data.d.ts +25 -0
- package/target/main/ts/core/model/plugin-data.js +1 -0
- package/target/main/ts/core/model/pull-review.d.ts +48 -0
- package/target/main/ts/core/model/pull-review.js +1 -0
- package/target/main/ts/core/model/quality-summary.d.ts +42 -0
- package/target/main/ts/core/model/quality-summary.js +64 -0
- package/target/main/ts/core/model/report.d.ts +32 -0
- package/target/main/ts/core/model/report.js +5 -0
- package/target/main/ts/core/model/scanner-import.d.ts +39 -0
- package/target/main/ts/core/model/scanner-import.js +28 -0
- package/target/main/ts/core/model/snapshot.d.ts +15 -0
- package/target/main/ts/core/model/snapshot.js +50 -0
- package/target/main/ts/core/model/source-locator.d.ts +7 -0
- package/target/main/ts/core/model/source-locator.js +1 -0
- package/target/main/ts/core/model/source.d.ts +23 -0
- package/target/main/ts/core/model/source.js +1 -0
- package/target/main/ts/core/ports/report.d.ts +9 -0
- package/target/main/ts/core/ports/report.js +1 -0
- package/target/main/ts/core/ports/source-provider.d.ts +14 -0
- package/target/main/ts/core/ports/source-provider.js +4 -0
- package/target/main/ts/core/profile/exclusions.d.ts +11 -0
- package/target/main/ts/core/profile/exclusions.js +57 -0
- package/target/main/ts/core/profile/maintainability.d.ts +31 -0
- package/target/main/ts/core/profile/maintainability.js +9 -0
- package/target/main/ts/core/profile/profile.d.ts +4 -0
- package/target/main/ts/core/profile/profile.js +10 -0
- package/target/main/ts/core/usecases/compare.d.ts +6 -0
- package/target/main/ts/core/usecases/compare.js +145 -0
- package/target/main/ts/core/usecases/diff-coverage-policy.d.ts +6 -0
- package/target/main/ts/core/usecases/diff-coverage-policy.js +10 -0
- package/target/main/ts/core/usecases/diff-coverage.d.ts +40 -0
- package/target/main/ts/core/usecases/diff-coverage.js +15 -0
- package/target/main/ts/core/usecases/inspect.d.ts +38 -0
- package/target/main/ts/core/usecases/inspect.js +41 -0
- package/target/main/ts/core/usecases/review.d.ts +31 -0
- package/target/main/ts/core/usecases/review.js +128 -0
- package/target/main/ts/index.d.ts +35 -0
- package/target/main/ts/index.js +17 -13
- package/target/main/ts/plugin.d.ts +14 -0
- package/target/main/ts/plugin.js +5 -0
- package/target/main/ts/plugins/coverage/coverage.d.ts +23 -0
- package/target/main/ts/plugins/coverage/coverage.js +120 -0
- package/target/main/ts/plugins/coverage/diff-coverage.d.ts +7 -0
- package/target/main/ts/plugins/coverage/diff-coverage.js +89 -0
- package/target/main/ts/plugins/duplication/duplication.d.ts +4 -0
- package/target/main/ts/plugins/duplication/duplication.js +93 -0
- package/target/main/ts/plugins/language-js/babel-provider.d.ts +10 -0
- package/target/main/ts/plugins/language-js/babel-provider.js +46 -0
- package/target/main/ts/plugins/language-js/clone-candidates.d.ts +3 -0
- package/target/main/ts/plugins/language-js/clone-candidates.js +64 -0
- package/target/main/ts/plugins/language-js/code-lines.d.ts +6 -0
- package/target/main/ts/plugins/language-js/code-lines.js +35 -0
- package/target/main/ts/plugins/language-js/function-facts.d.ts +8 -0
- package/target/main/ts/plugins/language-js/function-facts.js +60 -0
- package/target/main/ts/plugins/language-js/structural-observations.d.ts +8 -0
- package/target/main/ts/plugins/language-js/structural-observations.js +79 -0
- package/target/main/ts/plugins/maintainability/maintainability.d.ts +9 -0
- package/target/main/ts/plugins/maintainability/maintainability.js +42 -0
- package/target/main/ts/plugins/maintainability/structural-smells.d.ts +3 -0
- package/target/main/ts/plugins/maintainability/structural-smells.js +15 -0
- package/target/main/ts/plugins/source/forges.d.ts +4 -0
- package/target/main/ts/plugins/source/forges.js +79 -0
- package/target/main/ts/plugins/structure/structure.d.ts +2 -0
- package/target/main/ts/plugins/structure/structure.js +37 -0
- package/target/main/ts/report/html/app/viewer-access.d.ts +19 -0
- package/target/main/ts/report/html/app/viewer-access.js +25 -0
- package/target/main/ts/report/html/app/viewer-chart.d.ts +101 -0
- package/target/main/ts/report/html/app/viewer-chart.js +711 -0
- package/target/main/ts/report/html/app/viewer-commits.d.ts +36 -0
- package/target/main/ts/report/html/app/viewer-commits.js +280 -0
- package/target/main/ts/report/html/app/viewer-data.d.ts +23 -0
- package/target/main/ts/report/html/app/viewer-data.js +105 -0
- package/target/main/ts/report/html/app/viewer-files.d.ts +117 -0
- package/target/main/ts/report/html/app/viewer-files.js +2087 -0
- package/target/main/ts/report/html/app/viewer-filter.d.ts +113 -0
- package/target/main/ts/report/html/app/viewer-filter.js +492 -0
- package/target/main/ts/report/html/app/viewer-metric-registry.d.ts +68 -0
- package/target/main/ts/report/html/app/viewer-metric-registry.js +206 -0
- package/target/main/ts/report/html/app/viewer-metrics.d.ts +44 -0
- package/target/main/ts/report/html/app/viewer-metrics.js +172 -0
- package/target/main/ts/report/html/app/viewer-profile.d.ts +17 -0
- package/target/main/ts/report/html/app/viewer-profile.js +401 -0
- package/target/main/ts/report/html/app/viewer-projects.d.ts +1 -0
- package/target/main/ts/report/html/app/viewer-projects.js +90 -0
- package/target/main/ts/report/html/app/viewer-pulls.d.ts +3 -0
- package/target/main/ts/report/html/app/viewer-pulls.js +242 -0
- package/target/main/ts/report/html/app/viewer-source.d.ts +75 -0
- package/target/main/ts/report/html/app/viewer-source.js +313 -0
- package/target/main/ts/report/html/app/viewer-trends.d.ts +74 -0
- package/target/main/ts/report/html/app/viewer-trends.js +413 -0
- package/target/main/ts/report/html/app/viewer.d.ts +1 -0
- package/target/main/ts/report/html/app/viewer.js +794 -0
- package/target/main/ts/report/html/site/badges.d.ts +14 -0
- package/target/main/ts/report/html/site/badges.js +39 -0
- package/target/main/ts/report/html/site/export-site.d.ts +21 -0
- package/target/main/ts/report/html/site/export-site.js +157 -0
- package/target/main/ts/report/html/site/exported-server.d.ts +6 -0
- package/target/main/ts/report/html/site/exported-server.js +70 -0
- package/target/main/ts/report/html/site/local-server.d.ts +7 -0
- package/target/main/ts/report/html/site/local-server.js +64 -0
- package/target/main/ts/report/html/site/projects.d.ts +3 -0
- package/target/main/ts/report/html/site/projects.js +79 -0
- package/target/main/ts/report/html/site/server.d.ts +4 -0
- package/target/main/ts/report/html/site/server.js +64 -0
- package/target/main/ts/report/html/site/source-route.d.ts +6 -0
- package/target/main/ts/report/html/site/source-route.js +22 -0
- package/target/main/ts/report/json/projections/commit-summary.d.ts +11 -0
- package/target/main/ts/report/json/projections/commit-summary.js +118 -0
- package/target/main/ts/report/json/projections/components.d.ts +4 -0
- package/target/main/ts/report/json/projections/components.js +29 -0
- package/target/main/ts/report/json/projections/file-metrics.d.ts +5 -0
- package/target/main/ts/report/json/projections/file-metrics.js +243 -0
- package/target/main/ts/report/json/projections/inspection-context.d.ts +6 -0
- package/target/main/ts/report/json/projections/inspection-context.js +92 -0
- package/target/main/ts/report/json/projections/inspection-details.d.ts +11 -0
- package/target/main/ts/report/json/projections/inspection-details.js +69 -0
- package/target/main/ts/report/json/projections/metric-values.d.ts +7 -0
- package/target/main/ts/report/json/projections/metric-values.js +57 -0
- package/target/main/ts/report/json/projections/public-history.d.ts +4 -0
- package/target/main/ts/report/json/projections/public-history.js +16 -0
- package/target/main/ts/report/json/store/components.d.ts +2 -0
- package/target/main/ts/report/json/store/components.js +31 -0
- package/target/main/ts/report/json/store/local-store.d.ts +13 -0
- package/target/main/ts/report/json/store/local-store.js +179 -0
- package/target/main/ts/report/json/store/pull-review-store.d.ts +3 -0
- package/target/main/ts/report/json/store/pull-review-store.js +51 -0
- package/target/main/ts/report/json/validation/metabranch.d.ts +4 -0
- package/target/main/ts/report/json/validation/metabranch.js +7 -0
- package/target/main/ts/report/json/validation/report.d.ts +15 -0
- package/target/main/ts/report/json/validation/report.js +64 -0
- package/target/main/ts/report/json/validation/summary-validation.d.ts +4 -0
- package/target/main/ts/report/json/validation/summary-validation.js +27 -0
- package/target/main/ts/report/json/wire/compact-json.d.ts +11 -0
- package/target/main/ts/report/json/wire/compact-json.js +76 -0
- package/target/main/ts/report/json/wire/component-model.d.ts +9 -0
- package/target/main/ts/report/json/wire/component-model.js +1 -0
- package/target/main/ts/report/json/wire/domain-types.d.ts +2 -0
- package/target/main/ts/report/json/wire/domain-types.js +1 -0
- package/target/main/ts/report/json/wire/file-metrics-model.d.ts +77 -0
- package/target/main/ts/report/json/wire/file-metrics-model.js +1 -0
- package/target/main/ts/report/json/wire/inspection-context-model.d.ts +26 -0
- package/target/main/ts/report/json/wire/inspection-context-model.js +1 -0
- package/target/main/ts/report/json/wire/inspection-details-model.d.ts +82 -0
- package/target/main/ts/report/json/wire/inspection-details-model.js +1 -0
- package/target/main/ts/report/json/wire/project-model.d.ts +7 -0
- package/target/main/ts/report/json/wire/project-model.js +1 -0
- package/target/main/ts/report/json/wire/public-history-model.d.ts +42 -0
- package/target/main/ts/report/json/wire/public-history-model.js +6 -0
- package/target/main/ts/report/json/wire/pull-review-model.d.ts +7 -0
- package/target/main/ts/report/json/wire/pull-review-model.js +1 -0
- package/target/main/ts/report/json/wire/summary-model.d.ts +45 -0
- package/target/main/ts/report/json/wire/summary-model.js +1 -0
- package/target/main/ts/report.d.ts +11 -0
- package/target/main/ts/report.js +5 -0
- package/index.js +0 -1
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { isRecord } from '../../core/engine/plugin.js';
|
|
2
|
+
const marker = '<!-- repometry-coverage:v1 -->';
|
|
3
|
+
const sha = /^(?:[a-f0-9]{40}|[a-f0-9]{64})$/;
|
|
4
|
+
const repositoryName = /^[a-z0-9_.-]+\/[a-z0-9_.-]+$/i;
|
|
5
|
+
export async function publishPullCoverageComment(env, request = fetch) {
|
|
6
|
+
const { GITHUB_EVENT_NAME, GITHUB_REPOSITORY: repository, GITHUB_API_URL, REPOMETRY_COMMENT_TOKEN: token, REPOMETRY_PR_REPOSITORY, REPOMETRY_PR_NUMBER, REPOMETRY_PR_HEAD, REPOMETRY_PR_BASE, REPOMETRY_REVIEW_HEAD, REPOMETRY_REVIEW_BASE } = env;
|
|
7
|
+
if (GITHUB_EVENT_NAME !== 'pull_request' || !repository || REPOMETRY_PR_REPOSITORY !== repository
|
|
8
|
+
|| REPOMETRY_REVIEW_HEAD !== REPOMETRY_PR_HEAD || REPOMETRY_REVIEW_BASE !== REPOMETRY_PR_BASE)
|
|
9
|
+
return 'skipped';
|
|
10
|
+
if (!repositoryName.test(repository) || repository.split('/').some(part => part === '.' || part === '..')
|
|
11
|
+
|| !sha.test(REPOMETRY_PR_HEAD ?? '') || !sha.test(REPOMETRY_PR_BASE ?? ''))
|
|
12
|
+
return 'skipped';
|
|
13
|
+
const number = count(REPOMETRY_PR_NUMBER);
|
|
14
|
+
if (!number)
|
|
15
|
+
return 'skipped';
|
|
16
|
+
const body = coverageComment(env);
|
|
17
|
+
if (body === null)
|
|
18
|
+
return 'skipped';
|
|
19
|
+
if (!GITHUB_API_URL || !token)
|
|
20
|
+
throw new Error('Repometry PR comments require the GitHub Actions token and API URL');
|
|
21
|
+
const api = new URL(GITHUB_API_URL);
|
|
22
|
+
if (api.protocol !== 'https:' || api.username || api.password || api.search || api.hash)
|
|
23
|
+
throw new Error('Invalid GitHub API URL');
|
|
24
|
+
api.pathname = api.pathname.replace(/\/+$/, '') + '/';
|
|
25
|
+
const prefix = `repos/${repository}`;
|
|
26
|
+
const signal = AbortSignal.timeout(20_000);
|
|
27
|
+
async function call(path, method = 'GET', content) {
|
|
28
|
+
const response = await request(new URL(path, api), {
|
|
29
|
+
method, signal, redirect: 'error',
|
|
30
|
+
headers: { Accept: 'application/vnd.github+json', Authorization: `Bearer ${token}`, 'Content-Type': 'application/json' },
|
|
31
|
+
body: content === undefined ? undefined : JSON.stringify({ body: content }),
|
|
32
|
+
});
|
|
33
|
+
if (!response.ok)
|
|
34
|
+
throw new Error(`GitHub PR comment request returned HTTP ${response.status}`);
|
|
35
|
+
return response;
|
|
36
|
+
}
|
|
37
|
+
const pull = await (await call(`${prefix}/pulls/${number}`)).json();
|
|
38
|
+
if (!isRecord(pull) || pull.state !== 'open' || !matchesRef(pull.head, REPOMETRY_PR_HEAD, repository)
|
|
39
|
+
|| !matchesRef(pull.base, REPOMETRY_PR_BASE, repository))
|
|
40
|
+
return 'skipped';
|
|
41
|
+
const comments = `${prefix}/issues/${number}/comments`;
|
|
42
|
+
for (let page = 1; page <= 100; page++) {
|
|
43
|
+
const result = await (await call(`${comments}?per_page=100&page=${page}`)).json();
|
|
44
|
+
if (!Array.isArray(result))
|
|
45
|
+
throw new Error('Invalid GitHub PR comments response');
|
|
46
|
+
const owned = result.find(isOwnedComment);
|
|
47
|
+
if (owned) {
|
|
48
|
+
if (owned.body === body)
|
|
49
|
+
return 'unchanged';
|
|
50
|
+
await call(`${prefix}/issues/comments/${owned.id}`, 'PATCH', body);
|
|
51
|
+
return 'updated';
|
|
52
|
+
}
|
|
53
|
+
if (result.length < 100) {
|
|
54
|
+
await call(comments, 'POST', body);
|
|
55
|
+
return 'created';
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
throw new Error('Could not check all GitHub PR comments for the Repometry marker');
|
|
59
|
+
}
|
|
60
|
+
function matchesRef(ref, id, repository) {
|
|
61
|
+
return isRecord(ref) && ref.sha === id && isRecord(ref.repo) && ref.repo.full_name === repository;
|
|
62
|
+
}
|
|
63
|
+
function isOwnedComment(value) {
|
|
64
|
+
return isRecord(value) && typeof value.id === 'number' && Number.isSafeInteger(value.id) && value.id > 0
|
|
65
|
+
&& typeof value.body === 'string' && value.body.startsWith(marker + '\n')
|
|
66
|
+
&& isRecord(value.user) && value.user.login === 'github-actions[bot]' && value.user.type === 'Bot';
|
|
67
|
+
}
|
|
68
|
+
function coverageComment(env) {
|
|
69
|
+
const total = decimal(env.REPOMETRY_TOTAL_PERCENT, 0, 100);
|
|
70
|
+
const delta = decimal(env.REPOMETRY_TOTAL_DELTA, -100, 100);
|
|
71
|
+
const diff = decimal(env.REPOMETRY_DIFF_PERCENT, 0, 100);
|
|
72
|
+
const covered = count(env.REPOMETRY_DIFF_COVERED);
|
|
73
|
+
const measured = count(env.REPOMETRY_DIFF_TOTAL);
|
|
74
|
+
const status = env.REPOMETRY_DIFF_STATUS;
|
|
75
|
+
if (total === null && diff === null && status !== 'partial' && status !== 'not-applicable')
|
|
76
|
+
return null;
|
|
77
|
+
const totalText = total === null ? 'Unavailable' : `${total.toFixed(2)}%`
|
|
78
|
+
+ (delta === null ? '' : ` (${delta > 0 ? '+' : ''}${delta.toFixed(2)} pp from base)`);
|
|
79
|
+
const counts = covered !== null && measured !== null && covered <= measured && measured > 0 ? ` · ${covered}/${measured} measured lines` : '';
|
|
80
|
+
const diffText = diff === null ? status === 'not-applicable' ? 'No measured changed lines' : 'Unavailable' : `${diff.toFixed(2)}%${counts}`;
|
|
81
|
+
return [marker, '### Repometry coverage', '',
|
|
82
|
+
'| Coverage | Result | Gate |', '| --- | --- | --- |',
|
|
83
|
+
`| Total | ${totalText} | ${gate(env.REPOMETRY_TOTAL_GATE)} |`,
|
|
84
|
+
`| Changed lines | ${diffText}${status === 'partial' ? ' · Partial' : ''} | ${gate(env.REPOMETRY_DIFF_GATE)} |`, '',
|
|
85
|
+
].join('\n');
|
|
86
|
+
}
|
|
87
|
+
function decimal(value, lower, upper) {
|
|
88
|
+
if (!value || !/^-?\d+(?:\.\d+)?(?:e[+-]?\d+)?$/i.test(value))
|
|
89
|
+
return null;
|
|
90
|
+
const number = Number(value);
|
|
91
|
+
return Number.isFinite(number) && number >= lower && number <= upper ? number : null;
|
|
92
|
+
}
|
|
93
|
+
function count(value) {
|
|
94
|
+
if (!value || !/^\d+$/.test(value))
|
|
95
|
+
return null;
|
|
96
|
+
const number = Number(value);
|
|
97
|
+
return Number.isSafeInteger(number) ? number : null;
|
|
98
|
+
}
|
|
99
|
+
function gate(value) {
|
|
100
|
+
switch (value) {
|
|
101
|
+
case 'passed': return 'Passed';
|
|
102
|
+
case 'failed': return 'Failed';
|
|
103
|
+
case 'not-applicable': return 'Skipped';
|
|
104
|
+
case 'indeterminate': return 'Incomplete';
|
|
105
|
+
default: return '—';
|
|
106
|
+
}
|
|
107
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { DiffCoverageReport } from '../../core/usecases/diff-coverage.js';
|
|
2
|
+
import type { PullReview } from '../../core/model/pull-review.js';
|
|
3
|
+
import type { Report } from '../../core/model/report.js';
|
|
4
|
+
export declare function writeGitHubResult(report: Report): Promise<void>;
|
|
5
|
+
export declare function writeGitHubDiffCoverage(report: DiffCoverageReport): Promise<void>;
|
|
6
|
+
export declare function writeGitHubReview(report: PullReview, repository?: string): Promise<void>;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { appendFile, realpath } from 'node:fs/promises';
|
|
2
|
+
import { isAbsolute, relative, resolve, sep } from 'node:path';
|
|
3
|
+
export async function writeGitHubResult(report) {
|
|
4
|
+
const { output, summary } = githubFiles();
|
|
5
|
+
await appendFile(output, `subject=${report.subject.id}\nstatus=${report.status}\n`);
|
|
6
|
+
await appendFile(summary, [
|
|
7
|
+
'## Repometry', '',
|
|
8
|
+
`**${report.status}** · \`${report.subject.id}\``, '',
|
|
9
|
+
`Parsed ${report.scope.parsed} of ${report.scope.files} files.`,
|
|
10
|
+
`Measurements: ${report.measurements.length}. Diagnostics: ${report.diagnostics.length}.`, '', '',
|
|
11
|
+
].join('\n'));
|
|
12
|
+
}
|
|
13
|
+
export async function writeGitHubDiffCoverage(report) {
|
|
14
|
+
const { output, summary } = githubFiles();
|
|
15
|
+
const decision = report.decision?.status ?? '';
|
|
16
|
+
await appendFile(output, `status=${report.status}\npercent=${report.percent ?? ''}\ndecision=${decision}\n`);
|
|
17
|
+
const observed = report.percent === null ? '—' : `${report.percent.toFixed(2)}%`;
|
|
18
|
+
const policy = report.decision === null ? [] : [`Minimum ${report.decision.minimumPercent}% · **${report.decision.status}**`];
|
|
19
|
+
await appendFile(summary, ['## Repometry diff coverage', '',
|
|
20
|
+
`**${report.status}** · \`${report.base}\` → \`${report.head}\``, '',
|
|
21
|
+
`Observed coverage ${observed} (${report.covered}/${report.total} reported lines). Gaps: ${report.gaps.length}.`,
|
|
22
|
+
...policy, '', '',
|
|
23
|
+
].join('\n'));
|
|
24
|
+
}
|
|
25
|
+
export async function writeGitHubReview(report, repository) {
|
|
26
|
+
const { output, summary } = githubFiles();
|
|
27
|
+
const { diff, coverage, findings } = report;
|
|
28
|
+
await appendFile(output, [
|
|
29
|
+
`status=${diff.status}`, `percent=${diff.percent ?? ''}`, `decision=${diff.gate?.status ?? ''}`,
|
|
30
|
+
`review-base=${report.base}`, `review-head=${report.head}`, `diff-covered=${diff.covered}`, `diff-total=${diff.total}`,
|
|
31
|
+
`coverage-percent=${coverage.head?.value ?? ''}`, `coverage-delta=${coverage.delta ?? ''}`,
|
|
32
|
+
`coverage-decision=${coverage.gate?.status ?? ''}`,
|
|
33
|
+
`new-issues=${findings.qualityCount ?? ''}`, `issues-decision=${findings.gate?.status ?? ''}`, '',
|
|
34
|
+
].join('\n'));
|
|
35
|
+
const label = report.number === null ? 'Repometry review' : `Repometry review · PR #${report.number}`;
|
|
36
|
+
const totals = [
|
|
37
|
+
'| Check | Result | Gate |',
|
|
38
|
+
'| --- | --- | --- |',
|
|
39
|
+
`| Total coverage | ${percent(coverage.base?.value)} → ${percent(coverage.head?.value)} | ${coverage.gate ? `${coverage.gate.status}, max drop ${coverage.gate.maxDrop} pp` : '—'} |`,
|
|
40
|
+
`| Changed-line coverage | ${percent(diff.percent)} (${diff.covered}/${diff.total}), ${diff.status} | ${diff.gate ? `${diff.gate.status}, min ${diff.gate.minimumPercent}% on ${diff.gate.minimumLines}+ lines` : '—'} |`,
|
|
41
|
+
`| New quality issues | ${findings.qualityCount ?? 'Unavailable'} | ${findings.gate ? `${findings.gate.status}, max ${findings.gate.maximum}` : '—'} |`,
|
|
42
|
+
];
|
|
43
|
+
const files = diff.files.slice(0, 50).map(file => {
|
|
44
|
+
const result = file.covered === null || file.total === null ? '—' : `${file.covered}/${file.total}`;
|
|
45
|
+
const missed = file.covered === null || file.total === null ? '—' : String(file.total - file.covered);
|
|
46
|
+
return `| ${pathCode(file.path)} | ${file.addedLines} | ${result} | ${missed} |`;
|
|
47
|
+
});
|
|
48
|
+
const changes = files.length === 0 ? [] : ['', '### Changed files', '',
|
|
49
|
+
'| File | Added lines | Covered / measured | Missed |',
|
|
50
|
+
'| --- | ---: | ---: | ---: |', ...files,
|
|
51
|
+
...(diff.files.length > files.length ? ['', `Showing ${files.length} of ${diff.files.length} files.`] : [])];
|
|
52
|
+
await appendFile(summary, [`## ${label}`, '', `\`${report.base}\` → \`${report.head}\``, '',
|
|
53
|
+
...totals, ...(diff.gaps.length ? ['', `Coverage gaps in ${diff.gaps.length} records.`] : []), ...changes, '', '',
|
|
54
|
+
].join('\n'));
|
|
55
|
+
if (repository !== undefined && diff.uncovered.length > 0)
|
|
56
|
+
await annotateUncovered(diff.uncovered, repository);
|
|
57
|
+
}
|
|
58
|
+
function percent(value) {
|
|
59
|
+
return value == null ? '—' : `${value.toFixed(2)}%`;
|
|
60
|
+
}
|
|
61
|
+
function pathCode(value) {
|
|
62
|
+
const clean = value.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>')
|
|
63
|
+
.replaceAll('|', '|').replaceAll('\r', ' ').replaceAll('\n', ' ');
|
|
64
|
+
return `<code>${clean}</code>`;
|
|
65
|
+
}
|
|
66
|
+
async function annotateUncovered(uncovered, repository) {
|
|
67
|
+
const [workspace, root] = await Promise.all([realpath(process.env.GITHUB_WORKSPACE ?? process.cwd()), realpath(repository)]);
|
|
68
|
+
const prefix = relative(workspace, root);
|
|
69
|
+
if (isAbsolute(prefix) || prefix.split(sep).includes('..'))
|
|
70
|
+
return;
|
|
71
|
+
let count = 0;
|
|
72
|
+
for (const { path, lines } of uncovered) {
|
|
73
|
+
const checkoutPath = relative(workspace, resolve(root, path));
|
|
74
|
+
if (isAbsolute(checkoutPath) || checkoutPath.split(sep).includes('..'))
|
|
75
|
+
continue;
|
|
76
|
+
const file = checkoutPath.split(sep).join('/').replaceAll('%', '%25').replaceAll('\r', '%0D')
|
|
77
|
+
.replaceAll('\n', '%0A').replaceAll(':', '%3A').replaceAll(',', '%2C');
|
|
78
|
+
for (const line of lines) {
|
|
79
|
+
process.stderr.write(`::warning file=${file},line=${line},title=Repometry diff coverage::Changed line is not covered\n`);
|
|
80
|
+
if (++count === 10)
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
function githubFiles() {
|
|
86
|
+
const output = process.env.GITHUB_OUTPUT;
|
|
87
|
+
const summary = process.env.GITHUB_STEP_SUMMARY;
|
|
88
|
+
if (!output || !summary)
|
|
89
|
+
throw new Error('GitHub reporting requires GITHUB_OUTPUT and GITHUB_STEP_SUMMARY');
|
|
90
|
+
return { output, summary };
|
|
91
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare class GitBlobBatch {
|
|
2
|
+
private readonly directory;
|
|
3
|
+
private session?;
|
|
4
|
+
private tail;
|
|
5
|
+
private timer?;
|
|
6
|
+
constructor(directory: string);
|
|
7
|
+
read(oid: string, signal?: AbortSignal): Promise<Buffer | undefined>;
|
|
8
|
+
close(): void;
|
|
9
|
+
private readOne;
|
|
10
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { validSubjectId } from '../../core/model/report.js';
|
|
3
|
+
const idleTimeout = 10_000;
|
|
4
|
+
const maxBlobBytes = 64 * 1024 * 1024;
|
|
5
|
+
export class GitBlobBatch {
|
|
6
|
+
directory;
|
|
7
|
+
session;
|
|
8
|
+
tail = Promise.resolve();
|
|
9
|
+
timer;
|
|
10
|
+
constructor(directory) {
|
|
11
|
+
this.directory = directory;
|
|
12
|
+
}
|
|
13
|
+
read(oid, signal) {
|
|
14
|
+
if (!validSubjectId('commit', oid))
|
|
15
|
+
throw new Error('Invalid source blob ID');
|
|
16
|
+
const pending = this.tail.then(() => this.readOne(oid, signal));
|
|
17
|
+
this.tail = pending.catch(() => undefined);
|
|
18
|
+
return pending;
|
|
19
|
+
}
|
|
20
|
+
close() {
|
|
21
|
+
clearTimeout(this.timer);
|
|
22
|
+
this.session?.close();
|
|
23
|
+
this.session = undefined;
|
|
24
|
+
}
|
|
25
|
+
async readOne(oid, signal) {
|
|
26
|
+
signal?.throwIfAborted();
|
|
27
|
+
clearTimeout(this.timer);
|
|
28
|
+
const session = this.session ??= new BlobSession(this.directory);
|
|
29
|
+
const abort = () => this.close();
|
|
30
|
+
signal?.addEventListener('abort', abort, { once: true });
|
|
31
|
+
try {
|
|
32
|
+
return await session.read(oid);
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
this.close();
|
|
36
|
+
throw error;
|
|
37
|
+
}
|
|
38
|
+
finally {
|
|
39
|
+
signal?.removeEventListener('abort', abort);
|
|
40
|
+
if (this.session)
|
|
41
|
+
this.timer = setTimeout(() => this.close(), idleTimeout).unref();
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
class BlobSession {
|
|
46
|
+
child;
|
|
47
|
+
chunks;
|
|
48
|
+
buffer = Buffer.alloc(0);
|
|
49
|
+
constructor(directory) {
|
|
50
|
+
this.child = spawn('git', ['--no-replace-objects', 'cat-file', '--batch'], { cwd: directory });
|
|
51
|
+
this.child.stderr.resume();
|
|
52
|
+
this.child.stdin.on('error', () => { });
|
|
53
|
+
this.child.on('error', error => this.child.stdout.destroy(error));
|
|
54
|
+
this.chunks = this.child.stdout[Symbol.asyncIterator]();
|
|
55
|
+
}
|
|
56
|
+
close() {
|
|
57
|
+
this.child.stdin.end();
|
|
58
|
+
this.child.kill();
|
|
59
|
+
}
|
|
60
|
+
async read(oid) {
|
|
61
|
+
await new Promise((resolve, reject) => this.child.stdin.write(oid + '\n', error => error ? reject(error) : resolve()));
|
|
62
|
+
const header = await this.header();
|
|
63
|
+
if (header === oid + ' missing')
|
|
64
|
+
return undefined;
|
|
65
|
+
const parsed = /^([a-f0-9]+) blob ([0-9]+)$/.exec(header);
|
|
66
|
+
const size = Number(parsed?.[2]);
|
|
67
|
+
if (parsed?.[1] !== oid || !Number.isSafeInteger(size))
|
|
68
|
+
throw new Error('Invalid Git source blob response');
|
|
69
|
+
if (size > maxBlobBytes)
|
|
70
|
+
throw new Error('Git source blob exceeds the read limit');
|
|
71
|
+
const bytes = Buffer.allocUnsafe(size);
|
|
72
|
+
let offset = 0;
|
|
73
|
+
while (offset < size) {
|
|
74
|
+
if (this.buffer.length === 0)
|
|
75
|
+
this.buffer = await this.chunk();
|
|
76
|
+
const copied = this.buffer.copy(bytes, offset, 0, size - offset);
|
|
77
|
+
offset += copied;
|
|
78
|
+
this.buffer = this.buffer.subarray(copied);
|
|
79
|
+
}
|
|
80
|
+
if (this.buffer.length === 0)
|
|
81
|
+
this.buffer = await this.chunk();
|
|
82
|
+
if (this.buffer[0] !== 10)
|
|
83
|
+
throw new Error('Invalid Git source blob terminator');
|
|
84
|
+
this.buffer = this.buffer.subarray(1);
|
|
85
|
+
return bytes;
|
|
86
|
+
}
|
|
87
|
+
async header() {
|
|
88
|
+
let index = this.buffer.indexOf(10);
|
|
89
|
+
while (index < 0) {
|
|
90
|
+
if (this.buffer.length > 256)
|
|
91
|
+
throw new Error('Invalid Git source blob header');
|
|
92
|
+
this.buffer = Buffer.concat([this.buffer, await this.chunk()]);
|
|
93
|
+
index = this.buffer.indexOf(10);
|
|
94
|
+
}
|
|
95
|
+
const header = this.buffer.toString('ascii', 0, index);
|
|
96
|
+
this.buffer = this.buffer.subarray(index + 1);
|
|
97
|
+
return header;
|
|
98
|
+
}
|
|
99
|
+
async chunk() {
|
|
100
|
+
const result = await this.chunks.next();
|
|
101
|
+
if (result.done)
|
|
102
|
+
throw new Error('Git source reader stopped');
|
|
103
|
+
return result.value;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { SourceSnapshot } from '../../core/model/snapshot.js';
|
|
2
|
+
import type { GitChanges, LineRange } from '../../core/model/changes.js';
|
|
3
|
+
export type { GitChanges, LineRange } from '../../core/model/changes.js';
|
|
4
|
+
export declare const diffMethod: {
|
|
5
|
+
readonly algorithm: 'myers';
|
|
6
|
+
readonly indentHeuristic: false;
|
|
7
|
+
readonly renameThresholdPercent: 50;
|
|
8
|
+
readonly renameLimit: 1000;
|
|
9
|
+
};
|
|
10
|
+
export declare function findMergeBase(repository: string, base: string, head: string): Promise<string>;
|
|
11
|
+
export declare function readChanges(repository: string, base: string, head: SourceSnapshot, excludes: (path: string) => boolean): Promise<GitChanges>;
|
|
12
|
+
interface RawChange {
|
|
13
|
+
readonly path: string;
|
|
14
|
+
readonly mode: string;
|
|
15
|
+
readonly oid: string;
|
|
16
|
+
readonly previous?: {
|
|
17
|
+
readonly path: string;
|
|
18
|
+
readonly oid: string;
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export declare function rawChanges(text: string): readonly RawChange[];
|
|
22
|
+
export declare function addedRanges(patch: string): readonly LineRange[];
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { gitText, gitUtf8 } from './git.js';
|
|
2
|
+
import { isSourcePath, physicalLines, utf8 } from '../../core/model/snapshot.js';
|
|
3
|
+
const oid = /^(?:[a-f0-9]{40}|[a-f0-9]{64})$/;
|
|
4
|
+
const regularFile = /^100(?:644|755)$/;
|
|
5
|
+
export const diffMethod = { algorithm: 'myers', indentHeuristic: false, renameThresholdPercent: 50, renameLimit: 1000 };
|
|
6
|
+
const stableDiff = ['--no-color', '--no-ext-diff', '--no-textconv', `--diff-algorithm=${diffMethod.algorithm}`, '--no-indent-heuristic'];
|
|
7
|
+
export async function findMergeBase(repository, base, head) {
|
|
8
|
+
if (!oid.test(base) || !oid.test(head))
|
|
9
|
+
throw new Error('Merge base requires full commit IDs');
|
|
10
|
+
let found;
|
|
11
|
+
try {
|
|
12
|
+
found = await gitText(repository, ['merge-base', '--all', base, head]);
|
|
13
|
+
}
|
|
14
|
+
catch (error) {
|
|
15
|
+
if (error instanceof Error && 'code' in error && error.code === 1) {
|
|
16
|
+
throw new Error('No merge base found. Make the histories for both commits available locally.');
|
|
17
|
+
}
|
|
18
|
+
throw error;
|
|
19
|
+
}
|
|
20
|
+
const candidates = found.trim().split(/\s+/);
|
|
21
|
+
const first = candidates[0];
|
|
22
|
+
if (candidates.length !== 1 || !first || !oid.test(first))
|
|
23
|
+
throw new Error('The selected commits need one unambiguous merge base');
|
|
24
|
+
return first;
|
|
25
|
+
}
|
|
26
|
+
export async function readChanges(repository, base, head, excludes) {
|
|
27
|
+
if (!oid.test(base) || head.subject.kind !== 'commit' || !oid.test(head.subject.id))
|
|
28
|
+
throw new Error('Git diff requires full commit IDs');
|
|
29
|
+
const files = new Map();
|
|
30
|
+
const gaps = new Map();
|
|
31
|
+
if (base === head.subject.id)
|
|
32
|
+
return { files, gaps };
|
|
33
|
+
const contents = new Map(head.files.map(file => [file.path, file.bytes]));
|
|
34
|
+
const raw = await gitUtf8(repository, ['diff', ...stableDiff, '--raw', '-z', '--no-abbrev',
|
|
35
|
+
`--find-renames=${diffMethod.renameThresholdPercent}%`, `-l${diffMethod.renameLimit}`, base, head.subject.id, '--']);
|
|
36
|
+
for (const change of rawChanges(raw)) {
|
|
37
|
+
const bytes = contents.get(change.path);
|
|
38
|
+
if (!bytes || !regularFile.test(change.mode))
|
|
39
|
+
continue;
|
|
40
|
+
const before = change.previous && isSourcePath(change.previous.path) && !excludes(change.previous.path)
|
|
41
|
+
? change.previous.oid : undefined;
|
|
42
|
+
if (before === change.oid)
|
|
43
|
+
continue;
|
|
44
|
+
let source;
|
|
45
|
+
try {
|
|
46
|
+
source = utf8(bytes);
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
gaps.set(change.path, 'Changed source is not valid UTF-8');
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (/\0|\r(?!\n)|[\u2028\u2029]/.test(source)) {
|
|
53
|
+
gaps.set(change.path, 'Git and LCOV line coordinates may differ for this file');
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
const length = physicalLines(source);
|
|
57
|
+
const ranges = before
|
|
58
|
+
? addedRanges(await gitText(repository, ['diff', ...stableDiff, '--text', '--unified=0', '--inter-hunk-context=0', before, change.oid, '--']))
|
|
59
|
+
: (length === 0 ? [] : [{ first: 1, last: length }]);
|
|
60
|
+
if (ranges.some(range => range.last > length))
|
|
61
|
+
throw new Error('Git reported changes beyond the source lines');
|
|
62
|
+
if (ranges.length > 0)
|
|
63
|
+
files.set(change.path, ranges);
|
|
64
|
+
}
|
|
65
|
+
return { files, gaps };
|
|
66
|
+
}
|
|
67
|
+
export function rawChanges(text) {
|
|
68
|
+
if (text === '')
|
|
69
|
+
return [];
|
|
70
|
+
const fields = text.split('\0');
|
|
71
|
+
if (fields.pop() !== '')
|
|
72
|
+
throw new Error('Invalid Git raw diff terminator');
|
|
73
|
+
const changes = [];
|
|
74
|
+
for (let i = 0; i < fields.length;) {
|
|
75
|
+
const header = /^:(\d{6}) (\d{6}) ([a-f0-9]+) ([a-f0-9]+) ([ACDMRT])\d*$/.exec(fields[i++] ?? '');
|
|
76
|
+
const oldPath = fields[i++];
|
|
77
|
+
const status = header?.[5];
|
|
78
|
+
const path = status === 'C' || status === 'R' ? fields[i++] : oldPath;
|
|
79
|
+
const oldId = header?.[3];
|
|
80
|
+
const newId = header?.[4];
|
|
81
|
+
const mode = header?.[2];
|
|
82
|
+
if (!oldPath || !path || !mode || !oldId || !newId || !oid.test(oldId) || !oid.test(newId)) {
|
|
83
|
+
throw new Error('Invalid Git raw diff entry');
|
|
84
|
+
}
|
|
85
|
+
const previous = status !== 'A' && status !== 'C' && regularFile.test(header[1])
|
|
86
|
+
? { path: oldPath, oid: oldId } : undefined;
|
|
87
|
+
changes.push({ path, mode, oid: newId, previous });
|
|
88
|
+
}
|
|
89
|
+
return changes;
|
|
90
|
+
}
|
|
91
|
+
export function addedRanges(patch) {
|
|
92
|
+
const ranges = [];
|
|
93
|
+
for (const line of patch.split('\n')) {
|
|
94
|
+
const match = /^@@ -\d+(?:,\d+)? \+(\d+)(?:,(\d+))? @@(?: |$)/.exec(line);
|
|
95
|
+
if (!match)
|
|
96
|
+
continue;
|
|
97
|
+
const first = Number(match[1]);
|
|
98
|
+
const count = Number(match[2] ?? 1);
|
|
99
|
+
if (count === 0)
|
|
100
|
+
continue;
|
|
101
|
+
const last = first + count - 1;
|
|
102
|
+
if (first < 1 || !Number.isSafeInteger(last) || first <= (ranges.at(-1)?.last ?? 0))
|
|
103
|
+
throw new Error('Invalid Git diff hunk range');
|
|
104
|
+
ranges.push({ first, last });
|
|
105
|
+
}
|
|
106
|
+
return ranges;
|
|
107
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { gitText, resolveGitCommit } from './git.js';
|
|
2
|
+
export function commitMetadataReader(repository) {
|
|
3
|
+
return async (revisions) => {
|
|
4
|
+
const result = new Map();
|
|
5
|
+
for (let start = 0; start < revisions.length; start += 128) {
|
|
6
|
+
const batch = revisions.slice(start, start + 128);
|
|
7
|
+
const log = await gitText(repository, ['log', '--ignore-missing', '--no-walk=unsorted', '-z', '--no-show-signature',
|
|
8
|
+
'--format=%H%x00%an%x00%ae%x00%B', ...batch, '--']);
|
|
9
|
+
const fields = log.split('\0');
|
|
10
|
+
for (let index = 0; index + 3 < fields.length; index += 4) {
|
|
11
|
+
const [sha, name, email, message] = fields.slice(index, index + 4);
|
|
12
|
+
result.set(sha, { subject: message.split(/\r?\n/, 1)[0], author: { name, email } });
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
if (result.size) {
|
|
16
|
+
for (const [sha, tags] of await tagsByCommit(repository)) {
|
|
17
|
+
const commit = result.get(sha);
|
|
18
|
+
if (commit)
|
|
19
|
+
result.set(sha, { ...commit, tags });
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return result;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
async function tagsByCommit(repository) {
|
|
26
|
+
const refs = await gitText(repository, ['show-ref', '--tags', '--dereference']).catch((error) => {
|
|
27
|
+
if (error instanceof Error && 'code' in error && error.code === 1)
|
|
28
|
+
return '';
|
|
29
|
+
throw error;
|
|
30
|
+
});
|
|
31
|
+
const tags = new Map();
|
|
32
|
+
for (const line of refs.split('\n')) {
|
|
33
|
+
const match = /^([a-f0-9]+) refs\/tags\/(.+?)(?:\^\{\})?$/.exec(line);
|
|
34
|
+
if (match)
|
|
35
|
+
tags.set(match[2], match[1]);
|
|
36
|
+
}
|
|
37
|
+
const result = new Map();
|
|
38
|
+
for (const tag of [...tags.keys()].sort()) {
|
|
39
|
+
const sha = tags.get(tag);
|
|
40
|
+
const names = result.get(sha) ?? [];
|
|
41
|
+
names.push(tag);
|
|
42
|
+
result.set(sha, names);
|
|
43
|
+
}
|
|
44
|
+
return result;
|
|
45
|
+
}
|
|
46
|
+
export async function resolveGitBranch(repository, name) {
|
|
47
|
+
await gitText(repository, ['check-ref-format', `refs/heads/${name}`]);
|
|
48
|
+
try {
|
|
49
|
+
return await resolveGitCommit(repository, `refs/heads/${name}`);
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
return resolveGitCommit(repository, `refs/remotes/origin/${name}`);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ChurnWindow } from '../../core/model/churn.js';
|
|
2
|
+
export { churnSettings } from '../../core/model/churn.js';
|
|
3
|
+
export type { ChurnWindow } from '../../core/model/churn.js';
|
|
4
|
+
interface Churn {
|
|
5
|
+
readonly touches: ReadonlyMap<string, number>;
|
|
6
|
+
readonly window: ChurnWindow;
|
|
7
|
+
}
|
|
8
|
+
export declare function gitChurn(directory: string, commit: string, paths: readonly string[]): Promise<Churn>;
|
|
9
|
+
export declare function summarizeGitHistory(log: string, commit: string, paths: readonly string[], shallow: boolean): Churn;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { gitText, gitUtf8 } from './git.js';
|
|
2
|
+
import { churnSettings } from '../../core/model/churn.js';
|
|
3
|
+
export { churnSettings } from '../../core/model/churn.js';
|
|
4
|
+
const maxTimeMs = 8000;
|
|
5
|
+
const oid = /^(?:[a-f0-9]{40}|[a-f0-9]{64})$/;
|
|
6
|
+
export async function gitChurn(directory, commit, paths) {
|
|
7
|
+
if (!oid.test(commit))
|
|
8
|
+
throw new Error('Git history requires a full commit ID');
|
|
9
|
+
const [log, shallow] = await Promise.all([
|
|
10
|
+
gitUtf8(directory, ['-c', 'log.showRoot=false', 'log', '--first-parent', '--diff-merges=first-parent',
|
|
11
|
+
`--max-count=${churnSettings.maxCommits + 1}`, '--format=%x00%H %ct %P', '--name-only', '-z',
|
|
12
|
+
'--no-renames', '--no-notes', '--no-color', '--no-ext-diff', '--no-textconv', commit, '--'], maxTimeMs),
|
|
13
|
+
gitText(directory, ['rev-parse', '--is-shallow-repository'], maxTimeMs),
|
|
14
|
+
]);
|
|
15
|
+
return summarizeGitHistory(log, commit, paths, shallow.trim() === 'true');
|
|
16
|
+
}
|
|
17
|
+
export function summarizeGitHistory(log, commit, paths, shallow) {
|
|
18
|
+
const fields = log.split('\0');
|
|
19
|
+
if (fields[0] !== '' || fields.at(-1) !== '')
|
|
20
|
+
throw new Error('Invalid Git history record');
|
|
21
|
+
const touches = new Map(paths.map(path => [path, 0]));
|
|
22
|
+
let index = 1;
|
|
23
|
+
let visited = 0;
|
|
24
|
+
let commits = 0;
|
|
25
|
+
let endingAt;
|
|
26
|
+
let capReached = false;
|
|
27
|
+
while (index < fields.length - 1) {
|
|
28
|
+
const [sha, date, ...parents] = fields[index++].split(' ');
|
|
29
|
+
const timestamp = Number(date);
|
|
30
|
+
const hasParent = parents.length > 0 && parents.every(parent => oid.test(parent));
|
|
31
|
+
const root = parents.length === 1 && parents[0] === '';
|
|
32
|
+
if (sha === undefined || !oid.test(sha) || date === undefined || !/^\d+$/.test(date) || !Number.isSafeInteger(timestamp) || (!hasParent && !root)) {
|
|
33
|
+
throw new Error('Invalid Git history record');
|
|
34
|
+
}
|
|
35
|
+
if (endingAt === undefined) {
|
|
36
|
+
if (sha !== commit)
|
|
37
|
+
throw new Error('Git history did not start at the inspected commit');
|
|
38
|
+
endingAt = timestamp;
|
|
39
|
+
}
|
|
40
|
+
if (visited >= churnSettings.maxCommits) {
|
|
41
|
+
capReached = true;
|
|
42
|
+
break;
|
|
43
|
+
}
|
|
44
|
+
visited++;
|
|
45
|
+
const cutoff = endingAt - churnSettings.lookbackDays * 86400;
|
|
46
|
+
const overlaps = hasParent && timestamp >= cutoff && timestamp <= endingAt;
|
|
47
|
+
const changed = new Set();
|
|
48
|
+
let first = true;
|
|
49
|
+
while (index < fields.length && fields[index] !== '') {
|
|
50
|
+
const path = fields[index++];
|
|
51
|
+
if (overlaps) {
|
|
52
|
+
const normalized = first ? path.replace(/^\n/, '') : path;
|
|
53
|
+
if (touches.has(normalized))
|
|
54
|
+
changed.add(normalized);
|
|
55
|
+
}
|
|
56
|
+
first = false;
|
|
57
|
+
}
|
|
58
|
+
index++;
|
|
59
|
+
if (overlaps)
|
|
60
|
+
commits++;
|
|
61
|
+
for (const path of changed)
|
|
62
|
+
touches.set(path, touches.get(path) + 1);
|
|
63
|
+
}
|
|
64
|
+
if (endingAt === undefined)
|
|
65
|
+
throw new Error('Empty Git history');
|
|
66
|
+
return {
|
|
67
|
+
touches: shallow && commits === 0 ? new Map() : touches,
|
|
68
|
+
window: { endingAt: new Date(endingAt * 1000).toISOString(), commits, shallow, capReached },
|
|
69
|
+
};
|
|
70
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Subject } from '../../core/model/snapshot.js';
|
|
2
|
+
import type { Report, ReportReader } from '../../report/json/validation/report.js';
|
|
3
|
+
import type { CommitSummary } from '../../report/json/wire/summary-model.js';
|
|
4
|
+
export declare class GitReportReader implements ReportReader {
|
|
5
|
+
private readonly directory;
|
|
6
|
+
private readonly revision;
|
|
7
|
+
private snapshot?;
|
|
8
|
+
constructor(directory: string, revision: string);
|
|
9
|
+
read(subject: Subject): Promise<Report>;
|
|
10
|
+
list(): Promise<readonly Report[]>;
|
|
11
|
+
listSummaries(): Promise<readonly CommitSummary[]>;
|
|
12
|
+
private reportEntries;
|
|
13
|
+
private readEntries;
|
|
14
|
+
private pinnedSnapshot;
|
|
15
|
+
private resolveSnapshot;
|
|
16
|
+
}
|