Skip to content

Commit 49e37df

Browse files
authored
Add Incremental Code Coverage to the Workflow (#8197)
1 parent ac7bcdc commit 49e37df

File tree

5 files changed

+48
-24
lines changed

5 files changed

+48
-24
lines changed

.github/workflows/test_coverage.yml

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,10 @@ jobs:
258258
if: always()
259259
runs-on: macOS-latest
260260
steps:
261-
- uses: actions/checkout@v2
261+
- name: Checkout code
262+
uses: actions/checkout@v2
263+
with:
264+
fetch-depth: 0
262265
- name: Access to Metrics Service
263266
if: ${{ env.METRICS_SERVICE_SECRET }} && (github.event.action != 'closed' || github.event.pull_request.merged)
264267
run: |
@@ -287,6 +290,31 @@ jobs:
287290
cd scripts/code_coverage_report/generate_code_coverage_report
288291
swift run CoverageReportGenerator --presubmit "firebase/firebase-ios-sdk" --head-commit "${GITHUB_SHA}" --token $(gcloud auth print-identity-token) --xcresult-dir "/Users/runner/test/codecoverage" --log-link "https://github.com/firebase/firebase-ios-sdk/actions/runs/${GITHUB_RUN_ID}" --pull-request-num ${{github.event.pull_request.number}} --base-commit "$base_commit"
289292
fi
293+
- name: Incremental Code Coverage
294+
if: github.event.pull_request.merged != true && github.event.action != 'closed'
295+
env:
296+
base_commit: ${{ needs.check.outputs.base_commit }}
297+
run: |
298+
# Get Head commit of the branch, instead of a merge commit created by actions/checkout.
299+
GITHUB_SHA=$(cat $GITHUB_EVENT_PATH | jq -r .pull_request.head.sha)
300+
# Get a JSON of `git diff` from the base commit.
301+
git diff -U0 ${base_commit} "${GITHUB_SHA}" | scripts/code_coverage_report/git_diff_to_json.sh > scripts/code_coverage_report/generate_code_coverage_report/git_diff.json
302+
cat "scripts/code_coverage_report/generate_code_coverage_report/git_diff.json"
303+
if [ -d "${{steps.download.outputs.download-path}}" ]; then
304+
# Create an uncovered_file_lines.json including code lines not covered by tests.
305+
cd scripts/code_coverage_report/generate_code_coverage_report
306+
swift run IncrementalCoverageReportGenerator --changed-files "git_diff.json" --file-archive-root-path "${GITHUB_WORKSPACE}" --xcresult-dir "${{steps.download.outputs.download-path}}" --uncovered-line-file-json "uncovered_file_lines.json"
307+
# Post uncovered lines to PRs.
308+
pull_number=$(jq --raw-output .pull_request.number "$GITHUB_EVENT_PATH")
309+
cd ..
310+
bundle install
311+
INPUT_ACCESS_TOKEN=${{ secrets.GITHUB_TOKEN }} \
312+
GITHUB_REPOSITORY="${GITHUB_REPOSITORY}" \
313+
UNCOVERED_LINE_FILE="generate_code_coverage_report/uncovered_file_lines.json" \
314+
TESTING_COMMIT="${GITHUB_SHA}" \
315+
PULL_REQUEST="${pull_number}" \
316+
bundle exec ruby post_incremental_coverage_in_pr.rb
317+
fi
290318
- name: Update New Coverage Data
291319
if: github.event.pull_request.merged && env.METRICS_SERVICE_SECRET
292320
run: |

scripts/code_coverage_report/Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
source "https://rubygems.org"
2+
3+
gem "octokit", "~> 4.19"

scripts/code_coverage_report/code_coverage_file_list.json

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@
44
"filePatterns": [
55
"FirebaseABTesting.*",
66
"Interop/Analytics/Public/[^/]+\\.h",
7-
"\\.github/workflows/abtesting\\.yml",
8-
"Gemfile"
7+
"\\.github/workflows/abtesting\\.yml"
98
]
109
},
1110
{
1211
"sdk": "auth",
1312
"filePatterns": [
1413
"FirebaseAuth.*",
1514
"Interop/Auth/Public/[^/]+\\.h",
16-
"\\.github/workflows/auth\\.yml",
17-
"Gemfile"
15+
"\\.github/workflows/auth\\.yml"
1816
]
1917
},
2018
{
@@ -23,17 +21,15 @@
2321
"FirebaseDatabase.*",
2422
"\\.github/workflows/database\\.yml",
2523
"Example/Database/",
26-
"Interop/Auth/Public/[^/]+\\.h",
27-
"Gemfile"
24+
"Interop/Auth/Public/[^/]+\\.h"
2825
]
2926
},
3027
{
3128
"sdk": "dynamiclinks",
3229
"filePatterns": [
3330
"FirebaseDynamicLinks.*",
3431
"\\.github/workflows/dynamiclinks\\.yml",
35-
"Interop/Analytics/Public/[^/]+\\.h",
36-
"Gemfile"
32+
"Interop/Analytics/Public/[^/]+\\.h"
3733
]
3834
},
3935
{
@@ -46,8 +42,7 @@
4642
"FirebaseFirestore\\.podspec",
4743
"CMakeLists\\.txt",
4844
"cmake/.*",
49-
"\\.github/workflows/firestore\\.yml",
50-
"Gemfile"
45+
"\\.github/workflows/firestore\\.yml"
5146
]
5247
},
5348
{
@@ -56,17 +51,15 @@
5651
"Functions.*",
5752
"\\.github/workflows/functions\\.yml",
5853
"Interop/Auth/Public/.*\\.h",
59-
"FirebaseMessaging/Sources/Interop/[^/]+\\.h",
60-
"Gemfile"
54+
"FirebaseMessaging/Sources/Interop/[^/]+\\.h"
6155
]
6256
},
6357
{
6458
"sdk": "inappmessaging",
6559
"filePatterns": [
6660
"FirebaseInAppMessaging.*",
6761
"Interop/Analytics/Public/[^/]+\\.h",
68-
"\\.github/workflows/inappmessaging\\.yml",
69-
"Gemfile"
62+
"\\.github/workflows/inappmessaging\\.yml"
7063
]
7164
},
7265
{
@@ -75,17 +68,15 @@
7568
"FirebaseMessaging/.*",
7669
"Interop/Analytics/Public/[^/]+\\.h",
7770
"FirebaseMessaging\\.podspec",
78-
"\\.github/workflows/messaging\\.yml",
79-
"Gemfile"
71+
"\\.github/workflows/messaging\\.yml"
8072
]
8173
},
8274
{
8375
"sdk": "performance",
8476
"filePatterns": [
8577
"FirebasePerformance/.*",
8678
"FirebasePerformance\\.podspec",
87-
"\\.github/workflows/performance\\.yml",
88-
"Gemfile"
79+
"\\.github/workflows/performance\\.yml"
8980
]
9081
},
9182
{
@@ -94,7 +85,6 @@
9485
"FirebaseRemoteConfig.*",
9586
"Interop/Analytics/Public/[^/]+\\.h",
9687
"\\.github/workflows/remoteconfig\\.yml",
97-
"Gemfile",
9888
"scripts/generate_access_token\\.sh"
9989
]
10090
},
@@ -103,8 +93,7 @@
10393
"filePatterns": [
10494
"FirebaseStorage.*",
10595
"Interop/Auth/Public/[^/]+\\.h",
106-
"\\.github/workflows/storage\\.yml",
107-
"Gemfile"
96+
"\\.github/workflows/storage\\.yml"
10897
]
10998
}
11099
]

scripts/code_coverage_report/generate_code_coverage_report/Sources/IncrementalCoverageReportGenerator/main.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,16 @@ struct IncrementalCoverageReportGenerator: ParsableCommand {
142142
coverage: [],
143143
xcresultBundle: coverageFile.xcresultBundle
144144
)
145+
print("The following lines shown below, if any, are found not tested in \(xcresultFile)")
145146
for addedLineIndex in change.addedLines {
146147
// `xccov` report will not involve unexecutable lines which are at
147148
// the end of the file. That means if the last couple lines are
148149
// comments, these lines will not be in the `coverageFile.coverage`.
149-
if addedLineIndex < coverageFile.coverage.count,
150-
let testCoverRun = coverageFile.coverage[addedLineIndex], testCoverRun == 0 {
150+
// Indices in an array, starting from 0, correspond to lineIndex,
151+
// starting from 0, minus 1.
152+
if addedLineIndex <= coverageFile.coverage.count,
153+
let testCoverRun = coverageFile.coverage[addedLineIndex - 1], testCoverRun == 0 {
154+
print(addedLineIndex)
151155
uncoveredLine.coverage.append(addedLineIndex)
152156
}
153157
}

scripts/code_coverage_report/git_diff_to_json.sh

100644100755
File mode changed.

0 commit comments

Comments
 (0)