findbugs-up.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 FindBugs 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 FindBugs report. Must match build.gradle file:
  12. # lintOptions {
  13. # htmlOutput file("[FILE_NAME].html")
  14. # }
  15. FINDBUGS_REPORT_FILE = String.new("build/reports/spotbugs/spotbugs.html")
  16. # File name and relative path of previous results of this script.
  17. PREVIOUS_FINDBUGS_RESULTS_FILE=String.new("scripts/analysis/findbugs-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 FindBugs rules; Can be null or "".
  21. CUSTOM_FINDBUGS_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. # run FindBugs
  35. puts "running FindBugs..."
  36. system './gradlew assembleGplayDebug'
  37. system './gradlew spotbugsGplayDebug'
  38. # find FindBugs report file
  39. findbugs_reports = Dir.glob(FINDBUGS_REPORT_FILE)
  40. if findbugs_reports.length == 0
  41. puts "Findbugs HTML report not found."
  42. exit 1
  43. end
  44. findbugs_report = String.new(findbugs_reports[0])
  45. # find number of warnings
  46. current_warning_count = `grep -A 3 "<b>Total</b>" build/reports/spotbugs/spotbugs.html | tail -n1 | cut -f2 -d">" | cut -f1 -d"<"`.to_i
  47. puts "found warnings: " + current_warning_count.to_s
  48. # get warning counts from last successful build
  49. previous_results = false
  50. previous_findbugs_reports = Dir.glob(PREVIOUS_FINDBUGS_RESULTS_FILE)
  51. if previous_findbugs_reports.nil? || previous_findbugs_reports.length == 0
  52. previous_findbugs_report = File.new(PREVIOUS_FINDBUGS_RESULTS_FILE, "w") # create for writing to later
  53. else
  54. previous_findbugs_report = String.new(previous_findbugs_reports[0])
  55. previous_warning_count = File.open(previous_findbugs_report, &:readline).match(/[0-9]*/)[0].to_i
  56. if previous_warning_count.nil?
  57. previous_results = false
  58. else
  59. previous_results = true
  60. puts "previous warnings: " + previous_warning_count.to_s
  61. end
  62. end
  63. # compare previous warning count with current warning count
  64. if previous_results == true && current_warning_count > previous_warning_count
  65. puts "FAIL: warning count increased"
  66. exit 1
  67. end
  68. # check if warning and error count stayed the same
  69. if previous_results == true && current_warning_count == previous_warning_count
  70. puts "SUCCESS: count stayed the same"
  71. exit 2
  72. end
  73. # warning count DECREASED
  74. puts "SUCCESS: count decreased from " + previous_warning_count.to_s + " to " + current_warning_count.to_s
  75. # write new results to file (will overwrite existing, or create new)
  76. File.write(previous_findbugs_report, current_warning_count)
  77. # push changes to github (if this script is run locally, we don't want to overwrite git username and email, so save temporarily)
  78. previous_git_username, _ = Open3.capture2('git config user.name')
  79. previous_git_username = previous_git_username.strip
  80. previous_git_email, _ = Open3.capture3('git config user.email')
  81. previous_git_email = previous_git_email.strip
  82. # update git user name and email for this script
  83. system ("git config --local user.name '" + git_user + "'")
  84. system ("git config --local user.email 'android@nextcloud.com'")
  85. system ("git remote rm origin")
  86. system ("git remote add origin https://" + git_user + ":" + git_token + "@github.com/nextcloud/android")
  87. # add previous FindBugs result file to git
  88. system ('git add ' + PREVIOUS_FINDBUGS_RESULTS_FILE)
  89. # commit changes; Add "skip ci" so that we don't accidentally trigger another Drone build
  90. system ('git commit -sm "Drone: update FindBugs results to reflect reduced error/warning count [skip ci]" ')
  91. # push to origin
  92. system ('git push origin HEAD:' + git_branch)
  93. # restore previous git user name and email
  94. system("git config --local user.name '#{previous_git_username}'")
  95. system("git config --local user.email '#{previous_git_email}'")
  96. puts "SUCCESS: count was reduced"
  97. exit 0 # success