Python Gerrit API Client

3.1.0 · active · verified Wed Apr 15

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `GerritClient` and perform a basic operation, such as listing projects. It uses environment variables for authentication credentials, which is recommended for security. Ensure your Gerrit instance uses HTTP Basic Authentication and you have an HTTP password configured (not an SSH password).

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.")

view raw JSON →