lint-up.rb 5.8 KB

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