Skip to content

Commit 116e8f9

Browse files
committed
misc: extract CI test logic to reusable script
1 parent 260f1a0 commit 116e8f9

File tree

4 files changed

+333
-179
lines changed

4 files changed

+333
-179
lines changed

.github/workflows/ci.yml

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

.github/workflows/test.yml

Lines changed: 0 additions & 179 deletions
This file was deleted.

scripts/common.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
# Common utilities for project scripts
4+
# This file contains shared functions used across multiple scripts
5+
6+
# Colors for output
7+
RED='\033[0;31m'
8+
GREEN='\033[0;32m'
9+
YELLOW='\033[1;33m'
10+
BLUE='\033[1;36m'
11+
NC='\033[0m' # No Color
12+
13+
# Logging functions with consistent formatting
14+
log_info() { echo -e "${BLUE}ℹ️ $1${NC}"; }
15+
log_success() { echo -e "${GREEN}$1${NC}"; }
16+
log_warning() { echo -e "${YELLOW}⚠️ $1${NC}"; }
17+
log_error() { echo -e "${RED}$1${NC}"; }

0 commit comments

Comments
 (0)