Skip to content

GODRIVER-2978 Automate syncing code between Go driver v1 and v2 #1391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/sync.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Sync
on:
push:
branches:
- v1

jobs:
sync-branches:
runs-on: ubuntu-latest
name: Syncing branches
permissions:
pull-requests: write
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Create a sync PR
run: |
git config --global github.user $GITHUB_ACTOR
git config user.email [email protected]
git config user.name $GITHUB_ACTOR
export AUTH_TOKEN=${{secrets.GITHUB_TOKEN}}
sha=$(git rev-parse HEAD)
bash ./etc/cherry-picker.sh $sha
18 changes: 18 additions & 0 deletions docs/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@ If any tests do not pass, or relevant tests are not included, the patch will not

If you are working on a bug or feature listed in Jira, please include the ticket number prefixed with GODRIVER in the commit message and GitHub pull request title, (e.g. GODRIVER-123). For the patch commit message itself, please follow the [How to Write a Git Commit Message](https://chris.beams.io/posts/git-commit/) guide.

### Cherry-picking between branches

You must first install the `gh` cli (`brew install gh`), then set your GitHub username:

```bash
git config --global github.user <github_handle>
```

If a Pull Request needs to be cherry-picked to a new branch, get the sha of the commit in the base branch, and then run

```bash
bash etc/cherry-picker.sh <sha>
```

The cherry-picker script is configured to use `v1` as the base branch and `master` as the target branch.
It will create a new checkout in a temp dir, create a new branch, perform the cherry-pick, and then
prompt before creating a PR to the target branch.

## Testing / Development

The driver tests can be run against several database configurations. The most simple configuration is a standalone mongod with no auth, no ssl, and no compression. To run these basic driver tests, make sure a standalone MongoDB server instance is running at localhost:27017. To run the tests, you can run `make` (on Windows, run `nmake`). This will run coverage, run go-lint, run go-vet, and build the examples.
Expand Down
67 changes: 67 additions & 0 deletions etc/cherry-picker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/bin/bash
set -e

sha=$1
base=v1
target=master
dirname=$(mktemp -d)
user=$(git config github.user)

if [ -z "$user" ]; then
echo "Please set GitHub User"
echo "git config --global github.user <github_handle>"
exit 1
fi

mkdir -p $dirname
if [ -z $AUTH_TOKEN ]; then
git clone [email protected]:mongodb/mongo-go-driver.git $dirname
else
echo "$AUTH_TOKEN" > mytoken.txt
gh auth login --with-token < mytoken.txt
git clone https://github.com/mongodb/mongo-go-driver.git $dirname
fi

cd $dirname
if [ -z $AUTH_TOKEN ]; then
git remote add $user [email protected]:$user/mongo-go-driver.git
else
git remote add $user https://$user:${AUTH_TOKEN}@github.com/$user/mongo-go-driver.git
fi

gh repo set-default mongodb/mongo-go-driver
branch="cherry-pick-$sha"
head="$user:$branch"
git fetch origin $base
git fetch origin $target
git checkout -b $branch origin/$target
git cherry-pick -x $sha

old_title=$(git --no-pager log -1 --pretty=%B | head -n 1)
ticket=$(echo $old_title | sed -r 's/([A-Z]+-[0-9]+).*/\1/')
text=$(echo $old_title | sed -r 's/([A-Z]+-[0-9]+) (.*) \(#[0-9]*\)/\2/')
pr_number=$(echo $old_title| sed -r 's/.*(#[0-9]*)\)/\1/')

title="$ticket [$target] $text"
body="Cherry-pick of $pr_number from $base to $target"

echo
echo "Creating PR..."
echo "Title: $title"
echo "Body: $body"
echo "Base: $target"
echo "Head: $head"
echo

if [ -n $GITHUB_ACTOR ]; then
choice=Y
else
read -p 'Push changes? (Y/n) ' choice
fi

if [[ "$choice" == "Y" || "$choice" == "y" || -z "$choice" ]]; then
if [ -n $user ]; then
git push $user
fi
gh pr create --title "$title" --base $target --head $head --body "$body"
fi