patch_upstream_branch_name=$1 patch_stack_upstream_branch_name_relative_to_remote=$2 patch_stack_upstream_remote_name=$3 patch_stack_upstream_remote_url=$4
if ! glab --version >/dev/null 2>&1; then
echo "glab not found, please make sure it is installed and available on $PATH"
exit 1
fi
if ! jq --version >/dev/null 2>&1; then
echo "jq not found, please make sure it is installed and available on $PATH"
exit 1
fi
if ! glab auth status >/dev/null 2>&1; then
echo "glab is not logged in, please check the output of \"glab auth status\" and resolve"
exit 1
fi
state_json=$(glab mr -R "$patch_stack_upstream_remote_url" view $patch_upstream_branch_name -F json 2>/dev/null)
if [ $? -eq 0 ]; then
state=$(echo "$state_json" | jq -r .state)
if [ "$state" = "opened" ]; then
echo "Open PR already exists, updating description"
action="update_description"
else
echo "PR found but it isn't open, creating a new one"
action="create"
fi
else
echo "PR not found, creating a new one"
action="create"
fi
just_head="$patch_upstream_branch_name^..$patch_upstream_branch_name"
new_title=$(git log "$just_head" --pretty=format:%s)
new_description=$(git log "$just_head" --pretty=format:%b)
if [ $action = "create" ]; then
glab mr create \
--title "$new_title" \
--description "$new_description" \
--head "$patch_stack_upstream_remote_url" \
--source-branch "$patch_upstream_branch_name" \
--target-branch "$patch_stack_upstream_branch_name_relative_to_remote" \
--remove-source-branch
elif [ $action = "update_description" ]; then
glab mr update "$patch_upstream_branch_name" --title "$new_title" --description "$new_description"
fi