如何使用 python 访问 Gerrit Rest API

How to access Gerrit Rest API using python

首先,我对gerrit的了解有限。

我正在尝试使用 python 访问 Gerrit Rest API,但无法访问。我想获取与帐户相关的所有信息(提交、评论)。

from requests.auth import HTTPDigestAuth
from pygerrit2 import GerritRestAPI, HTTPBasicAuth
auth = HTTPBasicAuth('user', 'password')
from pygerrit2 import GerritRestAPI
rest = GerritRestAPI(url='https://xxx.xx.xxx.xxx.com/', auth = auth)
changes = rest.get("changes/?q=is:open&q=is:close&q=all&o=DETAILED_ACCOUNTS&o=ALL_REVISIONS&o=ALL_COMMITS&o=ALL_FILES&o=MESSAGES", headers={'Content-Type': 'application/json'})

我得到的错误是:

ConnectionError: HTTPSConnectionPool(host='xxx.xx.xxx.xxx.com', port=443): Max retries exceeded with url: /login/a/changes/?q=is:open&q=is:close&q=all&o=DETAILED_ACCOUNTS&o=ALL_REVISIONS&o=ALL_COMMITS&o=ALL_FILES&o=MESSAGES (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x825bad0>: Failed to establish a new connection: [Errno 110] Connection timed out',))

如果我在 url 中复制粘贴查询,我可以获取信息,但不能通过 python。怎么做? 如果问题不清楚,请发表评论/编辑。 谢谢

您遇到来自请求库的连接错误(pygerrit2 是 dependent on requests) - it is occurring because your connection is timing out. To avoid this I recommend using a library like backoff。退避将捕获此连接错误并重试建立连接。这可以通过装饰器和导入轻松完成。

from requests.auth import HTTPDigestAuth
from pygerrit2 import GerritRestAPI, HTTPBasicAuth
import backoff
import requests

@backoff.on_exception(backoff.expo, 
                      requests.exceptions.ConnectionError,
                      max_time=10)
def makeGerritAPICall():
     auth = HTTPBasicAuth('user', 'password')
     rest = GerritRestAPI(url='https://xxx.xx.xxx.xxx.com/', auth = auth)
     changes = rest.get("changes/?q=is:open&q=is:close&q=all&o=DETAILED_ACCOUNTS&o=ALL_REVISIONS&o=ALL_COMMITS&o=ALL_FILES&o=MESSAGES", headers={'Content-Type': 'application/json'})

以下函数将在失败并引发 ConnectionError 之前重试发出任何遇到 ConnectionError 的请求 10 秒。

我建议访问退避 git README 文档,因为它们包含大量关于退避的有用信息。