Skip to content

Instantly share code, notes, and snippets.

@sergiitk
Forked from ejona86/backport.sh
Last active April 16, 2025 16:25
Show Gist options
  • Save sergiitk/af606cbab72b00d8db8a53d21e766ee9 to your computer and use it in GitHub Desktop.
Save sergiitk/af606cbab72b00d8db8a53d21e766ee9 to your computer and use it in GitHub Desktop.

Revisions

  1. sergiitk revised this gist Apr 16, 2025. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion backport.sh
    Original file line number Diff line number Diff line change
    @@ -46,13 +46,19 @@ main() {

    readonly pr_metadata="$(curl -sSf -H "Accept: application/vnd.github.v3+json" "https://api.github.com/repos/grpc/${repo}/pulls/$pr")"
    readonly assignee="$(echo "$pr_metadata" | jq -r '.assignee.login')"
    readonly commits="$(echo "$pr_metadata" | jq -r '.merge_commit_sha')"
    # assignee=""
    if [[ -n "${COMMITS}" ]]; then
    readonly commits=${COMMITS}
    else
    readonly commits="$(echo "$pr_metadata" | jq -r '.merge_commit_sha')"
    fi
    readonly branch_id="$(echo "$pr_metadata" | jq -r '.head.ref')"
    readonly title="$(echo "$pr_metadata" | jq -r '.title')"
    readonly body="$(echo "$pr_metadata" | jq -r '.body')"

    cat <<EOT
    PR: https://github.com/grpc/${repo}/pull/$pr
    Title: ${title}
    Versions: ${minors}
    Commits: ${commits}
    Branch id: ${branch_id}
  2. sergiitk revised this gist Dec 16, 2023. No changes.
  3. sergiitk revised this gist May 31, 2023. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions backport.sh
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    #!/bin/bash
    #!/usr/bin/env bash
    # Copyright 2020 The gRPC Authors
    #
    # Licensed under the Apache License, Version 2.0 (the "License");
    @@ -88,7 +88,7 @@ EOT
    fi
    git checkout -b "${backport_branch}" "upstream/${version_tag}"
    pick_result=0
    git cherry-pick -m 1 $commits || pick_result=$?
    git cherry-pick --rerere-autoupdate -m 1 $commits || pick_result=$?
    if [[ $pick_result -ne 0 ]]; then
    read -n 1 -p "Conflict detected. Resolve manually in another shell. Resolved? (y/N) " answer
    if [ "$answer" != "${answer#[Yy]}" ] ;then
  4. sergiitk revised this gist Nov 9, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion backport.sh
    Original file line number Diff line number Diff line change
    @@ -134,4 +134,4 @@ EOT
    git checkout "${orig_branch}"
    }

    main "$@"
    main "$@"
  5. sergiitk revised this gist Nov 9, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion backport.sh
    Original file line number Diff line number Diff line change
    @@ -134,4 +134,4 @@ EOT
    git checkout "${orig_branch}"
    }

    main "$@"
    main "$@"
  6. sergiitk revised this gist Sep 7, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion backport.sh
    Original file line number Diff line number Diff line change
    @@ -107,7 +107,7 @@ EOT
    arg_title="${title} (${version_tag} backport)"
    arg_body=$(cat <<EOT
    Backport of #${pr} to ${version_tag}.
    ---
    ${body}
    EOT
    )
  7. sergiitk revised this gist Sep 7, 2022. 1 changed file with 9 additions and 3 deletions.
    12 changes: 9 additions & 3 deletions backport.sh
    Original file line number Diff line number Diff line change
    @@ -23,6 +23,7 @@ EXAMPLES:
    $0 grpc-go 5434 47 46 45
    $0 grpc 5434 47
    $0 grpc-java 5434 {47..38}
    $0 grpc-node 2208 5 6 7
    EOF
    exit 1
    }
    @@ -80,10 +81,14 @@ EOT

    for minor in $minors; do
    backport_branch="backport-1.$minor-${branch_id}"
    version_tag="v1.${minor}.x"
    if [[ "${repo}" == "grpc-node" ]]; then
    version_tag="@grpc/grpc-js@1.${minor}.x"
    else
    version_tag="v1.${minor}.x"
    fi
    git checkout -b "${backport_branch}" "upstream/${version_tag}"
    pick_result=0
    git cherry-pick $commits || pick_result=$?
    git cherry-pick -m 1 $commits || pick_result=$?
    if [[ $pick_result -ne 0 ]]; then
    read -n 1 -p "Conflict detected. Resolve manually in another shell. Resolved? (y/N) " answer
    if [ "$answer" != "${answer#[Yy]}" ] ;then
    @@ -107,7 +112,8 @@ ${body}
    EOT
    )
    # https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/using-query-parameters-to-create-a-pull-request
    url="https://github.com/grpc/${repo}/compare/${version_tag}...${owner}:${backport_branch}?quick_pull=1"
    url="https://github.com/grpc/${repo}/compare"
    url="${url}/$(urlencode "${version_tag}")...${owner}:${backport_branch}?quick_pull=1"
    url="${url}&title=$(urlencode "${arg_title}")"
    url="${url}&body=$(urlencode "${arg_body}")"
    if [[ "${assignee}" != "null" ]]; then
  8. sergiitk revised this gist Sep 7, 2022. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion backport.sh
    Original file line number Diff line number Diff line change
    @@ -38,6 +38,7 @@ main() {

    readonly repo="$1"
    readonly pr="$2"
    readonly labels="${LABELS:-}"
    shift 2
    readonly minors="$@"
    readonly owner="$(git remote get-url origin | sed -E "s#.*[:/]([a-zA-Z0-9-]+)/${repo}.*#\1#")"
    @@ -56,6 +57,7 @@ Commits: ${commits}
    Branch id: ${branch_id}
    Repo: ${owner}
    Assignee: ${assignee}
    Labels: ${labels}
    EOT

    read -n 1 -p "Continue? (y/N) " answer
    @@ -77,7 +79,7 @@ EOT
    local url

    for minor in $minors; do
    backport_branch="backport-${branch_id}-1.$minor"
    backport_branch="backport-1.$minor-${branch_id}"
    version_tag="v1.${minor}.x"
    git checkout -b "${backport_branch}" "upstream/${version_tag}"
    pick_result=0
    @@ -111,6 +113,9 @@ EOT
    if [[ "${assignee}" != "null" ]]; then
    url="${url}&assignees=${assignee}"
    fi
    if [[ -n "${labels}" ]]; then
    url="${url}&labels=$(urlencode "${labels}")"
    fi

    echo
    echo
  9. sergiitk revised this gist Sep 6, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion backport.sh
    Original file line number Diff line number Diff line change
    @@ -120,7 +120,7 @@ EOT
    echo
    echo
    done
    # git checkout "${orig_branch}"
    git checkout "${orig_branch}"
    }

    main "$@"
  10. sergiitk revised this gist Sep 6, 2022. 1 changed file with 29 additions and 11 deletions.
    40 changes: 29 additions & 11 deletions backport.sh
    Original file line number Diff line number Diff line change
    @@ -51,11 +51,11 @@ main() {

    cat <<EOT
    PR: https://github.com/grpc/${repo}/pull/$pr
    Versions: $minors
    Assignee: $assignee
    Commits: $commits
    Branch id: $branch_id
    Repo: $owner
    Versions: ${minors}
    Commits: ${commits}
    Branch id: ${branch_id}
    Repo: ${owner}
    Assignee: ${assignee}
    EOT

    read -n 1 -p "Continue? (y/N) " answer
    @@ -69,14 +69,17 @@ EOT

    git fetch upstream
    readonly orig_branch="$(git rev-parse --abbrev-ref HEAD)"
    #readonly title="$(git log -1 --pretty=format:%s "$commit")"
    #readonly body="$(git log -1 --pretty=format:%b "$commit")"
    local pick_result
    local backport_branch
    local arg_title
    local arg_body
    local version_tag
    local url

    for minor in $minors; do
    backport_branch="backport-${branch_id}-1.$minor"
    git checkout -b "${backport_branch}" "upstream/v1.$minor.x"
    version_tag="v1.${minor}.x"
    git checkout -b "${backport_branch}" "upstream/${version_tag}"
    pick_result=0
    git cherry-pick $commits || pick_result=$?
    if [[ $pick_result -ne 0 ]]; then
    @@ -92,17 +95,32 @@ EOT
    exit 1
    fi
    fi

    git push --set-upstream origin "${backport_branch}"

    arg_title="${title} (${version_tag} backport)"
    arg_body=$(cat <<EOT
    Backport of #${pr} to ${version_tag}.
    ${body}
    EOT
    )
    # https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/using-query-parameters-to-create-a-pull-request
    url="https://github.com/grpc/${repo}/compare/${version_tag}...${owner}:${backport_branch}?quick_pull=1"
    url="${url}&title=$(urlencode "${arg_title}")"
    url="${url}&body=$(urlencode "${arg_body}")"
    if [[ "${assignee}" != "null" ]]; then
    url="${url}&assignees=${assignee}"
    fi

    echo
    echo
    echo ------------------------------------------------------
    echo "https://github.com/grpc/${repo}/compare/v1.$minor.x...$owner:${backport_branch}?expand=1&assignees=$assignee&title=$(urlencode "$title")%20%281.$minor.x%20backport%29&body=Backport%20of%20%23${pr}%0A%0A$(urlencode "$body")"
    echo "${url}"
    echo ------------------------------------------------------
    echo
    echo
    done
    git checkout "${orig_branch}"
    # git checkout "${orig_branch}"
    }

    main "$@"
  11. sergiitk revised this gist Aug 9, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion backport.sh
    Original file line number Diff line number Diff line change
    @@ -97,7 +97,7 @@ EOT
    echo
    echo
    echo ------------------------------------------------------
    echo "https://github.com/grpc/${repo}/compare/v1.$minor.x...$owner:${backport_branch}?expand=1&assignees=$assignee&title=$(urlencode "$title")%20%281.$minor.x%20backport%29&body=$(urlencode "$body")%0A%0ABackport%20of%20%23$pr"
    echo "https://github.com/grpc/${repo}/compare/v1.$minor.x...$owner:${backport_branch}?expand=1&assignees=$assignee&title=$(urlencode "$title")%20%281.$minor.x%20backport%29&body=Backport%20of%20%23${pr}%0A%0A$(urlencode "$body")"
    echo ------------------------------------------------------
    echo
    echo
  12. sergiitk revised this gist Jul 12, 2022. No changes.
  13. sergiitk revised this gist Jun 17, 2022. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions backport.sh
    Original file line number Diff line number Diff line change
    @@ -27,6 +27,10 @@ EOF
    exit 1
    }

    urlencode() {
    python3 -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))" "$*"
    }

    main() {
    if (( $# < 3 )); then
    display_usage
    @@ -63,10 +67,6 @@ EOT
    exit 1
    fi

    urlencode() {
    python3 -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))" "$*"
    }

    git fetch upstream
    readonly orig_branch="$(git rev-parse --abbrev-ref HEAD)"
    #readonly title="$(git log -1 --pretty=format:%s "$commit")"
  14. sergiitk revised this gist Jun 17, 2022. 1 changed file with 76 additions and 42 deletions.
    118 changes: 76 additions & 42 deletions backport.sh
    Original file line number Diff line number Diff line change
    @@ -15,25 +15,37 @@

    set -e

    if (( $# < 3 )); then
    echo "Usage: $0 REPO PR MINOR_VERSIONS"
    display_usage() {
    cat <<EOF >/dev/stderr
    Usage: $0 REPO PR MINOR_VERSIONS
    EXAMPLES:
    $0 grpc-go 5434 47 46 45
    $0 grpc 5434 47
    $0 grpc-java 5434 {47..38}
    EOF
    exit 1
    fi
    }

    readonly repo="$1"
    readonly pr="$2"
    shift 2
    readonly minors="$@"
    readonly owner="$(git remote get-url origin | sed -E "s#.*[:/]([a-zA-Z0-9-]+)/${repo}.*#\1#")"
    main() {
    if (( $# < 3 )); then
    display_usage
    fi

    readonly pr_metadata="$(curl -sSf -H "Accept: application/vnd.github.v3+json" "https://api.github.com/repos/grpc/${repo}/pulls/$pr")"
    readonly assignee="$(echo "$pr_metadata" | jq -r '.assignee.login')"
    readonly commits="$(echo "$pr_metadata" | jq -r '.merge_commit_sha')"
    readonly branch_id="$(echo "$pr_metadata" | jq -r '.head.ref')"
    readonly title="$(echo "$pr_metadata" | jq -r '.title')"
    readonly body="$(echo "$pr_metadata" | jq -r '.body')"
    readonly repo="$1"
    readonly pr="$2"
    shift 2
    readonly minors="$@"
    readonly owner="$(git remote get-url origin | sed -E "s#.*[:/]([a-zA-Z0-9-]+)/${repo}.*#\1#")"

    cat <<EOT
    readonly pr_metadata="$(curl -sSf -H "Accept: application/vnd.github.v3+json" "https://api.github.com/repos/grpc/${repo}/pulls/$pr")"
    readonly assignee="$(echo "$pr_metadata" | jq -r '.assignee.login')"
    readonly commits="$(echo "$pr_metadata" | jq -r '.merge_commit_sha')"
    readonly branch_id="$(echo "$pr_metadata" | jq -r '.head.ref')"
    readonly title="$(echo "$pr_metadata" | jq -r '.title')"
    readonly body="$(echo "$pr_metadata" | jq -r '.body')"

    cat <<EOT
    PR: https://github.com/grpc/${repo}/pull/$pr
    Versions: $minors
    Assignee: $assignee
    @@ -42,33 +54,55 @@ Branch id: $branch_id
    Repo: $owner
    EOT

    read -n 1 -p "Continue? (y/N) " answer
    if [ "$answer" != "${answer#[Yy]}" ] ;then
    echo
    else
    echo
    echo "Exit"
    exit 1
    fi
    read -n 1 -p "Continue? (y/N) " answer
    if [ "$answer" != "${answer#[Yy]}" ] ;then
    echo
    else
    echo
    echo "Exit"
    exit 1
    fi

    urlencode() {
    python3 -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))" "$*"
    }

    git fetch upstream
    readonly orig_branch="$(git rev-parse --abbrev-ref HEAD)"
    #readonly title="$(git log -1 --pretty=format:%s "$commit")"
    #readonly body="$(git log -1 --pretty=format:%b "$commit")"
    local pick_result
    local backport_branch

    for minor in $minors; do
    backport_branch="backport-${branch_id}-1.$minor"
    git checkout -b "${backport_branch}" "upstream/v1.$minor.x"
    pick_result=0
    git cherry-pick $commits || pick_result=$?
    if [[ $pick_result -ne 0 ]]; then
    read -n 1 -p "Conflict detected. Resolve manually in another shell. Resolved? (y/N) " answer
    if [ "$answer" != "${answer#[Yy]}" ] ;then
    echo
    echo "Continuing..."
    else
    echo
    echo "Aborting. Cleaning up..."
    git checkout -f "${orig_branch}"
    git branch -D "${backport_branch}"
    exit 1
    fi
    fi

    urlencode() {
    python3 -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))" "$*"
    git push --set-upstream origin "${backport_branch}"
    echo
    echo
    echo ------------------------------------------------------
    echo "https://github.com/grpc/${repo}/compare/v1.$minor.x...$owner:${backport_branch}?expand=1&assignees=$assignee&title=$(urlencode "$title")%20%281.$minor.x%20backport%29&body=$(urlencode "$body")%0A%0ABackport%20of%20%23$pr"
    echo ------------------------------------------------------
    echo
    echo
    done
    git checkout "${orig_branch}"
    }

    git fetch upstream
    readonly orig_branch="$(git rev-parse --abbrev-ref HEAD)"
    #readonly title="$(git log -1 --pretty=format:%s "$commit")"
    #readonly body="$(git log -1 --pretty=format:%b "$commit")"
    for minor in $minors; do
    git checkout -b "backport-${branch_id}-1.$minor" "upstream/v1.$minor.x"
    git cherry-pick $commits
    git push --set-upstream origin "backport-${branch_id}-1.$minor"
    echo
    echo
    echo ------------------------------------------------------
    echo "https://github.com/grpc/${repo}/compare/v1.$minor.x...$owner:backport-${branch_id}-1.$minor?expand=1&assignees=$assignee&title=$(urlencode "$title")%20%281.$minor.x%20backport%29&body=$(urlencode "$body")%0A%0ABackport%20of%20%23$pr"
    echo ------------------------------------------------------
    echo
    echo
    done
    git checkout "$orig_branch"
    main "$@"
  15. sergiitk revised this gist Jun 17, 2022. 1 changed file with 20 additions and 11 deletions.
    31 changes: 20 additions & 11 deletions backport.sh
    Original file line number Diff line number Diff line change
    @@ -15,32 +15,41 @@

    set -e

    if [[ $# < 2 ]]; then
    echo "Usage: $0 PR MINOR_VERSIONS"
    if (( $# < 3 )); then
    echo "Usage: $0 REPO PR MINOR_VERSIONS"
    exit 1
    fi

    readonly pr="$1"
    readonly minors="$2"
    readonly repo="$(git remote get-url origin | sed -E 's#.*[:/]([a-zA-Z0-9-]+)/grpc-java.*#\1#')"
    readonly repo="$1"
    readonly pr="$2"
    shift 2
    readonly minors="$@"
    readonly owner="$(git remote get-url origin | sed -E "s#.*[:/]([a-zA-Z0-9-]+)/${repo}.*#\1#")"

    readonly pr_metadata="$(curl -sSf -H "Accept: application/vnd.github.v3+json" "https://api.github.com/repos/grpc/grpc-java/pulls/$pr")"
    readonly pr_metadata="$(curl -sSf -H "Accept: application/vnd.github.v3+json" "https://api.github.com/repos/grpc/${repo}/pulls/$pr")"
    readonly assignee="$(echo "$pr_metadata" | jq -r '.assignee.login')"
    readonly commits="$(echo "$pr_metadata" | jq -r '.merge_commit_sha')"
    readonly branch_id="$(echo "$pr_metadata" | jq -r '.head.ref')"
    readonly title="$(echo "$pr_metadata" | jq -r '.title')"
    readonly body="$(echo "$pr_metadata" | jq -r '.body')"

    cat <<EOT
    PR: https://github.com/grpc/grpc-java/pull/$pr
    PR: https://github.com/grpc/${repo}/pull/$pr
    Versions: $minors
    Assignee: $assignee
    Commits: $commits
    Branch id: $branch_id
    Repo: $repo
    Repo: $owner
    EOT

    read -p "Continue? "
    read -n 1 -p "Continue? (y/N) " answer
    if [ "$answer" != "${answer#[Yy]}" ] ;then
    echo
    else
    echo
    echo "Exit"
    exit 1
    fi

    urlencode() {
    python3 -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))" "$*"
    @@ -57,9 +66,9 @@ for minor in $minors; do
    echo
    echo
    echo ------------------------------------------------------
    echo "https://github.com/grpc/grpc-java/compare/v1.$minor.x...$repo:backport-${branch_id}-1.$minor?expand=1&assignees=$assignee&title=$(urlencode "$title")%20%281.$minor.x%20backport%29&body=$(urlencode "$body")%0A%0ABackport%20of%20%23$pr"
    echo "https://github.com/grpc/${repo}/compare/v1.$minor.x...$owner:backport-${branch_id}-1.$minor?expand=1&assignees=$assignee&title=$(urlencode "$title")%20%281.$minor.x%20backport%29&body=$(urlencode "$body")%0A%0ABackport%20of%20%23$pr"
    echo ------------------------------------------------------
    echo
    echo
    done
    git checkout "$orig_branch"
    git checkout "$orig_branch"
  16. @ejona86 ejona86 revised this gist May 12, 2021. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion backport.sh
    Original file line number Diff line number Diff line change
    @@ -43,7 +43,7 @@ EOT
    read -p "Continue? "

    urlencode() {
    python -c "import urllib, sys; print urllib.quote(sys.argv[1])" "$*"
    python3 -c "import urllib.parse, sys; print(urllib.parse.quote(sys.argv[1]))" "$*"
    }

    git fetch upstream
  17. @ejona86 ejona86 created this gist Dec 7, 2020.
    65 changes: 65 additions & 0 deletions backport.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    #!/bin/bash
    # Copyright 2020 The gRPC Authors
    #
    # Licensed under the Apache License, Version 2.0 (the "License");
    # you may not use this file except in compliance with the License.
    # You may obtain a copy of the License at
    #
    # http://www.apache.org/licenses/LICENSE-2.0
    #
    # Unless required by applicable law or agreed to in writing, software
    # distributed under the License is distributed on an "AS IS" BASIS,
    # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    # See the License for the specific language governing permissions and
    # limitations under the License.

    set -e

    if [[ $# < 2 ]]; then
    echo "Usage: $0 PR MINOR_VERSIONS"
    exit 1
    fi

    readonly pr="$1"
    readonly minors="$2"
    readonly repo="$(git remote get-url origin | sed -E 's#.*[:/]([a-zA-Z0-9-]+)/grpc-java.*#\1#')"

    readonly pr_metadata="$(curl -sSf -H "Accept: application/vnd.github.v3+json" "https://api.github.com/repos/grpc/grpc-java/pulls/$pr")"
    readonly assignee="$(echo "$pr_metadata" | jq -r '.assignee.login')"
    readonly commits="$(echo "$pr_metadata" | jq -r '.merge_commit_sha')"
    readonly branch_id="$(echo "$pr_metadata" | jq -r '.head.ref')"
    readonly title="$(echo "$pr_metadata" | jq -r '.title')"
    readonly body="$(echo "$pr_metadata" | jq -r '.body')"

    cat <<EOT
    PR: https://github.com/grpc/grpc-java/pull/$pr
    Versions: $minors
    Assignee: $assignee
    Commits: $commits
    Branch id: $branch_id
    Repo: $repo
    EOT

    read -p "Continue? "

    urlencode() {
    python -c "import urllib, sys; print urllib.quote(sys.argv[1])" "$*"
    }

    git fetch upstream
    readonly orig_branch="$(git rev-parse --abbrev-ref HEAD)"
    #readonly title="$(git log -1 --pretty=format:%s "$commit")"
    #readonly body="$(git log -1 --pretty=format:%b "$commit")"
    for minor in $minors; do
    git checkout -b "backport-${branch_id}-1.$minor" "upstream/v1.$minor.x"
    git cherry-pick $commits
    git push --set-upstream origin "backport-${branch_id}-1.$minor"
    echo
    echo
    echo ------------------------------------------------------
    echo "https://github.com/grpc/grpc-java/compare/v1.$minor.x...$repo:backport-${branch_id}-1.$minor?expand=1&assignees=$assignee&title=$(urlencode "$title")%20%281.$minor.x%20backport%29&body=$(urlencode "$body")%0A%0ABackport%20of%20%23$pr"
    echo ------------------------------------------------------
    echo
    echo
    done
    git checkout "$orig_branch"