Created
May 8, 2018 17:57
-
-
Save gianm/15eef56888f5eed88c4f142c930f8dc2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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 addedmyusername=b-slim
then