|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# This script computes the amount of method Javadoc errors found by |
| 4 | +# checkstyle and compares with the master branch. If the amount of |
| 5 | +# errors have increased relative to master, it exits non-zero. |
| 6 | +# |
| 7 | +# WARNING: Running this script resets the state of the repository to the |
| 8 | +# latest commit, do NOT run this locally if you have any uncommitted changes. |
| 9 | + |
| 10 | +set -o errexit |
| 11 | +set -o nounset |
| 12 | +set -o pipefail |
| 13 | + |
| 14 | +COMPARE_BRANCH="master" |
| 15 | +JAVADOC_CHECKSTYLE_CONFIG="__SPOON_CI_checkstyle-javadoc.xml" |
| 16 | + |
| 17 | +if [[ $(git branch --show-current) == "$COMPARE_BRANCH" ]]; then |
| 18 | + # nothing to compare, we're on the main branch |
| 19 | + exit 0 |
| 20 | +fi |
| 21 | + |
| 22 | +function cleanup() { |
| 23 | + rm "$JAVADOC_CHECKSTYLE_CONFIG" |
| 24 | +} |
| 25 | + |
| 26 | +trap cleanup EXIT |
| 27 | + |
| 28 | +function create_checkstyle_config() { |
| 29 | + echo '<?xml version="1.0"?> |
| 30 | +<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.2//EN" "http://www.puppycrawl.com/dtds/configuration_1_2.dtd"> |
| 31 | +<module name="Checker"> |
| 32 | + <module name="TreeWalker"> |
| 33 | + <module name="JavadocMethod"> |
| 34 | + <property name="scope" value="public"/> |
| 35 | + </module> |
| 36 | + </module> |
| 37 | +</module>' > "$JAVADOC_CHECKSTYLE_CONFIG" |
| 38 | +} |
| 39 | + |
| 40 | +function compute_num_errors() { |
| 41 | + echo $(mvn -B checkstyle:check --fail-never -Dcheckstyle.config.location="$JAVADOC_CHECKSTYLE_CONFIG" \ |
| 42 | + | grep -Po '(?<=There are )\d+(?= errors reported by Checkstyle)') |
| 43 | +} |
| 44 | + |
| 45 | +function main() { |
| 46 | + cd "$(git rev-parse --show-toplevel)" |
| 47 | + |
| 48 | + # compute compare score |
| 49 | + git checkout --force "$COMPARE_BRANCH" &> /dev/null |
| 50 | + create_checkstyle_config |
| 51 | + compare_num_errors=`compute_num_errors` |
| 52 | + |
| 53 | + # compute current score |
| 54 | + git checkout --force - &> /dev/null |
| 55 | + create_checkstyle_config |
| 56 | + current_num_errors=`compute_num_errors` |
| 57 | + |
| 58 | + echo "JAVADOC QUALITY SCORE (lower is better) |
| 59 | + Compare: $compare_num_errors |
| 60 | + Current: $current_num_errors |
| 61 | + " |
| 62 | + |
| 63 | + if [[ -z $compare_num_errors ]]; then |
| 64 | + echo "Failed to compute compare score"; |
| 65 | + exit 1; |
| 66 | + elif [[ -z $current_num_errors ]]; then |
| 67 | + echo "Failed to compute current score"; |
| 68 | + exit 1; |
| 69 | + elif [[ $compare_num_errors < $current_num_errors ]]; then |
| 70 | + echo "Javadoc quality has deteriorated!" |
| 71 | + exit 1 |
| 72 | + else |
| 73 | + echo "Javadoc quality has not deteriorated" |
| 74 | + fi |
| 75 | +} |
| 76 | + |
| 77 | +main |
0 commit comments