Registry / http-networking / pygerrit2

pygerrit2

JSON →
library2.0.15pypypi✓ verified 87d ago

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.

pip install pygerrit2
INSTALL
IMPORT
SIG · PYGERRIT2
P
pygerrit2
http-networkingpythonv2.0.15
Install
5.2s avg
Import
582ms
Disk
24MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.0.15 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.601s · 22.2MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 5.2s · import 0.562s · 23MB
24MB installed
● package 24MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

GerritRestAPI
from pygerrit2 import GerritRestAPI
HTTPBasicAuth
from pygerrit2 import HTTPBasicAuth
from requests.auth import HTTPDigestAuth
Gerrit 2.14 and later primarily use HTTP Basic Auth. HTTP Digest Auth was deprecated.
HTTPBasicAuthFromNetrc
from pygerrit2 import HTTPBasicAuthFromNetrc
from pygerrit2.rest.auth import HTTPDigestAuthFromNetrc
Similar to HTTPBasicAuth, prefer Basic Auth for modern Gerrit.

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}")
Debug
Known issues
breakingGerrit Code Review versions 2.14 and later deprecated HTTP Digest authentication in favor of HTTP Basic authentication. Using `HTTPDigestAuth` with newer Gerrit versions will likely result in authentication failures.
fix
Use `pygerrit2.HTTPBasicAuth` or `pygerrit2.HTTPBasicAuthFromNetrc` for authentication.
affects: Gerrit 2.14+
gotchaThe `/a/` prefix for authenticated REST API calls is automatically handled by `pygerrit2` when an authentication object is provided. Manually adding `/a/` to the base URL or API endpoints can lead to `400 Bad Request` or `403 Forbidden` errors.
fix
Do not include the `/a/` prefix in the `url` parameter when initializing `GerritRestAPI` or in your API endpoint paths if authentication is used.
affects: All versions
gotchaThe HTTP password used for Gerrit REST API authentication is not the same as your SSH password. It must be obtained from your Gerrit user settings (usually under 'HTTP Password' or 'Settings > HTTP Credentials').
fix
Ensure you are using the correct HTTP password generated by Gerrit, not your SSH key passphrase or login password.
affects: All versions
gotchaUnlike its predecessor `pygerrit`, `pygerrit2` exclusively supports the Gerrit REST API. It does not provide any SSH interface for interacting with Gerrit.
fix
If SSH functionality is required, consider using the original `pygerrit` library (which is no longer actively maintained) or a different tool that provides SSH access.
affects: All versions
Errors
Common errors & fixes
requests.exceptions.HTTPError: 401 Client Error: Unauthorized for url:...
This typically occurs due to incorrect authentication. Common reasons include using the wrong authentication type (e.g., `HTTPDigestAuth` with Gerrit 2.14+), providing incorrect HTTP credentials, or a mismatch between the provided username and the associated HTTP password.
fix
Verify that you are using `pygerrit2.HTTPBasicAuth` for modern Gerrit versions (2.14+). Double-check the username and the specific HTTP password obtained from your Gerrit user settings (not your SSH password).
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))
This error indicates a network-level issue where the connection to the Gerrit server was unexpectedly closed or could not be established. This can be due to transient network problems, server overload, or an incorrect `GERRIT_URL`.
fix
Check your `GERRIT_URL` for correctness and ensure network connectivity to the Gerrit server. For production environments, consider implementing retry logic with a library like `backoff` to handle transient connection issues gracefully.
requests.exceptions.HTTPError: 400 Client Error: Bad Request for url:...
A `400 Bad Request` usually means the server understood your request but couldn't process it due to invalid syntax or parameters in your request body or URL. This is common when making POST/PUT requests with malformed JSON data.
fix
Carefully review the Gerrit REST API documentation for the specific endpoint you are calling, paying close attention to the required JSON input format and URL parameters. Ensure your Python dictionary is correctly serialized to JSON and sent with the appropriate `Content-Type` header (though `pygerrit2` often handles this).
Upgrade
Version history
2.0.15latest on PyPI · released Jun 3, 2021
Audit
Dependencies
requestsrequiredCore HTTP client library for making API calls.
Agent activity
11 hits · last 30 days
node
8
OpenAI (training)
2
Resources
pygerrit2 — pip install pygerrit2 · libregistry