Python Gerrit API Client
python-gerrit-api is a Python wrapper for the Gerrit REST API, enabling programmatic interaction with Gerrit Code Review. It is currently at version 3.1.0 and is actively maintained, with frequent releases often including dependency updates and minor enhancements.
Warnings
- breaking Python 2.x support has been dropped. Versions of python-gerrit-api 3.x and above require Python 3.8 or newer.
- gotcha Gerrit REST API authentication requires an HTTP password, which is distinct from an SSH password. Authenticated calls also typically require prefixing the endpoint URL with '/a/' (e.g., /a/projects/). The library handles the '/a/' prefix automatically when `username` and `password` are provided to `GerritClient`.
- gotcha The API structure and available methods within `GerritClient` may have evolved between major versions (e.g., from 2.x to 3.x). While not explicitly documented as breaking changes for the client library, internal Gerrit server API changes (like those in Gerrit 3.0.x) can necessitate updates to the client library and its usage patterns.
Install
-
pip install python-gerrit-api
Imports
- GerritClient
from gerrit import Gerrit
from gerrit import GerritClient
Quickstart
import os
from gerrit import GerritClient
GERRIT_HOST = os.environ.get('GERRIT_HOST', 'http://review.example.com')
GERRIT_USERNAME = os.environ.get('GERRIT_USERNAME', 'your_username')
GERRIT_PASSWORD = os.environ.get('GERRIT_PASSWORD', 'your_http_password')
try:
# Initialize the Gerrit client with HTTP Basic Authentication
client = GerritClient(
base_url=GERRIT_HOST,
username=GERRIT_USERNAME,
password=GERRIT_PASSWORD
)
# Example: List projects
projects = client.projects.list(limit=5)
print(f"Successfully connected to Gerrit. Found {len(projects)} projects:")
for project in projects:
print(f"- {project['name']}")
# Example: Get a specific change (replace with a real change ID/number)
# Note: Gerrit REST API often expects the 'id' of a change, not just the change number.
# You might need to fetch a list of changes first to get an ID.
# change = client.changes.get('12345') # Example change number
# print(f"Change {change['id']}: {change['subject']}")
except Exception as e:
print(f"An error occurred: {e}")
print("Please ensure GERRIT_HOST, GERRIT_USERNAME, and GERRIT_PASSWORD are set correctly.")