{"id":9217,"library":"pygerrit2","title":"Pygerrit2","description":"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.","status":"active","version":"2.0.15","language":"en","source_language":"en","source_url":"https://github.com/dpursehouse/pygerrit2","tags":["gerrit","rest-api","client","code-review","automation","devops"],"install":[{"cmd":"pip install pygerrit2","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core HTTP client library for making API calls.","package":"requests","optional":false}],"imports":[{"symbol":"GerritRestAPI","correct":"from pygerrit2 import GerritRestAPI"},{"note":"Gerrit 2.14 and later primarily use HTTP Basic Auth. HTTP Digest Auth was deprecated.","wrong":"from requests.auth import HTTPDigestAuth","symbol":"HTTPBasicAuth","correct":"from pygerrit2 import HTTPBasicAuth"},{"note":"Similar to HTTPBasicAuth, prefer Basic Auth for modern Gerrit.","wrong":"from pygerrit2.rest.auth import HTTPDigestAuthFromNetrc","symbol":"HTTPBasicAuthFromNetrc","correct":"from pygerrit2 import HTTPBasicAuthFromNetrc"}],"quickstart":{"code":"import os\nfrom pygerrit2 import GerritRestAPI, HTTPBasicAuth\n\nGERRIT_URL = os.environ.get('GERRIT_URL', 'http://localhost:8080')\nGERRIT_USERNAME = os.environ.get('GERRIT_USERNAME', 'admin')\nGERRIT_HTTP_PASSWORD = os.environ.get('GERRIT_HTTP_PASSWORD', 'secret') # This is NOT your SSH password\n\nif not all([GERRIT_URL, GERRIT_USERNAME, GERRIT_HTTP_PASSWORD]):\n    print(\"Please set GERRIT_URL, GERRIT_USERNAME, and GERRIT_HTTP_PASSWORD environment variables.\")\n    exit(1)\n\ntry:\n    auth = HTTPBasicAuth(GERRIT_USERNAME, GERRIT_HTTP_PASSWORD)\n    rest = GerritRestAPI(url=GERRIT_URL, auth=auth)\n\n    # Get open changes owned by the authenticated user\n    changes = rest.get(\"/changes/?q=owner:self+status:open\")\n    print(f\"Found {len(changes)} open changes for {GERRIT_USERNAME}:\")\n    for change in changes:\n        print(f\"  Change ID: {change['id']}, Subject: {change['subject']}\")\n\n    # Example of getting a specific project\n    project_name = \"All-Projects\"\n    project_info = rest.get(f\"/projects/{project_name}\")\n    print(f\"\\nProject '{project_name}': {project_info['description']}\")\n\nexcept Exception as e:\n    print(f\"An error occurred: {e}\")","lang":"python","description":"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."},"warnings":[{"fix":"Use `pygerrit2.HTTPBasicAuth` or `pygerrit2.HTTPBasicAuthFromNetrc` for authentication.","message":"Gerrit 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.","severity":"breaking","affected_versions":"Gerrit 2.14+"},{"fix":"Do not include the `/a/` prefix in the `url` parameter when initializing `GerritRestAPI` or in your API endpoint paths if authentication is used.","message":"The `/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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure you are using the correct HTTP password generated by Gerrit, not your SSH key passphrase or login password.","message":"The 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').","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"Unlike its predecessor `pygerrit`, `pygerrit2` exclusively supports the Gerrit REST API. It does not provide any SSH interface for interacting with Gerrit.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"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).","cause":"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.","error":"requests.exceptions.HTTPError: 401 Client Error: Unauthorized for 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.","cause":"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`.","error":"requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))"},{"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).","cause":"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.","error":"requests.exceptions.HTTPError: 400 Client Error: Bad Request for url:..."}]}