|
| 1 | +name: CI |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + - misc/RMET-4346/ci-cd-v2 # For testing purposes |
| 8 | + pull_request: |
| 9 | + types: [opened, synchronize, reopened] |
| 10 | + |
| 11 | +permissions: |
| 12 | + contents: read |
| 13 | + pull-requests: write # For PR comments |
| 14 | + |
| 15 | +jobs: |
| 16 | + test: |
| 17 | + name: Run Tests |
| 18 | + runs-on: macos-latest |
| 19 | + |
| 20 | + steps: |
| 21 | + - name: Checkout code |
| 22 | + uses: actions/checkout@v4 |
| 23 | + |
| 24 | + - name: Install Dependencies |
| 25 | + run: | |
| 26 | + chmod +x scripts/run_tests.sh |
| 27 | + scripts/run_tests.sh install-deps |
| 28 | + |
| 29 | + - name: Run Unit Tests |
| 30 | + run: | |
| 31 | + scripts/run_tests.sh test |
| 32 | +
|
| 33 | + - name: Extract Code Coverage |
| 34 | + run: | |
| 35 | + scripts/run_tests.sh coverage |
| 36 | +
|
| 37 | + - name: Generate Code Coverage Report for SonarQube |
| 38 | + run: | |
| 39 | + scripts/run_tests.sh coverage-report |
| 40 | + |
| 41 | + - name: Run SwiftLint for SonarQube |
| 42 | + run: | |
| 43 | + scripts/run_tests.sh swiftlint |
| 44 | + |
| 45 | + - name: Setup SonarQube Scanner |
| 46 | + uses: warchant/setup-sonar-scanner@v8 |
| 47 | + |
| 48 | + - name: Send to SonarCloud |
| 49 | + id: sonarcloud |
| 50 | + continue-on-error: true |
| 51 | + run: | |
| 52 | + if [ -f "sonar-project.properties" ]; then |
| 53 | + echo "🔍 Sending results to SonarCloud..." |
| 54 | + echo "📦 Commit: ${{ github.sha }}" |
| 55 | + |
| 56 | + if [ "${{ github.ref_name }}" = "main" ]; then |
| 57 | + echo "🌟 Analyzing main branch" |
| 58 | + sonar-scanner |
| 59 | + else |
| 60 | + echo "🌿 Analyzing feature branch: ${{ github.ref_name }}" |
| 61 | + sonar-scanner -Dsonar.branch.name="${{ github.ref_name }}" |
| 62 | + fi |
| 63 | + else |
| 64 | + echo "⚠️ sonar-project.properties not found, skipping SonarCloud" |
| 65 | + fi |
| 66 | + env: |
| 67 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 68 | + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} |
| 69 | + |
| 70 | + - name: Upload Test Results |
| 71 | + uses: actions/upload-artifact@v4 |
| 72 | + if: always() |
| 73 | + with: |
| 74 | + name: test-results |
| 75 | + path: | |
| 76 | + TestResults.xcresult |
| 77 | + sonar-reports/ |
| 78 | + build/reports/ |
| 79 | + |
| 80 | + - name: Comment Test Results |
| 81 | + if: github.event_name == 'pull_request' && always() |
| 82 | + uses: actions/github-script@v7 |
| 83 | + with: |
| 84 | + script: | |
| 85 | + const { execSync } = require('child_process'); |
| 86 | + |
| 87 | + // Get Xcode version dynamically |
| 88 | + const xcodeVersion = execSync('xcodebuild -version | head -1', { encoding: 'utf8' }).trim(); |
| 89 | + |
| 90 | + // Get coverage percentage from environment |
| 91 | + const coveragePercentage = process.env.COVERAGE_PERCENTAGE || 'N/A'; |
| 92 | + |
| 93 | + // Check if SonarCloud step succeeded by checking job status |
| 94 | + const sonarStepSucceeded = '${{ steps.sonarcloud.outcome }}' === 'success'; |
| 95 | + |
| 96 | + // Dynamic message based on SonarCloud success/failure |
| 97 | + const sonarMessage = sonarStepSucceeded |
| 98 | + ? '☁️ **SonarCloud**: Analysis completed - [View detailed report →](https://sonarcloud.io)' |
| 99 | + : '⚠️ **SonarCloud**: Upload failed - check workflow logs for details'; |
| 100 | + |
| 101 | + const nextStepsMessage = sonarStepSucceeded |
| 102 | + ? '📋 **Next Steps**: Review the SonarCloud analysis for code quality insights and coverage details.' |
| 103 | + : '📋 **Next Steps**: Coverage data is available in test artifacts. SonarCloud integration needs attention.'; |
| 104 | + |
| 105 | + await github.rest.issues.createComment({ |
| 106 | + issue_number: context.issue.number, |
| 107 | + owner: context.repo.owner, |
| 108 | + repo: context.repo.repo, |
| 109 | + body: `## 🧪 Test Results |
| 110 | +
|
| 111 | + ✅ **Tests**: All tests passed successfully! |
| 112 | + 📊 **Code Coverage**: ${coveragePercentage} |
| 113 | + ${sonarMessage} |
| 114 | +
|
| 115 | + **Environment:** |
| 116 | + - ${xcodeVersion} |
| 117 | + - iOS Simulator (${{ env.IOS_SIMULATOR_DEVICE }}) |
| 118 | + - macOS runner: ${{ runner.os }} |
| 119 | + |
| 120 | + ${nextStepsMessage}` |
| 121 | + }); |
0 commit comments