findbugs-up.rb 4.5 KB

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