Add GitHub Notifications for cherry picks (#8115)

* add github PR notification for backport cherry picks

* count number of backport failures and exit after looping through all of them
This commit is contained in:
Alvin Huang 2020-06-16 14:18:53 -04:00 committed by GitHub
parent ffc948383b
commit 6a721418c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 42 additions and 5 deletions

View File

@ -23,6 +23,10 @@ function cherry_pick_with_slack_notification {
# $1 - branch to cherry-pick to # $1 - branch to cherry-pick to
# $2 - commit to cherry-pick # $2 - commit to cherry-pick
# $3 - url to PR of commit # $3 - url to PR of commit
#
# Return:
# 0 for success
# 1 for error
local branch="$1" local branch="$1"
local commit="$2" local commit="$2"
@ -32,6 +36,8 @@ function cherry_pick_with_slack_notification {
# If git cherry-pick fails, we send a failure notification # If git cherry-pick fails, we send a failure notification
if ! git cherry-pick --mainline 1 "$commit"; then if ! git cherry-pick --mainline 1 "$commit"; then
status "🍒❌ Cherry pick of commit ${commit:0:7} from $pr_url onto $branch failed!" status "🍒❌ Cherry pick of commit ${commit:0:7} from $pr_url onto $branch failed!"
# send slack notification
curl -X POST -H 'Content-type: application/json' \ curl -X POST -H 'Content-type: application/json' \
--data \ --data \
"{ \ "{ \
@ -45,11 +51,22 @@ function cherry_pick_with_slack_notification {
} \ } \
] \ ] \
}" "${CONSUL_SLACK_WEBHOOK_URL}" }" "${CONSUL_SLACK_WEBHOOK_URL}"
# post PR comment to GitHub
github_message=":cherries::x: Cherry pick of commit ${commit} onto \`$branch\` failed! [Build Log]($CIRCLE_BUILD_URL)"
pr_id=$(basename ${pr_url})
curl -f -s -H "Authorization: token ${GITHUB_TOKEN}" \
-X POST \
-d "{ \"body\": \"${github_message}\"}" \
"https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/issues/${pr_id}/comments"
# run git status to leave error in CircleCI log
git status git status
exit 1 return 1
# Else we send a success notification # Else we send a success notification
else else
status "🍒✅ Cherry picking of PR commit ${commit:0:7} from $pr_url succeeded!" status "🍒✅ Cherry picking of PR commit ${commit:0:7} from ${pr_url} succeeded!"
# push changes to the specified branch # push changes to the specified branch
git push origin "$branch" git push origin "$branch"
curl -X POST -H 'Content-type: application/json' \ curl -X POST -H 'Content-type: application/json' \
@ -58,18 +75,28 @@ function cherry_pick_with_slack_notification {
\"attachments\": [ \ \"attachments\": [ \
{ \ { \
\"fallback\": \"Cherry pick succeeded!\", \ \"fallback\": \"Cherry pick succeeded!\", \
\"text\": \"🍒✅ Cherry picking of <$pr_url|${commit:0:7}> to \`$branch\` succeeded!\n\nBuild Log: ${CIRCLE_BUILD_URL}\", \ \"text\": \"🍒✅ Cherry picking of <$pr_url|${commit:0:7}> to \`$branch\` succeeded!\", \
\"footer\": \"${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}\", \ \"footer\": \"${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}\", \
\"ts\": \"$(date +%s)\", \ \"ts\": \"$(date +%s)\", \
\"color\": \"good\" \ \"color\": \"good\" \
} \ } \
] \ ] \
}" "${CONSUL_SLACK_WEBHOOK_URL}" }" "${CONSUL_SLACK_WEBHOOK_URL}"
# post PR comment to GitHub
github_message=":cherries::white_check_mark: Cherry pick of commit ${commit} onto \`$branch\` succeeded!"
pr_id=$(basename ${pr_url})
curl -f -s -H "Authorization: token ${GITHUB_TOKEN}" \
-X POST \
-d "{ \"body\": \"${github_message}\"}" \
"https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/issues/${pr_id}/comments"
fi fi
return 0
} }
# search for the PR labels applicable to the specified commit # search for the PR labels applicable to the specified commit
resp=$(curl -f -s -H "Authorization: token $GITHUB_TOKEN" "https://api.github.com/search/issues?q=repo:$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME+sha:$CIRCLE_SHA1") resp=$(curl -f -s -H "Authorization: token ${GITHUB_TOKEN}" "https://api.github.com/search/issues?q=repo:${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}+sha:${CIRCLE_SHA1}")
ret="$?" ret="$?"
if [[ "$ret" -ne 0 ]]; then if [[ "$ret" -ne 0 ]]; then
status "The GitHub API returned $ret which means it was probably rate limited." status "The GitHub API returned $ret which means it was probably rate limited."
@ -85,7 +112,9 @@ fi
# If the API returned a non-zero count, we have found a PR with that commit so we find # If the API returned a non-zero count, we have found a PR with that commit so we find
# the labels from the PR # the labels from the PR
labels=$(echo "$resp" | jq --raw-output '.items[].labels[] | .name')
# sorts the labels from a PR via version sort
labels=$(echo "$resp" | jq --raw-output '.items[].labels[] | .name' | sort -rV)
ret="$?" ret="$?"
pr_url=$(echo "$resp" | jq --raw-output '.items[].pull_request.html_url') pr_url=$(echo "$resp" | jq --raw-output '.items[].pull_request.html_url')
if [[ "$ret" -ne 0 ]]; then if [[ "$ret" -ne 0 ]]; then
@ -95,6 +124,7 @@ if [[ "$ret" -ne 0 ]]; then
exit 0 exit 0
fi fi
backport_failures=0
# loop through all labels on the PR # loop through all labels on the PR
for label in $labels; do for label in $labels; do
git config --local user.email "hashicorp-ci@users.noreply.github.com" git config --local user.email "hashicorp-ci@users.noreply.github.com"
@ -105,10 +135,17 @@ for label in $labels; do
status "backporting to stable-website" status "backporting to stable-website"
branch="stable-website" branch="stable-website"
cherry_pick_with_slack_notification "$branch" "$CIRCLE_SHA1" "$pr_url" cherry_pick_with_slack_notification "$branch" "$CIRCLE_SHA1" "$pr_url"
(( backport_failures += "$?" ))
# else if the label matches backport/*, it will attempt to cherry-pick to the release branch # else if the label matches backport/*, it will attempt to cherry-pick to the release branch
elif [[ $label =~ backport/* ]]; then elif [[ $label =~ backport/* ]]; then
status "backporting to $label" status "backporting to $label"
branch="${label/backport/release}.x" branch="${label/backport/release}.x"
cherry_pick_with_slack_notification "$branch" "$CIRCLE_SHA1" "$pr_url" cherry_pick_with_slack_notification "$branch" "$CIRCLE_SHA1" "$pr_url"
(( backport_failures += "$?" ))
fi fi
done done
if [ "$backport_failures" -ne 0 ]; then
echo "$backport_failures backports failed"
exit $backport_failures
fi