Skip to content

Commit

Permalink
Add script to auto-update badger (#5449)
Browse files Browse the repository at this point in the history
Fixes DGRAPH-1345

This PR adds a script that can be used to automate badger updates
in dgraph. The script will be used to automatically update badger
in dgraph. The script is intended to be run on CI (team city) and
automatically create PR for the update.

The script performs the following operations
1. Clone Dgraph
2. Run `go get -v github.com/dgraph-io/badger/v2@master`
3. If no change, exit
4. Create a new commit with the changes to go mod and go.sum
5. Commit and result and push it to a branch `update-badger-$(date)`
6. Create a Pull Request on Dgraph repo with the new branch.

**The newly created pull request has to be merged manually**.
The script needs the following environment variables set
- `RELEASE_BRANCHES` => This are the branches on which badger should
    be updated. For ex: `RELEASE_BRANCHES=("master" "release/v1.2" "release/v20.03")`
- `GH_TOKEN` => Used to push changes to github and create the pull request
- `GH_USERNAME` => Used to push the branch to dgraph repo
- `GIT_EMAIL` => The email id used in the commit (author email id)
- `GIT_NAME` => The name used in the commit (author name)
  • Loading branch information
Ibrahim Jarif committed Jun 3, 2020
1 parent 4ea149a commit a2dd9ff
Showing 1 changed file with 92 additions and 0 deletions.
92 changes: 92 additions & 0 deletions contrib/update-badger.sh
@@ -0,0 +1,92 @@
#!/bin/bash

set -eo pipefail

repo="dgraph-io/dgraph"


[ -z "$RELEASE_BRANCHES" ] && echo "Please set RELEASE_BRANCHES" && exit 0
[ -z "$GIT_EMAIL" ] && echo "Please set GIT_EMAIL" && exit 0
[ -z "$GIT_NAME" ] && echo "Please set GIT_NAME" && exit 0
[ -z "$GH_USERNAME" ] && echo "Please set GH_USERNAME" && exit 0
[ -z "$GH_TOKEN" ] && echo "Please set GH_TOKEN" && exit 0

if [[ ${RELEASE_BRANCHES[@]} == *","* ]]; then
echo "Release branches should not contain commas. Set it as RELEASE_BRANCHES=('x' 'y')"
fi


TMP="/tmp/badger-update"
rm -Rf $TMP
mkdir $TMP

cd $TMP
git clone https://github.com/$repo

cd dgraph

git config user.name "$GIT_NAME"
git config user.email "$GIT_EMAIL"

for base in "${RELEASE_BRANCHES[@]}"
do
# Ensure directory is clean before updating badger
if [[ $(git diff --stat) != '' ]]; then
echo 'Working directory dirty. Following changes were found'
git --no-pager diff
echo 'Exiting'
exit 0
fi

git fetch origin $base
git --no-pager branch
git checkout origin/$base

echo "Preparing for base branch $base"
branch="$GH_USERNAME/$base-update-$(date +'%m/%d/%Y')"

echo "Creating new branch $branch"
git checkout -b $branch

echo "Updating badger to master branch version"
go get -v github.com/dgraph-io/badger/v2@master

go mod tidy

if [[ $(git diff --stat) == '' ]]; then
echo 'No changes found.'
echo 'Exiting'
exit 0
fi

echo "Ready to commit following changes"
git --no-pager diff

git add go.mod go.sum

message="$base: Update badger $(date +'%m/%d/%Y')"
git commit -m "$message"

# Set authentication credentials to allow "git push"
git remote set-url origin https://${GH_USERNAME}:${GH_TOKEN}@github.com/$repo.git
git push origin $branch
echo "Done"

# Create PR
apiURL="https://api.github.com/repos/$repo/pulls"

echo "Creating PR"
body="{
\"title\": \"${message}\",
\"head\": \"${branch}\",
\"base\": \"${base}\"
}"

PR_id=$(curl --silent -X POST -H "Authorization: Bearer $GH_TOKEN" -d "${body}" "${apiURL}" \
| sed -n 's/.*"number": \(.*\),/\1/p' )

[[ -z $PR_id ]] && echo "Failed to create PR" && exit 0

echo "Created PR https://github.com/$repo/pull/${PR_id}"
echo "DONE"
done

0 comments on commit a2dd9ff

Please sign in to comment.