Skip to content

Gerrit Integration

Copybara integrates with Gerrit for organizations using Gerrit for code review.

Read from a Gerrit repository:

origin = git.gerrit_origin(
url = "https://gerrit.example.com/repo",
ref = "refs/heads/main",
)
ParameterDescription
urlGerrit repository URL
refGit reference to sync
change_number_urlURL pattern for changes

Create Gerrit change lists (CLs):

destination = git.gerrit_destination(
url = "https://gerrit.example.com/repo",
fetch = "main",
push_to_refs_for = "main",
)
ParameterDescription
urlGerrit repository URL
fetchBranch to fetch from
push_to_refs_forTarget branch for the CL
submitAuto-submit if approved
notifyNotification level
topicCL topic
change_id_policyHow to handle Change-Id
# Basic CL creation
destination = git.gerrit_destination(
url = "https://gerrit.example.com/repo",
fetch = "main",
push_to_refs_for = "main",
)
# With topic
destination = git.gerrit_destination(
url = "https://gerrit.example.com/repo",
fetch = "main",
push_to_refs_for = "main",
topic = "copybara-sync",
)
# With notification control
destination = git.gerrit_destination(
url = "https://gerrit.example.com/repo",
fetch = "main",
push_to_refs_for = "main",
notify = "OWNER", # Only notify owner
)

Interact with Gerrit API in feedback workflows:

destination = git.gerrit_api(
url = "https://gerrit.example.com",
)
def _my_action(ctx):
# Post a review
ctx.destination.post_review(
change_id = ctx.ref,
review_input = git.review_input(
labels = {"Code-Review": 1},
message = "Automated review",
),
)
return ctx.success()

Gerrit uses Change-Id to track changes. Copybara handles this automatically:

destination = git.gerrit_destination(
url = "...",
change_id_policy = "REQUIRE", # Fail if no Change-Id
)

Options:

  • "REQUIRE" - Fail if Change-Id is missing
  • "FAIL_IF_PRESENT" - Fail if Change-Id exists
  • "REUSE" - Reuse existing Change-Id
  • "REPLACE" - Replace with new Change-Id
core.workflow(
name = "import",
origin = git.github_pr_origin(
url = "https://github.com/org/public-repo",
branch = "main",
),
destination = git.gerrit_destination(
url = "https://gerrit.internal/repo",
fetch = "main",
push_to_refs_for = "main",
),
mode = "CHANGE_REQUEST_FROM_SOT",
...
)
core.workflow(
name = "export",
origin = git.gerrit_origin(
url = "https://gerrit.internal/repo",
ref = "refs/heads/main",
),
destination = git.github_destination(
url = "https://github.com/org/public-repo",
push = "main",
),
mode = "SQUASH",
...
)

Gerrit typically uses:

  • HTTP credentials (username + password/token)
  • SSH keys
Terminal window
# HTTP credentials
git config --global credential.helper store
echo "https://user:token@gerrit.example.com" > ~/.git-credentials
# SSH
ssh-add ~/.ssh/id_ed25519