Skip to content

Instantly share code, notes, and snippets.

@gianm
Created November 10, 2017 16:30
Show Gist options
  • Save gianm/68145b09a55e6f6460a11ae9adfc25a4 to your computer and use it in GitHub Desktop.
Save gianm/68145b09a55e6f6460a11ae9adfc25a4 to your computer and use it in GitHub Desktop.
#!/bin/bash -eux
# Note: assumes "base" and "milestone" are the same (for the backport PR)
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