Skip to content

Commit

Permalink
feat!: new environment verification flag
Browse files Browse the repository at this point in the history
  • Loading branch information
paambaati committed Jun 12, 2024
2 parents fed36ec + bae515b commit fa81470
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ This action requires that you set the [`CC_TEST_REPORTER_ID`](https://docs.codec
| `coverageLocations` | | Locations to find code coverage as a multiline string.<br>Each line should be of the form `<location>:<type>`.<br>`type` can be any one of `clover, cobertura, coverage.py, excoveralls, gcov, gocov, jacoco, lcov, lcov-json, simplecov, xccov`. See examples below. |
| `prefix` | `undefined` | See [`--prefix`](https://docs.codeclimate.com/docs/configuring-test-coverage) |
| `verifyDownload` | `true` | Verifies the downloaded Code Climate reporter binary's checksum and GPG signature. See [Verifying binaries](https://github.com/codeclimate/test-reporter#verifying-binaries) |
| `verifyEnvironment` | `true` | Verifies the current runtime environment (operating system and CPU architecture) is supported by the Code Climate reporter. See [list of supported platforms](https://github.com/codeclimate/test-reporter#binaries) |

> **Note**
> If you are a Ruby developer using [SimpleCov](https://github.com/simplecov-ruby/simplecov), other users have recommended installing an additional gem – `gem "simplecov_json_formatter"` – this gem fixes `json` error from the default `coverage/.resultset.json` output from SimpleCov.
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@actions/exec": "1.1.1",
"@actions/github": "6.0.0",
"@actions/glob": "0.4.0",
"arch": "3.0.0",
"hook-std": "3.0.0",
"node-fetch": "3.3.2",
"openpgp": "5.11.1"
Expand Down
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 28 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { unlinkSync } from 'node:fs';
import { arch, platform } from 'node:os';
import { resolve } from 'node:path';
import { chdir } from 'node:process';
import { fileURLToPath } from 'node:url';
Expand All @@ -11,6 +10,7 @@ import * as glob from '@actions/glob';
import {
downloadToFile,
getOptionalString,
getSupportedEnvironmentInfo,
parsePathAndFormat,
verifyChecksum,
verifySignature,
Expand All @@ -36,14 +36,17 @@ export interface ActionArguments {
coveragePrefix?: string;
/** Verifies the downloaded binary with a Code Climate-provided SHA 256 checksum and GPG sinature. @default true */
verifyDownload?: string;
/** Verifies if the current OS and CPU architecture is supported by CodeClimate test reporter. */
verifyEnvironment?: string;
}

const PLATFORM = platform();
const CURRENT_ENVIRONMENT = getSupportedEnvironmentInfo();
const PLATFORM = CURRENT_ENVIRONMENT.platform;
// REFER: https://docs.codeclimate.com/docs/configuring-test-coverage#locations-of-pre-built-binaries
/** Canonical download URL for the official CodeClimate reporter. */
export const DOWNLOAD_URL = `https://codeclimate.com/downloads/test-reporter/test-reporter-latest-${
PLATFORM === 'win32' ? 'windows' : PLATFORM
}-${arch() === 'arm64' ? 'arm64' : 'amd64'}`;
}-${CURRENT_ENVIRONMENT.architecture === 'arm64' ? 'arm64' : 'amd64'}`;
/** Local file name of the CodeClimate reporter. */
export const EXECUTABLE = './cc-reporter';
export const CODECLIMATE_GPG_PUBLIC_KEY_ID =
Expand All @@ -55,6 +58,7 @@ const DEFAULT_WORKING_DIRECTORY = '';
const DEFAULT_CODECLIMATE_DEBUG = 'false';
const DEFAULT_COVERAGE_LOCATIONS = '';
const DEFAULT_VERIFY_DOWNLOAD = 'true';
const DEFAULT_VERIFY_ENVIRONMENT = 'true';

const SUPPORTED_GITHUB_EVENTS = [
// Regular PRs.
Expand Down Expand Up @@ -204,10 +208,24 @@ export async function run({
coverageLocationsParam = DEFAULT_COVERAGE_LOCATIONS,
coveragePrefix,
verifyDownload = DEFAULT_VERIFY_DOWNLOAD,
verifyEnvironment = DEFAULT_VERIFY_ENVIRONMENT,
}: ActionArguments = {}): Promise<void> {
let lastExitCode = 1;
if (verifyEnvironment === 'true') {
debug('ℹ️ Verifying environment...');
const { supported, platform, architecture } = getSupportedEnvironmentInfo();
if (!supported) {
const errorMessage = `Unsupported platform and architecture! CodeClimate Test Reporter currently is not available for ${architecture} on ${platform} OS`;
error(errorMessage);
setFailed('🚨 Environment verification failed!');
throw new Error(errorMessage);
}
lastExitCode = 0;
debug('✅ Environment verification completed...');
}

if (workingDirectory) {
debug(`Changing working directory to ${workingDirectory}`);
debug(`ℹ️ Changing working directory to ${workingDirectory}`);
try {
chdir(workingDirectory);
lastExitCode = 0;
Expand Down Expand Up @@ -410,6 +428,11 @@ if (isThisFileBeingRunViaCLI) {
'verifyDownload',
DEFAULT_VERIFY_DOWNLOAD,
);
const verifyEnvironment = getOptionalString(
'verifyEnvironment',
DEFAULT_VERIFY_ENVIRONMENT,
);

try {
run({
downloadUrl: DOWNLOAD_URL,
Expand All @@ -420,6 +443,7 @@ if (isThisFileBeingRunViaCLI) {
coverageLocationsParam: coverageLocations,
coveragePrefix,
verifyDownload,
verifyEnvironment,
});
} finally {
// Finally clean up all artifacts that we downloaded.
Expand Down
35 changes: 35 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createWriteStream, readFile } from 'node:fs';
import { platform } from 'node:os';
import { promisify } from 'node:util';
import { getInput } from '@actions/core';
import arch from 'arch';
import fetch from 'node-fetch';
import {
type VerificationResult,
Expand All @@ -15,6 +16,18 @@ import {
const readFileAsync = promisify(readFile);
type ReadFileAsyncOptions = Omit<Parameters<typeof readFileAsync>[1], 'string'>;

/** List of environments not supported by the CodeClimate test reporter. */
// REFER: https://github.com/codeclimate/test-reporter#download
export const UNSUPPORTED_ENVIRONMENTS: Array<{
platform: ReturnType<typeof platform>;
architecture: ReturnType<typeof arch>;
}> = [
{
platform: 'darwin',
architecture: 'arm64',
},
];

/**
* Parses GitHub Action input and returns the optional value as a string.
*
Expand Down Expand Up @@ -249,3 +262,25 @@ export function parsePathAndFormat(coverageConfigLine: string): {
const pattern = lineParts.slice(0, -1)[0] as string;
return { format, pattern };
}

/**
* Reads information about the current operating system and the CPU architecture
* and tells if the CodeClimate test reporter supports it.
*/
export function getSupportedEnvironmentInfo() {
const currentEnvironment = {
platform: platform(),
architecture: arch(),
};

return {
supported: !UNSUPPORTED_ENVIRONMENTS.some((e) => {
return (
e.architecture === currentEnvironment.architecture &&
e.platform === currentEnvironment.platform
);
}),
platform: currentEnvironment.platform,
architecture: currentEnvironment.architecture,
};
}
3 changes: 2 additions & 1 deletion test/integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { unlinkSync } from 'node:fs';
import { EOL, arch, platform } from 'node:os';
import { EOL, platform } from 'node:os';
import arch from 'arch';
import { hookStd } from 'hook-std';
import t from 'tap';
import {
Expand Down
Loading

0 comments on commit fa81470

Please sign in to comment.