mirror of
https://github.com/SonarSource/sonarqube-scan-action.git
synced 2026-01-28 15:43:16 +03:00
SQSCANGHA-107 Migrate install-build-wrapper
This commit is contained in:
committed by
Julien HENRY
parent
a88c96d7e4
commit
ff001fd600
80
src/install-build-wrapper/__tests__/utils.test.js
Normal file
80
src/install-build-wrapper/__tests__/utils.test.js
Normal file
@@ -0,0 +1,80 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { describe, it } from "node:test";
|
||||
import { getBuildWrapperInfo } from "../utils.js";
|
||||
|
||||
describe("getBuildWrapperInfo", () => {
|
||||
const supportedPlatforms = [
|
||||
{
|
||||
platform: "Linux",
|
||||
arch: "X64",
|
||||
expectedSuffix: "linux-x86",
|
||||
expectedName: "build-wrapper-linux-x86-64",
|
||||
},
|
||||
{
|
||||
platform: "Linux",
|
||||
arch: "ARM64",
|
||||
expectedSuffix: "linux-aarch64",
|
||||
expectedName: "build-wrapper-linux-aarch64",
|
||||
},
|
||||
{
|
||||
platform: "Windows",
|
||||
arch: "X64",
|
||||
expectedSuffix: "win-x86",
|
||||
expectedName: "build-wrapper-win-x86-64.exe",
|
||||
},
|
||||
{
|
||||
platform: "macOS",
|
||||
arch: "X64",
|
||||
expectedSuffix: "macosx-x86",
|
||||
expectedName: "build-wrapper-macosx-x86",
|
||||
},
|
||||
{
|
||||
platform: "macOS",
|
||||
arch: "ARM64",
|
||||
expectedSuffix: "macosx-x86",
|
||||
expectedName: "build-wrapper-macosx-x86",
|
||||
},
|
||||
];
|
||||
|
||||
const unsupportedPlatforms = [
|
||||
{ platform: "linux", arch: "arm" },
|
||||
{ platform: "openbsd", arch: "X64" },
|
||||
{ platform: undefined, arch: "X64" },
|
||||
{ platform: "Linux", arch: undefined },
|
||||
{ platform: null, arch: "X64" },
|
||||
{ platform: "Linux", arch: null },
|
||||
];
|
||||
|
||||
supportedPlatforms.forEach(
|
||||
({ platform, arch, expectedSuffix, expectedName }) => {
|
||||
it(`returns ${expectedName} for ${platform} ${arch}`, () => {
|
||||
const result = getBuildWrapperInfo({
|
||||
runnerOS: platform,
|
||||
runnerArch: arch,
|
||||
runnerTemp: "/tmp",
|
||||
sonarHostUrl: "https://sonarcloud.io"
|
||||
});
|
||||
assert.equal(result.buildWrapperUrl, `https://sonarcloud.io/static/cpp/build-wrapper-${expectedSuffix}.zip`);
|
||||
assert.equal(result.buildWrapperDir, `/tmp/build-wrapper-${expectedSuffix}`);
|
||||
assert.equal(result.buildWrapperBin, `/tmp/build-wrapper-${expectedSuffix}/${expectedName}`);
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
unsupportedPlatforms.forEach(({ platform, arch }) => {
|
||||
it(`throws for unsupported platform ${platform} ${arch}`, () => {
|
||||
assert.throws(
|
||||
() => getBuildWrapperInfo({
|
||||
runnerOS: platform,
|
||||
runnerArch: arch,
|
||||
runnerTemp: "/tmp",
|
||||
sonarHostUrl: "https://sonarcloud.io"
|
||||
}),
|
||||
(error) => {
|
||||
return error.message.includes('unsupported') || error.message.includes('Unsupported');
|
||||
},
|
||||
`should have thrown for ${platform} ${arch}`
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
85
src/install-build-wrapper/install-build-wrapper.js
Normal file
85
src/install-build-wrapper/install-build-wrapper.js
Normal file
@@ -0,0 +1,85 @@
|
||||
import * as core from "@actions/core";
|
||||
import * as exec from "@actions/exec";
|
||||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
import { getBuildWrapperInfo, getRealPath } from "./utils";
|
||||
|
||||
async function installMacOSPackages() {
|
||||
if (process.platform === "darwin") {
|
||||
core.info("Installing required packages for macOS");
|
||||
await exec.exec("brew", ["install", "coreutils"]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* These RUNNER_XX env variables come from GitHub by default.
|
||||
* See https://docs.github.com/en/actions/reference/workflows-and-actions/variables#default-environment-variables
|
||||
*
|
||||
* If SONAR_HOST_URL is omitted, we assume sonarcloud.io
|
||||
*/
|
||||
function getEnvVariables() {
|
||||
const sonarHostUrl = process.env.SONAR_HOST_URL
|
||||
? process.env.SONAR_HOST_URL.replace(/\/$/, "")
|
||||
: "https://sonarcloud.io";
|
||||
|
||||
return {
|
||||
runnerOS: process.env.RUNNER_OS,
|
||||
runnerArch: process.env.RUNNER_ARCH,
|
||||
runnerTemp: process.env.RUNNER_TEMP,
|
||||
sonarHostUrl,
|
||||
};
|
||||
}
|
||||
|
||||
async function downloadAndInstallBuildWrapper(downloadUrl, runnerEnv) {
|
||||
const { runnerArch, runnerOS, runnerTemp } = runnerEnv;
|
||||
const tmpZipPath = path.join(
|
||||
runnerTemp,
|
||||
`build-wrapper-${runnerOS}-${runnerArch}.zip`
|
||||
);
|
||||
|
||||
core.startGroup(`Download ${downloadUrl}`);
|
||||
|
||||
core.info(`Downloading '${downloadUrl}'`);
|
||||
|
||||
if (!fs.existsSync(runnerTemp)) {
|
||||
fs.mkdirSync(runnerTemp, { recursive: true });
|
||||
}
|
||||
|
||||
await exec.exec("curl", ["-sSLo", tmpZipPath, downloadUrl]);
|
||||
|
||||
core.info("Decompressing");
|
||||
await exec.exec("unzip", ["-o", "-d", runnerTemp, tmpZipPath]);
|
||||
|
||||
core.endGroup();
|
||||
}
|
||||
|
||||
async function run() {
|
||||
try {
|
||||
await installMacOSPackages();
|
||||
|
||||
const envVariables = getEnvVariables();
|
||||
|
||||
const { buildWrapperBin, buildWrapperDir, buildWrapperUrl } =
|
||||
getBuildWrapperInfo(envVariables);
|
||||
|
||||
await downloadAndInstallBuildWrapper(buildWrapperUrl, envVariables);
|
||||
|
||||
const buildWrapperBinDir = await getRealPath(
|
||||
buildWrapperDir,
|
||||
envVariables.runnerOS
|
||||
);
|
||||
core.addPath(buildWrapperBinDir);
|
||||
core.info(`'${buildWrapperBinDir}' added to the path`);
|
||||
|
||||
const buildWrapperBinPath = await getRealPath(
|
||||
buildWrapperBin,
|
||||
envVariables.runnerOS
|
||||
);
|
||||
core.setOutput("build-wrapper-binary", buildWrapperBinPath);
|
||||
core.info(`'build-wrapper-binary' output set to '${buildWrapperBinPath}'`);
|
||||
} catch (error) {
|
||||
core.setFailed(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
run();
|
||||
98
src/install-build-wrapper/utils.js
Normal file
98
src/install-build-wrapper/utils.js
Normal file
@@ -0,0 +1,98 @@
|
||||
import * as exec from "@actions/exec";
|
||||
import * as path from "path";
|
||||
|
||||
/**
|
||||
* Compute all names and paths related to the build wrapper
|
||||
* based on the runner environment
|
||||
*/
|
||||
export function getBuildWrapperInfo({
|
||||
runnerOS,
|
||||
runnerArch,
|
||||
runnerTemp,
|
||||
sonarHostUrl,
|
||||
}) {
|
||||
const { buildWrapperSuffix, buildWrapperName } = getSuffixAndName(
|
||||
runnerOS,
|
||||
runnerArch
|
||||
);
|
||||
|
||||
const buildWrapperDir = `${runnerTemp}/build-wrapper-${buildWrapperSuffix}`;
|
||||
const buildWrapperUrl = `${sonarHostUrl}/static/cpp/build-wrapper-${buildWrapperSuffix}.zip`;
|
||||
const buildWrapperBin = `${buildWrapperDir}/${buildWrapperName}`;
|
||||
|
||||
return {
|
||||
buildWrapperUrl,
|
||||
buildWrapperDir,
|
||||
buildWrapperBin,
|
||||
};
|
||||
}
|
||||
|
||||
function getSuffixAndName(runnerOS, runnerArch) {
|
||||
if (
|
||||
runnerArch !== "X64" &&
|
||||
!(runnerArch === "ARM64" && (runnerOS === "macOS" || runnerOS === "Linux"))
|
||||
) {
|
||||
throw new Error(
|
||||
`Architecture '${runnerArch}' is unsupported by build-wrapper`
|
||||
);
|
||||
}
|
||||
|
||||
switch (runnerOS) {
|
||||
case "Windows":
|
||||
return {
|
||||
buildWrapperSuffix: "win-x86",
|
||||
buildWrapperName: "build-wrapper-win-x86-64.exe",
|
||||
};
|
||||
|
||||
case "Linux":
|
||||
switch (runnerArch) {
|
||||
case "X64":
|
||||
return {
|
||||
buildWrapperSuffix: "linux-x86",
|
||||
buildWrapperName: "build-wrapper-linux-x86-64",
|
||||
};
|
||||
|
||||
case "ARM64":
|
||||
return {
|
||||
buildWrapperSuffix: "linux-aarch64",
|
||||
buildWrapperName: "build-wrapper-linux-aarch64",
|
||||
};
|
||||
}
|
||||
break; // handled before the switch
|
||||
|
||||
case "macOS":
|
||||
return {
|
||||
buildWrapperSuffix: "macosx-x86",
|
||||
buildWrapperName: "build-wrapper-macosx-x86",
|
||||
};
|
||||
|
||||
default:
|
||||
throw new Error(`Unsupported runner OS '${runnerOS}'`);
|
||||
}
|
||||
}
|
||||
|
||||
export async function getRealPath(filePath, runnerOS) {
|
||||
switch (runnerOS) {
|
||||
case "Windows": {
|
||||
const windowsResult = await exec.getExecOutput("cygpath", [
|
||||
"--absolute",
|
||||
"--windows",
|
||||
filePath,
|
||||
]);
|
||||
return windowsResult.stdout.trim();
|
||||
}
|
||||
case "Linux": {
|
||||
const linuxResult = await exec.getExecOutput("readlink", [
|
||||
"-f",
|
||||
filePath,
|
||||
]);
|
||||
return linuxResult.stdout.trim();
|
||||
}
|
||||
case "macOS": {
|
||||
const macResult = await exec.getExecOutput("greadlink", ["-f", filePath]);
|
||||
return macResult.stdout.trim();
|
||||
}
|
||||
default:
|
||||
return path.resolve(filePath);
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
import * as core from "@actions/core";
|
||||
import { installSonarScanner } from "./main/install-sonar-scanner";
|
||||
import { runSonarScanner } from "./main/run-sonar-scanner";
|
||||
import { installSonarScanner } from "./install-sonar-scanner";
|
||||
import { runSonarScanner } from "./run-sonar-scanner";
|
||||
import {
|
||||
checkGradleProject,
|
||||
checkMavenProject,
|
||||
checkSonarToken,
|
||||
validateScannerVersion,
|
||||
} from "./main/sanity-checks";
|
||||
} from "./sanity-checks";
|
||||
|
||||
/**
|
||||
* Inputs are defined in action.yml
|
||||
Reference in New Issue
Block a user