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