main.swift 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /*
  2. * Copyright 2019 Google
  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. import CommandLineKit
  17. import Foundation
  18. import MetricsLib
  19. var flags = Flags()
  20. let coveragePath = flags.string("c",
  21. "coverage",
  22. description: "Required - The path of the JSON coverage report generated by XCov.")
  23. let pullRequest = flags.int("p",
  24. "pull_request",
  25. description: "Required - The number of the pull request that corresponds to this coverage run.")
  26. /// Returns the current UTC time in a string format.
  27. func currentTime() -> String {
  28. let formatter = DateFormatter()
  29. formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
  30. formatter.timeZone = TimeZone(identifier: "UTC")
  31. return formatter.string(from: Date())
  32. }
  33. do {
  34. try flags.parse()
  35. if !coveragePath.wasSet {
  36. print("Please specify the path of the JSON coverage report from XCov. -c or --coverage")
  37. exit(1)
  38. }
  39. if !pullRequest.wasSet {
  40. print("Please specify the corresponding pull request number. -p or --pull_request")
  41. exit(1)
  42. }
  43. let coverageReport = try CoverageReport.load(path: coveragePath.value!)
  44. let coverageTable = TableUpdate.createFrom(coverage: coverageReport,
  45. pullRequest: pullRequest.value!,
  46. currentTime: currentTime())
  47. let metrics = UploadMetrics(tables: [coverageTable])
  48. try Uploader.upload(metrics: metrics)
  49. } catch {
  50. print("Error occurred: \(error)")
  51. exit(1)
  52. }