From a43399299c007e20f633dcef34325e7461bca86b Mon Sep 17 00:00:00 2001 From: Guillermo Ignacio Enriquez Gutierrez Date: Sun, 25 Jul 2021 16:17:19 +0900 Subject: [PATCH 1/7] Add sonar qube analisys. to test code coverage --- .gitignore | 5 ++++- sonar.sh | 8 ++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) create mode 100755 sonar.sh diff --git a/.gitignore b/.gitignore index 0e1a8a6..4d7b303 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,7 @@ # Jazzy generated folders /docs/ -/build/ \ No newline at end of file +/build/ + +# SonarQube +.scannerwork/ diff --git a/sonar.sh b/sonar.sh new file mode 100755 index 0000000..40226bd --- /dev/null +++ b/sonar.sh @@ -0,0 +1,8 @@ +#! /bin/bash + +sonar-scanner \ + -X \ + -Dsonar.projectKey=DataStructuresKey \ + -Dsonar.sources=. \ + -Dsonar.host.url=http://localhost:9000 \ + -Dsonar.login=ee4b2751f7ecbf413cd2e67ab8372419fadb2a79 \ No newline at end of file From e03b6dbc750d669f73005a099d04c29e4e57bcee Mon Sep 17 00:00:00 2001 From: Guillermo Ignacio Enriquez Gutierrez Date: Sun, 25 Jul 2021 17:02:14 +0900 Subject: [PATCH 2/7] Upgrade to swift 5.3 tools --- Package.swift | 2 +- sonar.sh | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Package.swift b/Package.swift index d6b18cc..9e7002b 100644 --- a/Package.swift +++ b/Package.swift @@ -1,4 +1,4 @@ -// swift-tools-version:5.1 +// swift-tools-version:5.3 // The swift-tools-version declares the minimum version of Swift required to build this package. import PackageDescription diff --git a/sonar.sh b/sonar.sh index 40226bd..87280e3 100755 --- a/sonar.sh +++ b/sonar.sh @@ -1,5 +1,6 @@ #! /bin/bash - +GIT_BRANCH=$(git branch | grep '\*' | awk '{print $2}') +echo GIT_BRANCH $GIT_BRANCH sonar-scanner \ -X \ -Dsonar.projectKey=DataStructuresKey \ From 166e7d58cd22b6d053cc33a452ee02514eeca10b Mon Sep 17 00:00:00 2001 From: Guillermo Ignacio Enriquez Gutierrez Date: Mon, 26 Jul 2021 02:39:53 +0900 Subject: [PATCH 3/7] Fix workflow --- .github/workflows/swift.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/swift.yml b/.github/workflows/swift.yml index 3fdfa7d..293dc87 100644 --- a/.github/workflows/swift.yml +++ b/.github/workflows/swift.yml @@ -17,6 +17,6 @@ jobs: run: swift build -v - name: Run tests run: | - xcrun xcodebuild -list; \ - xcrun xcodebuild -scheme DataStructures-Package test; \ - echo done; + xcrun xcodebuild -list && \ + xcrun xcodebuild -scheme DataStructures test && \ + echo done From bddf544a5a817f7165a38dfd10808c9b60c5adc8 Mon Sep 17 00:00:00 2001 From: Guillermo Ignacio Enriquez Gutierrez Date: Mon, 26 Jul 2021 03:17:27 +0900 Subject: [PATCH 4/7] generate l --- .github/workflows/swift.yml | 12 +++++++--- tmp.sonarcloud.properties | 15 +++++++++++++ xccov-to-sonarqube-generic.sh | 41 +++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 tmp.sonarcloud.properties create mode 100755 xccov-to-sonarqube-generic.sh diff --git a/.github/workflows/swift.yml b/.github/workflows/swift.yml index 293dc87..8583969 100644 --- a/.github/workflows/swift.yml +++ b/.github/workflows/swift.yml @@ -15,8 +15,14 @@ jobs: - uses: actions/checkout@v2 - name: Build run: swift build -v - - name: Run tests + - name: Test and Analyze run: | - xcrun xcodebuild -list && \ - xcrun xcodebuild -scheme DataStructures test && \ + xcodebuild -list + xcodebuild -scheme DataStructures test -resultBundlePath "${GITHUB_WORKSPACE}/testResults.xcresult" -enableCodeCoverage YES -quiet + xccov-to-sonarqube-generic.sh testResults.xcresult > generic-coverage.xml echo done + - name: Archive code coverage results + uses: actions/upload-artifact@v2 + with: + name: code-coverage-report + path: generic-coverage.xml diff --git a/tmp.sonarcloud.properties b/tmp.sonarcloud.properties new file mode 100644 index 0000000..881bd74 --- /dev/null +++ b/tmp.sonarcloud.properties @@ -0,0 +1,15 @@ +# Path to sources +sonar.sources=Sources/ +#sonar.exclusions= +#sonar.inclusions= + +# Path to tests +sonar.tests=Tests/ +#sonar.test.exclusions= +#sonar.test.inclusions= + +# Source encoding +sonar.sourceEncoding=UTF-8 + +# Exclusions for copy-paste detection +#sonar.cpd.exclusions= diff --git a/xccov-to-sonarqube-generic.sh b/xccov-to-sonarqube-generic.sh new file mode 100755 index 0000000..f89e36e --- /dev/null +++ b/xccov-to-sonarqube-generic.sh @@ -0,0 +1,41 @@ +#!/usr/bin/env bash + +# Swift Coverage Results Import +# https://docs.sonarqube.org/pages/viewpage.action?pageId=6963001 +# +# Original source +# https://github.com/SonarSource/sonar-scanning-examples/blob/master/swift-coverage/swift-coverage-example/xccov-to-sonarqube-generic.sh + +set -euo pipefail + +function convert_file { + local xccovarchive_file="$1" + local file_name="$2" + local xccov_options="$3" + echo " " + # Ignore errors (2> /dev/null) because sometimes xcode output pollutes input of the next command + xcrun xccov view $xccov_options --file "$file_name" "$xccovarchive_file" 2> /dev/null | \ + sed -n ' + s/^ *\([0-9][0-9]*\): 0.*$/ /p; + s/^ *\([0-9][0-9]*\): [1-9].*$/ /p + ' + echo ' ' +} + +function xccov_to_generic { + echo '' + for xccovarchive_file in "$@"; do + local xccov_options="" + if [[ $xccovarchive_file == *".xcresult"* ]]; then + xccov_options="--archive" + fi + # Ignore errors (2> /dev/null) because sometimes xcode output pollutes input of the next command. + # Filter m files since they are not analized by sonar project yet. + xcrun xccov view $xccov_options --file-list "$xccovarchive_file" 2> /dev/null | grep -v '.m$' | while read -r file_name; do + convert_file "$xccovarchive_file" "$file_name" "$xccov_options" + done + done + echo '' +} + +xccov_to_generic "$@" \ No newline at end of file From ff4734f08be1118d26ae699a424ce3972adc3cb0 Mon Sep 17 00:00:00 2001 From: Guillermo Ignacio Enriquez Gutierrez Date: Mon, 26 Jul 2021 03:21:15 +0900 Subject: [PATCH 5/7] Fix ci --- .github/workflows/swift.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/swift.yml b/.github/workflows/swift.yml index 8583969..9cdd453 100644 --- a/.github/workflows/swift.yml +++ b/.github/workflows/swift.yml @@ -19,7 +19,7 @@ jobs: run: | xcodebuild -list xcodebuild -scheme DataStructures test -resultBundlePath "${GITHUB_WORKSPACE}/testResults.xcresult" -enableCodeCoverage YES -quiet - xccov-to-sonarqube-generic.sh testResults.xcresult > generic-coverage.xml + ./xccov-to-sonarqube-generic.sh testResults.xcresult > generic-coverage.xml echo done - name: Archive code coverage results uses: actions/upload-artifact@v2 From dda1f39992f13e273b66e1fb02b34ce1bd148589 Mon Sep 17 00:00:00 2001 From: Guillermo Ignacio Enriquez Gutierrez Date: Mon, 26 Jul 2021 03:50:44 +0900 Subject: [PATCH 6/7] Add sonar cloud as github action --- .github/workflows/swift.yml | 26 +++++++++++++++++++++++--- .sonarcloud.properties | 21 +++++++++++++++++++++ tmp.sonarcloud.properties | 15 --------------- 3 files changed, 44 insertions(+), 18 deletions(-) create mode 100644 .sonarcloud.properties delete mode 100644 tmp.sonarcloud.properties diff --git a/.github/workflows/swift.yml b/.github/workflows/swift.yml index 9cdd453..2b4d7e9 100644 --- a/.github/workflows/swift.yml +++ b/.github/workflows/swift.yml @@ -5,6 +5,7 @@ on: branches: [ master ] pull_request: branches: [ master ] + types: [opened, synchronize, reopened] jobs: build: @@ -15,14 +16,33 @@ jobs: - uses: actions/checkout@v2 - name: Build run: swift build -v - - name: Test and Analyze + - name: Test run: | xcodebuild -list xcodebuild -scheme DataStructures test -resultBundlePath "${GITHUB_WORKSPACE}/testResults.xcresult" -enableCodeCoverage YES -quiet ./xccov-to-sonarqube-generic.sh testResults.xcresult > generic-coverage.xml echo done - - name: Archive code coverage results + - name: Upload code coverage results uses: actions/upload-artifact@v2 with: - name: code-coverage-report + name: generic-code-coverage-report path: generic-coverage.xml + + sonarcloud: + name: SonarCloud + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis + - name: Download code coverage report + uses: actions/download-artifact@v2 + with: + name: generic-code-coverage-report + - name: Check xml exists + run: head generic-coverage.xml + - name: SonarCloud Scan + uses: SonarSource/sonarcloud-github-action@master + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} \ No newline at end of file diff --git a/.sonarcloud.properties b/.sonarcloud.properties new file mode 100644 index 0000000..1410ab7 --- /dev/null +++ b/.sonarcloud.properties @@ -0,0 +1,21 @@ +sonar.projectKey=nacho4d_DataStructures +sonar.organization=nacho4d +# This is the name and version displayed in the SonarCloud UI. +#sonar.projectName=DataStructures +#sonar.projectVersion=1.0 + + +# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows. +sonar.sources=Sources/ +#sonar.exclusions= +#sonar.inclusions= +sonar.tests=Tests/ +#sonar.test.exclusions= +#sonar.test.inclusions= + +# Source encoding +sonar.sourceEncoding=UTF-8 + +# Exclusions for copy-paste detection +#sonar.cpd.exclusions= +sonar.coverageReportPaths=generic-coverage.xml diff --git a/tmp.sonarcloud.properties b/tmp.sonarcloud.properties deleted file mode 100644 index 881bd74..0000000 --- a/tmp.sonarcloud.properties +++ /dev/null @@ -1,15 +0,0 @@ -# Path to sources -sonar.sources=Sources/ -#sonar.exclusions= -#sonar.inclusions= - -# Path to tests -sonar.tests=Tests/ -#sonar.test.exclusions= -#sonar.test.inclusions= - -# Source encoding -sonar.sourceEncoding=UTF-8 - -# Exclusions for copy-paste detection -#sonar.cpd.exclusions= From de146c976181887141e93114de04aac204c38fe4 Mon Sep 17 00:00:00 2001 From: Guillermo Ignacio Enriquez Gutierrez Date: Mon, 26 Jul 2021 03:54:26 +0900 Subject: [PATCH 7/7] needs build --- .github/workflows/swift.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/swift.yml b/.github/workflows/swift.yml index 2b4d7e9..e6c7853 100644 --- a/.github/workflows/swift.yml +++ b/.github/workflows/swift.yml @@ -9,9 +9,8 @@ on: jobs: build: - + name: Build runs-on: macos-latest - steps: - uses: actions/checkout@v2 - name: Build @@ -31,6 +30,7 @@ jobs: sonarcloud: name: SonarCloud runs-on: ubuntu-latest + needs: build steps: - uses: actions/checkout@v2 with: