From 22142f191510ee02fb981c95b251a69c52e68be9 Mon Sep 17 00:00:00 2001 From: tobiasKaminsky Date: Fri, 7 Sep 2018 13:11:47 +0200 Subject: Findbugs, lint Signed-off-by: tobiasKaminsky --- scripts/analysis/analysis-wrapper.sh | 80 ++++++++++++++ scripts/analysis/getBranchName.sh | 9 ++ scripts/analysis/lint-results.txt | 2 + scripts/analysis/lint-up.rb | 199 +++++++++++++++++++++++++++++++++++ scripts/lint/getBranchName.sh | 9 -- scripts/lint/lint-results.txt | 2 - scripts/lint/lint-up-wrapper.sh | 36 ------- scripts/lint/lint-up.rb | 199 ----------------------------------- 8 files changed, 290 insertions(+), 246 deletions(-) create mode 100755 scripts/analysis/analysis-wrapper.sh create mode 100755 scripts/analysis/getBranchName.sh create mode 100644 scripts/analysis/lint-results.txt create mode 100644 scripts/analysis/lint-up.rb delete mode 100755 scripts/lint/getBranchName.sh delete mode 100644 scripts/lint/lint-results.txt delete mode 100755 scripts/lint/lint-up-wrapper.sh delete mode 100644 scripts/lint/lint-up.rb (limited to 'scripts') diff --git a/scripts/analysis/analysis-wrapper.sh b/scripts/analysis/analysis-wrapper.sh new file mode 100755 index 000000000..bf2b6b324 --- /dev/null +++ b/scripts/analysis/analysis-wrapper.sh @@ -0,0 +1,80 @@ +#!/bin/sh + +#1: GIT_USERNAME +#2: GIT_TOKEN +#3: BRANCH +#4: LOG_USERNAME +#5: LOG_PASSWORD +#6: DRONE_BUILD_NUMBER +#7: PULL_REQUEST_NUMBER + +echo "1: " $1 +echo "2: " $2 +echo "3: " $3 +echo "4: " $4 +echo "5: " $5 +echo "6: " $6 +echo "7: " $7 + +exit 1 + +ruby scripts/analysis/lint-up.rb $1 $2 $3 +lintValue=$? + + + +./gradlew assemble app:findbugs + +# exit codes: +# 0: count was reduced +# 1: count was increased +# 2: count stayed the same + +echo "Branch: $3" + +if [ $3 = "master" ]; then + echo "New findbugs result for master at: https://www.kaminsky.me/nc-dev/talk-findbugs/master.html" + curl -u $4:$5 -X PUT https://nextcloud.kaminsky.me/remote.php/webdav/talk-findbugs/master.html --upload-file build/reports/findbugs/findbugs.html + + summary=$(sed -n "/

Summary<\/h1>/,/

Warnings<\/h1>/p" build/reports/findbugs/findbugs.html | head -n-1 | sed s'/<\/a>//'g | sed s'///'g | sed s'/Summary/FindBugs (master)/' | tr "\"" "\'" | tr -d "\r\n") + curl -u $4:$5 -X PUT -d "$summary" https://nextcloud.kaminsky.me/remote.php/webdav/talk-findbugs/findbugs.html + + if [ $lintValue -ne 1 ]; then + echo "New lint result for master at: https://www.kaminsky.me/nc-dev/talk-lint/master.html" + curl -u $4:$5 -X PUT https://nextcloud.kaminsky.me/remote.php/webdav/talk-droneLogs/master.html --upload-file build/reports/lint/lint.html + exit 0 + fi +else + if [ -e $6 ]; then + 6="master-"$(date +%F) + fi + echo "New lint results at https://www.kaminsky.me/nc-dev/talk-lint/$6.html" + curl 2>/dev/null -u $4:$5 -X PUT https://nextcloud.kaminsky.me/remote.php/webdav/talk-droneLogs/$6.html --upload-file build/reports/lint/lint.html + + echo "New findbugs results at https://www.kaminsky.me/nc-dev/talk-findbugs/$6.html" + curl 2>/dev/null -u $4:$5 -X PUT https://nextcloud.kaminsky.me/remote.php/webdav/talk-findbugs/$6.html --upload-file build/reports/findbugs/findbugs.html + + # delete all old comments + oldComments=$(curl 2>/dev/null -u $1:$2 -X GET https://api.github.com/repos/nextcloud/talk-android/issues/$7/comments | jq '.[] | (.id |tostring) + "|" + (.user.login | test("nextcloud-android-bot") | tostring) ' | grep true | tr -d "\"" | cut -f1 -d"|") + + echo $oldComments | while read comment ; do + curl 2>/dev/null -u $1:$2 -X DELETE https://api.github.com/repos/nextcloud/talk-android/issues/comments/$comment + done + + # add comment with results + lintResultNew=$(grep "Lint Report.* [0-9]* warnings" build/reports/lint/lint.html | cut -f2 -d':' |cut -f1 -d'<') + lintErrorNew=$(echo $lintResultNew | grep "[0-9]* error" -o | cut -f1 -d" ") + lintWarningNew=$(echo $lintResultNew | grep "[0-9]* warning" -o | cut -f1 -d" ") + lintErrorOld=$(grep "[0-9]* error" scripts/analysis/lint-results.txt -o | cut -f1 -d" ") + lintWarningOld=$(grep "[0-9]* warning" scripts/analysis/lint-results.txt -o | cut -f1 -d" ") + lintResult="

Lint

TypeMasterPR
Warnings"$lintWarningOld""$lintWarningNew"
Errors"$lintErrorOld""$lintErrorNew"
" + findbugsResultNew=$(sed -n "/

Summary<\/h1>/,/

Warnings<\/h1>/p" build/reports/findbugs/findbugs.html |head -n-1 | sed s'/<\/a>//'g | sed s'///'g | sed s"#Summary#FindBugs (new)#" | tr "\"" "\'" | tr -d "\n") + findbugsResultOld=$(curl 2>/dev/null https://nextcloud.kaminsky.me/index.php/s/YCD729NgcMAYkJT/download | tr "\"" "\'" | tr -d "\r\n" | sed s'#FindBugs#FindBugs#'| tr "\"" "\'" | tr -d "\n") + curl -u $1:$2 -X POST https://api.github.com/repos/nextcloud/talk-android/issues/$7/comments -d "{ \"body\" : \"$lintResult $findbugsResultNew $findbugsResultOld \" }" + + if [ $lintValue -eq 2 ]; then + exit 0 + else + exit $lintValue + fi +fi diff --git a/scripts/analysis/getBranchName.sh b/scripts/analysis/getBranchName.sh new file mode 100755 index 000000000..7b91215c5 --- /dev/null +++ b/scripts/analysis/getBranchName.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +# $1: username, $2: password/token, $3: pull request number + +if [ -z $3 ] ; then + echo "master"; +else + curl 2>/dev/null -u $1:$2 https://api.github.com/repos/nextcloud/talk-android/pulls/$3 | grep \"ref\": | grep -v master | cut -d"\"" -f4 +fi diff --git a/scripts/analysis/lint-results.txt b/scripts/analysis/lint-results.txt new file mode 100644 index 000000000..9a13c695d --- /dev/null +++ b/scripts/analysis/lint-results.txt @@ -0,0 +1,2 @@ +DO NOT TOUCH; GENERATED BY DRONE + Lint Report: 91 warnings diff --git a/scripts/analysis/lint-up.rb b/scripts/analysis/lint-up.rb new file mode 100644 index 000000000..ad8c8b81c --- /dev/null +++ b/scripts/analysis/lint-up.rb @@ -0,0 +1,199 @@ +## Script from https://github.com/tir38/android-lint-entropy-reducer at 07.05.2017 +# adapts to drone, use git username / token as parameter + +Encoding.default_external = Encoding::UTF_8 +Encoding.default_internal = Encoding::UTF_8 + +puts "=================== starting Android Lint Entropy Reducer ====================" + +# get args +git_user, git_token, git_branch = ARGV + +# ======================== SETUP ============================ + +# User name for git commits made by this script. +TRAVIS_GIT_USERNAME = String.new("Drone CI server") + +# File name and relative path of generated Lint report. Must match build.gradle file: +# lintOptions { +# htmlOutput file("[FILE_NAME].html") +# } +LINT_REPORT_FILE = String.new("app/build/reports/lint/lint.html") + +# File name and relative path of previous results of this script. +PREVIOUS_LINT_RESULTS_FILE=String.new("scripts/analysis/lint-results.txt") + +# Flag to evaluate warnings. true = check warnings; false = ignore warnings +CHECK_WARNINGS = true + +# File name and relative path to custom lint rules; Can be null or "". +CUSTOM_LINT_FILE = String.new("") + +# ================ SETUP DONE; DON'T TOUCH ANYTHING BELOW ================ + +require 'fileutils' +require 'pathname' +require 'open3' + +# since we need the xml-simple gem, and we want this script self-contained, let's grab it just when we need it +begin + gem "xml-simple" + rescue LoadError + system("gem install xml-simple") + Gem.clear_paths +end + +require 'xmlsimple' + +# add custom Lint jar +if !CUSTOM_LINT_FILE.nil? && + CUSTOM_LINT_FILE.length > 0 + + ENV["ANDROID_LINT_JARS"] = Dir.pwd + "/" + CUSTOM_LINT_FILE + puts "adding custom lint rules to default set: " + puts ENV["ANDROID_LINT_JARS"] +end + +# run Lint +puts "running Lint..." +system './gradlew clean assemble lint' + +# confirm that Lint ran w/out error +result = $?.to_i +if result != 0 + puts "FAIL: failed to run ./gradlew clean assembleGplay lint" + exit 1 +end + +# find Lint report file +lint_reports = Dir.glob(LINT_REPORT_FILE) +if lint_reports.length == 0 + puts "Lint HTML report not found." + exit 1 +end +lint_report = String.new(lint_reports[0]) + +# find error/warning count string in HTML report +error_warning_string = "" +File.open lint_report do |file| + error_warning_string = file.find { |line| line =~ /([0-9]* error[s]? and )?[0-9]* warning[s]?/ } +end + +# find number of errors +error_string = error_warning_string.match(/[0-9]* error[s]?/) + +if (error_string.nil?) + current_error_count = 0 +else + current_error_count = error_string[0].match(/[0-9]*/)[0].to_i +end + +puts "found errors: " + current_error_count.to_s + +# find number of warnings +if CHECK_WARNINGS == true + warning_string = error_warning_string.match(/[0-9]* warning[s]?/)[0] + current_warning_count = warning_string.match(/[0-9]*/)[0].to_i + puts "found warnings: " + current_warning_count.to_s +end + +# get previous error and warning counts from last successful build + +previous_results = false + +previous_lint_reports = Dir.glob(PREVIOUS_LINT_RESULTS_FILE) +if previous_lint_reports.nil? || + previous_lint_reports.length == 0 + + previous_lint_report = File.new(PREVIOUS_LINT_RESULTS_FILE, "w") # create for writing to later +else + previous_lint_report = String.new(previous_lint_reports[0]) + + previous_error_warning_string = "" + File.open previous_lint_report do |file| + previous_error_warning_string = file.find { |line| line =~ /([0-9]* error[s]? and )?[0-9]* warning[s]?/ } + end + + unless previous_error_warning_string.nil? + previous_results = true + + previous_error_string = previous_error_warning_string.match(/[0-9]* error[s]?/) + if previous_error_string.nil? + previous_error_string = "0 errors" + else + previous_error_string = previous_error_string[0] + end + previous_error_count = previous_error_string.match(/[0-9]*/)[0].to_i + puts "previous errors: " + previous_error_count.to_s + + if CHECK_WARNINGS == true + previous_warning_string = previous_error_warning_string.match(/[0-9]* warning[s]?/) + if previous_warning_string.nil? + previous_warning_string = "0 warnings" + else + previous_warning_string = previous_warning_string[0] + end + previous_warning_count = previous_warning_string.match(/[0-9]*/)[0].to_i + puts "previous warnings: " + previous_warning_count.to_s + end + end +end + +# compare previous error count with current error count +if previous_results == true && + current_error_count > previous_error_count + puts "FAIL: error count increased" + exit 1 +end + +# compare previous warning count with current warning count +if CHECK_WARNINGS == true && + previous_results == true && + current_warning_count > previous_warning_count + + puts "FAIL: warning count increased" + exit 1 +end + +# check if warning and error count stayed the same +if previous_results == true && + current_error_count == previous_error_count && + current_warning_count == previous_warning_count + + puts "SUCCESS: count stayed the same" + exit 2 +end + +# either error count or warning count DECREASED + +# write new results to file (will overwrite existing, or create new) +File.write(previous_lint_report, "DO NOT TOUCH; GENERATED BY DRONE\n" + error_warning_string) + +# push changes to github (if this script is run locally, we don't want to overwrite git username and email, so save temporarily) +previous_git_username, _ = Open3.capture2('git config user.name') +previous_git_username = previous_git_username.strip + +previous_git_email, _ = Open3.capture3('git config user.email') +previous_git_email = previous_git_email.strip + +# update git user name and email for this script +system ("git config --local user.name '" + git_user + "'") +system ("git config --local user.email '.'") # set email blank +system ("git remote rm origin") +system ("git remote add origin https://" + git_user + ":" + git_token + "@github.com/nextcloud/talk-android") + +# add previous Lint result file to git +system ('git add ' + PREVIOUS_LINT_RESULTS_FILE) + +# commit changes; Add "skip ci" so that we don't accidentally trigger another Drone build +system ('git commit -m "Drone: update Lint results to reflect reduced error/warning count [skip ci]" ') + +# push to origin +system ('git push origin HEAD:' + git_branch) + +# restore previous git user name and email +system("git config --local user.name '#{previous_git_username}'") +system("git config --local user.email '#{previous_git_email}'") + +puts "SUCCESS: count was reduced" +exit 0 # success diff --git a/scripts/lint/getBranchName.sh b/scripts/lint/getBranchName.sh deleted file mode 100755 index 7b91215c5..000000000 --- a/scripts/lint/getBranchName.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash - -# $1: username, $2: password/token, $3: pull request number - -if [ -z $3 ] ; then - echo "master"; -else - curl 2>/dev/null -u $1:$2 https://api.github.com/repos/nextcloud/talk-android/pulls/$3 | grep \"ref\": | grep -v master | cut -d"\"" -f4 -fi diff --git a/scripts/lint/lint-results.txt b/scripts/lint/lint-results.txt deleted file mode 100644 index 24702d5bc..000000000 --- a/scripts/lint/lint-results.txt +++ /dev/null @@ -1,2 +0,0 @@ -DO NOT TOUCH; GENERATED BY DRONE - Lint Report: 88 warnings diff --git a/scripts/lint/lint-up-wrapper.sh b/scripts/lint/lint-up-wrapper.sh deleted file mode 100755 index b4efc0687..000000000 --- a/scripts/lint/lint-up-wrapper.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/sh - -#1: GIT_USERNAME -#2: GIT_TOKEN -#3: BRANCH -#4: LOG_USERNAME -#5: LOG_PASSWORD -#6: DRONE_BUILD_NUMBER - -ruby scripts/lint/lint-up.rb $1 $2 $3 -returnValue=$? - -# exit codes: -# 0: count was reduced -# 1: count was increased -# 2: count stayed the same - -echo "Branch: $3" - -if [ $3 = "master" -a $returnValue -ne 1 ]; then - echo "New master at: https://nextcloud.kaminsky.me/index.php/s/fyxdQjc7LCiy57C" - curl -u $4:$5 -X PUT https://nextcloud.kaminsky.me/remote.php/webdav/talk-droneLogs/master.html --upload-file app/build/reports/lint/lint.html - exit 0 -else - if [ -e $6 ]; then - 6="master-"$(date +%F) - fi - echo "New results at https://nextcloud.kaminsky.me/index.php/s/fyxdQjc7LCiy57C ->" $6.html - curl -u $4:$5 -X PUT https://nextcloud.kaminsky.me/remote.php/webdav/talk-droneLogs/$6.html --upload-file app/build/reports/lint/lint.html - - if [ $returnValue -eq 2 ]; then - exit 0 - else - exit $returnValue - fi -fi diff --git a/scripts/lint/lint-up.rb b/scripts/lint/lint-up.rb deleted file mode 100644 index 1d02185be..000000000 --- a/scripts/lint/lint-up.rb +++ /dev/null @@ -1,199 +0,0 @@ -## Script from https://github.com/tir38/android-lint-entropy-reducer at 07.05.2017 -# adapts to drone, use git username / token as parameter - -Encoding.default_external = Encoding::UTF_8 -Encoding.default_internal = Encoding::UTF_8 - -puts "=================== starting Android Lint Entropy Reducer ====================" - -# get args -git_user, git_token, git_branch = ARGV - -# ======================== SETUP ============================ - -# User name for git commits made by this script. -TRAVIS_GIT_USERNAME = String.new("Drone CI server") - -# File name and relative path of generated Lint report. Must match build.gradle file: -# lintOptions { -# htmlOutput file("[FILE_NAME].html") -# } -LINT_REPORT_FILE = String.new("app/build/reports/lint/lint.html") - -# File name and relative path of previous results of this script. -PREVIOUS_LINT_RESULTS_FILE=String.new("scripts/lint/lint-results.txt") - -# Flag to evaluate warnings. true = check warnings; false = ignore warnings -CHECK_WARNINGS = true - -# File name and relative path to custom lint rules; Can be null or "". -CUSTOM_LINT_FILE = String.new("") - -# ================ SETUP DONE; DON'T TOUCH ANYTHING BELOW ================ - -require 'fileutils' -require 'pathname' -require 'open3' - -# since we need the xml-simple gem, and we want this script self-contained, let's grab it just when we need it -begin - gem "xml-simple" - rescue LoadError - system("gem install xml-simple") - Gem.clear_paths -end - -require 'xmlsimple' - -# add custom Lint jar -if !CUSTOM_LINT_FILE.nil? && - CUSTOM_LINT_FILE.length > 0 - - ENV["ANDROID_LINT_JARS"] = Dir.pwd + "/" + CUSTOM_LINT_FILE - puts "adding custom lint rules to default set: " - puts ENV["ANDROID_LINT_JARS"] -end - -# run Lint -puts "running Lint..." -system './gradlew clean assembleGplay lint' - -# confirm that Lint ran w/out error -result = $?.to_i -if result != 0 - puts "FAIL: failed to run ./gradlew clean assembleGplay lint" - exit 1 -end - -# find Lint report file -lint_reports = Dir.glob(LINT_REPORT_FILE) -if lint_reports.length == 0 - puts "Lint HTML report not found." - exit 1 -end -lint_report = String.new(lint_reports[0]) - -# find error/warning count string in HTML report -error_warning_string = "" -File.open lint_report do |file| - error_warning_string = file.find { |line| line =~ /([0-9]* error[s]? and )?[0-9]* warning[s]?/ } -end - -# find number of errors -error_string = error_warning_string.match(/[0-9]* error[s]?/) - -if (error_string.nil?) - current_error_count = 0 -else - current_error_count = error_string[0].match(/[0-9]*/)[0].to_i -end - -puts "found errors: " + current_error_count.to_s - -# find number of warnings -if CHECK_WARNINGS == true - warning_string = error_warning_string.match(/[0-9]* warning[s]?/)[0] - current_warning_count = warning_string.match(/[0-9]*/)[0].to_i - puts "found warnings: " + current_warning_count.to_s -end - -# get previous error and warning counts from last successful build - -previous_results = false - -previous_lint_reports = Dir.glob(PREVIOUS_LINT_RESULTS_FILE) -if previous_lint_reports.nil? || - previous_lint_reports.length == 0 - - previous_lint_report = File.new(PREVIOUS_LINT_RESULTS_FILE, "w") # create for writing to later -else - previous_lint_report = String.new(previous_lint_reports[0]) - - previous_error_warning_string = "" - File.open previous_lint_report do |file| - previous_error_warning_string = file.find { |line| line =~ /([0-9]* error[s]? and )?[0-9]* warning[s]?/ } - end - - unless previous_error_warning_string.nil? - previous_results = true - - previous_error_string = previous_error_warning_string.match(/[0-9]* error[s]?/) - if previous_error_string.nil? - previous_error_string = "0 errors" - else - previous_error_string = previous_error_string[0] - end - previous_error_count = previous_error_string.match(/[0-9]*/)[0].to_i - puts "previous errors: " + previous_error_count.to_s - - if CHECK_WARNINGS == true - previous_warning_string = previous_error_warning_string.match(/[0-9]* warning[s]?/) - if previous_warning_string.nil? - previous_warning_string = "0 warnings" - else - previous_warning_string = previous_warning_string[0] - end - previous_warning_count = previous_warning_string.match(/[0-9]*/)[0].to_i - puts "previous warnings: " + previous_warning_count.to_s - end - end -end - -# compare previous error count with current error count -if previous_results == true && - current_error_count > previous_error_count - puts "FAIL: error count increased" - exit 1 -end - -# compare previous warning count with current warning count -if CHECK_WARNINGS == true && - previous_results == true && - current_warning_count > previous_warning_count - - puts "FAIL: warning count increased" - exit 1 -end - -# check if warning and error count stayed the same -if previous_results == true && - current_error_count == previous_error_count && - current_warning_count == previous_warning_count - - puts "SUCCESS: count stayed the same" - exit 2 -end - -# either error count or warning count DECREASED - -# write new results to file (will overwrite existing, or create new) -File.write(previous_lint_report, "DO NOT TOUCH; GENERATED BY DRONE\n" + error_warning_string) - -# push changes to github (if this script is run locally, we don't want to overwrite git username and email, so save temporarily) -previous_git_username, _ = Open3.capture2('git config user.name') -previous_git_username = previous_git_username.strip - -previous_git_email, _ = Open3.capture3('git config user.email') -previous_git_email = previous_git_email.strip - -# update git user name and email for this script -system ("git config --local user.name '" + git_user + "'") -system ("git config --local user.email '.'") # set email blank -system ("git remote rm origin") -system ("git remote add origin https://" + git_user + ":" + git_token + "@github.com/nextcloud/talk-android") - -# add previous Lint result file to git -system ('git add ' + PREVIOUS_LINT_RESULTS_FILE) - -# commit changes; Add "skip ci" so that we don't accidentally trigger another Drone build -system ('git commit -m "Drone: update Lint results to reflect reduced error/warning count [skip ci]" ') - -# push to origin -system ('git push origin HEAD:' + git_branch) - -# restore previous git user name and email -system("git config --local user.name '#{previous_git_username}'") -system("git config --local user.email '#{previous_git_email}'") - -puts "SUCCESS: count was reduced" -exit 0 # success -- cgit v1.2.3