Skip to content

Instantly share code, notes, and snippets.

@yangirov
Created April 4, 2024 14:19
Show Gist options
  • Save yangirov/92928bea113dbdf2a7cdd719d9b942c6 to your computer and use it in GitHub Desktop.
Save yangirov/92928bea113dbdf2a7cdd719d9b942c6 to your computer and use it in GitHub Desktop.
sonarqube-history
const simpleGit = require("simple-git");
const { execSync, spawn } = require("child_process");
// ПОМЕНЯТЬ ЗНАЧЕНИЯ
const repositoryPath = "/Users/user/Projects/project-key";
const sonarScannerCliPath = "/Users/user/Downloads/sonar-scanner/bin/sonar-scanner";
const getConfig = (version, date, index) => {
// Рандомизация даты чтобы не было коллизий (когда несколько релизов в один день)
const baseTimestamp = new Date(date + "T00:00:00+0000").getTime();
const timestamp = new Date(baseTimestamp + index * (60 * 1000));
const customDate = timestamp.toISOString().replace(/\.\d{3}Z$/, "+0000");
// ПОМЕНЯТЬ ЗНАЧЕНИЯ
const config = {
"sonar.host.url": "https://sonarqube.company.com",
"sonar.login": "SONAR_LOGIN",
"sonar.projectKey": "awesome-project",
"sonar.projectName": "awesome-project",
"sonar.sources": "src",
"sonar.sourceEncoding": "UTF-8",
"sonar.projectVersion": version,
"sonar.projectDate": customDate,
};
const params = Object.entries(config)
.map(([k, v]) => ` -D${k}=${v}`)
.join("");
return params;
};
const runBashScript = async (version, index) => {
console.log(`Выполнение скрипта для тега: ${version}`);
try {
const git = simpleGit(repositoryPath);
await git.checkout(version);
process.chdir(repositoryPath);
// Дата тега в YYYY-MM-DD
const dateCommand = `git show -s --format=%ci ${version} | cut -d' ' -f1`;
const tagDate = execSync(dateCommand, { encoding: "utf-8" }).trim();
const date = tagDate.split("\n").pop();
console.log(`Дата релиза: ${date}`);
// Конфиг для cli
const cliParams = getConfig(version, date, index);
const command = `${sonarScannerCliPath}${cliParams}`;
console.log(`Выполнение команды: ${command}`);
const childProcess = spawn(command, { shell: true });
childProcess.stdout.on("data", (data) => console.log(`${data}`));
childProcess.stderr.on("data", (data) => console.error(`${data}`));
return new Promise((resolve, reject) => {
childProcess.on("close", (code) => {
if (code === 0) {
resolve(`Скрипт для тега ${version} выполнен успешно`);
} else {
reject(
`Скрипт для тега ${version} завершился с кодом ошибки ${code}`
);
}
});
});
} catch (error) {
throw new Error(`Ошибка при переключении на тег ${version}: ${error}`);
}
};
const runScriptForTags = async () => {
try {
const git = simpleGit(repositoryPath);
const { all: tags } = await git.tags();
const releaseTags = tags.filter((t) => t.startsWith("v"));
console.log(`Теги: `, releaseTags);
for (const [index, tag] of releaseTags.entries()) {
try {
const result = await runBashScript(tag, index);
console.log(result);
} catch (error) {
console.error(error);
}
}
} catch (error) {
console.error(error);
}
};
runScriptForTags();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment