Last active
December 22, 2021 18:27
-
-
Save nickboldt/eac07a4d83a9ae9abffd060a4a30a86e to your computer and use it in GitHub Desktop.
add reviewer, approve, and merge a PR via GH api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Copyright (c) 2021 Red Hat, Inc. | |
# This program and the accompanying materials are made | |
# available under the terms of the Eclipse Public License 2.0 | |
# which is available at https://www.eclipse.org/legal/epl-2.0/ | |
# | |
# SPDX-License-Identifier: EPL-2.0 | |
# | |
# Contributors: | |
# Red Hat, Inc. - initial API and implementation | |
# | |
# push locally built asset* files to github | |
set -e | |
# defaults | |
usageGHT() { | |
echo 'Setup: | |
First, export your GITHUB_TOKEN: | |
export GITHUB_TOKEN="...github-token..." | |
' | |
usage | |
} | |
usage () { | |
echo " | |
Usage: | |
$0 -u GITHUB_USER https://github.com/[org]/[project]/pull/[pr-num1] [https://github.com/[org]/[project]/pull/[pr-num2] ...] | |
" | |
} | |
while [[ "$#" -gt 0 ]]; do | |
case $1 in | |
'-ght') GITHUB_TOKEN="$2"; export GITHUB_TOKEN="${GITHUB_TOKEN}"; shift 1;; | |
'-u') GITHUB_USERNAME="$2"; shift 1;; | |
'-h'|'--help') usageGHT; exit 0;; | |
*) URLs="${URLs} $1";; | |
esac | |
shift 1 | |
done | |
if [[ ! "${GITHUB_TOKEN}" ]]; then usageGHT; exit 1; fi | |
if [[ ! "${GITHUB_USERNAME}" ]]; then usage; exit 1; fi | |
for URL in $URLs; do | |
# compute org, repo, and PR number | |
# URL=https://github.com/eclipse-che/che-server/pull/220#pullrequestreview-838533873 | |
URL=${URL%%#*}; URL=${URL##*:}; URL=${URL##*github.com/}; # echo $URL # eclipse-che/che-server/pull/220 | |
orgAndRepo=${URL%/pull/*}; # echo $orgAndRepo # eclipse-che/che-server | |
PR=${URL#*/pull/}; # echo $PR # 220 | |
# Run 3 steps: | |
curlAuthPostJSON="curl -sS -u ${GITHUB_USERNAME}:$GITHUB_TOKEN -X POST -H \"Accept: application/vnd.github.v3+json\"" | |
# add reviewer to PR | |
$curlAuthPostJSON https://api.github.com/repos/${orgAndRepo}/pulls/${PR}/requested_reviewers \ | |
-d '{"reviewers":["'${GITHUB_USERNAME}'"]}' \ | |
| jq -r '.requested_reviewers[].login' | |
# approve PR | |
$curlAuthPostJSON https://api.github.com/repos/${orgAndRepo}/pulls/${PR}/reviews \ | |
-d '{"body":"approved via API", "event":"APPROVE"}' \ | |
| jq -r '[.state,.body,.html_url] | @tsv' | |
# wait for ECA aproval | |
sleep 5s | |
# merge PR (assuming no required PR checks are still running) | |
curlAuthPutJSON="curl -sS -u ${GITHUB_USERNAME}:$GITHUB_TOKEN -X PUT -H \"Accept: application/vnd.github.v3+json\"" | |
$curlAuthPutJSON https://api.github.com/repos/${orgAndRepo}/pulls/${PR}/merge \ | |
-d '{"commit_message":"merged by API", "merge_method":"squash"}' | |
echo | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment