SQSCANGHA-112 Migrate installation step
This commit is contained in:
committed by
Julien HENRY
parent
8f448484d9
commit
ed9f3aad50
81
src/__tests__/utils.test.js
Normal file
81
src/__tests__/utils.test.js
Normal file
@@ -0,0 +1,81 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { describe, it } from "node:test";
|
||||
import {
|
||||
getPlatformFlavor,
|
||||
getScannerDownloadURL,
|
||||
scannerDirName,
|
||||
} from "../utils.js";
|
||||
|
||||
describe("getPlatformFlavor", () => {
|
||||
const supportedPlatforms = [
|
||||
{ platform: "linux", arch: "x64", expected: "linux-x64" },
|
||||
{ platform: "linux", arch: "arm64", expected: "linux-aarch64" },
|
||||
{ platform: "win32", arch: "x64", expected: "windows-x64" },
|
||||
{ platform: "darwin", arch: "x64", expected: "macosx-x64" },
|
||||
{ platform: "darwin", arch: "arm64", expected: "macosx-aarch64" },
|
||||
];
|
||||
|
||||
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, expected }) => {
|
||||
it(`returns ${expected} for ${platform} ${arch}`, () => {
|
||||
assert.equal(getPlatformFlavor(platform, arch), expected);
|
||||
});
|
||||
});
|
||||
|
||||
unsupportedPlatforms.forEach(({ platform, arch }) => {
|
||||
it(`throws for unsupported platform ${platform} ${arch}`, () => {
|
||||
assert.throws(
|
||||
() => getPlatformFlavor(platform, arch),
|
||||
{
|
||||
message: `Platform ${platform} ${arch} not supported`,
|
||||
},
|
||||
`should have thrown for ${platform} ${arch}`
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("getScannerDownloadURL", () => {
|
||||
it("generates correct URL without trailing slash", () => {
|
||||
const result = getScannerDownloadURL({
|
||||
scannerBinariesUrl:
|
||||
"https://binaries.sonarsource.com/Distribution/sonar-scanner-cli",
|
||||
scannerVersion: "7.2.0.5079",
|
||||
flavor: "linux-x64",
|
||||
});
|
||||
assert.equal(
|
||||
result,
|
||||
"https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-7.2.0.5079-linux-x64.zip"
|
||||
);
|
||||
});
|
||||
|
||||
it("generates correct URL with trailing slash", () => {
|
||||
const result = getScannerDownloadURL({
|
||||
scannerBinariesUrl:
|
||||
"https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/",
|
||||
scannerVersion: "7.2.0.5079",
|
||||
flavor: "linux-x64",
|
||||
});
|
||||
assert.equal(
|
||||
result,
|
||||
"https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-7.2.0.5079-linux-x64.zip"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("scannerDirName", () => {
|
||||
it("handles special characters", () => {
|
||||
assert.equal(
|
||||
scannerDirName("7.2.0-SNAPSHOT", "linux_x64"),
|
||||
"sonar-scanner-7.2.0-SNAPSHOT-linux_x64"
|
||||
);
|
||||
});
|
||||
});
|
||||
73
src/index.js
73
src/index.js
@@ -1,19 +1,28 @@
|
||||
import * as core from "@actions/core";
|
||||
import * as tc from "@actions/tool-cache";
|
||||
import * as os from "os";
|
||||
import * as path from "path";
|
||||
import {
|
||||
checkGradleProject,
|
||||
checkMavenProject,
|
||||
checkSonarToken,
|
||||
validateScannerVersion,
|
||||
} from "./sanity-checks";
|
||||
import {
|
||||
getPlatformFlavor,
|
||||
getScannerDownloadURL,
|
||||
scannerDirName,
|
||||
} from "./utils";
|
||||
|
||||
const TOOLNAME = "sonar-scanner-cli";
|
||||
|
||||
function getInputs() {
|
||||
//FIXME: should not rely on ENV vars
|
||||
const scannerVersion = process.env.INPUT_SCANNERVERSION; // core.getInput("scannerVersion");
|
||||
const projectBaseDir = process.env.INPUT_PROJECTBASEDIR; // core.getInput("projectBaseDir") || ".";
|
||||
const scannerBinariesUrl = process.env.INPUT_SCANNERBINARIESURL; // core.getInput("scannerBinariesUrl");
|
||||
|
||||
console.log("scannerVersion: ", scannerVersion);
|
||||
|
||||
return { scannerVersion, projectBaseDir };
|
||||
return { scannerVersion, projectBaseDir, scannerBinariesUrl };
|
||||
}
|
||||
|
||||
function runSanityChecks(inputs) {
|
||||
@@ -30,10 +39,62 @@ function runSanityChecks(inputs) {
|
||||
}
|
||||
}
|
||||
|
||||
function run() {
|
||||
const inputs = getInputs();
|
||||
async function installSonarScannerCLI(scannerVersion, scannerBinariesUrl) {
|
||||
const flavor = getPlatformFlavor(os.platform(), os.arch());
|
||||
|
||||
runSanityChecks(inputs);
|
||||
// Check if tool is already cached
|
||||
let toolDir = tc.find(TOOLNAME, scannerVersion, flavor);
|
||||
|
||||
if (!toolDir) {
|
||||
console.log(
|
||||
`Installing Sonar Scanner CLI ${scannerVersion} for ${flavor}...`
|
||||
);
|
||||
|
||||
const downloadUrl = getScannerDownloadURL({
|
||||
scannerBinariesUrl,
|
||||
scannerVersion,
|
||||
flavor,
|
||||
});
|
||||
|
||||
console.log(`Downloading from: ${downloadUrl}`);
|
||||
|
||||
const downloadPath = await tc.downloadTool(downloadUrl);
|
||||
const extractedPath = await tc.extractZip(downloadPath);
|
||||
|
||||
// Find the actual scanner directory inside the extracted folder
|
||||
const scannerPath = path.join(
|
||||
extractedPath,
|
||||
scannerDirName(scannerVersion, flavor)
|
||||
);
|
||||
|
||||
toolDir = await tc.cacheDir(scannerPath, TOOLNAME, scannerVersion, flavor);
|
||||
|
||||
console.log(`Sonar Scanner CLI cached to: ${toolDir}`);
|
||||
} else {
|
||||
console.log(`Using cached Sonar Scanner CLI from: ${toolDir}`);
|
||||
}
|
||||
|
||||
// Add the bin directory to PATH
|
||||
const binDir = path.join(toolDir, "bin");
|
||||
core.addPath(binDir);
|
||||
|
||||
return toolDir;
|
||||
}
|
||||
|
||||
async function run() {
|
||||
try {
|
||||
const inputs = getInputs();
|
||||
const { scannerVersion, scannerBinariesUrl } = inputs;
|
||||
|
||||
// Run sanity checks first
|
||||
runSanityChecks(inputs);
|
||||
|
||||
// Install Sonar Scanner CLI using @actions/tool-cache
|
||||
await installSonarScannerCLI(scannerVersion, scannerBinariesUrl);
|
||||
} catch (error) {
|
||||
core.setFailed(`Action failed: ${error.message}`);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
run();
|
||||
|
||||
35
src/utils.js
Normal file
35
src/utils.js
Normal file
@@ -0,0 +1,35 @@
|
||||
const platformFlavor = {
|
||||
linux: {
|
||||
x64: "linux-x64",
|
||||
arm64: "linux-aarch64",
|
||||
},
|
||||
win32: {
|
||||
x64: "windows-x64",
|
||||
},
|
||||
darwin: {
|
||||
x64: "macosx-x64",
|
||||
arm64: "macosx-aarch64",
|
||||
},
|
||||
};
|
||||
|
||||
export function getPlatformFlavor(platform, arch) {
|
||||
const flavor = platformFlavor[platform]?.[arch];
|
||||
|
||||
if (!flavor) {
|
||||
throw new Error(`Platform ${platform} ${arch} not supported`);
|
||||
}
|
||||
|
||||
return flavor;
|
||||
}
|
||||
|
||||
export function getScannerDownloadURL({
|
||||
scannerBinariesUrl,
|
||||
scannerVersion,
|
||||
flavor,
|
||||
}) {
|
||||
const trimURL = scannerBinariesUrl.replace(/\/$/, "");
|
||||
return `${trimURL}/sonar-scanner-cli-${scannerVersion}-${flavor}.zip`;
|
||||
}
|
||||
|
||||
export const scannerDirName = (version, flavor) =>
|
||||
`sonar-scanner-${version}-${flavor}`;
|
||||
Reference in New Issue
Block a user