Skip to content

Commit

Permalink
feat: remove dependency on git repos
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarvin8 committed Oct 28, 2024
1 parent 438ebbf commit c2bc72d
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 118 deletions.
32 changes: 28 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,27 @@

[![NPM](https://img.shields.io/npm/v/apex-code-coverage-transformer.svg?label=apex-code-coverage-transformer)](https://www.npmjs.com/package/apex-code-coverage-transformer) [![Downloads/week](https://img.shields.io/npm/dw/apex-code-coverage-transformer.svg)](https://npmjs.org/package/apex-code-coverage-transformer) [![License](https://img.shields.io/badge/License-MIT-yellow.svg)](https://raw.githubusercontent.com/mcarvin8/apex-code-coverage-transformer/main/LICENSE.md)

<!-- TABLE OF CONTENTS -->
<details>
<summary>Table of Contents</summary>

- [Install](#install)
- [Command](#command)
- [`sf acc-transformer transform`](#sf-acc-transformer-transform)
- [Hook](#hook)
- [Errors and Warnings](#errors-and-warnings)
- [Example](#example)
- [Issues](#issues)
- [License](#license)
</details>

The `apex-code-coverage-transformer` is a Salesforce CLI plugin to transform the Apex Code Coverage JSON files created during deployments and test runs into the Generic Test Coverage Format (XML). This format is accepted by static code analysis tools like SonarQube.

This plugin supports code coverage metrics created for Apex Classes and Apex Triggers. This also supports multiple package directories as listed in your project's `sfdx-project.json` configuration, assuming unique file-names are used in your package directories.

This plugin is intended for users who deploy their Apex codebase from a git-based repository and use SonarQube for code quality. This plugin will work if you run local tests or run all tests in an org, including tests that originate from installed managed and unlocked packages. SonarQube relies on file-paths to map code coverage to the files in their file explorer interface. Since files from managed and unlocked packages aren't retrieved into git-based Salesforce repositories, these files cannot be included in your SonarQube scans. If your Apex code coverage JSON output includes managed/unlocked package files, they will not be added to the coverage XML created by this plugin. A warning will be printed for each file not found in a package directory in your git repository. See [Errors and Warnings](https://github.com/mcarvin8/apex-code-coverage-transformer?tab=readme-ov-file#errors-and-warnings) for more information.
This plugin is intended for users who deploy their Apex codebase from a Salesforce DX repository and use SonarQube for code quality. This plugin is intended to work for any Salesforce DX repository with a `sfdx-project.json` file, not just git-based repositories. This plugin will work if you run local tests or run all tests in an org, including tests that originate from installed managed and unlocked packages.

SonarQube relies on file-paths to map code coverage to the files in their file explorer interface. Since files from managed and unlocked packages aren't retrieved into Salesforce DX repositories, these files cannot be included in your SonarQube scans. If your Apex code coverage JSON output includes managed/unlocked package files, they will not be added to the coverage XML created by this plugin. A warning will be printed for each file not found in a package directory in your repository. See [Errors and Warnings](https://github.com/mcarvin8/apex-code-coverage-transformer?tab=readme-ov-file#errors-and-warnings) for more information.

To create the code coverage JSON during a Salesforce CLI deployment/validation, append `--coverage-formatters json --results-dir "coverage"` to the `sf project deploy` command. This will create a coverage JSON in this relative path - `coverage/coverage/coverage.json`.

Expand All @@ -21,7 +37,7 @@ sf apex run test --code-coverage --result-format json --output-dir "coverage"
sf apex get test --test-run-id <test run id> --code-coverage --result-format json --output-dir "coverage"
```

The code coverage JSONs created by the Salesforce CLI aren't accepted by SonarQube automatically for git-based Salesforce repositories and needs to be converted using this plugin.
The code coverage JSONs created by the Salesforce CLI aren't accepted by SonarQube automatically for Salesforce DX repositories and needs to be converted using this plugin.

**Disclaimer**: Due to existing bugs with how the Salesforce CLI reports covered lines during deployments (see [5511](https://github.com/forcedotcom/salesforcedx-vscode/issues/5511) and [1568](https://github.com/forcedotcom/cli/issues/1568)), to add support for covered lines in this plugin for deployment coverage files, I had to add a function to re-number out-of-range covered lines the CLI may report (ex: line 100 in a 98-line Apex Class is reported back as covered by the Salesforce CLI deploy command). Salesforce's coverage result may also include extra lines as covered (ex: 120 lines are included in the coverage report for a 100 line file), so the coverage percentage may vary based on how many lines the API returns in the coverage report. Once Salesforce fixes the API to correctly return covered lines in the deploy command, this function will be removed.

Expand All @@ -37,7 +53,7 @@ The `apex-code-coverage-transformer` has 1 command:

- `sf acc-transformer transform`

This command needs to be ran somewhere inside your Salesforce DX git repository, whether in the root folder (recommended) or in a subfolder. This plugin will determine the root folder of this repository and read the `sfdx-project.json` file in the root folder. All package directories listed in the `sfdx-project.json` file will be processed when running this plugin.
This command needs to be ran somewhere inside your Salesforce DX repository, whether in the root folder (recommended) or in a subfolder. This plugin will determine the root folder of this repository and read the `sfdx-project.json` file in the root folder. All package directories listed in the `sfdx-project.json` file will be processed when running this plugin.

## `sf acc-transformer transform`

Expand Down Expand Up @@ -110,7 +126,7 @@ Error (1): The provided JSON does not match a known coverage data format from th
If the `sfdx-project.json` file was not found in your repository's root folder, the plugin will fail with:

```
Error (1): Salesforce DX Config File does not exist in this path: {filePath}
Error (1): sfdx-project.json not found in any parent directory.
```

Any ENOENT failures indicate that the plugin had issues finding one of the package directories in the `sfdx-project.json` file:
Expand Down Expand Up @@ -194,3 +210,11 @@ This [code coverage JSON file](https://raw.githubusercontent.com/mcarvin8/apex-c
</file>
</coverage>
```

## Issues

If you encounter any issues, please create an issue in the repository's [issue tracker](https://github.com/mcarvin8/apex-code-coverage-transformer/issues). Please also create issues to suggest any new features.

## License

This project is licensed under the MIT license. Please see the [LICENSE](https://raw.githubusercontent.com/mcarvin8/apex-code-coverage-transformer/main/LICENSE.md) file for details.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"@oclif/core": "^3.18.1",
"@salesforce/core": "^6.4.7",
"@salesforce/sf-plugins-core": "^7.1.3",
"isomorphic-git": "^1.27.1",
"xmlbuilder2": "^3.1.1"
},
"devDependencies": {
Expand All @@ -29,7 +28,8 @@
"/lib",
"/messages",
"/oclif.manifest.json",
"/oclif.lock"
"/oclif.lock",
"/CHANGELOG.md"
],
"keywords": [
"force",
Expand Down
12 changes: 5 additions & 7 deletions src/helpers/getPackageDirectories.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
'use strict';
/* eslint-disable no-await-in-loop */

import { existsSync } from 'node:fs';
import { readFile } from 'node:fs/promises';
import { resolve } from 'node:path';

import { SfdxProject } from './types.js';
import { getRepoRoot } from './getRepoRoot.js';

export async function getPackageDirectories(): Promise<{ repoRoot: string; packageDirectories: string[] }> {
const repoRoot = await getRepoRoot();
const dxConfigPath = resolve(repoRoot, 'sfdx-project.json');
if (!existsSync(dxConfigPath)) {
throw Error(`Salesforce DX Config File does not exist in this path: ${dxConfigPath}`);
const { repoRoot, dxConfigFilePath } = await getRepoRoot();

if (!repoRoot || !dxConfigFilePath) {
throw new Error('Failed to retrieve repository root or sfdx-project.json path.');
}

const sfdxProjectRaw: string = await readFile(dxConfigPath, 'utf-8');
const sfdxProjectRaw: string = await readFile(dxConfigFilePath, 'utf-8');
const sfdxProject: SfdxProject = JSON.parse(sfdxProjectRaw) as SfdxProject;
const packageDirectories = sfdxProject.packageDirectories.map((directory) => resolve(repoRoot, directory.path));
return { repoRoot, packageDirectories };
Expand Down
38 changes: 29 additions & 9 deletions src/helpers/getRepoRoot.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,32 @@
'use strict';
import { promises as fsPromises, readFile, stat, readdir } from 'node:fs';
import git from 'isomorphic-git';
/* eslint-disable no-await-in-loop */
import { access } from 'node:fs/promises';
import { join, dirname } from 'node:path';

export async function getRepoRoot(): Promise<string> {
const fs = { promises: fsPromises, readFile, stat, readdir };
const repoRoot = await git.findRoot({
fs,
filepath: process.cwd(),
});
return repoRoot;
export async function getRepoRoot(): Promise<{ repoRoot: string | undefined; dxConfigFilePath: string | undefined }> {
let currentDir = process.cwd();
let found = false;
let dxConfigFilePath: string | undefined;
let repoRoot: string | undefined;

do {
const filePath = join(currentDir, 'sfdx-project.json');

try {
// Check if sfdx-project.json exists in the current directory
await access(filePath);
dxConfigFilePath = filePath;
repoRoot = currentDir;
found = true;
} catch {
// If file not found, move up one directory level
const parentDir = dirname(currentDir);
if (currentDir === parentDir) {
// Reached the root without finding the file, throw an error
throw new Error('sfdx-project.json not found in any parent directory.');
}
currentDir = parentDir;
}
} while (!found);
return { repoRoot, dxConfigFilePath };
}
5 changes: 4 additions & 1 deletion src/hooks/postrun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ export const postrun: Hook<'postrun'> = async function (options) {
return;
}
let configFile: ConfigFile;
const repoRoot = await getRepoRoot();
const { repoRoot } = await getRepoRoot();
if (!repoRoot) {
return;
}
const configPath = resolve(repoRoot, '.apexcodecovtransformer.config.json');

try {
Expand Down
99 changes: 4 additions & 95 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3180,11 +3180,6 @@ astral-regex@^2.0.0:
resolved "https://registry.npmjs.org/astral-regex/-/astral-regex-2.0.0.tgz"
integrity sha512-Z7tMw1ytTXt5jqMcOP+OQteU1VuNK9Y02uuJtKQ1Sv69jXQKKg5cibLwGJow8yzZP+eAc18EmLGPal0bp36rvQ==

async-lock@^1.4.1:
version "1.4.1"
resolved "https://registry.yarnpkg.com/async-lock/-/async-lock-1.4.1.tgz#56b8718915a9b68b10fce2f2a9a3dddf765ef53f"
integrity sha512-Az2ZTpuytrtqENulXwO3GGv1Bztugx6TT37NIo7imr/Qo0gsYiGtSdBa2B6fsXhTpVZDNfu1Qn3pk531e3q+nQ==

async-retry@^1.3.3:
version "1.3.3"
resolved "https://registry.npmjs.org/async-retry/-/async-retry-1.3.3.tgz"
Expand Down Expand Up @@ -3603,11 +3598,6 @@ ci-info@^3.8.0:
resolved "https://registry.npmjs.org/ci-info/-/ci-info-3.9.0.tgz"
integrity sha512-NIxF55hv4nSqQswkAeiOi1r83xy8JldOFDTWiug55KBu9Jnblncd2U6ViHmYgHf01TPZS77NJBhBMKdWj9HQMQ==

clean-git-ref@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/clean-git-ref/-/clean-git-ref-2.0.1.tgz#dcc0ca093b90e527e67adb5a5e55b1af6816dcd9"
integrity sha512-bLSptAy2P0s6hU4PzuIMKmMJJSE6gLXGH1cntDu7bWJUksvuM+7ReOK61mozULErYvP6a15rnYl0zFDef+pyPw==

clean-regexp@^1.0.0:
version "1.0.0"
resolved "https://registry.npmjs.org/clean-regexp/-/clean-regexp-1.0.0.tgz"
Expand Down Expand Up @@ -3960,11 +3950,6 @@ cosmiconfig@^8.0.0, cosmiconfig@^8.3.6:
parse-json "^5.2.0"
path-type "^4.0.0"

crc-32@^1.2.0:
version "1.2.2"
resolved "https://registry.yarnpkg.com/crc-32/-/crc-32-1.2.2.tgz#3cad35a934b8bf71f25ca524b6da51fb7eace2ff"
integrity sha512-ROmzCKrTnOwybPcJApAA6WBWij23HVfGVNKqqrZpuyZOHqK2CwHSvpGuyt/UNNvaIjEd8X5IFGp4Mh+Ie1IHJQ==

create-require@^1.1.0:
version "1.1.1"
resolved "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz"
Expand Down Expand Up @@ -4149,11 +4134,6 @@ dezalgo@^1.0.0:
asap "^2.0.0"
wrappy "1"

[email protected]:
version "0.0.3"
resolved "https://registry.yarnpkg.com/diff3/-/diff3-0.0.3.tgz#d4e5c3a4cdf4e5fe1211ab42e693fcb4321580fc"
integrity sha512-iSq8ngPOt0K53A6eVr4d5Kn6GNrM2nQZtC740pzIriHtn4pOQ2lyzEXQMBeVcWERN0ye7fhBsk9PbLLQOnUx/g==

[email protected], diff@^5.0.0:
version "5.0.0"
resolved "https://registry.npmjs.org/diff/-/diff-5.0.0.tgz"
Expand Down Expand Up @@ -5889,23 +5869,6 @@ isexe@^2.0.0:
resolved "https://registry.npmjs.org/isexe/-/isexe-2.0.0.tgz"
integrity sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==

isomorphic-git@^1.27.1:
version "1.27.1"
resolved "https://registry.yarnpkg.com/isomorphic-git/-/isomorphic-git-1.27.1.tgz#a2752fce23a09f04baa590c41cfaf61e973405b3"
integrity sha512-X32ph5zIWfT75QAqW2l3JCIqnx9/GWd17bRRehmn3qmWc34OYbSXY6Cxv0o9bIIY+CWugoN4nQFHNA+2uYf2nA==
dependencies:
async-lock "^1.4.1"
clean-git-ref "^2.0.1"
crc-32 "^1.2.0"
diff3 "0.0.3"
ignore "^5.1.4"
minimisted "^2.0.0"
pako "^1.0.10"
pify "^4.0.1"
readable-stream "^3.4.0"
sha.js "^2.4.9"
simple-get "^4.0.1"

istanbul-lib-coverage@^3.0.0, istanbul-lib-coverage@^3.2.0:
version "3.2.0"
resolved "https://registry.npmjs.org/istanbul-lib-coverage/-/istanbul-lib-coverage-3.2.0.tgz"
Expand Down Expand Up @@ -6721,13 +6684,6 @@ minimist@^1.2.0, minimist@^1.2.3, minimist@^1.2.5, minimist@^1.2.6:
resolved "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz"
integrity sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==

minimisted@^2.0.0:
version "2.0.1"
resolved "https://registry.yarnpkg.com/minimisted/-/minimisted-2.0.1.tgz#d059fb905beecf0774bc3b308468699709805cb1"
integrity sha512-1oPjfuLQa2caorJUM8HV8lGgWCc0qqAO1MNv/k05G4qslmsndV/5WdNZrqCiyqiz3wohia2Ij2B7w2Dr7/IyrA==
dependencies:
minimist "^1.2.5"

minipass-collect@^1.0.2:
version "1.0.2"
resolved "https://registry.npmjs.org/minipass-collect/-/minipass-collect-1.0.2.tgz"
Expand Down Expand Up @@ -7543,7 +7499,7 @@ pacote@^15.2.0:
ssri "^10.0.0"
tar "^6.1.11"

pako@^1.0.10, pako@~1.0.2:
pako@~1.0.2:
version "1.0.11"
resolved "https://registry.npmjs.org/pako/-/pako-1.0.11.tgz"
integrity sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw==
Expand Down Expand Up @@ -8391,14 +8347,6 @@ setimmediate@^1.0.5:
resolved "https://registry.npmjs.org/setimmediate/-/setimmediate-1.0.5.tgz"
integrity sha512-MATJdZp8sLqDl/68LfQmbP8zKPLQNV6BIZoIgrscFDQ+RsvK/BxeDQOgyxKKoh0y/8h3BqVFnCqQ/gd+reiIXA==

sha.js@^2.4.9:
version "2.4.11"
resolved "https://registry.yarnpkg.com/sha.js/-/sha.js-2.4.11.tgz#37a5cf0b81ecbc6943de109ba2960d1b26584ae7"
integrity sha512-QMEp5B7cftE7APOjk5Y6xgrbWu+WkLVQwk8JNjZ8nKRciZaByEW6MubieAiToS7+dwvrjGhH8jRXz3MVd0AYqQ==
dependencies:
inherits "^2.0.1"
safe-buffer "^5.0.1"

shebang-command@^2.0.0:
version "2.0.0"
resolved "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz"
Expand Down Expand Up @@ -8466,20 +8414,6 @@ sigstore@^1.3.0:
make-fetch-happen "^11.0.1"
tuf-js "^1.1.3"

simple-concat@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/simple-concat/-/simple-concat-1.0.1.tgz#f46976082ba35c2263f1c8ab5edfe26c41c9552f"
integrity sha512-cSFtAPtRhljv69IK0hTVZQ+OfE9nePi/rtJmw5UjHeVyVroEqJXP1sFztKUy1qU+xvz3u/sfYJLa947b7nAN2Q==

simple-get@^4.0.1:
version "4.0.1"
resolved "https://registry.yarnpkg.com/simple-get/-/simple-get-4.0.1.tgz#4a39db549287c979d352112fa03fd99fd6bc3543"
integrity sha512-brv7p5WgH0jmQJr1ZDDfKDOSeWWg+OVypG99A/5vYGPqJ6pxiaHLy8nxtFjBA7oMa01ebA9gfh1uMCFqOuXxvA==
dependencies:
decompress-response "^6.0.0"
once "^1.3.1"
simple-concat "^1.0.0"

simple-swizzle@^0.2.2:
version "0.2.2"
resolved "https://registry.npmjs.org/simple-swizzle/-/simple-swizzle-0.2.2.tgz"
Expand Down Expand Up @@ -8684,16 +8618,7 @@ ssri@^9.0.0:
dependencies:
minipass "^3.1.1"

"string-width-cjs@npm:string-width@^4.2.0":
version "4.2.3"
resolved "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
dependencies:
emoji-regex "^8.0.0"
is-fullwidth-code-point "^3.0.0"
strip-ansi "^6.0.1"

"string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.0.0, string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
"string-width-cjs@npm:string-width@^4.2.0", "string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.0.0, string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
version "4.2.3"
resolved "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz"
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
Expand Down Expand Up @@ -8752,14 +8677,7 @@ string_decoder@^1.3.0:
dependencies:
safe-buffer "~5.2.0"

"strip-ansi-cjs@npm:strip-ansi@^6.0.1":
version "6.0.1"
resolved "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
dependencies:
ansi-regex "^5.0.1"

[email protected], strip-ansi@^6.0.0, strip-ansi@^6.0.1:
"strip-ansi-cjs@npm:strip-ansi@^6.0.1", [email protected], strip-ansi@^6.0.0, strip-ansi@^6.0.1:
version "6.0.1"
resolved "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz"
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
Expand Down Expand Up @@ -9490,7 +9408,7 @@ [email protected]:
resolved "https://registry.npmjs.org/workerpool/-/workerpool-6.2.1.tgz"
integrity sha512-ILEIE97kDZvF9Wb9f6h5aXK4swSlKGUcOEGiIYb2OOu/IrDU9iwj0fD//SsA6E5ibwJxpEvhullJY4Sl4GcpAw==

"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0":
"wrap-ansi-cjs@npm:wrap-ansi@^7.0.0", wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
Expand All @@ -9508,15 +9426,6 @@ wrap-ansi@^6.2.0:
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^7.0.0:
version "7.0.0"
resolved "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz"
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
dependencies:
ansi-styles "^4.0.0"
string-width "^4.1.0"
strip-ansi "^6.0.0"

wrap-ansi@^8.1.0:
version "8.1.0"
resolved "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-8.1.0.tgz"
Expand Down

0 comments on commit c2bc72d

Please sign in to comment.