|
|
@@ -18,43 +18,9 @@ import ArgumentParser
|
|
|
import Foundation
|
|
|
import Utils
|
|
|
|
|
|
-private enum Constants {}
|
|
|
-
|
|
|
-extension Constants {
|
|
|
- // Binary Size Metrics flag for the Metrics Service.
|
|
|
- static let metric = "BinarySize"
|
|
|
- static let cocoapodSizeReportFile = "binary_report.json"
|
|
|
-}
|
|
|
-
|
|
|
-/// Pod Config
|
|
|
-struct PodConfigs: Codable {
|
|
|
- let pods: [Pod]
|
|
|
-}
|
|
|
-
|
|
|
-struct Pod: Codable {
|
|
|
- let sdk: String
|
|
|
- let path: String
|
|
|
-}
|
|
|
-
|
|
|
-/// Cocoapods-size tool report,
|
|
|
-struct SDKBinaryReport: Codable {
|
|
|
- let combinedPodsExtraSize: Int
|
|
|
-
|
|
|
- enum CodingKeys: String, CodingKey {
|
|
|
- case combinedPodsExtraSize = "combined_pods_extra_size"
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-/// Metrics Service API request data
|
|
|
-struct BinaryMetricsReport: Codable {
|
|
|
- let metric: String
|
|
|
- let results: [Result]
|
|
|
- let log: String
|
|
|
-}
|
|
|
-
|
|
|
-struct Result: Codable {
|
|
|
- let sdk, type: String
|
|
|
- let value: Int
|
|
|
+enum RequestType: EnumerableFlag {
|
|
|
+ case presubmit
|
|
|
+ case merge
|
|
|
}
|
|
|
|
|
|
struct BinarySizeReportGenerator: ParsableCommand {
|
|
|
@@ -70,60 +36,62 @@ struct BinarySizeReportGenerator: ParsableCommand {
|
|
|
@Option(parsing: .upToNextOption, help: "SDKs to be measured.")
|
|
|
var SDK: [String]
|
|
|
|
|
|
- @Option(help: "SDKs to be measured.")
|
|
|
- var logPath: String
|
|
|
+ @Option(help: "Link to the log, leave \"\" if none.")
|
|
|
+ var logLink: String
|
|
|
|
|
|
- func CreatePodConfigJSON(of sdks: [String], from sdk_dir: URL) throws {
|
|
|
- var pods: [Pod] = []
|
|
|
- for sdk in sdks {
|
|
|
- let pod: Pod = Pod(sdk: sdk, path: sdk_dir.path)
|
|
|
- pods.append(pod)
|
|
|
- }
|
|
|
- let podConfigs: PodConfigs = PodConfigs(pods: pods)
|
|
|
- try JSONParser.writeJSON(of: podConfigs, to: "./cocoapods_source_config.json")
|
|
|
- }
|
|
|
+ // Send to the Metrics Service
|
|
|
+ @Flag(help: "Determine if the request to Metrics Service is for pull_requests or merge.")
|
|
|
+ var requestType: RequestType
|
|
|
|
|
|
- // Create a JSON format data prepared to send to the Metrics Service.
|
|
|
- func CreateMetricsRequestData(of sdks: [String], type: String, log: String) throws -> Data {
|
|
|
- var reports: [Result] = []
|
|
|
- // Create a report for each individual SDK and collect all of them into reports.
|
|
|
- for sdk in sdks {
|
|
|
- // Create a report, generated by cocoapods-size, for each SDK. `.stdout` is used
|
|
|
- // since `pipe` could cause the tool to hang. That is probably caused by cocopod-size
|
|
|
- // is using pipe and a pipe is shared by multiple parent/child process and cause
|
|
|
- // deadlock. `.stdout` is a quick way to resolve at the moment. The difference is
|
|
|
- // that `.stdout` will print out logs in the console while pipe can assign logs a
|
|
|
- // variable.
|
|
|
- Shell.run(
|
|
|
- "cd cocoapods-size && python3 measure_cocoapod_size.py --cocoapods \(sdk) --cocoapods_source_config ../cocoapods_source_config.json --json \(Constants.cocoapodSizeReportFile)",
|
|
|
- stdout: .stdout
|
|
|
- )
|
|
|
- let SDKBinarySize = try JSONParser.readJSON(
|
|
|
- of: SDKBinaryReport.self,
|
|
|
- from: "cocoapods-size/\(Constants.cocoapodSizeReportFile)"
|
|
|
- )
|
|
|
- // Append reports for data for API request to the Metrics Service.
|
|
|
- reports.append(Result(sdk: sdk, type: type, value: SDKBinarySize.combinedPodsExtraSize))
|
|
|
- }
|
|
|
- let metricsRequestReport = BinaryMetricsReport(
|
|
|
- metric: Constants.metric,
|
|
|
- results: reports,
|
|
|
- log: log
|
|
|
- )
|
|
|
- let data = try JSONEncoder().encode(metricsRequestReport)
|
|
|
- return data
|
|
|
- }
|
|
|
+ @Argument(help: "A repo coverage data will be related to.")
|
|
|
+ var repo: String
|
|
|
+
|
|
|
+ @Option(
|
|
|
+ help: "presubmit: compare the diff to the base_commit; merge: store coverage data linking to this commit."
|
|
|
+ )
|
|
|
+ var headCommit: String
|
|
|
+
|
|
|
+ @Option(help: "Token to access an account of the Metrics Service")
|
|
|
+ var token: String
|
|
|
+
|
|
|
+ @Option(
|
|
|
+ help: "This is for presubmit request. Number of a pull request that a coverage report will be posted on."
|
|
|
+ )
|
|
|
+ var pullRequestNum: Int?
|
|
|
+
|
|
|
+ @Option(help: "This is for presubmit request. Additional note for the report.")
|
|
|
+ var pullRequestNote: String?
|
|
|
+
|
|
|
+ @Option(
|
|
|
+ help: "This is for presubmit request. Coverage of commit will be compared to the coverage of this base commit."
|
|
|
+ )
|
|
|
+ var baseCommit: String?
|
|
|
+
|
|
|
+ @Option(
|
|
|
+ help: "This is for merge request. Branch here will be linked to the coverage data, with the merged commit, in the database. "
|
|
|
+ )
|
|
|
+ var sourceBranch: String?
|
|
|
|
|
|
func run() throws {
|
|
|
- try CreatePodConfigJSON(of: SDK, from: SDKRepoDir)
|
|
|
- let data = try CreateMetricsRequestData(
|
|
|
- of: SDK,
|
|
|
- type: "firebase-ios-sdk-testing",
|
|
|
- log: logPath
|
|
|
- )
|
|
|
- // TODO: Simply print out the report for debug purpose, and will apply the
|
|
|
- // data to API request sent to the Metrics Service.
|
|
|
- print(String(decoding: data, as: UTF8.self))
|
|
|
+ if let binarySizeRequest = try CreateMetricsRequestData(
|
|
|
+ SDK: SDK,
|
|
|
+ SDKRepoDir: SDKRepoDir,
|
|
|
+ logPath: logLink
|
|
|
+ ) {
|
|
|
+ sendMetricsServiceRequest(
|
|
|
+ repo: repo,
|
|
|
+ commits: headCommit,
|
|
|
+ jsonContent: binarySizeRequest.toData(),
|
|
|
+ token: token,
|
|
|
+ is_presubmit: requestType == RequestType.presubmit,
|
|
|
+ branch: sourceBranch,
|
|
|
+ pullRequest: pullRequestNum,
|
|
|
+ pullRequestNote: pullRequestNote,
|
|
|
+ baseCommit: baseCommit
|
|
|
+ )
|
|
|
+ } else {
|
|
|
+ print("coverageRequest is nil.")
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|