diff --git a/app/build.gradle b/app/build.gradle index 4f085b9aa..0a0b58ecb 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -30,14 +30,19 @@ configurations { ktlint } -def taskRequest = getGradle().getStartParameter().getTaskRequests().toString() -if (taskRequest.contains("Gplay") || taskRequest.contains("findbugs") || taskRequest.contains("lint")) { - apply from: 'gplay.gradle' +for (TaskExecutionRequest tr : getGradle().getStartParameter().getTaskRequests()) { + for (String arg : tr.args) { + // any gplay, but only exact "build", as e.g. buildGeneric shall not apply gplay.grade + if (arg.contains("Gplay") || arg.contains("lint") || arg == "build") { + apply from: 'gplay.gradle' + System.console().println("Applying gplay.gradle") + } + } } android { compileSdkVersion 29 - buildToolsVersion '29.0.2' + buildToolsVersion '30.0.3' defaultConfig { minSdkVersion 21 targetSdkVersion 29 diff --git a/scripts/analysis/analysis-wrapper.sh b/scripts/analysis/analysis-wrapper.sh index a1eae6390..b85116e8b 100755 --- a/scripts/analysis/analysis-wrapper.sh +++ b/scripts/analysis/analysis-wrapper.sh @@ -1,4 +1,4 @@ -#!/bin/sh +#!/usr/bin/env bash #1: GIT_USERNAME #2: GIT_TOKEN @@ -9,11 +9,13 @@ #7: PULL_REQUEST_NUMBER stableBranch="master" +repository="talk-android" ruby scripts/analysis/lint-up.rb $1 $2 $3 lintValue=$? -./gradlew assembleGplay app:spotbugs +ruby scripts/analysis/findbugs-up.rb $1 $2 $3 +findbugsValue=$? # exit codes: # 0: count was reduced @@ -22,37 +24,60 @@ lintValue=$? echo "Branch: $3" -if [ $3 = "master" ]; then - echo "New spotbugs result for master at: https://www.kaminsky.me/nc-dev/talk-spotbugs/master.html" - curl -u $4:$5 -X PUT https://nextcloud.kaminsky.me/remote.php/webdav/talk-spotbugs/master.html --upload-file app/build/reports/spotbugs/spotbugs.html - - summary=$(sed -n "/

Summary<\/h1>/,/

Warnings<\/h1>/p" app/build/reports/spotbugs/spotbugs.html | head -n-1 | sed s'/<\/a>//'g | sed s'///'g | sed s'/Summary/Spotbugs (master)/' | tr "\"" "\'" | tr -d "\r\n") - curl -u $4:$5 -X PUT -d "$summary" https://nextcloud.kaminsky.me/remote.php/webdav/talk-spotbugs/spotbugs-summary-master.html - +if [ $3 = $stableBranch ]; then + echo "New findbugs result for $stableBranch at: https://www.kaminsky.me/nc-dev/$repository-findbugs/$stableBranch.html" + curl -u $4:$5 -X PUT https://nextcloud.kaminsky.me/remote.php/webdav/$repository-findbugs/$stableBranch.html --upload-file build/reports/spotbugs/spotbugs.html + + summary=$(sed -n "/

Summary<\/h1>/,/

Warnings<\/h1>/p" build/reports/spotbugs/spotbugs.html | head -n-1 | sed s'/<\/a>//'g | sed s'///'g | sed s"/Summary/SpotBugs ($stableBranch)/" | tr "\"" "\'" | tr -d "\r\n") + curl -u $4:$5 -X PUT -d "$summary" https://nextcloud.kaminsky.me/remote.php/webdav/$repository-findbugs/findbugs-summary-$stableBranch.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-lint/master.html --upload-file app/build/reports/lint/lint.html + echo "New lint result for $stableBranch at: https://www.kaminsky.me/nc-dev/$repository-lint/$stableBranch.html" + curl -u $4:$5 -X PUT https://nextcloud.kaminsky.me/remote.php/webdav/$repository-lint/$stableBranch.html --upload-file build/reports/lint/lint.html exit 0 fi else if [ -e $6 ]; then - 6="master-"$(date +%F) + 6=$stableBranch"-"$(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-lint/$6.html --upload-file app/build/reports/lint/lint.html - - echo "New spotbugs results at https://www.kaminsky.me/nc-dev/talk-spotbugs/$6.html" - curl 2>/dev/null -u $4:$5 -X PUT https://nextcloud.kaminsky.me/remote.php/webdav/talk-spotbugs/$6.html --upload-file app/build/reports/spotbugs/spotbugs.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 + echo "New lint results at https://www.kaminsky.me/nc-dev/$repository-lint/$6.html" + curl 2>/dev/null -u $4:$5 -X PUT https://nextcloud.kaminsky.me/remote.php/webdav/$repository-lint/$6.html --upload-file build/reports/lint/lint.html + + echo "New findbugs results at https://www.kaminsky.me/nc-dev/$repository-findbugs/$6.html" + curl 2>/dev/null -u $4:$5 -X PUT https://nextcloud.kaminsky.me/remote.php/webdav/$repository-findbugs/$6.html --upload-file build/reports/spotbugs/spotbugs.html + + # delete all old comments, starting with Codacy + oldComments=$(curl 2>/dev/null -u $1:$2 -X GET https://api.github.com/repos/nextcloud/android/issues/$7/comments | jq '.[] | (.id |tostring) + "|" + (.user.login | test("nextcloud-android-bot") | tostring) + "|" + (.body | test("

Codacy.*") | tostring)' | grep "true|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/android/issues/comments/$comment done - + + # check library, only if base branch is master + baseBranch=$(scripts/analysis/getBranchBase.sh $1 $2 $7 | tr -d "\"") + if [ $baseBranch = "master" -a $(grep "androidLibraryVersion = \"master-SNAPSHOT\"" build.gradle -c) -ne 1 ]; then + checkLibraryMessage="

Android-library is not set to master branch in build.gradle

" + checkLibrary=1 + elif [ $baseBranch != "master" -a $baseBranch = $stableBranch -a $(grep "androidLibraryVersion.*SNAPSHOT" build.gradle -c) -ne 0 ]; then + checkLibraryMessage="

Android-library is set to a SNAPSHOT in build.gradle

" + checkLibrary=1 + else + checkLibrary=0 + fi + + # lint and findbugs file must exist + if [ ! -s build/reports/lint/lint.html ] ; then + echo "lint.html file is missing!" + exit 1 + fi + + if [ ! -s build/reports/spotbugs/spotbugs.html ] ; then + echo "spotbugs.html file is missing!" + exit 1 + fi + # add comment with results - lintResultNew=$(grep "Lint Report.* [0-9]* warning" app/build/reports/lint/lint.html | cut -f2 -d':' |cut -f1 -d'<') + lintResultNew=$(grep "Lint Report.* [0-9]* warning" build/reports/lint/lint.html | cut -f2 -d':' |cut -f1 -d'<') lintErrorNew=$(echo $lintResultNew | grep "[0-9]* error" -o | cut -f1 -d" ") if ( [ -z $lintErrorNew ] ); then @@ -64,7 +89,7 @@ else lintWarningNew=0 fi - lintResultOld=$(curl 2>/dev/null https://raw.githubusercontent.com/nextcloud/talk-android/$stableBranch/scripts/analysis/lint-results.txt) + lintResultOld=$(curl 2>/dev/null https://raw.githubusercontent.com/nextcloud/android/$stableBranch/scripts/analysis/lint-results.txt) lintErrorOld=$(echo $lintResultOld | grep "[0-9]* error" -o | cut -f1 -d" ") if ( [ -z $lintErrorOld ] ); then lintErrorOld=0 @@ -74,14 +99,60 @@ else if ( [ -z $lintWarningOld ] ); then lintWarningOld=0 fi - lintResult="

Lint

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

Summary<\/h1>/,/

Warnings<\/h1>/p" app/build/reports/spotbugs/spotbugs.html |head -n-1 | sed s'/<\/a>//'g | sed s'///'g | sed s"#Summary#SpotBugs (new)#" | tr "\"" "\'" | tr -d "\n") - spotbugsResultOld=$(curl 2>/dev/null https://www.kaminsky.me/nc-dev/talk-spotbugs/spotbugs-summary-master.html | tr "\"" "\'" | tr -d "\r\n" | sed s'#SpotBugs#SpotBugs#'| 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 + + if [ $stableBranch = "master" ] ; then + codacyValue=$(curl 2>/dev/null https://app.codacy.com/dashboards/breakdown\?projectId\=44248 | grep "total issues" | cut -d">" -f3 | cut -d"<" -f1) + codacyResult="

Codacy

$codacyValue" + else + codacyResult="" + fi + + lintResult="

Lint

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

Summary<\/h1>/,/

Warnings<\/h1>/p" build/reports/spotbugs/spotbugs.html |head -n-1 | sed s'/<\/a>//'g | sed s'///'g | sed s"#Summary#SpotBugs (new)#" | tr "\"" "\'" | tr -d "\n") + findbugsResultOld=$(curl 2>/dev/null https://www.kaminsky.me/nc-dev/$repository-findbugs/findbugs-summary-$stableBranch.html | tr "\"" "\'" | tr -d "\r\n" | sed s"#SpotBugs#SpotBugs#" | tr "\"" "\'" | tr -d "\n") + + + if ( [ $lintValue -eq 1 ] ) ; then + lintMessage="

Lint increased!

" + fi + + if ( [ $findbugsValue -eq 1 ] ) ; then + findbugsMessage="

SpotBugs increased!

" + fi + + # check gplay limitation: all changelog files must only have 500 chars + gplayLimitation=$(scripts/checkGplayLimitation.sh) + + if [ ! -z "$gplayLimitation" ]; then + gplayLimitation="

Following files are beyond 500 char limit:



"$gplayLimitation + fi + + # check for NotNull + if [[ $(grep org.jetbrains.annotations src/main/* -ir -c) -gt 0 ]] ; then + notNull="org.jetbrains.annotations.NotNull is used. Please use androidx.annotation.NonNull instead.

" + fi + + curl -u $1:$2 -X POST https://api.github.com/repos/nextcloud/android/issues/$7/comments -d "{ \"body\" : \"$codacyResult $lintResult $findbugsResultNew $findbugsResultOld $checkLibraryMessage $lintMessage $findbugsMessage $gplayLimitation $notNull\" }" + + if [ ! -z "$gplayLimitation" ]; then + exit 1 + fi + + if [ $checkLibrary -eq 1 ]; then + exit 1 + fi + + if [ ! $lintValue -eq 2 ]; then + exit $lintValue + fi + + if [ $notNull -gt 0 ]; then + exit 1 + fi + + if [ $findbugsValue -eq 2 ]; then exit 0 else - exit $lintValue - fi + exit $findbugsValue + fi fi diff --git a/scripts/analysis/findbugs-results.txt b/scripts/analysis/findbugs-results.txt new file mode 100644 index 000000000..0e92c3c0f --- /dev/null +++ b/scripts/analysis/findbugs-results.txt @@ -0,0 +1 @@ +306 \ No newline at end of file diff --git a/scripts/analysis/findbugs-up.rb b/scripts/analysis/findbugs-up.rb new file mode 100755 index 000000000..cc59d8e35 --- /dev/null +++ b/scripts/analysis/findbugs-up.rb @@ -0,0 +1,139 @@ +## 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 FindBugs 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 FindBugs report. Must match build.gradle file: +# lintOptions { +# htmlOutput file("[FILE_NAME].html") +# } +FINDBUGS_REPORT_FILE = String.new("build/reports/spotbugs/spotbugs.html") + +# File name and relative path of previous results of this script. +PREVIOUS_FINDBUGS_RESULTS_FILE=String.new("scripts/analysis/findbugs-results.txt") + +# Flag to evaluate warnings. true = check warnings; false = ignore warnings +CHECK_WARNINGS = true + +# File name and relative path to custom FindBugs rules; Can be null or "". +CUSTOM_FINDBUGS_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' + +# run FindBugs +puts "running FindBugs..." +system './gradlew assembleGplayDebug 1>/dev/null' + +# confirm that assemble ran w/out error +result = $?.to_i +if result != 0 + puts "FAIL: failed to run ./gradlew assembleGplayDebug" + exit 1 +end + +system './gradlew spotbugsGplayDebugReport 1>/dev/null 2>&1' + +# find FindBugs report file +findbugs_reports = Dir.glob(FINDBUGS_REPORT_FILE) +if findbugs_reports.length == 0 + puts "Findbugs HTML report not found." + exit 1 +end +findbugs_report = String.new(findbugs_reports[0]) + +# find number of warnings +current_warning_count = `grep -A 3 "Total" build/reports/spotbugs/spotbugs.html | tail -n1 | cut -f2 -d">" | cut -f1 -d"<"`.to_i +puts "found warnings: " + current_warning_count.to_s + +# get warning counts from last successful build + +previous_results = false + +previous_findbugs_reports = Dir.glob(PREVIOUS_FINDBUGS_RESULTS_FILE) +if previous_findbugs_reports.nil? || previous_findbugs_reports.length == 0 + previous_findbugs_report = File.new(PREVIOUS_FINDBUGS_RESULTS_FILE, "w") # create for writing to later +else + previous_findbugs_report = String.new(previous_findbugs_reports[0]) + + previous_warning_count = File.open(previous_findbugs_report, &:readline).match(/[0-9]*/)[0].to_i + + if previous_warning_count.nil? + previous_results = false + else + previous_results = true + + puts "previous warnings: " + previous_warning_count.to_s + end +end + +# compare previous warning count with current warning count +if 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_warning_count == previous_warning_count + puts "SUCCESS: count stayed the same" + exit 2 +end + +# warning count DECREASED +puts "SUCCESS: count decreased from " + previous_warning_count.to_s + " to " + current_warning_count.to_s + +# write new results to file (will overwrite existing, or create new) +File.write(previous_findbugs_report, current_warning_count) + +# 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 'android@nextcloud.com'") +system ("git remote rm origin") +system ("git remote add origin https://" + git_user + ":" + git_token + "@github.com/nextcloud/android") + +# add previous FindBugs result file to git +system ('git add ' + PREVIOUS_FINDBUGS_RESULTS_FILE) + +# commit changes; Add "skip ci" so that we don't accidentally trigger another Drone build +system ('git commit -sm "Drone: update FindBugs 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