lint-up.rb 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/lint/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'
  44. # confirm that Lint ran w/out error
  45. result = 0
  46. #result = $?.to_i
  47. if result != 0
  48. puts "FAIL: failed to run ./gradlew clean lint"
  49. exit 1
  50. end
  51. # find Lint report file
  52. lint_reports = Dir.glob(LINT_REPORT_FILE)
  53. if lint_reports.length == 0
  54. puts "Lint HTML report not found."
  55. exit 1
  56. end
  57. lint_report = String.new(lint_reports[0])
  58. # find error/warning count string in HTML report
  59. error_warning_string = ""
  60. File.open lint_report do |file|
  61. error_warning_string = file.find { |line| line =~ /([0-9]* error[s]? and )?[0-9]* warning[s]?/ }
  62. end
  63. # find number of errors
  64. error_string = error_warning_string.match(/[0-9]* error[s]?/)
  65. if (error_string.nil?)
  66. current_error_count = 0
  67. else
  68. current_error_count = error_string[0].match(/[0-9]*/)[0].to_i
  69. end
  70. puts "found errors: " + current_error_count.to_s
  71. # find number of warnings
  72. if CHECK_WARNINGS == true
  73. warning_string = error_warning_string.match(/[0-9]* warning[s]?/)[0]
  74. current_warning_count = warning_string.match(/[0-9]*/)[0].to_i
  75. puts "found warnings: " + current_warning_count.to_s
  76. end
  77. # get previous error and warning counts from last successful build
  78. previous_results = false
  79. previous_lint_reports = Dir.glob(PREVIOUS_LINT_RESULTS_FILE)
  80. if previous_lint_reports.nil? ||
  81. previous_lint_reports.length == 0
  82. previous_lint_report = File.new(PREVIOUS_LINT_RESULTS_FILE, "w") # create for writing to later
  83. else
  84. previous_lint_report = String.new(previous_lint_reports[0])
  85. previous_error_warning_string = ""
  86. File.open previous_lint_report do |file|
  87. previous_error_warning_string = file.find { |line| line =~ /([0-9]* error[s]? and )?[0-9]* warning[s]?/ }
  88. end
  89. unless previous_error_warning_string.nil?
  90. previous_results = true
  91. previous_error_string = previous_error_warning_string.match(/[0-9]* error[s]?/)
  92. if previous_error_string.nil?
  93. previous_error_string = "0 errors"
  94. else
  95. previous_error_string = previous_error_string[0]
  96. end
  97. previous_error_count = previous_error_string.match(/[0-9]*/)[0].to_i
  98. puts "previous errors: " + previous_error_count.to_s
  99. if CHECK_WARNINGS == true
  100. previous_warning_string = previous_error_warning_string.match(/[0-9]* warning[s]?/)
  101. if previous_warning_string.nil?
  102. previous_warning_string = "0 warnings"
  103. else
  104. previous_warning_string = previous_warning_string[0]
  105. end
  106. previous_warning_count = previous_warning_string.match(/[0-9]*/)[0].to_i
  107. puts "previous warnings: " + previous_warning_count.to_s
  108. end
  109. end
  110. end
  111. # compare previous error count with current error count
  112. if previous_results == true &&
  113. current_error_count > previous_error_count
  114. puts "FAIL: error count increased"
  115. exit 1
  116. end
  117. # compare previous warning count with current warning count
  118. if CHECK_WARNINGS == true &&
  119. previous_results == true &&
  120. current_warning_count > previous_warning_count
  121. puts "FAIL: warning count increased"
  122. exit 1
  123. end
  124. # check if warning and error count stayed the same
  125. if previous_results == true &&
  126. current_error_count == previous_error_count &&
  127. current_warning_count == previous_warning_count
  128. puts "SUCCESS: count stayed the same"
  129. exit 2
  130. end
  131. # either error count or warning count DECREASED
  132. # write new results to file (will overwrite existing, or create new)
  133. File.write(previous_lint_report, "DO NOT TOUCH; GENERATED BY DRONE\n" + error_warning_string)
  134. # push changes to github (if this script is run locally, we don't want to overwrite git username and email, so save temporarily)
  135. previous_git_username, _ = Open3.capture2('git config user.name')
  136. previous_git_username = previous_git_username.strip
  137. previous_git_email, _ = Open3.capture3('git config user.email')
  138. previous_git_email = previous_git_email.strip
  139. # update git user name and email for this script
  140. system ("git config --local user.name '" + git_user + "'")
  141. system ("git config --local user.email '.'") # set email blank
  142. system ("git remote rm origin")
  143. system ("git remote add origin https://" + git_user + ":" + git_token + "@github.com/nextcloud/android")
  144. # add previous Lint result file to git
  145. system ('git add ' + PREVIOUS_LINT_RESULTS_FILE)
  146. # commit changes; Add "skip ci" so that we don't accidentally trigger another Drone build
  147. system ('git commit -m "Drone: update Lint results to reflect reduced error/warning count [skip ci]" ')
  148. # push to origin
  149. system ('git push origin HEAD:' + git_branch)
  150. # restore previous git user name and email
  151. system("git config --local user.name '#{previous_git_username}'")
  152. system("git config --local user.email '#{previous_git_email}'")
  153. puts "SUCCESS: count was reduced"
  154. exit 0 # success