checkstyle.sh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #!/usr/bin/env bash
  2. # Licensed to the Apache Software Foundation (ASF) under one or more
  3. # contributor license agreements. See the NOTICE file distributed with
  4. # this work for additional information regarding copyright ownership.
  5. # The ASF licenses this file to You under the Apache License, Version 2.0
  6. # (the "License"); you may not use this file except in compliance with
  7. # the License. You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. add_plugin checkstyle
  17. CHECKSTYLE_TIMER=0
  18. # if it ends in an explicit .sh, then this is shell code.
  19. # if it doesn't have an extension, we assume it is shell code too
  20. function checkstyle_filefilter
  21. {
  22. local filename=$1
  23. if [[ ${filename} =~ \.java$ ]]; then
  24. add_test checkstyle
  25. fi
  26. }
  27. function checkstyle_mvnrunner
  28. {
  29. local logfile=$1
  30. local output=$2
  31. local tmp=${PATCH_DIR}/$$.${RANDOM}
  32. local j
  33. "${MVN}" clean test checkstyle:checkstyle -DskipTests \
  34. -Dcheckstyle.consoleOutput=true \
  35. "-D${PROJECT_NAME}PatchProcess" 2>&1 \
  36. | tee "${logfile}" \
  37. | ${GREP} ^/ \
  38. | ${SED} -e "s,${BASEDIR},.,g" \
  39. > "${tmp}"
  40. # the checkstyle output files are massive, so
  41. # let's reduce the work by filtering out files
  42. # that weren't changed. Some modules are
  43. # MASSIVE and this can cut the output down to
  44. # by orders of magnitude!!
  45. for j in ${CHANGED_FILES}; do
  46. ${GREP} "${j}" "${tmp}" >> "${output}"
  47. done
  48. rm "${tmp}" 2>/dev/null
  49. }
  50. function checkstyle_preapply
  51. {
  52. local module_suffix
  53. local modules=${CHANGED_MODULES}
  54. local module
  55. verify_needed_test checkstyle
  56. if [[ $? == 0 ]]; then
  57. return 0
  58. fi
  59. big_console_header "checkstyle plugin: prepatch"
  60. start_clock
  61. for module in ${modules}
  62. do
  63. pushd "${module}" >/dev/null
  64. echo " Running checkstyle in ${module}"
  65. module_suffix=$(basename "${module}")
  66. checkstyle_mvnrunner \
  67. "${PATCH_DIR}/maven-${PATCH_BRANCH}checkstyle-${module_suffix}.txt" \
  68. "${PATCH_DIR}/${PATCH_BRANCH}checkstyle${module_suffix}.txt"
  69. if [[ $? != 0 ]] ; then
  70. echo "Pre-patch ${PATCH_BRANCH} checkstyle compilation is broken?"
  71. add_jira_table -1 checkstyle "Pre-patch ${PATCH_BRANCH} ${module} checkstyle compilation may be broken."
  72. fi
  73. popd >/dev/null
  74. done
  75. # keep track of how much as elapsed for us already
  76. CHECKSTYLE_TIMER=$(stop_clock)
  77. return 0
  78. }
  79. function checkstyle_calcdiffs
  80. {
  81. local orig=$1
  82. local new=$2
  83. local diffout=$3
  84. local tmp=${PATCH_DIR}/cs.$$.${RANDOM}
  85. local count=0
  86. local j
  87. # first, pull out just the errors
  88. # shellcheck disable=SC2016
  89. ${AWK} -F: '{print $NF}' "${orig}" >> "${tmp}.branch"
  90. # shellcheck disable=SC2016
  91. ${AWK} -F: '{print $NF}' "${new}" >> "${tmp}.patch"
  92. # compare the errors, generating a string of line
  93. # numbers. Sorry portability: GNU diff makes this too easy
  94. ${DIFF} --unchanged-line-format="" \
  95. --old-line-format="" \
  96. --new-line-format="%dn " \
  97. "${tmp}.branch" \
  98. "${tmp}.patch" > "${tmp}.lined"
  99. # now, pull out those lines of the raw output
  100. # shellcheck disable=SC2013
  101. for j in $(cat "${tmp}.lined"); do
  102. # shellcheck disable=SC2086
  103. head -${j} "${new}" | tail -1 >> "${diffout}"
  104. done
  105. if [[ -f "${diffout}" ]]; then
  106. # shellcheck disable=SC2016
  107. count=$(wc -l "${diffout}" | ${AWK} '{print $1}' )
  108. fi
  109. rm "${tmp}.branch" "${tmp}.patch" "${tmp}.lined" 2>/dev/null
  110. echo "${count}"
  111. }
  112. function checkstyle_postapply
  113. {
  114. local rc=0
  115. local module
  116. local modules=${CHANGED_MODULES}
  117. local module_suffix
  118. local numprepatch=0
  119. local numpostpatch=0
  120. local diffpostpatch=0
  121. verify_needed_test checkstyle
  122. if [[ $? == 0 ]]; then
  123. return 0
  124. fi
  125. big_console_header "checkstyle plugin: postpatch"
  126. start_clock
  127. # add our previous elapsed to our new timer
  128. # by setting the clock back
  129. offset_clock "${CHECKSTYLE_TIMER}"
  130. for module in ${modules}
  131. do
  132. pushd "${module}" >/dev/null
  133. echo " Running checkstyle in ${module}"
  134. module_suffix=$(basename "${module}")
  135. checkstyle_mvnrunner \
  136. "${PATCH_DIR}/maven-patchcheckstyle-${module_suffix}.txt" \
  137. "${PATCH_DIR}/patchcheckstyle${module_suffix}.txt"
  138. if [[ $? != 0 ]] ; then
  139. ((rc = rc +1))
  140. echo "Post-patch checkstyle compilation is broken."
  141. add_jira_table -1 checkstyle "Post-patch checkstyle ${module} compilation is broken."
  142. continue
  143. fi
  144. #shellcheck disable=SC2016
  145. diffpostpatch=$(checkstyle_calcdiffs \
  146. "${PATCH_DIR}/${PATCH_BRANCH}checkstyle${module_suffix}.txt" \
  147. "${PATCH_DIR}/patchcheckstyle${module_suffix}.txt" \
  148. "${PATCH_DIR}/diffcheckstyle${module_suffix}.txt" )
  149. if [[ ${diffpostpatch} -gt 0 ]] ; then
  150. ((rc = rc + 1))
  151. # shellcheck disable=SC2016
  152. numprepatch=$(wc -l "${PATCH_DIR}/${PATCH_BRANCH}checkstyle${module_suffix}.txt" | ${AWK} '{print $1}')
  153. # shellcheck disable=SC2016
  154. numpostpatch=$(wc -l "${PATCH_DIR}/patchcheckstyle${module_suffix}.txt" | ${AWK} '{print $1}')
  155. add_jira_table -1 checkstyle "The applied patch generated "\
  156. "${diffpostpatch} new checkstyle issues (total was ${numprepatch}, now ${numpostpatch})."
  157. footer="${footer} @@BASE@@/diffcheckstyle${module_suffix}.txt"
  158. fi
  159. popd >/dev/null
  160. done
  161. if [[ ${rc} -gt 0 ]] ; then
  162. add_jira_footer checkstyle "${footer}"
  163. return 1
  164. fi
  165. add_jira_table +1 checkstyle "There were no new checkstyle issues."
  166. return 0
  167. }