checkstyle.sh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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_preapply
  28. {
  29. verify_needed_test checkstyle
  30. if [[ $? == 0 ]]; then
  31. return 0
  32. fi
  33. big_console_header "checkstyle plugin: prepatch"
  34. start_clock
  35. echo_and_redirect "${PATCH_DIR}/${PATCH_BRANCH}checkstyle.txt" "${MVN}" test checkstyle:checkstyle-aggregate -DskipTests "-D${PROJECT_NAME}PatchProcess"
  36. if [[ $? != 0 ]] ; then
  37. echo "Pre-patch ${PATCH_BRANCH} checkstyle compilation is broken?"
  38. add_jira_table -1 checkstyle "Pre-patch ${PATCH_BRANCH} checkstyle compilation may be broken."
  39. return 1
  40. fi
  41. cp -p "${BASEDIR}/target/checkstyle-result.xml" \
  42. "${PATCH_DIR}/checkstyle-result-${PATCH_BRANCH}.xml"
  43. # keep track of how much as elapsed for us already
  44. CHECKSTYLE_TIMER=$(stop_clock)
  45. return 0
  46. }
  47. function checkstyle_postapply
  48. {
  49. verify_needed_test checkstyle
  50. if [[ $? == 0 ]]; then
  51. return 0
  52. fi
  53. big_console_header "checkstyle plugin: postpatch"
  54. start_clock
  55. # add our previous elapsed to our new timer
  56. # by setting the clock back
  57. offset_clock "${CHECKSTYLE_TIMER}"
  58. echo_and_redirect "${PATCH_DIR}/patchcheckstyle.txt" "${MVN}" test checkstyle:checkstyle-aggregate -DskipTests "-D${PROJECT_NAME}PatchProcess"
  59. if [[ $? != 0 ]] ; then
  60. echo "Post-patch checkstyle compilation is broken."
  61. add_jira_table -1 checkstyle "Post-patch checkstyle compilation is broken."
  62. return 1
  63. fi
  64. cp -p "${BASEDIR}/target/checkstyle-result.xml" \
  65. "${PATCH_DIR}/checkstyle-result-patch.xml"
  66. checkstyle_runcomparison
  67. # shellcheck disable=SC2016
  68. CHECKSTYLE_POSTPATCH=$(wc -l "${PATCH_DIR}/checkstyle-result-diff.txt" | ${AWK} '{print $1}')
  69. if [[ ${CHECKSTYLE_POSTPATCH} -gt 0 ]] ; then
  70. add_jira_table -1 checkstyle "The applied patch generated "\
  71. "${CHECKSTYLE_POSTPATCH}" \
  72. " additional checkstyle issues."
  73. add_jira_footer checkstyle "@@BASE@@/checkstyle-result-diff.txt"
  74. return 1
  75. fi
  76. add_jira_table +1 checkstyle "There were no new checkstyle issues."
  77. return 0
  78. }
  79. function checkstyle_runcomparison
  80. {
  81. python <(cat <<EOF
  82. import os
  83. import sys
  84. import xml.etree.ElementTree as etree
  85. from collections import defaultdict
  86. if len(sys.argv) != 3 :
  87. print "usage: %s checkstyle-result-master.xml checkstyle-result-patch.xml" % sys.argv[0]
  88. exit(1)
  89. def path_key(x):
  90. path = x.attrib['name']
  91. return path[path.find('${PROJECT_NAME}-'):]
  92. def print_row(path, master_errors, patch_errors):
  93. print '%s\t%s\t%s' % (k,master_dict[k],child_errors)
  94. master = etree.parse(sys.argv[1])
  95. patch = etree.parse(sys.argv[2])
  96. master_dict = defaultdict(int)
  97. for child in master.getroot().getchildren():
  98. if child.tag != 'file':
  99. continue
  100. child_errors = len(child.getchildren())
  101. if child_errors == 0:
  102. continue
  103. master_dict[path_key(child)] = child_errors
  104. for child in patch.getroot().getchildren():
  105. if child.tag != 'file':
  106. continue
  107. child_errors = len(child.getchildren())
  108. if child_errors == 0:
  109. continue
  110. k = path_key(child)
  111. if child_errors > master_dict[k]:
  112. print_row(k, master_dict[k], child_errors)
  113. EOF
  114. ) "${PATCH_DIR}/checkstyle-result-${PATCH_BRANCH}.xml" "${PATCH_DIR}/checkstyle-result-patch.xml" > "${PATCH_DIR}/checkstyle-result-diff.txt"
  115. }