From 15d5e2a6d330143550327bfef19fa364eee3f9b6 Mon Sep 17 00:00:00 2001
From: Benjamin Bertrand <benjamin.bertrand@esss.se>
Date: Mon, 12 Mar 2018 08:50:58 +0100
Subject: [PATCH] Add code analysis stage using sonar-scanner

Create unit test and coverage report with pytest
---
 .gitignore               |  3 +++
 .gitlab-ci.yml           | 16 +++++++++++++++-
 sonar-project.properties | 11 +++++++++++
 3 files changed, 29 insertions(+), 1 deletion(-)
 create mode 100644 sonar-project.properties

diff --git a/.gitignore b/.gitignore
index 8b50b39..91a2b40 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,3 +7,6 @@ settings*.cfg
 .cache
 .coverage
 /docs/_build
+.scannerwork
+coverage.xml
+junit.xml
diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index ec17fbf..b9dfed8 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -15,6 +15,7 @@ variables:
 stages:
   - build
   - test
+  - analyse
   - release
   - deploy
 
@@ -38,7 +39,20 @@ test:
     - redis:4.0
   before_script: []
   script:
-    - pytest --cov=app -v
+    - pytest --junitxml=junit.xml --cov-report xml:coverage.xml --cov=app -v
+  artifacts:
+    paths:
+      - junit.xml
+      - coverage.xml
+    expire_in: 1 hour
+
+analyse:
+  <<: *runner_tags
+  stage: analyse
+  image: registry.esss.lu.se/ics-docker/sonar-scanner:3
+  before_script: []
+  script:
+    - sonar-scanner -Dsonar.login=$SONARQUBE_TOKEN -Dsonar.projectVersion=$CI_COMMIT_REF_NAME
 
 release-image:
   <<: *runner_tags
diff --git a/sonar-project.properties b/sonar-project.properties
new file mode 100644
index 0000000..da42bdf
--- /dev/null
+++ b/sonar-project.properties
@@ -0,0 +1,11 @@
+# must be unique in a given SonarQube instance
+sonar.projectKey=csentry
+# this is the name displayed in the SonarQube UI.
+sonar.projectName=CSEntry
+ 
+# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
+# This property is optional if sonar.modules is set. 
+sonar.sources=app
+
+sonar.python.xunit.reportPath=junit.xml
+sonar.python.coverage.reportPath=coverage.xml
-- 
GitLab