From 8d34f3e755a5c0db87482c609e8e96d5d1228110 Mon Sep 17 00:00:00 2001 From: Ashraf Hofny Date: Wed, 16 Mar 2022 07:02:28 +0200 Subject: [PATCH] feature: add sonar project status report --- package-lock.json | 41 +++++++++++++++++++++++++++++++++++++++++ package.json | 5 +++-- sonarqube.js | 37 +++++++++++++++++++++++++++++++++++-- 3 files changed, 79 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 39cef41..52c9eff 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,6 +9,7 @@ "version": "1.0.0", "license": "ISC", "dependencies": { + "axios": "^0.26.1", "dotenv": "^8.2.0" }, "devDependencies": { @@ -1732,6 +1733,14 @@ "integrity": "sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA==", "dev": true }, + "node_modules/axios": { + "version": "0.26.1", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.26.1.tgz", + "integrity": "sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==", + "dependencies": { + "follow-redirects": "^1.14.8" + } + }, "node_modules/babel-code-frame": { "version": "6.26.0", "resolved": "https://registry.npmjs.org/babel-code-frame/-/babel-code-frame-6.26.0.tgz", @@ -3569,6 +3578,25 @@ "node": ">=4" } }, + "node_modules/follow-redirects": { + "version": "1.14.9", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.9.tgz", + "integrity": "sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w==", + "funding": [ + { + "type": "individual", + "url": "https://github.com/sponsors/RubenVerborgh" + } + ], + "engines": { + "node": ">=4.0" + }, + "peerDependenciesMeta": { + "debug": { + "optional": true + } + } + }, "node_modules/for-in": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz", @@ -10599,6 +10627,14 @@ "integrity": "sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA==", "dev": true }, + "axios": { + "version": "0.26.1", + "resolved": "https://registry.npmjs.org/axios/-/axios-0.26.1.tgz", + "integrity": "sha512-fPwcX4EvnSHuInCMItEhAGnaSEXRBjtzh9fOtsE6E1G6p7vl7edEeZe11QHf18+6+9gR5PbKV/sGKNaD8YaMeA==", + "requires": { + "follow-redirects": "^1.14.8" + } + }, "babel-code-frame": { "version": "6.26.0", "resolved": "https://registry.npmjs.org/babel-code-frame/-/babel-code-frame-6.26.0.tgz", @@ -12113,6 +12149,11 @@ "locate-path": "^2.0.0" } }, + "follow-redirects": { + "version": "1.14.9", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.9.tgz", + "integrity": "sha512-MQDfihBQYMcyy5dhRDJUHcw7lb2Pv/TuE6xP1vyraLukNDHKbDxDNaOE3NbCAdKQApno+GPRyo1YAp89yCjK4w==" + }, "for-in": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz", diff --git a/package.json b/package.json index 7a330cd..255375b 100644 --- a/package.json +++ b/package.json @@ -12,14 +12,15 @@ "author": "", "license": "ISC", "dependencies": { + "axios": "^0.26.1", "dotenv": "^8.2.0" }, "devDependencies": { - "@types/jest": "^27.4.0", - "@types/node": "^17.0.16", "@commitlint/cli": "^16.1.0", "@commitlint/config-conventional": "^16.0.0", "@commitlint/types": "^16.0.0", + "@types/jest": "^27.4.0", + "@types/node": "^17.0.16", "jest": "^21.2.1", "jest-sonar-reporter": "^2.0.0", "sonarqube-scanner": "^2.8.1", diff --git a/sonarqube.js b/sonarqube.js index a8a0f6e..3cc629f 100644 --- a/sonarqube.js +++ b/sonarqube.js @@ -1,14 +1,47 @@ const scanner = require("sonarqube-scanner"); const dotenv = require("dotenv"); +const { default: axios } = require("axios"); dotenv.config(); +const projectKey = "node-ts-conventions"; + +const checkProjectStatus = async () => { + const projectStatus = await axios + .get(`${process.env.SONAR_URL}/api/qualitygates/project_status`, { + params: { + projectKey, + }, + auth: { + username: process.env.SONAR_LOGIN, + password: "", + }, + }) + .then((response) => { + return response?.data?.projectStatus; + }) + .catch((error) => { + console.log(`${error?.name}: ${error?.message}`); + + return null; + }); + + if (!projectStatus) { + throw new Error("Failed to fetch Project Status"); + } + + console.table(projectStatus.conditions); + + if (projectStatus.status !== "OK") { + throw new Error("QualityGate Failed!"); + } +}; scanner( { // this example uses local instance of SQ serverUrl: process.env.SONAR_URL, options: { - "sonar.projectKey": "node-ts-conventions", + "sonar.projectKey": projectKey, "sonar.projectVersion": "1.0.0", "sonar.login": process.env.SONAR_LOGIN, "sonar.sources": "src", @@ -20,5 +53,5 @@ scanner( "**/*.test.*,**/*.spec.js,**/*.spec.ts,**/*.mock.js,**/*.mock.ts,node_modules/*,coverage/lcov-report/*", }, }, - () => process.exit() + checkProjectStatus );