Add script to import github pull requests to local repo
The script also makes suggestions on how to upload the changes for review and distinguishes between PRs with successful and failed CI tests.
Change-Id: I9e83e3ca6393a0cdeb4eba9c4b2e2e93652efc9b
diff --git a/bin/import_github_pull_requests b/bin/import_github_pull_requests
new file mode 100755
index 0000000..cb13206
--- /dev/null
+++ b/bin/import_github_pull_requests
@@ -0,0 +1,96 @@
+#!/usr/bin/env bash
+read -r -d '\n' USAGE <<EOF
+Script to import pull requests from github to your local repository and to give you
+some suggestion how to send them to your gerrit server.
+
+Usage: $0 [-h] [-v] [-f]
+ -f import PRs with **failed** CI tests
+
+Example use:
+cd ~/KorAP/Kustvakt
+$0
+EOF
+
+ORG="KorAP"
+REPO=$(basename $(git rev-parse --show-toplevel))
+
+if [ -z "$GITHUB_KORAP_REPO_AUTH" ]; then
+ cat <<EOF
+
+If you experience limited API access, create a personal github access token:
+https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token
+Then set GITHUB_KORAP_REPO_AUTH="-u <user>:<access token>", for example:
+export GITHUB_KORAP_REPO_AUTH="-u janedoe:53bd435ed3e456548ec2d1a94172591cc0ae1167"
+
+EOF
+fi
+GET_CI_STATUS_SUCCESS="true"
+REVIEW_SUGGESTION="+1"
+while getopts ":vfh" opt; do
+ case ${opt} in
+ f )
+ GET_CI_STATUS_SUCCESS="false"
+ REVIEW_SUGGESTION="-2"
+ ;;
+ v )
+ set -x
+ VERBOSE=1
+ ;;
+ h )
+ echo "$USAGE"
+ exit
+ ;;
+ \? )
+ echo "$USAGE"
+ exit
+ ;;
+ esac
+done
+
+WORKFLOW=$(curl $GITHUB_KORAP_REPO_AUTH -s "https://api.github.com/repos/${ORG}/${REPO}/actions/workflows" | jq '.workflows[0].id')
+if [ -z $(git remote get-url github 2> /dev/null) ]; then
+ git remote add github https://github.com/$ORG/$REPO
+fi
+git fetch github
+git fetch origin
+OLDHEAD=$(git log --pretty=format:%H -1)
+IFS=','
+while read -r success nr sha message; do
+ if [ "$success" == "$GET_CI_STATUS_SUCCESS" ]; then
+ if git cherry-pick "$sha" > /dev/null 2> /dev/null; then
+ # add closes #<nr> to let github automatically close the PR and add change-id via hook
+ EDITOR="echo 'Closes #${nr}' >> $1" git commit --amend
+ else
+ git cherry-pick --skip
+ fi
+ else
+ if [ "$GET_CI_STATUS_SUCCESS" == "true" ]; then
+ echo "Skipping because of **failed** CI test: $nr - $message $sha"
+ else
+ echo "Skipping because of **sucessfull** CI test: $nr - $message $sha"
+ fi
+ fi
+done <<<$(
+ curl $GITHUB_KORAP_REPO_AUTH -s "https://api.github.com/repos/${ORG}/${REPO}/actions/workflows/${WORKFLOW}/runs?event=pull_request" |
+ tee out.json |
+ jq '.workflow_runs[] | select(.head_commit.message!="trigger github actions") | {success: (.conclusion=="success"), pr:.pull_requests[].number, sha:.pull_requests[].head.sha, message:.head_commit.message} | join(",")' |
+ sed 's/["]//g' | sed 's/\\n\\n.*//g'
+)
+NEWHEAD=$(git log --pretty=format:%H -1)
+if [ "$NEWHEAD" == "$OLDHEAD" ]; then
+ echo
+ echo "No new pull requests"
+else
+ echo
+ echo "New pull requests:"
+ git log --graph --pretty=reference "$OLDHEAD"..HEAD
+ cat <<EOF
+
+# Copy and paste for code review:
+#
+# git push origin HEAD:refs/for/master
+#
+# ssh -p 29418 korap gerrit review --code-review $REVIEW_SUGGESTION \$(git rev-list $OLDHEAD..HEAD)
+
+EOF
+fi