Skip to content

Commit 496432f

Browse files
authored
Update changed file tool (#7463)
1 parent 8d92434 commit 496432f

File tree

5 files changed

+115
-42
lines changed

5 files changed

+115
-42
lines changed

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ DerivedData
6969
ReleaseTooling/Packages
7070
ReleaseTooling/*.xcodeproj
7171
ReleaseTooling/Package.resolved
72-
scripts/code_coverage_report/generate_code_coverage_report/Package.resolved
73-
scripts/code_coverage_report/generate_code_coverage_report/.build
72+
scripts/code_coverage_report/*/Package.resolved
73+
scripts/code_coverage_report/*/.build
7474

7575
# Mint package manager
7676
Mint
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
[
2+
{
3+
"sdk": "database",
4+
"filePatterns": [
5+
"FirebaseDatabase.*",
6+
"\\.github/workflows/database\\.yml",
7+
"Example/Database/",
8+
"Interop/Auth/Public/.*\\.h"
9+
]
10+
},
11+
{
12+
"sdk": "functions",
13+
"filePatterns": [
14+
"Functions.*",
15+
"\\.github/workflows/functions\\.yml",
16+
"Interop/Auth/Public/.*\\.h",
17+
"FirebaseMessaging/Sources/Interop/.*\\.h"
18+
]
19+
}
20+
]

scripts/code_coverage_report/generate_code_coverage_report/Package.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,17 @@
1919
import PackageDescription
2020

2121
let package = Package(
22-
name: "CoverageReportGenerator",
22+
name: "CodeCoverage",
2323
products: [
2424
// Products define the executables and libraries a package produces, and make them visible to other packages.
2525
.executable(
2626
name: "CoverageReportGenerator",
2727
targets: ["CoverageReportGenerator"]
2828
),
29+
.executable(
30+
name: "UpdatedFilesCollector",
31+
targets: ["UpdatedFilesCollector"]
32+
),
2933
],
3034
dependencies: [
3135
// Dependencies declare other packages that this package depends on.
@@ -41,5 +45,11 @@ let package = Package(
4145
.product(name: "ArgumentParser", package: "swift-argument-parser"),
4246
]
4347
),
48+
.target(
49+
name: "UpdatedFilesCollector",
50+
dependencies: [
51+
.product(name: "ArgumentParser", package: "swift-argument-parser"),
52+
]
53+
),
4454
]
4555
)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2021 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import ArgumentParser
18+
import Foundation
19+
20+
struct SDKFilePattern: Codable {
21+
let sdk: String
22+
let filePatterns: [String]
23+
}
24+
25+
struct UpdatedFilesCollector: ParsableCommand {
26+
@Option(help: "A txt File with updated files.",
27+
transform: { str in
28+
let url = URL(fileURLWithPath: str)
29+
let data = try String(contentsOf: url)
30+
return data.components(separatedBy: .newlines)
31+
})
32+
var changedFilePaths: [String]
33+
34+
@Option(help: "A JSON file path conforming to the struct SDKFilePattern",
35+
transform: { str in
36+
let url = URL(fileURLWithPath: str)
37+
let jsonData = try Data(contentsOf: url)
38+
return try JSONDecoder().decode([SDKFilePattern].self, from: jsonData)
39+
})
40+
var codeCoverageFilePatterns: [SDKFilePattern]
41+
42+
func run() throws {
43+
print("=============== list changed files ===============")
44+
print(changedFilePaths.joined(separator: "\n"))
45+
// Initiate all run_job flag to false.
46+
for sdkPatterns in codeCoverageFilePatterns {
47+
print("::set-output name=\(sdkPatterns.sdk)_run_job::false")
48+
}
49+
// Go through patterns of each sdk. Once there is a path of changed file matching
50+
// any pattern of this sdk, the run_job flag of this sdk will be turned to true.
51+
for sdkPatterns in codeCoverageFilePatterns {
52+
var trigger_pod_test_for_coverage_report = false
53+
for pattern in sdkPatterns.filePatterns {
54+
let regex = try! NSRegularExpression(pattern: pattern)
55+
// If one changed file path match one path of this sdk, the run_job flag of
56+
// the sdk will be turned on.
57+
for changedFilePath in changedFilePaths {
58+
let range = NSRange(location: 0, length: changedFilePath.utf16.count)
59+
if regex.firstMatch(in: changedFilePath, options: [], range: range) != nil {
60+
print("=============== paths of changed files ===============")
61+
print("::set-output name=\(sdkPatterns.sdk)_run_job::true")
62+
print("\(sdkPatterns.sdk): \(changedFilePath) is updated under the pattern, \(pattern)")
63+
trigger_pod_test_for_coverage_report = true
64+
// Once this sdk run_job flag is turned to true, then the loop
65+
// will skip to the next sdk.
66+
break
67+
}
68+
if trigger_pod_test_for_coverage_report { break }
69+
}
70+
if trigger_pod_test_for_coverage_report { break }
71+
}
72+
}
73+
}
74+
}
75+
76+
UpdatedFilesCollector.main()

scripts/code_coverage_report/get_updated_files.sh

Lines changed: 6 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,53 +12,20 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# Updated files under the following paths will trigger code coverage workflows.
16-
# Updates in a pull request will generate a code coverage report in a pr.
17-
DATABASE_PATHS=("FirebaseDatabase.*" \
18-
".github/workflows/database\\.yml" \
19-
"Example/Database/" \
20-
"Interop/Auth/Public/\.\*.h")
21-
FUNCTIONS_PATHS=("Functions.*" \
22-
".github/workflows/functions\\.yml" \
23-
"Interop/Auth/Public/\.\*.h" \
24-
"FirebaseMessaging/Sources/Interop/\.\*.h")
25-
26-
# Set default flag variables which will determine if an sdk workflow will be triggererd.
27-
echo "::set-output name=database_run_job::false"
28-
echo "::set-output name=functions_run_job::false"
15+
set -ex
16+
# Updated files in paths in code_coverage_file_list.json will trigger code coverage workflows.
17+
# Updates in a pull request will generate a code coverage report in a PR.
2918

3019
# Get most rescent ancestor commit.
3120
common_commit=$(git merge-base remotes/origin/${pr_branch} remotes/origin/master)
3221
echo "The common commit is ${common_commit}."
3322
# Set base commit and this will be used to compare diffs of coverage to the current commit.
3423
echo "::set-output name=base_commit::${common_commit}"
3524

25+
cd scripts/code_coverage_report/generate_code_coverage_report
26+
3627
# List changed file from the base commit. This is generated by comparing the
3728
# head of the branch and the common commit from the master branch.
38-
echo "=============== list changed files ==============="
39-
cat < <(git diff --name-only $common_commit remotes/origin/${pr_branch})
40-
41-
echo "============= paths of changed files ============="
4229
git diff --name-only $common_commit remotes/origin/${pr_branch} > updated_files.txt
4330

44-
# Loop over updated files if their path match patterns then flags to trigger
45-
# SDK pod test workflow will be set to true.
46-
while IFS= read -r file
47-
do
48-
for path in "${DATABASE_PATHS[@]}"
49-
do
50-
if [[ "${file}" =~ $path ]]; then
51-
echo "This file is updated under the path, ${path}"
52-
echo "::set-output name=database_run_job::true"
53-
break
54-
fi
55-
done
56-
for path in "${FUNCTIONS_PATHS[@]}"
57-
do
58-
if [[ "${file}" =~ $path ]]; then
59-
echo "This file is updated under the path, ${path}"
60-
echo "::set-output name=functions_run_job::true"
61-
break
62-
fi
63-
done
64-
done < updated_files.txt
31+
swift run UpdatedFilesCollector --changed-file-paths updated_files.txt --code-coverage-file-patterns ../code_coverage_file_list.json

0 commit comments

Comments
 (0)