diff --git a/.github/workflows/swift.yml b/.github/workflows/swift.yml index 3fdfa7d..e6c7853 100644 --- a/.github/workflows/swift.yml +++ b/.github/workflows/swift.yml @@ -5,18 +5,44 @@ on: branches: [ master ] pull_request: branches: [ master ] + types: [opened, synchronize, reopened] jobs: build: - + name: Build runs-on: macos-latest - steps: - uses: actions/checkout@v2 - name: Build run: swift build -v - - name: Run tests + - name: Test run: | - xcrun xcodebuild -list; \ - xcrun xcodebuild -scheme DataStructures-Package test; \ - echo done; + 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: Upload code coverage results + uses: actions/upload-artifact@v2 + with: + name: generic-code-coverage-report + path: generic-coverage.xml + + sonarcloud: + name: SonarCloud + runs-on: ubuntu-latest + needs: build + 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/.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/.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/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 new file mode 100755 index 0000000..87280e3 --- /dev/null +++ b/sonar.sh @@ -0,0 +1,9 @@ +#! /bin/bash +GIT_BRANCH=$(git branch | grep '\*' | awk '{print $2}') +echo GIT_BRANCH $GIT_BRANCH +sonar-scanner \ + -X \ + -Dsonar.projectKey=DataStructuresKey \ + -Dsonar.sources=. \ + -Dsonar.host.url=http://localhost:9000 \ + -Dsonar.login=ee4b2751f7ecbf413cd2e67ab8372419fadb2a79 \ No newline at end of file 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