From bd5d71cb4a93b4b72736b83f57ea7ce58d7c11d4 Mon Sep 17 00:00:00 2001
From: Joris Mancini <53527338+TheMaskedTurtle@users.noreply.github.com>
Date: Wed, 18 Sep 2024 10:24:13 +0200
Subject: [PATCH] chore: add sonar analysis in the CI (#97)

Signed-off-by: Joris Mancini <joris.mancini_externe@rte-france.com>
---
 .eslintrc.cjs                  |  3 +++
 .github/config/.licenserc.yaml |  1 +
 .github/workflows/build.yml    | 15 +++++++++++++--
 package.json                   |  1 +
 sonar-project.properties       |  4 ++++
 5 files changed, 22 insertions(+), 2 deletions(-)
 create mode 100644 sonar-project.properties

diff --git a/.eslintrc.cjs b/.eslintrc.cjs
index 68fe640e..8f16977f 100644
--- a/.eslintrc.cjs
+++ b/.eslintrc.cjs
@@ -14,4 +14,7 @@ module.exports = {
     rules: {
         'prettier/prettier': 'warn',
     },
+    ignorePatterns: [
+        'coverage',
+    ]
 };
diff --git a/.github/config/.licenserc.yaml b/.github/config/.licenserc.yaml
index 17acef94..eba10c7b 100644
--- a/.github/config/.licenserc.yaml
+++ b/.github/config/.licenserc.yaml
@@ -22,5 +22,6 @@ header:
     - '**/*.svg'
     - '**/*.css'
     - '**/*.conf'
+    - '**/*.properties'
 
   comment: on-failure
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index 4e6b9994..93453516 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -1,5 +1,9 @@
 name: CI
-on: [push]
+on:
+  push:
+    branches:
+      - 'main'
+  pull_request:
 
 jobs:
     license-headers:
@@ -24,6 +28,7 @@ jobs:
             - name: Checkout sources
               uses: actions/checkout@v2
               with:
+                  fetch-depth: 0
                   persist-credentials: false
 
             - name: FixForNodeWarnings
@@ -35,5 +40,11 @@ jobs:
                   npm install
                   npm run licenses-check
                   npm run lint
-                  npm run test
+                  npm run test:coverage
                   npm run build
+
+            - name: SonarCloud Scan
+              uses: SonarSource/sonarcloud-github-action@v3.0.0
+              env:
+                GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
+                SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
diff --git a/package.json b/package.json
index 71f48f71..55792546 100644
--- a/package.json
+++ b/package.json
@@ -27,6 +27,7 @@
         "build": "vite build",
         "prepublishOnly": "$npm_execpath run build",
         "test": "jest",
+        "test:coverage": "jest --coverage",
         "start": "vite demo/ --config vite.config.ts --open",
         "lint": "eslint . --ext js,mjs,jsx,ts,mts,tsx --max-warnings 0",
         "licenses-check": "license-checker --summary --excludePrivatePackages --production --onlyAllow \"$( jq -r .onlyAllow[] license-checker-config.json | tr '\n' ';')\" --excludePackages \"$( jq -r .excludePackages[] license-checker-config.json | tr '\n' ';')\""
diff --git a/sonar-project.properties b/sonar-project.properties
new file mode 100644
index 00000000..563b62dc
--- /dev/null
+++ b/sonar-project.properties
@@ -0,0 +1,4 @@
+sonar.projectKey=powsybl_powsybl-diagram-viewer
+sonar.organization=powsybl-ci-github
+sonar.projectBaseDir=src
+sonar.javascript.lcov.reportPaths=./coverage/lcov.info