1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
#!/usr/bin/env bash
#
# Echo <msg> and exit
#
abort() {
echo >&2 "$@"
exit 1
}
#
# Produce json with <title>, <body>, <head> and <base>
#
json() {
local title="${1//\"/\\\"}"
local body="${2//\"/\\\"}"
local head="${3//\"/\\\"}"
local base="${4//\"/\\\"}"
cat <<EOF
{
"title": "$title",
"body": "$body",
"head": "$head",
"base": "$base"
}
EOF
}
# personal access token
# config name is github-personal-access-token '_' is not allowed in git config
github_personal_access_token=$(git config --default "$GITHUB_TOKEN" git-extras.github-personal-access-token)
test -z "$github_personal_access_token" && abort "GITHUB_TOKEN or git config git-extras.github-personal-access-token required"
# branch
branch=${1-$(git symbolic-ref HEAD | sed 's/refs\/heads\///')}
remote=$(git config branch."$branch".remote)
if [ -z "$remote" ]; then
echo 'no upstream found, push to origin as default'
remote="origin"
fi
[ "$remote" = "." ] && abort "the upstream should be a remote branch."
# make sure it's pushed
git push "$remote" "$branch" || abort "failed to push $branch"
remote_url=$(git config remote."$remote".url)
if [[ "$remote_url" == git@* ]]; then
project=${remote_url##*:}
else
project=${remote_url#https://*/}
fi
project=${project%.git}
# prompt
echo
echo " create pull-request for $project '$branch'"
echo
printf " title: " && read -r title
printf " body: " && read -r body
printf " base [%s]: " "$(git_extra_default_branch)" && read -r base
printf " GitHub two-factor authentication code (leave blank if not set up): " && read -r mfa_code
echo
# create pull request
if [ -z "$base" ]
then
base="$(git_extra_default_branch)"
fi
body=$(json "$title" "$body" "$branch" "$base")
curl \
-X POST \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token $github_personal_access_token" \
-H "X-GitHub-OTP: $mfa_code" \
"https://api.github.com/repos/$project/pulls" -d "$body"
|