Skip to content

Instantly share code, notes, and snippets.

@gianm
Created May 8, 2018 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gianm/15eef56888f5eed88c4f142c930f8dc2 to your computer and use it in GitHub Desktop.
Save gianm/15eef56888f5eed88c4f142c930f8dc2 to your computer and use it in GitHub Desktop.
#!/bin/bash -eux
#
# Backports patches from master to a release branch.
#
# Usage:
# export GIT_TOKEN=...
# druid-backport [branch] [pr-number], like: "druid-backport 0.12.1 5706"
#
# Bugs:
# - Attempts to set "Backport" label and milestone when creating the PR, but this doesn't work.
# So you have to do it manually.
# - "myrepo" is not configurable, edit it here in this script.
base="$1"
pr="$2"
myrepo="gianm"
backport="backport-$pr-to-$base"
if [ "$(curl -u "$myrepo:$GIT_TOKEN" -s https://api.github.com/repos/druid-io/druid/pulls/"$pr" |jq -r '.merged')" != "true" ]
then
echo "PR $2 not yet merged" >&2
exit 1
fi
milestone="$(curl -u "$myrepo:$GIT_TOKEN" -s https://api.github.com/repos/druid-io/druid/milestones | jq '.[]|select(.title == "$base")|.number')"
title="$(curl -u "$myrepo:$GIT_TOKEN" -s https://api.github.com/repos/druid-io/druid/pulls/"$pr" |jq '.title'|perl -pe's/^"/"[Backport] /')"
commit="$(git log --pretty=oneline master |perl -nle '/^(\w+)\s.+\(#'"$pr"'\)$/ && print $1' | perl -pe'$x++; END { die unless $x == 1 }')"
git checkout "$base"
git pull
if git rev-list "$base" | fgrep -q "$commit"
then
exit 0
fi
git branch -D "$backport" ||: >/dev/null 2>&1
git checkout -b "$backport"
git cherry-pick "$commit"
git push --set-upstream "$myrepo" "$backport"
if [ -n "$GIT_TOKEN" ]
then
curl -u "$myrepo:$GIT_TOKEN" -XPOST -d@- https://api.github.com/repos/druid-io/druid/pulls <<EOF
{
"title" : $title,
"head" : "$myrepo:$backport",
"base" : "$base",
"body" : "Backport of #$pr to $base.",
"milestone" : "$milestone",
"labels" : ["Backport"]
}
EOF
else
echo "GitHub personal token not provided, not submitting pull request" >&2
exit 1
fi
@b-slim
Copy link

b-slim commented May 8, 2018

@gianm thanks i had to add a username var, i guess it works for you since your remote_name == user_name, which is not my case where i use origin. Small fix i added myusername=b-slim
then

{
  "title"     : $title,
  "head"      : "$myusername:$backport",
  "base"      : "$base",
  "body"      : "Backport of #$pr to $base.",
  "milestone" : "$milestone",
  "labels"    : ["Backport"]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment