SQSCANGHA-112 Extract installation step and other fixes

This commit is contained in:
Jeremy Davis
2025-09-10 14:37:34 +02:00
committed by Julien HENRY
parent ee80e84272
commit ece10df5d7
6 changed files with 182 additions and 174 deletions

View File

@@ -63,9 +63,6 @@ describe("validateScannerVersion", () => {
describe("checkSonarToken", () => {
it("calls core.warning when SONAR_TOKEN is not set", () => {
const originalToken = process.env.SONAR_TOKEN;
delete process.env.SONAR_TOKEN;
const warning = mock.fn();
checkSonarToken(mockCore({ warning }));
@@ -75,27 +72,14 @@ describe("checkSonarToken", () => {
warning.mock.calls[0].arguments[0],
"Running this GitHub Action without SONAR_TOKEN is not recommended"
);
if (originalToken) {
process.env.SONAR_TOKEN = originalToken;
}
});
it("does not call core.warning when SONAR_TOKEN is set", () => {
const originalToken = process.env.SONAR_TOKEN;
process.env.SONAR_TOKEN = "test-token";
const warning = mock.fn();
checkSonarToken(mockCore({ warning }));
checkSonarToken(mockCore({ warning }), "test-token");
assert.equal(warning.mock.calls.length, 0);
if (originalToken) {
process.env.SONAR_TOKEN = originalToken;
} else {
delete process.env.SONAR_TOKEN;
}
});
});

View File

@@ -1,7 +1,5 @@
import * as core from "@actions/core";
import * as tc from "@actions/tool-cache";
import * as os from "os";
import * as path from "path";
import { installSonarScanner } from "./install-sonar-scanner";
import { runSonarScanner } from "./run-sonar-scanner";
import {
checkGradleProject,
@@ -9,13 +7,6 @@ import {
checkSonarToken,
validateScannerVersion,
} from "./sanity-checks";
import {
getPlatformFlavor,
getScannerDownloadURL,
scannerDirName,
} from "./utils";
const TOOLNAME = "sonar-scanner-cli";
/**
* Inputs are defined in action.yml
@@ -29,13 +20,20 @@ function getInputs() {
return { args, projectBaseDir, scannerBinariesUrl, scannerVersion };
}
function getRunnerEnv() {
/**
* These RUNNER env variables come from GitHub by default.
* See https://docs.github.com/en/actions/reference/workflows-and-actions/variables#default-environment-variables
*
* The others are optional env variables provided by the user of the action
*/
function getEnvVariables() {
return {
RUNNER_OS: process.env.RUNNER_OS,
SONARCLOUD_URL: process.env.SONARCLOUD_URL,
RUNNER_DEBUG: process.env.RUNNER_DEBUG,
SONAR_ROOT_CERT: process.env.SONAR_ROOT_CERT,
RUNNER_OS: process.env.RUNNER_OS,
RUNNER_TEMP: process.env.RUNNER_TEMP,
SONAR_ROOT_CERT: process.env.SONAR_ROOT_CERT,
SONARCLOUD_URL: process.env.SONARCLOUD_URL,
SONAR_TOKEN: process.env.SONAR_TOKEN,
};
}
@@ -53,64 +51,20 @@ function runSanityChecks(inputs) {
}
}
async function installSonarScannerCLI({ scannerVersion, scannerBinariesUrl }) {
const flavor = getPlatformFlavor(os.platform(), os.arch());
// 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 { args, projectBaseDir, scannerVersion, scannerBinariesUrl } =
getInputs();
const runnerEnv = getEnvVariables();
const { sonarToken } = runnerEnv;
// Run sanity checks first
runSanityChecks({ projectBaseDir, scannerVersion });
runSanityChecks({ projectBaseDir, scannerVersion, sonarToken });
// Install Sonar Scanner CLI using @actions/tool-cache
const scannerDir = await installSonarScannerCLI({
const scannerDir = await installSonarScanner({
scannerVersion,
scannerBinariesUrl,
});
// Run the sonar scanner
const runnerEnv = getRunnerEnv();
await runSonarScanner(args, projectBaseDir, scannerDir, runnerEnv);
} catch (error) {
core.setFailed(`Action failed: ${error.message}`);

View File

@@ -0,0 +1,59 @@
import * as core from "@actions/core";
import * as tc from "@actions/tool-cache";
import * as os from "os";
import * as path from "path";
import {
getPlatformFlavor,
getScannerDownloadURL,
scannerDirName,
} from "./utils";
const TOOLNAME = "sonar-scanner-cli";
/**
* Download the Sonar Scanner CLI for the current environment and cache it.
*/
export async function installSonarScanner({
scannerVersion,
scannerBinariesUrl,
}) {
const flavor = getPlatformFlavor(os.platform(), os.arch());
// 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;
}

View File

@@ -14,8 +14,8 @@ export function validateScannerVersion(version) {
}
}
export function checkSonarToken(core) {
if (!process.env.SONAR_TOKEN) {
export function checkSonarToken(core, sonarToken) {
if (!sonarToken) {
core.warning(
"Running this GitHub Action without SONAR_TOKEN is not recommended"
);