git-pr-event (1058B)
1 #!/usr/bin/env bash 2 3 set -e 4 5 access_token=${GITHUB_ACCESS_TOKEN} 6 7 api() { 8 curl -sL -H "Authorization: token $access_token" "$@" 9 } 10 11 usage() { 12 printf "usage: git-pr-event <owner>/<repo> <pr> <body> <approve|reject|comment>\n" >&2 13 exit 1 14 } 15 16 getevent() { 17 case "$1" in 18 "approve") 19 printf "APPROVE\n" 20 ;; 21 "reject") 22 printf "REQUEST_CHANGES\n" 23 ;; 24 "comment") 25 printf "COMMENT\n" 26 ;; 27 *) 28 printf "invalid event '%s', choose approve, reject or comment\n" "$1" >&2 29 exit 1 30 ;; 31 esac 32 } 33 34 project="$1" 35 pr="$2" 36 body="$3" 37 event="${4:-approve}" 38 39 if [ -z $project ] || [ -z $pr ] || [ -z $body ]; then 40 usage 41 fi 42 43 base_url='https://api.github.com/repos/'"$project" 44 45 event=$(getevent $event) 46 47 review_id=$(api -X POST -d'{"body":"'"$body"'"}' "$base_url/pulls/$pr/reviews" | jq -r .id) 48 49 api -X POST -d '{"body":"'"$body"'", "event":"'"$event"'"}' \ 50 "$base_url/pulls/$pr/reviews/$review_id/events" | jq -r '._links.html.href' 51