Pygithub: 分支对象缺少大多数属性和方法

创建于 2018-08-17  ·  11评论  ·  资料来源: PyGithub/PyGithub

我正在玩这个库,希望我可以在我们的 Github Enterprise 上实现自动化。 我需要做的第一件事就是为我们的存储库设置分支保护。 但是,当我从测试存储库(已经设置了分支保护设置,请注意)启动 Branch 对象时,它分配的唯一属性是“名称”,并且文档中列出的大多数方法都不存在于目的:

import github
github.enable_console_debug_logging()
Github = github.Github

Github("myuser",
'mycreds',
base_url="https://ghe.workworkwork.com/api/v3")

master = g.get_user("myuser").get_repo('cookbook1').get_branch('master')
print master
print master.protection_url

输出:

...
GET https://ghe.workworkwork.com/api/v3/repos/myuser/cookbook1/branches/master {'Authorization': 'Basic (login and password removed)', 'User-Agent': 'PyGithub/Python'} None ==> 200 {'status': '200 OK', 'content-length': '2925', 'x-github-media-type': 'github.v3', 'x-content-type-options': 'nosniff', 'content-security-policy': "default-src 'none'", 'access-control-expose-headers': 'ETag, Link, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval', 'x-github-request-id': '0ec6f4e8-2355-41dc-859d-0e7076a294b1', 'strict-transport-security': 'max-age=31536000; includeSubdomains', 'vary': 'Accept, Authorization, Cookie, X-GitHub-OTP', 'server': 'GitHub.com', 'access-control-allow-origin': '*', 'etag': '"965449c848ed3319d7e028c2a587f414"', 'x-xss-protection': '1; mode=block', 'cache-control': 'private, max-age=60, s-maxage=60', 'date': 'Thu, 16 Aug 2018 23:41:19 GMT', 'x-frame-options': 'deny', 'x-oauth-scopes': 'admin:org_hook, admin:pre_receive_hook, admin:repo_hook, repo', 'content-type': 'application/json; charset=utf-8', 'x-accepted-oauth-scopes': ''} {"name":"master","commit":{"sha":"3548233b67c8ec4cad703ff947fa0ba022df7c66","commit":{"author":{"name":"Garrett Anderson","email":"[email protected]","date":"2018-08-16T18:44:44Z"},"committer":{"name":"GitHub Enterprise","email":"[email protected]","date":"2018-08-16T18:44:44Z"},"message":"Merge pull request #8 from myuser/newfork\n\nwhat","tree":{"sha":"44b3a300094b2ccbbfb79ea7df5f82e627c12b42","url":"https://ghe.workworkwork.com/api/v3/repos/myuser/cookbook1/git/trees/44b3a300094b2ccbbfb79ea7df5f82e627c12b42"},"url":"https://ghe.workworkwork.com/api/v3/repos/myuser/cookbook1/git/commits/3548233b67c8ec4cad703ff947fa0ba022df7c66","comment_count":0},"url":"https://ghe.workworkwork.com/api/v3/repos/myuser/cookbook1/commits/3548233b67c8ec4cad703ff947fa0ba022df7c66","html_url":"https://ghe.workworkwork.com/myuser/cookbook1/commit/3548233b67c8ec4cad703ff947fa0ba022df7c66","comments_url":"https://ghe.workworkwork.com/api/v3/repos/myuser/cookbook1/commits/3548233b67c8ec4cad703ff947fa0ba022df7c66/comments","author":{"login":"myuser","id":593,"avatar_url":"https://ghe.workworkwork.com/avatars/u/593?","gravatar_id":"","url":"https://ghe.workworkwork.com/api/v3/users/myuser","html_url":"https://ghe.workworkwork.com/myuser","followers_url":"https://ghe.workworkwork.com/api/v3/users/myuser/followers","following_url":"https://ghe.workworkwork.com/api/v3/users/myuser/following{/other_user}","gists_url":"https://ghe.workworkwork.com/api/v3/users/myuser/gists{/gist_id}","starred_url":"https://ghe.workworkwork.com/api/v3/users/myuser/starred{/owner}{/repo}","subscriptions_url":"https://ghe.workworkwork.com/api/v3/users/myuser/subscriptions","organizations_url":"https://ghe.workworkwork.com/api/v3/users/myuser/orgs","repos_url":"https://ghe.workworkwork.com/api/v3/users/myuser/repos","events_url":"https://ghe.workworkwork.com/api/v3/users/myuser/events{/privacy}","received_events_url":"https://ghe.workworkwork.com/api/v3/users/myuser/received_events","type":"User","site_admin":true},"committer":null,"parents":[{"sha":"067642113fe45ee77160ee28cafc4870f2c06b6a","url":"https://ghe.workworkwork.com/api/v3/repos/myuser/cookbook1/commits/067642113fe45ee77160ee28cafc4870f2c06b6a","html_url":"https://ghe.workworkwork.com/myuser/cookbook1/commit/067642113fe45ee77160ee28cafc4870f2c06b6a"},{"sha":"3a9479c605617a9035cd986a4bf77880ac5dde64","url":"https://ghe.workworkwork.com/api/v3/repos/myuser/cookbook1/commits/3a9479c605617a9035cd986a4bf77880ac5dde64","html_url":"https://ghe.workworkwork.com/myuser/cookbook1/commit/3a9479c605617a9035cd986a4bf77880ac5dde64"}]},"_links":{"self":"https://ghe.workworkwork.com/api/v3/repos/myuser/cookbook1/branches/master","html":"https://ghe.workworkwork.com/myuser/cookbook1/tree/master"}}

Branch(name="master")

AttributeError: 'Branch' object has no attribute 'protection_url'

那里也没有任何方法:

print master.get_required_status_checks()
OUTPUT:
AttributeError: 'Branch' object has no attribute 'get_required_status_checks'

或者这个属性:

print master.protection
OUTPUT:
return self._protected.value
AttributeError: 'Branch' object has no attribute '_protected'

该对象如下所示:

print dir(master)
OUTPUT:
['CHECK_AFTER_INIT_FLAG', '_GithubObject__makeSimpleAttribute', '_GithubObject__makeSimpleListAttribute', '_GithubObject__makeTransformedAttribute', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_commit', '_completeIfNeeded', '_headers', '_initAttributes', '_makeBoolAttribute', '_makeClassAttribute', '_makeDatetimeAttribute', '_makeDictAttribute', '_makeDictOfStringsToClassesAttribute', '_makeIntAttribute', '_makeListOfClassesAttribute', '_makeListOfIntsAttribute', '_makeListOfListOfStringsAttribute', '_makeListOfStringsAttribute', '_makeStringAttribute', '_makeTimestampAttribute', '_name', '_parentUrl', '_rawData', '_requester', '_storeAndUseAttributes', '_useAttributes', 'commit', 'contexts', 'enforcement_level', 'etag', 'get__repr__', 'last_modified', 'name', 'protected', 'raw_data', 'raw_headers', 'setCheckAfterInitFlag']

看起来对象没有被正确创建。

bug stale

最有用的评论

我在开发分支保护代码时遇到了这个问题——Repository.get_branches() 确实返回了一个列表
分支,但 GitHub 没有返回足够的信息来使分支上的方法有用。 如果您想利用保护功能,您必须调用 Repository.get_branch('name')。

所有11条评论

您使用的是哪个 PyGithub 版本? 请注意,#790 只是最近合并但尚未发布。

我正在使用 PyGithub-1.40 和 Github Enterprise 2.8.6。
https://github.com/PyGithub/PyGithub/pull/790看起来很有希望,虽然我公司的 GHE 版本有点过时,所以我不确定 API 的变化是否会影响它。

UPDATE:正在测试最新的master

看起来最新的合并修复了缺少属性和方法的问题,但它似乎与我的 API 版本不兼容:

master = g.get_user("ganderson").get_repo('cookbook1').get_branch('master')
print master.protection_url
master.get_protection()

输出:

None
Traceback (most recent call last):
  File "test_pygithub.py", line 12, in <module>
    master.get_protection()
  File "/Users/me/tmp/ghe-python/venv/lib/python2.7/site-packages/github/Branch.py", line 102, in get_protection
    self.protection_url
  File "/Users/me/tmp/ghe-python/venv/lib/python2.7/site-packages/github/Requester.py", line 260, in requestJsonAndCheck
    return self.__check(*self.requestJson(verb, url, parameters, headers, input, self.__customConnection(url)))
  File "/Users/me/tmp/ghe-python/venv/lib/python2.7/site-packages/github/Requester.py", line 276, in __customConnection
    if not url.startswith("/"):
AttributeError: 'NoneType' object has no attribute 'startswith'

这似乎是一个单独的问题,因此我将关闭此票证。 谢谢你。

如果主人坏了,一定要提出一个新问题。

这对我来说在标准 api.github.com 上的 1.43.2 上似乎也坏了

我有 2 个受保护的分支,但 Branch 对象从未获得 protection_url 集。

保护 URL 未设置并导致任何使用它来构建 URL 的错误。

[
  {
    "name": "develop",
    "commit": {
      "sha": "XXXX",
      "url": "https://api.github.com/repos/X/X/commits/X"
    }
  },
  {
    "name": "master",
    "commit": {
      "sha": "X",
      "url": "https://api.github.com/repos/X/X/commits/X"
    }
  }
]

如果我使用branches?protected=true卷曲,则它会返回负载中的 protection_url。

[
  {
    "name": "develop",
    "commit": {
      "sha": "X",
      "url": "https://api.github.com/repos/X/X/commits/X"
    },
    "protected": true,
    "protection": {
      "enabled": true,
      "required_status_checks": {
        "enforcement_level": "off",
        "contexts": [

        ]
      }
    },
    "protection_url": "https://api.github.com/repos/X/X/branches/develop/protection"
  },
  {
    "name": "master",
    "commit": {
      "sha": "X",
      "url": "https://api.github.com/repos/X/X/commits/X"
    },
    "protected": true,
    "protection": {
      "enabled": true,
      "required_status_checks": {
        "enforcement_level": "off",
        "contexts": [

        ]
      }
    },
    "protection_url": "https://api.github.com/repos/X/X/branches/master/protection"
  }
]

我代表最后一位评论者重新打开这个问题

您可以尝试使用get_branch("master")获取单个分支吗? 我认为问题可能是当前的get_branches方法不支持protected参数。

我花了一些时间调试这个问题,并针对 GitHub v3 API 提出了一个错误。

https://github.com/dear-github/dear-github/issues/325

我在开发分支保护代码时遇到了这个问题——Repository.get_branches() 确实返回了一个列表
分支,但 GitHub 没有返回足够的信息来使分支上的方法有用。 如果您想利用保护功能,您必须调用 Repository.get_branch('name')。

我在开发分支保护代码时遇到了这个问题——Repository.get_branches() 确实返回了一个列表
分支,但 GitHub 没有返回足够的信息来使分支上的方法有用。 如果您想利用保护功能,您必须调用 Repository.get_branch('name')。

谢谢,我今天遇到了这个问题,这个解决方法为我节省了很多时间和挫折!

此问题已自动标记为过时,因为它最近没有活动。 如果没有进一步的活动发生,它将被关闭。 感谢你的贡献。

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

nixoz2k7 picture nixoz2k7  ·  7评论

BBI-YggyKing picture BBI-YggyKing  ·  5评论

jacquev6 picture jacquev6  ·  3评论

men1n2 picture men1n2  ·  6评论

psychemedia picture psychemedia  ·  5评论