Pygerrit2

2.0.15 · active · verified Thu Apr 16

Pygerrit2 is a Python client library, version 2.0.15, for interacting with Gerrit Code Review's REST API. It is actively maintained and provides a simpler interface compared to its predecessor, `pygerrit`, by focusing solely on the REST API and omitting SSH functionality. The library is released as needed, with the latest stable version from June 2021.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to initialize `GerritRestAPI` with `HTTPBasicAuth` using credentials from environment variables and fetch a list of open changes. It also shows how to retrieve information about a specific project. Remember that the HTTP password is distinct from the SSH password.

import os
from pygerrit2 import GerritRestAPI, HTTPBasicAuth

GERRIT_URL = os.environ.get('GERRIT_URL', 'http://localhost:8080')
GERRIT_USERNAME = os.environ.get('GERRIT_USERNAME', 'admin')
GERRIT_HTTP_PASSWORD = os.environ.get('GERRIT_HTTP_PASSWORD', 'secret') # This is NOT your SSH password

if not all([GERRIT_URL, GERRIT_USERNAME, GERRIT_HTTP_PASSWORD]):
    print("Please set GERRIT_URL, GERRIT_USERNAME, and GERRIT_HTTP_PASSWORD environment variables.")
    exit(1)

try:
    auth = HTTPBasicAuth(GERRIT_USERNAME, GERRIT_HTTP_PASSWORD)
    rest = GerritRestAPI(url=GERRIT_URL, auth=auth)

    # Get open changes owned by the authenticated user
    changes = rest.get("/changes/?q=owner:self+status:open")
    print(f"Found {len(changes)} open changes for {GERRIT_USERNAME}:")
    for change in changes:
        print(f"  Change ID: {change['id']}, Subject: {change['subject']}")

    # Example of getting a specific project
    project_name = "All-Projects"
    project_info = rest.get(f"/projects/{project_name}")
    print(f"\nProject '{project_name}': {project_info['description']}")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →