Skip to content

Commit ece10df

Browse files
jeremy-davis-sonarsourcehenryju
authored andcommitted
SQSCANGHA-112 Extract installation step and other fixes
1 parent ee80e84 commit ece10df

File tree

6 files changed

+182
-174
lines changed

6 files changed

+182
-174
lines changed

dist/index.js

Lines changed: 103 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -29844,6 +29844,92 @@ function requireToolCache () {
2984429844

2984529845
var toolCacheExports = requireToolCache();
2984629846

29847+
const platformFlavor = {
29848+
linux: {
29849+
x64: "linux-x64",
29850+
arm64: "linux-aarch64",
29851+
},
29852+
win32: {
29853+
x64: "windows-x64",
29854+
},
29855+
darwin: {
29856+
x64: "macosx-x64",
29857+
arm64: "macosx-aarch64",
29858+
},
29859+
};
29860+
29861+
function getPlatformFlavor(platform, arch) {
29862+
const flavor = platformFlavor[platform]?.[arch];
29863+
29864+
if (!flavor) {
29865+
throw new Error(`Platform ${platform} ${arch} not supported`);
29866+
}
29867+
29868+
return flavor;
29869+
}
29870+
29871+
function getScannerDownloadURL({
29872+
scannerBinariesUrl,
29873+
scannerVersion,
29874+
flavor,
29875+
}) {
29876+
const trimURL = scannerBinariesUrl.replace(/\/$/, "");
29877+
return `${trimURL}/sonar-scanner-cli-${scannerVersion}-${flavor}.zip`;
29878+
}
29879+
29880+
const scannerDirName = (version, flavor) =>
29881+
`sonar-scanner-${version}-${flavor}`;
29882+
29883+
const TOOLNAME = "sonar-scanner-cli";
29884+
29885+
/**
29886+
* Download the Sonar Scanner CLI for the current environment and cache it.
29887+
*/
29888+
async function installSonarScanner({
29889+
scannerVersion,
29890+
scannerBinariesUrl,
29891+
}) {
29892+
const flavor = getPlatformFlavor(require$$0.platform(), require$$0.arch());
29893+
29894+
// Check if tool is already cached
29895+
let toolDir = toolCacheExports.find(TOOLNAME, scannerVersion, flavor);
29896+
29897+
if (!toolDir) {
29898+
console.log(
29899+
`Installing Sonar Scanner CLI ${scannerVersion} for ${flavor}...`
29900+
);
29901+
29902+
const downloadUrl = getScannerDownloadURL({
29903+
scannerBinariesUrl,
29904+
scannerVersion,
29905+
flavor,
29906+
});
29907+
29908+
console.log(`Downloading from: ${downloadUrl}`);
29909+
29910+
const downloadPath = await toolCacheExports.downloadTool(downloadUrl);
29911+
const extractedPath = await toolCacheExports.extractZip(downloadPath);
29912+
29913+
// Find the actual scanner directory inside the extracted folder
29914+
const scannerPath = path.join(
29915+
extractedPath,
29916+
scannerDirName(scannerVersion, flavor)
29917+
);
29918+
29919+
toolDir = await toolCacheExports.cacheDir(scannerPath, TOOLNAME, scannerVersion, flavor);
29920+
29921+
console.log(`Sonar Scanner CLI cached to: ${toolDir}`);
29922+
} else {
29923+
console.log(`Using cached Sonar Scanner CLI from: ${toolDir}`);
29924+
}
29925+
29926+
// Add the bin directory to PATH
29927+
const binDir = path.join(toolDir, "bin");
29928+
coreExports.addPath(binDir);
29929+
29930+
return toolDir;
29931+
}
29932+
2984729933
var execExports = requireExec();
2984829934

2984929935
function parseArgsStringToArgv(value, env, file) {
@@ -30029,8 +30115,8 @@ function validateScannerVersion(version) {
3002930115
}
3003030116
}
3003130117

30032-
function checkSonarToken(core) {
30033-
if (!process.env.SONAR_TOKEN) {
30118+
function checkSonarToken(core, sonarToken) {
30119+
{
3003430120
core.warning(
3003530121
"Running this GitHub Action without SONAR_TOKEN is not recommended"
3003630122
);
@@ -30058,44 +30144,6 @@ function checkGradleProject(core, projectBaseDir) {
3005830144
}
3005930145
}
3006030146

30061-
const platformFlavor = {
30062-
linux: {
30063-
x64: "linux-x64",
30064-
arm64: "linux-aarch64",
30065-
},
30066-
win32: {
30067-
x64: "windows-x64",
30068-
},
30069-
darwin: {
30070-
x64: "macosx-x64",
30071-
arm64: "macosx-aarch64",
30072-
},
30073-
};
30074-
30075-
function getPlatformFlavor(platform, arch) {
30076-
const flavor = platformFlavor[platform]?.[arch];
30077-
30078-
if (!flavor) {
30079-
throw new Error(`Platform ${platform} ${arch} not supported`);
30080-
}
30081-
30082-
return flavor;
30083-
}
30084-
30085-
function getScannerDownloadURL({
30086-
scannerBinariesUrl,
30087-
scannerVersion,
30088-
flavor,
30089-
}) {
30090-
const trimURL = scannerBinariesUrl.replace(/\/$/, "");
30091-
return `${trimURL}/sonar-scanner-cli-${scannerVersion}-${flavor}.zip`;
30092-
}
30093-
30094-
const scannerDirName = (version, flavor) =>
30095-
`sonar-scanner-${version}-${flavor}`;
30096-
30097-
const TOOLNAME = "sonar-scanner-cli";
30098-
3009930147
/**
3010030148
* Inputs are defined in action.yml
3010130149
*/
@@ -30108,13 +30156,20 @@ function getInputs() {
3010830156
return { args, projectBaseDir, scannerBinariesUrl, scannerVersion };
3010930157
}
3011030158

30111-
function getRunnerEnv() {
30159+
/**
30160+
* These RUNNER env variables come from GitHub by default.
30161+
* See https://docs.github.com/en/actions/reference/workflows-and-actions/variables#default-environment-variables
30162+
*
30163+
* The others are optional env variables provided by the user of the action
30164+
*/
30165+
function getEnvVariables() {
3011230166
return {
30113-
RUNNER_OS: process.env.RUNNER_OS,
30114-
SONARCLOUD_URL: process.env.SONARCLOUD_URL,
3011530167
RUNNER_DEBUG: process.env.RUNNER_DEBUG,
30116-
SONAR_ROOT_CERT: process.env.SONAR_ROOT_CERT,
30168+
RUNNER_OS: process.env.RUNNER_OS,
3011730169
RUNNER_TEMP: process.env.RUNNER_TEMP,
30170+
SONAR_ROOT_CERT: process.env.SONAR_ROOT_CERT,
30171+
SONARCLOUD_URL: process.env.SONARCLOUD_URL,
30172+
SONAR_TOKEN: process.env.SONAR_TOKEN,
3011830173
};
3011930174
}
3012030175

@@ -30132,64 +30187,20 @@ function runSanityChecks(inputs) {
3013230187
}
3013330188
}
3013430189

30135-
async function installSonarScannerCLI({ scannerVersion, scannerBinariesUrl }) {
30136-
const flavor = getPlatformFlavor(require$$0.platform(), require$$0.arch());
30137-
30138-
// Check if tool is already cached
30139-
let toolDir = toolCacheExports.find(TOOLNAME, scannerVersion, flavor);
30140-
30141-
if (!toolDir) {
30142-
console.log(
30143-
`Installing Sonar Scanner CLI ${scannerVersion} for ${flavor}...`
30144-
);
30145-
30146-
const downloadUrl = getScannerDownloadURL({
30147-
scannerBinariesUrl,
30148-
scannerVersion,
30149-
flavor,
30150-
});
30151-
30152-
console.log(`Downloading from: ${downloadUrl}`);
30153-
30154-
const downloadPath = await toolCacheExports.downloadTool(downloadUrl);
30155-
const extractedPath = await toolCacheExports.extractZip(downloadPath);
30156-
30157-
// Find the actual scanner directory inside the extracted folder
30158-
const scannerPath = path.join(
30159-
extractedPath,
30160-
scannerDirName(scannerVersion, flavor)
30161-
);
30162-
30163-
toolDir = await toolCacheExports.cacheDir(scannerPath, TOOLNAME, scannerVersion, flavor);
30164-
30165-
console.log(`Sonar Scanner CLI cached to: ${toolDir}`);
30166-
} else {
30167-
console.log(`Using cached Sonar Scanner CLI from: ${toolDir}`);
30168-
}
30169-
30170-
// Add the bin directory to PATH
30171-
const binDir = path.join(toolDir, "bin");
30172-
coreExports.addPath(binDir);
30173-
30174-
return toolDir;
30175-
}
30176-
3017730190
async function run() {
3017830191
try {
3017930192
const { args, projectBaseDir, scannerVersion, scannerBinariesUrl } =
3018030193
getInputs();
30194+
const runnerEnv = getEnvVariables();
30195+
const { sonarToken } = runnerEnv;
3018130196

30182-
// Run sanity checks first
30183-
runSanityChecks({ projectBaseDir, scannerVersion });
30197+
runSanityChecks({ projectBaseDir, scannerVersion, sonarToken });
3018430198

30185-
// Install Sonar Scanner CLI using @actions/tool-cache
30186-
const scannerDir = await installSonarScannerCLI({
30199+
const scannerDir = await installSonarScanner({
3018730200
scannerVersion,
3018830201
scannerBinariesUrl,
3018930202
});
3019030203

30191-
// Run the sonar scanner
30192-
const runnerEnv = getRunnerEnv();
3019330204
await runSonarScanner(args, projectBaseDir, scannerDir, runnerEnv);
3019430205
} catch (error) {
3019530206
coreExports.setFailed(`Action failed: ${error.message}`);

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/__tests__/sanity-checks.test.js

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@ describe("validateScannerVersion", () => {
6363

6464
describe("checkSonarToken", () => {
6565
it("calls core.warning when SONAR_TOKEN is not set", () => {
66-
const originalToken = process.env.SONAR_TOKEN;
67-
delete process.env.SONAR_TOKEN;
68-
6966
const warning = mock.fn();
7067

7168
checkSonarToken(mockCore({ warning }));
@@ -75,27 +72,14 @@ describe("checkSonarToken", () => {
7572
warning.mock.calls[0].arguments[0],
7673
"Running this GitHub Action without SONAR_TOKEN is not recommended"
7774
);
78-
79-
if (originalToken) {
80-
process.env.SONAR_TOKEN = originalToken;
81-
}
8275
});
8376

8477
it("does not call core.warning when SONAR_TOKEN is set", () => {
85-
const originalToken = process.env.SONAR_TOKEN;
86-
process.env.SONAR_TOKEN = "test-token";
87-
8878
const warning = mock.fn();
8979

90-
checkSonarToken(mockCore({ warning }));
80+
checkSonarToken(mockCore({ warning }), "test-token");
9181

9282
assert.equal(warning.mock.calls.length, 0);
93-
94-
if (originalToken) {
95-
process.env.SONAR_TOKEN = originalToken;
96-
} else {
97-
delete process.env.SONAR_TOKEN;
98-
}
9983
});
10084
});
10185

src/index.js

Lines changed: 16 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
11
import * as core from "@actions/core";
2-
import * as tc from "@actions/tool-cache";
3-
import * as os from "os";
4-
import * as path from "path";
2+
import { installSonarScanner } from "./install-sonar-scanner";
53
import { runSonarScanner } from "./run-sonar-scanner";
64
import {
75
checkGradleProject,
86
checkMavenProject,
97
checkSonarToken,
108
validateScannerVersion,
119
} from "./sanity-checks";
12-
import {
13-
getPlatformFlavor,
14-
getScannerDownloadURL,
15-
scannerDirName,
16-
} from "./utils";
17-
18-
const TOOLNAME = "sonar-scanner-cli";
1910

2011
/**
2112
* Inputs are defined in action.yml
@@ -29,13 +20,20 @@ function getInputs() {
2920
return { args, projectBaseDir, scannerBinariesUrl, scannerVersion };
3021
}
3122

32-
function getRunnerEnv() {
23+
/**
24+
* These RUNNER env variables come from GitHub by default.
25+
* See https://docs.github.com/en/actions/reference/workflows-and-actions/variables#default-environment-variables
26+
*
27+
* The others are optional env variables provided by the user of the action
28+
*/
29+
function getEnvVariables() {
3330
return {
34-
RUNNER_OS: process.env.RUNNER_OS,
35-
SONARCLOUD_URL: process.env.SONARCLOUD_URL,
3631
RUNNER_DEBUG: process.env.RUNNER_DEBUG,
37-
SONAR_ROOT_CERT: process.env.SONAR_ROOT_CERT,
32+
RUNNER_OS: process.env.RUNNER_OS,
3833
RUNNER_TEMP: process.env.RUNNER_TEMP,
34+
SONAR_ROOT_CERT: process.env.SONAR_ROOT_CERT,
35+
SONARCLOUD_URL: process.env.SONARCLOUD_URL,
36+
SONAR_TOKEN: process.env.SONAR_TOKEN,
3937
};
4038
}
4139

@@ -53,64 +51,20 @@ function runSanityChecks(inputs) {
5351
}
5452
}
5553

56-
async function installSonarScannerCLI({ scannerVersion, scannerBinariesUrl }) {
57-
const flavor = getPlatformFlavor(os.platform(), os.arch());
58-
59-
// Check if tool is already cached
60-
let toolDir = tc.find(TOOLNAME, scannerVersion, flavor);
61-
62-
if (!toolDir) {
63-
console.log(
64-
`Installing Sonar Scanner CLI ${scannerVersion} for ${flavor}...`
65-
);
66-
67-
const downloadUrl = getScannerDownloadURL({
68-
scannerBinariesUrl,
69-
scannerVersion,
70-
flavor,
71-
});
72-
73-
console.log(`Downloading from: ${downloadUrl}`);
74-
75-
const downloadPath = await tc.downloadTool(downloadUrl);
76-
const extractedPath = await tc.extractZip(downloadPath);
77-
78-
// Find the actual scanner directory inside the extracted folder
79-
const scannerPath = path.join(
80-
extractedPath,
81-
scannerDirName(scannerVersion, flavor)
82-
);
83-
84-
toolDir = await tc.cacheDir(scannerPath, TOOLNAME, scannerVersion, flavor);
85-
86-
console.log(`Sonar Scanner CLI cached to: ${toolDir}`);
87-
} else {
88-
console.log(`Using cached Sonar Scanner CLI from: ${toolDir}`);
89-
}
90-
91-
// Add the bin directory to PATH
92-
const binDir = path.join(toolDir, "bin");
93-
core.addPath(binDir);
94-
95-
return toolDir;
96-
}
97-
9854
async function run() {
9955
try {
10056
const { args, projectBaseDir, scannerVersion, scannerBinariesUrl } =
10157
getInputs();
58+
const runnerEnv = getEnvVariables();
59+
const { sonarToken } = runnerEnv;
10260

103-
// Run sanity checks first
104-
runSanityChecks({ projectBaseDir, scannerVersion });
61+
runSanityChecks({ projectBaseDir, scannerVersion, sonarToken });
10562

106-
// Install Sonar Scanner CLI using @actions/tool-cache
107-
const scannerDir = await installSonarScannerCLI({
63+
const scannerDir = await installSonarScanner({
10864
scannerVersion,
10965
scannerBinariesUrl,
11066
});
11167

112-
// Run the sonar scanner
113-
const runnerEnv = getRunnerEnv();
11468
await runSonarScanner(args, projectBaseDir, scannerDir, runnerEnv);
11569
} catch (error) {
11670
core.setFailed(`Action failed: ${error.message}`);

0 commit comments

Comments
 (0)