使用 PyGithub API 时出现错误“'Repository' 对象没有属性 'create_deployment'”

Getting error "'Repository' object has no attribute 'create_deployment'" when using the PyGithub API

我正在尝试使用 PyGithub (https://pygithub.readthedocs.io/en/latest/apis.html) 获取部署并在其他地方报告状态。我有以下脚本:

from github import Github

# Github Enterprise with custom hostname
g = Github(base_url="https://{hostname}/api/v3", login_or_token="sometoken")
# get the repository
repo = g.get_repo("PyGithub/PyGithub")
# HTTP POST to /repos/:owner/:repo/deployments
deployment = repo.create_deployment(ref = 'master', environment = 'DEV', description= 'Deploying to the environment')
# getting the status
status = deployment.get_status(deployment.id)
# HTTP POST to /repos/:owner/:repo/deployments/:deployment_id/statuses
deployment.create_status(state = status.state, environment = 'DEV', description = 'deploying completed')

但它抱怨:

'Repository' object has no attribute 'create_deployment'

而我在 (https://pygithub.readthedocs.io/en/latest/github_objects/Repository.html#github.Repository.Repository) 中看到函数。

我错过了什么?

我基本上想使用这两个端点。我无法在 python 中找到任何其他 github API,但即使那样也可以。

首先,确保您使用的是更新版本的 PyGithub。

部署 API 仅在 release version 1.47 添加。

  • 发行说明:

    "Add Deployments API (#1424) (3d93ee1)"

  • PR #1424:

    Add a new class, Deployment to describe a deployment performed utilising GitHub. Add three methods onto Repository to list them and create them.

如果您使用的是 <1.47,则该方法尚不存在。

其次,我认为您误解了 deployment.get_status 的参数。

文档有点不清楚,但它需要部署状态 ID,而不是 部署 ID。该方法向 /repos/{owner}/{repo}/deployments/{deployment_id}/statuses/{status_id} 端点发出请求以“查看部署的部署状态”,代码将传入的 id 附加到 /statuses/ URL:

https://github.com/PyGithub/PyGithub/blob/master/github/Deployment.py#L180

def get_status(self, id_):
    """
    :calls: `GET /repos/:owner/deployments/:deployment_id/statuses/:status_id  <https://developer.github.com/v3/repos/deployments/#get-a-deployment-status>`_
    :param id_: int
    :rtype: :class:`github.DeploymentStatus.DeploymentStatus`
    """
    assert isinstance(id_, int), id_
    headers, data = self._requester.requestJsonAndCheck(
        "GET",
        self.url + "/statuses/" + str(id_),
        headers={"Accept": self._get_accept_header()},
    )

正确的 API 可能是 get_statuses,用于列出特定部署的所有部署状态。然后获取其中一个 ID 并将其传递给 get_status 以获取特定的 DeploymentStatus 对象。 (不过,您也可以循环遍历 get_statuses 列表,每个列表都已经是一个 DeploymentStatus 对象)。如果您还没有任何部署状态,那么您可以使用 create_status:

创建一个
# create a Deployment
# POST /repos/{owner}/{repo}/deployments
deployment = repo.create_deployment(ref='master', description='Deploying to the environment')

# list Deployment Statuses
# https://docs.github.com/en/rest/reference/repos#list-deployment-statuses
# GET /repos/{owner}/{repo}/deployments/{deployment_id}/statuses
for status in deployment.get_statuses():
    print(status.id)  # For new deployments, this would be empty

# create a Deployment Status
# https://docs.github.com/en/rest/reference/repos#create-a-deployment-status
# POST /repos/{owner}/{repo}/deployments/{deployment_id}/statuses
new_deployment_status = deployment.create_status(state='inactive', description='inactive deployment')
print(new_deployment_status.state)

# list Deployment Statuses
# https://docs.github.com/en/rest/reference/repos#list-deployment-statuses
# GET /repos/{owner}/{repo}/deployments/{deployment_id}/statuses
for status in deployment.get_statuses():
    print(status.state, status.id)  # This should now the newly created status
#         'inactive',   461997990

# get a specific Deployment Status
deployment_status = deployment.get_statuses()[0]
deployment_status_0 = deployment.get_status(deployment_status.id))