|
| 1 | +name: CI |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + pull_request: |
| 8 | + types: [opened, synchronize, reopened] |
| 9 | + |
| 10 | +permissions: |
| 11 | + contents: read |
| 12 | + pull-requests: write # For PR comments |
| 13 | + |
| 14 | +env: |
| 15 | + PROJECT_NAME: OSBarcodeLib |
| 16 | + SCHEME_NAME: OSBarcodeLib |
| 17 | + XCODEPROJ_PATH: OSBarcodeLib.xcodeproj |
| 18 | + XCODE_VERSION: 16.4 |
| 19 | + DESTINATION: 'platform=iOS Simulator,OS=latest,name=iPhone 16' |
| 20 | + COVERAGE_TARGET_FILTER: OSBarcodeLib |
| 21 | + BUILD_REPORTS_DIR: build/reports |
| 22 | + SONAR_REPORTS_DIR: sonar-reports |
| 23 | + |
| 24 | +jobs: |
| 25 | + test: |
| 26 | + name: Run Tests |
| 27 | + runs-on: macos-15 |
| 28 | + |
| 29 | + steps: |
| 30 | + - name: Checkout code |
| 31 | + uses: actions/checkout@v4 |
| 32 | + |
| 33 | + - name: Install Dependencies |
| 34 | + uses: ./.github/actions/install-dependencies |
| 35 | + with: |
| 36 | + tools: swiftlint xcbeautify |
| 37 | + |
| 38 | + - name: Set Xcode version |
| 39 | + uses: ./.github/actions/set-xcode-version |
| 40 | + with: |
| 41 | + xcode-version: ${{ env.XCODE_VERSION }} |
| 42 | + |
| 43 | + - name: Run Unit Tests |
| 44 | + id: unit_tests |
| 45 | + env: |
| 46 | + SCHEME_NAME: ${{ env.SCHEME_NAME }} |
| 47 | + XCODEPROJ_PATH: ${{ env.XCODEPROJ_PATH }} |
| 48 | + IOS_SIMULATOR_DEVICE: ${{ env.IOS_SIMULATOR_DEVICE }} |
| 49 | + DESTINATION: ${{ env.DESTINATION }} |
| 50 | + run: | |
| 51 | + set -euo pipefail |
| 52 | + XCRESULT_NAME="TestResults.xcresult" |
| 53 | + mkdir -p "$BUILD_REPORTS_DIR" |
| 54 | + xcodebuild test \ |
| 55 | + -project "$XCODEPROJ_PATH" \ |
| 56 | + -scheme "$SCHEME_NAME" \ |
| 57 | + -destination "$DESTINATION" \ |
| 58 | + -configuration Debug \ |
| 59 | + -enableCodeCoverage YES \ |
| 60 | + -resultBundlePath "$XCRESULT_NAME" \ |
| 61 | + SKIP_SCRIPT_PHASES=YES \ |
| 62 | + CODE_SIGNING_ALLOWED=NO | xcbeautify --report junit --report-path "$BUILD_REPORTS_DIR" |
| 63 | + echo "xcresult_name=$XCRESULT_NAME" >> "$GITHUB_OUTPUT" |
| 64 | +
|
| 65 | + - name: Generate Code Coverage Report for SonarQube |
| 66 | + continue-on-error: true |
| 67 | + env: |
| 68 | + XCRESULT_NAME: ${{ steps.unit_tests.outputs.xcresult_name }} |
| 69 | + run: | |
| 70 | + set -euo pipefail |
| 71 | + echo "🔍 Generating SonarQube coverage report..." |
| 72 | + |
| 73 | + if [ ! -d "$XCRESULT_NAME" ]; then |
| 74 | + echo "⚠️ $XCRESULT_NAME not found. Skipping coverage report generation." |
| 75 | + exit 0 |
| 76 | + fi |
| 77 | +
|
| 78 | + mkdir -p ${{ env.SONAR_REPORTS_DIR }} |
| 79 | + |
| 80 | + echo "📦 Downloading coverage converter script..." |
| 81 | + curl -sSL https://raw.githubusercontent.com/SonarSource/sonar-scanning-examples/master/swift-coverage/swift-coverage-example/xccov-to-sonarqube-generic.sh -o xccov-to-sonarqube-generic.sh |
| 82 | + chmod +x xccov-to-sonarqube-generic.sh |
| 83 | +
|
| 84 | + echo "📝 Running coverage converter..." |
| 85 | + ./xccov-to-sonarqube-generic.sh TestResults.xcresult > ${{ env.SONAR_REPORTS_DIR }}/sonarqube-generic-coverage.xml |
| 86 | + echo "✅ SonarQube coverage report generated successfully" |
| 87 | +
|
| 88 | + - name: Run SwiftLint for SonarQube |
| 89 | + run: | |
| 90 | + set -euo pipefail |
| 91 | + echo "🔍 Running SwiftLint..." |
| 92 | + mkdir -p ${{ env.SONAR_REPORTS_DIR }} |
| 93 | + swiftlint --reporter checkstyle > "${{ env.SONAR_REPORTS_DIR }}/swiftlint.xml" || { |
| 94 | + echo "⚠️ SwiftLint finished with issues." |
| 95 | + exit 0 |
| 96 | + } |
| 97 | + echo "✅ SwiftLint report generated successfully" |
| 98 | + |
| 99 | + - name: Setup SonarQube Scanner |
| 100 | + uses: warchant/setup-sonar-scanner@v8 |
| 101 | + |
| 102 | + - name: Send to SonarCloud |
| 103 | + id: sonarcloud |
| 104 | + continue-on-error: true |
| 105 | + run: | |
| 106 | + set -euo pipefail |
| 107 | + if [ -z "${{ secrets.SONAR_TOKEN }}" ]; then |
| 108 | + echo "⚠️ SONAR_TOKEN secret is not set. Skipping SonarCloud analysis." |
| 109 | + exit 0 |
| 110 | + fi |
| 111 | + if [ -f "sonar-project.properties" ]; then |
| 112 | + echo "🔍 Sending results to SonarCloud..." |
| 113 | + echo "📦 Commit: ${{ github.sha }}" |
| 114 | + if [ "${{ github.ref_name }}" = "main" ]; then |
| 115 | + echo "🌟 Analyzing main branch" |
| 116 | + sonar-scanner |
| 117 | + else |
| 118 | + echo "🌿 Analyzing feature branch: ${{ github.ref_name }}" |
| 119 | + sonar-scanner -Dsonar.branch.name="${{ github.ref_name }}" |
| 120 | + fi |
| 121 | + else |
| 122 | + echo "⚠️ sonar-project.properties not found, skipping SonarCloud" |
| 123 | + fi |
| 124 | + env: |
| 125 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 126 | + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} |
| 127 | + |
| 128 | + - name: Upload Test Results |
| 129 | + uses: actions/upload-artifact@v4 |
| 130 | + if: always() |
| 131 | + with: |
| 132 | + name: test-results |
| 133 | + path: | |
| 134 | + ${{ steps.unit_tests.outputs.xcresult_name }} |
| 135 | + ${{ env.SONAR_REPORTS_DIR }} |
| 136 | + ${{ env.BUILD_REPORTS_DIR }} |
| 137 | +
|
| 138 | + - name: Comment Test Results |
| 139 | + if: github.event_name == 'pull_request' |
| 140 | + uses: actions/github-script@v7 |
| 141 | + env: |
| 142 | + XCRESULT_NAME: ${{ steps.unit_tests.outputs.xcresult_name }} |
| 143 | + COVERAGE_TARGET_FILTER: ${{ env.COVERAGE_TARGET_FILTER }} |
| 144 | + with: |
| 145 | + script: | |
| 146 | + const { execSync } = require('child_process'); |
| 147 | + const fs = require('fs'); |
| 148 | +
|
| 149 | + console.log('📝 Starting to comment test results...'); |
| 150 | + let coveragePercentage = 'N/A'; |
| 151 | + try { |
| 152 | + const xcresultName = process.env.XCRESULT_NAME; |
| 153 | + const coverageTarget = process.env.COVERAGE_TARGET_FILTER; |
| 154 | + console.log(`Checking result file: ${xcresultName}`); |
| 155 | + if (fs.existsSync(xcresultName)) { |
| 156 | + console.log('Result file found. Calculating coverage...'); |
| 157 | + const output = execSync(`xcrun xccov view --report "${xcresultName}"`).toString(); |
| 158 | + const match = output.match(new RegExp(`${coverageTarget}.*?([0-9]+\\.[0-9]+%)`)); |
| 159 | + if (match && match[1]) { |
| 160 | + coveragePercentage = match[1]; |
| 161 | + console.log(`Coverage found: ${coveragePercentage}`); |
| 162 | + } else { |
| 163 | + console.log('Coverage not found in report.'); |
| 164 | + } |
| 165 | + } else { |
| 166 | + console.log('Result file not found.'); |
| 167 | + } |
| 168 | + } catch (e) { |
| 169 | + console.error('Error calculating coverage:', e); |
| 170 | + coveragePercentage = 'N/A'; |
| 171 | + } |
| 172 | +
|
| 173 | + console.log('Commenting on PR with test results and coverage...'); |
| 174 | + await github.rest.issues.createComment({ |
| 175 | + issue_number: context.issue.number, |
| 176 | + owner: context.repo.owner, |
| 177 | + repo: context.repo.repo, |
| 178 | + body: `✅ **Tests**: All passed\n📊 **Coverage**: ${coveragePercentage}` |
| 179 | + }); |
| 180 | + console.log('Comment sent successfully.'); |
0 commit comments