findbugs-up.rb 4.7 KB

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