lint-up.rb 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. ## Script from https://github.com/tir38/android-lint-entropy-reducer at 07.05.2017
  2. # adapts to drone, use git username / token as parameter
  3. # TODO cleanup this script, it has a lot of unused stuff
  4. # SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  5. # SPDX-FileCopyrightText: 2017 Jason Atwood
  6. # SPDX-FileCopyrightText: 2017 Tobias Kaminsky <tobias@kaminsky.me>
  7. # SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only
  8. Encoding.default_external = Encoding::UTF_8
  9. Encoding.default_internal = Encoding::UTF_8
  10. puts "=================== starting Android Lint Entropy Reducer ===================="
  11. # ======================== SETUP ============================
  12. # User name for git commits made by this script.
  13. TRAVIS_GIT_USERNAME = String.new("Drone CI server")
  14. # File name and relative path of generated Lint report. Must match build.gradle file:
  15. # lintOptions {
  16. # htmlOutput file("[FILE_NAME].html")
  17. # }
  18. LINT_REPORT_FILE = String.new("app/build/reports/lint/lint.html")
  19. # File name and relative path of previous results of this script.
  20. PREVIOUS_LINT_RESULTS_FILE=String.new("scripts/analysis/lint-results.txt")
  21. # Flag to evaluate warnings. true = check warnings; false = ignore warnings
  22. CHECK_WARNINGS = true
  23. # File name and relative path to custom lint rules; Can be null or "".
  24. CUSTOM_LINT_FILE = String.new("")
  25. # ================ SETUP DONE; DON'T TOUCH ANYTHING BELOW ================
  26. require 'fileutils'
  27. require 'pathname'
  28. require 'open3'
  29. # since we need the xml-simple gem, and we want this script self-contained, let's grab it just when we need it
  30. begin
  31. gem "xml-simple"
  32. rescue LoadError
  33. system("gem install --user-install xml-simple")
  34. Gem.clear_paths
  35. end
  36. require 'xmlsimple'
  37. # add custom Lint jar
  38. if !CUSTOM_LINT_FILE.nil? &&
  39. CUSTOM_LINT_FILE.length > 0
  40. ENV["ANDROID_LINT_JARS"] = Dir.pwd + "/" + CUSTOM_LINT_FILE
  41. puts "adding custom lint rules to default set: "
  42. puts ENV["ANDROID_LINT_JARS"]
  43. end
  44. # run Lint
  45. puts "running Lint..."
  46. system './gradlew clean lintGplayDebug 1>/dev/null'
  47. # confirm that Lint ran w/out error
  48. result = $?.to_i
  49. if result != 0
  50. puts "FAIL: failed to run ./gradlew clean lintGplayDebug"
  51. exit 1
  52. end
  53. # find Lint report file
  54. lint_reports = Dir.glob(LINT_REPORT_FILE)
  55. if lint_reports.length == 0
  56. puts "Lint HTML report not found."
  57. exit 1
  58. end
  59. lint_report = String.new(lint_reports[0])
  60. # find error/warning count string in HTML report
  61. error_warning_string = ""
  62. File.open lint_report do |file|
  63. error_warning_string = file.find { |line| line =~ /([0-9]* error[s]? and )?[0-9]* warning[s]?/ }
  64. end
  65. # find number of errors
  66. error_string = error_warning_string.match(/[0-9]* error[s]?/)
  67. if (error_string.nil?)
  68. current_error_count = 0
  69. else
  70. current_error_count = error_string[0].match(/[0-9]*/)[0].to_i
  71. end
  72. puts "found errors: " + current_error_count.to_s
  73. # find number of warnings
  74. if CHECK_WARNINGS == true
  75. warning_string = error_warning_string.match(/[0-9]* warning[s]?/)[0]
  76. current_warning_count = warning_string.match(/[0-9]*/)[0].to_i
  77. puts "found warnings: " + current_warning_count.to_s
  78. end
  79. # get previous error and warning counts from last successful build
  80. previous_results = false
  81. previous_lint_reports = Dir.glob(PREVIOUS_LINT_RESULTS_FILE)
  82. if previous_lint_reports.nil? ||
  83. previous_lint_reports.length == 0
  84. previous_lint_report = File.new(PREVIOUS_LINT_RESULTS_FILE, "w") # create for writing to later
  85. else
  86. previous_lint_report = String.new(previous_lint_reports[0])
  87. previous_error_warning_string = ""
  88. File.open previous_lint_report do |file|
  89. previous_error_warning_string = file.find { |line| line =~ /([0-9]* error[s]? and )?[0-9]* warning[s]?/ }
  90. end
  91. unless previous_error_warning_string.nil?
  92. previous_results = true
  93. previous_error_string = previous_error_warning_string.match(/[0-9]* error[s]?/)
  94. if previous_error_string.nil?
  95. previous_error_string = "0 errors"
  96. else
  97. previous_error_string = previous_error_string[0]
  98. end
  99. previous_error_count = previous_error_string.match(/[0-9]*/)[0].to_i
  100. puts "previous errors: " + previous_error_count.to_s
  101. if CHECK_WARNINGS == true
  102. previous_warning_string = previous_error_warning_string.match(/[0-9]* warning[s]?/)
  103. if previous_warning_string.nil?
  104. previous_warning_string = "0 warnings"
  105. else
  106. previous_warning_string = previous_warning_string[0]
  107. end
  108. previous_warning_count = previous_warning_string.match(/[0-9]*/)[0].to_i
  109. puts "previous warnings: " + previous_warning_count.to_s
  110. end
  111. end
  112. end
  113. # compare previous error count with current error count
  114. if previous_results == true &&
  115. current_error_count > previous_error_count
  116. puts "FAIL: error count increased"
  117. exit 1
  118. end
  119. # compare previous warning count with current warning count
  120. if CHECK_WARNINGS == true &&
  121. previous_results == true &&
  122. current_warning_count > previous_warning_count
  123. puts "FAIL: warning count increased"
  124. exit 1
  125. end
  126. # check if warning and error count stayed the same
  127. if previous_results == true &&
  128. current_error_count == previous_error_count &&
  129. current_warning_count == previous_warning_count
  130. puts "SUCCESS: count stayed the same"
  131. exit 2
  132. end
  133. # either error count or warning count DECREASED
  134. # write new results to file (will overwrite existing, or create new)
  135. File.write(previous_lint_report, "DO NOT TOUCH; GENERATED BY DRONE\n" + error_warning_string)
  136. # update git user name and email for this script
  137. system ("git config --local user.name 'github-actions'")
  138. system ("git config --local user.email 'github-actions@github.com'")
  139. # add previous Lint result file to git
  140. system ('git add ' + PREVIOUS_LINT_RESULTS_FILE)
  141. # commit changes
  142. system('git commit -sm "Analysis: update lint results to reflect reduced error/warning count"')
  143. # push to origin
  144. system ('git push')
  145. puts "SUCCESS: count was reduced"
  146. exit 0 # success