Gerrit as a review tool, not as the repository of record

8.4k views Asked by At

I've spun up an instance of gerrit from the quick start guide.

My company uses BitBucket with pull requests and occasionally crucible for code reviews. We use Jenkins with an extensive build/deploy pipeline tied to BitBucket.

We'd like to integrate gerrit for code review because of its ability to stage commits and build/verify them before the review is accepted.

Basically, I want gerrit's "submit" button to push upstream to BitBucket. (I'd rather not deal with trying to do this by replication; I want to maintain BitBucket as upstream/repository of record because of corporate inertia.)

Does anyone have any suggestions for how to accomplish this? Does the capability exist, or is this a novel idea?

1

There are 1 answers

2
helmbert On

Gerrit implements it's code review functionalities by providing a (more-or-less) thin wrapper around an actual Git repository that is hosted within Gerrit itself. To my knowledge, there is no possibility to integrate an external Git repository directly in Gerrit.

This means that when using Gerrit, the Git repository needs to be hosted within Gerrit itself. In consequence of that, you will need to maintain a full copy of your BitBucket repository within your Gerrit instance. So this question basically boils down to keeping two Git repositories in sync.

Synchronizing new commits from BitBucket to Gerrit

As you're already using Jenkins, I'd recommend a Jenkins build to update your Gerrit repository whenever new commits are pushed to the BitBucket repository. For this, you'll need:

  1. A Gerrit user with direct push capabilities. For this, you'll need to grant Push privileges on the refs/heads/* ref in your Gerrit project. This user will be used by Jenkins. Be careful not to grant this privilege to any end-users, or they'll be able to bypass code review by pushing directly.
  2. A Jenkins job configured to build whenever new commits are pushed to your BitBucket repository. Within that job, simply push all branches to your Gerrit instance.
  3. Configure a BitBucket service hook to trigger your Jenkins build (for this, your Jenkins instance will need to be publicly accessible; otherwise simply set the schedule of your Jenkins job to a short interval to minimize the delay in synchronization).

Synchronizing new commits from Gerrit to BitBucket

When submitting code reviews in Gerrit, the new commits will need to be pushed back to BitBucket. Usually, I'd recommend using the replication plugin for that. Here's how a respective configuration file might look like (goes in etc/replication.config in your Gerrit directory):

[remote "bitbucket"]
    url = ssh://[email protected]/<your-user>/${name}.git
    push = +refs/tags/*:refs/tags/*
    push = +refs/heads/*:refs/heads/*
    mirror = true
    replicateOnStartup = true
    replicatePermissions = false

Since you mentioned that you'd like to avoid using replication, you can also use a Jenkins job for synchronizing commits from Gerrit back to BitBucket. To minimize the delay, you can use the Gerrit Trigger plugin for Jenkins (which you'll want to be using anyway for your pre-commit checks). Alternatively, you can use a custom Gerrit hook that you place in hooks/ref-updated to trigger a Jenkins build (drop a comment if you'd like me to elaborate on that).

Hope this helps!