test-patch-find-new-patch-available-jiras.sh 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #!/bin/bash
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. if [ "${TESTPATCHDEBUG}" == "true" ] ; then
  15. set -x
  16. fi
  17. BASEDIR=$(pwd)
  18. TEMPDIR=${BASEDIR}/tmp
  19. JIRAAVAILPATCHQUERY="https://issues.apache.org/jira/sr/jira.issueviews:searchrequest-xml/temp/SearchRequest.xml?jqlQuery=project+in+%28AMBARI%29+AND+status+%3D+%22Patch+Available%22+ORDER+BY+updated+DESC&tempMax=1000"
  20. TESTPATCHJOBURL="https://builds.apache.org/job/Ambari-trunk-test-patch"
  21. # TESTPATCHJOBURL="http://10.103.219.57:8080/job/Ambari-trunk-test-patch"
  22. TOKEN=""
  23. SUBMIT="false"
  24. DELETEHISTORYFILE="false"
  25. RUNTESTSFILE=${BASEDIR}/TESTED_PATCHES.txt
  26. printUsage() {
  27. echo "Usage: $0 <OPTIONS>"
  28. echo " --submit --token=<AMBARI PRECOMMIT JOB TOKEN>"
  29. echo " [--delete-history-file]"
  30. echo " [--script-debug]"
  31. echo
  32. }
  33. ###############################################################################
  34. parseArgs() {
  35. for i in $*
  36. do
  37. case $i in
  38. --submit)
  39. SUBMIT="true"
  40. ;;
  41. --token=*)
  42. TOKEN=${i#*=}
  43. ;;
  44. --script-debug)
  45. DEBUG="-x"
  46. ;;
  47. --delete-history-file)
  48. DELETEHISTORYFILE="true"
  49. ;;
  50. *)
  51. echo "Invalid option"
  52. echo
  53. printUsage
  54. exit 1
  55. ;;
  56. esac
  57. done
  58. if [[ "$SUBMIT" == "true" && "${TOKEN}" == "" ]] ; then
  59. echo "Token has not been specified"
  60. echo
  61. printUsage
  62. exit 1
  63. fi
  64. }
  65. ###############################################################################
  66. findAndSubmitAvailablePatches() {
  67. ## Grab all the key (issue numbers) and largest attachment id for each item in the XML
  68. curl --fail --location --retry 3 "${JIRAAVAILPATCHQUERY}" > ${TEMPDIR}/patch-availables.xml
  69. if [ "$?" != "0" ] ; then
  70. echo "Could not retrieve available patches from JIRA"
  71. exit 1
  72. fi
  73. xpath -q -e "//item/key/text() | //item/attachments/attachment[not(../attachment/@id > @id)]/@id" ${TEMPDIR}/patch-availables.xml > ${TEMPDIR}/patch-attachments.element
  74. ### Replace newlines with nothing, then replace id=" with =, then replace " with newline
  75. ### to yield lines with pairs (issueNumber,largestAttachmentId). Example: AMBARI-123,456984
  76. cat ${TEMPDIR}/patch-attachments.element | awk '{ if ( $1 ~ /^AMBARI\-/) {JIRA=$1 }; if ($1 ~ /id=/) { print JIRA","$1} }' | sed 's/id\="//' | sed 's/"//' > ${TEMPDIR}/patch-availables.pair
  77. ### Iterate through issue list and find the (issueNumber,largestAttachmentId) pairs that have
  78. ### not been tested (ie don't already exist in the patch_tested.txt file
  79. touch ${RUNTESTSFILE}
  80. cat ${TEMPDIR}/patch-availables.pair | while read PAIR ; do
  81. set +e
  82. COUNT=`grep -c "$PAIR" ${RUNTESTSFILE}`
  83. set -e
  84. if [ "$COUNT" -lt "1" ] ; then
  85. ### Parse $PAIR into project, issue number, and attachment id
  86. ISSUE=`echo $PAIR | sed -e "s/,.*$//"`
  87. echo "Found new patch for issue $ISSUE"
  88. if [ "$SUBMIT" == "true" ]; then
  89. ### Kick off job
  90. echo "Submitting job for issue $ISSUE"
  91. curl --fail --location --retry 3 "${TESTPATCHJOBURL}/buildWithParameters?token=${TOKEN}&JIRA_NUMBER=${ISSUE}" > /dev/null
  92. if [ "$?" != "0" ] ; then
  93. echo "Could not submit precommit job for $ISSUE"
  94. exit 1
  95. fi
  96. fi
  97. ### Mark this pair as tested by appending to file
  98. echo "$PAIR" >> ${RUNTESTSFILE}
  99. fi
  100. done
  101. }
  102. ###############################################################################
  103. mkdir ${TEMPDIR} 2>&1 $STDOUT
  104. parseArgs "$@"
  105. if [ -n "${DEBUG}" ] ; then
  106. set -x
  107. fi
  108. if [ "${DELETEHISTORYFILE}" == "true" ] ; then
  109. rm ${RUNTESTSFILE}
  110. fi
  111. findAndSubmitAvailablePatches
  112. exit 0