lint-up.rb 5.9 KB

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