approve.sh 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/bin/bash
  2. pushd $(dirname "$BASH_SOURCE") > /dev/null
  3. CUR_DIR=$(pwd)
  4. popd > /dev/null
  5. PR=$1
  6. REVIEWER_CHECK=$2
  7. if [ -z "$PR" ]; then
  8. echo "Usage: $0 <pr_number>"
  9. exit 1
  10. fi
  11. function mergeable() {
  12. local PRN=$1
  13. while true; do
  14. local STATE=$(hub api repos/{owner}/{repo}/pulls/$PRN | python -m json.tool | grep '"mergeable":' | cut -d ":" -f 2 | cut -d "," -f 1)
  15. STATE=$(eval "echo $STATE")
  16. echo "merge state is $STATE"
  17. case "$STATE" in
  18. true)
  19. return 0
  20. ;;
  21. null)
  22. ;;
  23. *)
  24. return 1
  25. ;;
  26. esac
  27. done
  28. }
  29. function pr_state() {
  30. local PRN=$1
  31. hub api repos/{owner}/{repo}/pulls/$PRN | python -m json.tool | grep '"state"' | cut -d '"' -f 4
  32. }
  33. function last_commits() {
  34. local PRN=$1
  35. hub api repos/{owner}/{repo}/pulls/$PRN/commits | python -m json.tool | grep '"comments_url"' | awk '{ lifo[NR]=$0; lno=NR } END{ for(;lno>0;lno--){ print lifo[lno]; } }' | cut -d "/" -f 8
  36. }
  37. function last_check() {
  38. local CMT=$1
  39. hub api repos/{owner}/{repo}/commits/${CMT}/check-runs -H 'Accept: application/vnd.github.antiope-preview+json' | python -m json.tool | grep conclusion | cut -d '"' -f 4
  40. }
  41. function check_label() {
  42. local PRN=$1
  43. local LABEL=$2
  44. hub api repos/{owner}/{repo}/issues/${PRN}/labels | python -m json.tool | grep '"name": "'$LABEL'"'
  45. }
  46. function label() {
  47. local PRN=$1
  48. local MSG=$2
  49. local LABEL=$3
  50. for try in $(seq 3)
  51. do
  52. echo "Send $MSG ..."
  53. hub api repos/{owner}/{repo}/issues/${PRN}/comments -f "body=$MSG" > /dev/null
  54. if [ "$?" -ne "0" ]; then
  55. echo "Send $MSG fail!"
  56. return 1
  57. fi
  58. for chk in $(seq 30)
  59. do
  60. sleep 1
  61. if check_label $PRN $LABEL > /dev/null; then
  62. echo "Label $LABEL success!"
  63. return 0
  64. fi
  65. done
  66. done
  67. return 1
  68. }
  69. echo "#1. check status of pull request $PR"
  70. STATE=$(pr_state $PR)
  71. if [ "$STATE" != "open" ]; then
  72. echo "Pull request $PR state $STATE, exit..."
  73. exit 0
  74. fi
  75. echo "Pull request state is open, continue..."
  76. echo "#2. check pull request $PR is mergeable"
  77. if ! mergeable $PR; then
  78. echo "Pull request $PR is not mergeable (DIRTY), exit..."
  79. exit 1
  80. fi
  81. echo "#3. check all checks of pull request $PR have been passed"
  82. CHECKED=
  83. for COMMIT in $(last_commits $PR)
  84. do
  85. for RESULT in $(last_check $COMMIT)
  86. do
  87. echo "Commit $COMMIT check result: $RESULT"
  88. if [ "$RESULT" != "success" ]; then
  89. echo "Cannot approve before all checks success"
  90. exit 1
  91. fi
  92. CHECKED=yes
  93. done
  94. if [ -n "$CHECKED" ]; then
  95. break
  96. fi
  97. done
  98. if [ -z "$CHECKED" ]; then
  99. echo "No check passed, give up and try later ..."
  100. exit 1
  101. fi
  102. echo "All check passed, going to approve and lgtm the Pull Request #$PR..."
  103. if ! label "$PR" "/lgtm" "lgtm"; then
  104. echo "Label lgtm failed"
  105. exit 1
  106. fi
  107. if [ -n "$REVIEWER_CHECK" ]; then
  108. echo "Check all requested reviwers /lgtm the pull request: "
  109. pullfile=$(mktemp)
  110. commentfile=$(mktemp)
  111. function cleanup {
  112. rm -rf "$pullfile" "$commentfile"
  113. }
  114. trap cleanup EXIT
  115. hub api repos/{owner}/{repo}/pulls/$PR > $pullfile
  116. hub api repos/{owner}/{repo}/issues/$PR/comments > $commentfile
  117. if ! $CUR_DIR/advchecks.py $pullfile $commentfile; then
  118. echo "Not all assigned reviwers comment lgtm, give up..."
  119. exit 1
  120. fi
  121. echo "passed!"
  122. fi
  123. if ! label "$PR" "/approve" "approved"; then
  124. echo "Label approved failed"
  125. exit 1
  126. fi
  127. echo "Success!"