lint-up.rb 6.4 KB

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