From d7b41eaa95645919af87439e3fae6f280756de2b Mon Sep 17 00:00:00 2001 From: Qiu Jian Date: Tue, 30 Jul 2019 16:20:49 +0800 Subject: [PATCH] feature: add pr tools, approve.sh and comment.sh --- scripts/approve.sh | 54 ++++++++++++++++++++++++++++++++++++++++++++++ scripts/comment.sh | 19 ++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100755 scripts/approve.sh create mode 100755 scripts/comment.sh diff --git a/scripts/approve.sh b/scripts/approve.sh new file mode 100755 index 0000000000..ac18a20073 --- /dev/null +++ b/scripts/approve.sh @@ -0,0 +1,54 @@ +#!/bin/bash + +PR=$1 +MSG=$2 + +if [ -z "$PR" ]; then + echo "Usage: $0 " + exit 1 +fi + +function pr_state() { + local PRN=$1 + hub api repos/{owner}/{repo}/pulls/$PRN | python -m json.tool | grep '"state"' | cut -d '"' -f 4 +} + +function last_commit() { + local PRN=$1 + hub api repos/{owner}/{repo}/pulls/$PRN/commits | python -m json.tool | grep '"comments_url"' | tail -1 | cut -d "/" -f 8 +} + +function last_check() { + local CMT=$1 + 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 +} + +STATE=$(pr_state $PR) +if [ "$STATE" != "open" ]; then + echo "Pull request $PR state != open, exit..." + exit 1 +fi + +COMMIT=$(last_commit $PR) +for RESULT in $(last_check $COMMIT) +do + echo "Last commit $COMMIT check result: $RESULT" + if [ "$RESULT" != "success" ]; then + echo "Cannot approve before all checks success" + exit 1 + fi +done + +echo "All check passed, going to approve and lgtm the Pull Request #$PR..." + +for msg in /lgtm /approve +do + echo "Send $msg ..." + hub api repos/{owner}/{repo}/issues/$PR/comments -f "body=$msg" > /dev/null + if [ "$?" -ne "0" ]; then + echo "Send $msg fail!" + exit 1 + fi +done + +echo "Success!" diff --git a/scripts/comment.sh b/scripts/comment.sh new file mode 100755 index 0000000000..658ef14eb0 --- /dev/null +++ b/scripts/comment.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +PR=$1 +MSG=$2 + +if [ -z "$PR" ]; then + echo "Usage: $0 " + exit 1 +fi + +shift +for msg in "$@" +do + echo Comment: $msg + hub api repos/{owner}/{repo}/issues/$PR/comments -f "body=$msg" > /dev/null + if [ "$?" -eq "0" ]; then + echo "Success!" + fi +done