Skip to content

Commit

Permalink
Add support for getting / replacing topics (#634) (#832)
Browse files Browse the repository at this point in the history
* Add support for getting and replacing topics on a repository #634

* Add / fix tests, accept list vs dict for replace_topics (#634)
  • Loading branch information
wyardley authored and sfdye committed Jul 6, 2018
1 parent 68af725 commit c6802b5
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
39 changes: 39 additions & 0 deletions github/Repository.py
Expand Up @@ -670,6 +670,14 @@ def teams_url(self):
self._completeIfNotSet(self._teams_url)
return self._teams_url.value

@property
def topics(self):
"""
:type: list of strings
"""
self._completeIfNotSet(self._topics)
return self._topics.value

@property
def trees_url(self):
"""
Expand Down Expand Up @@ -2304,6 +2312,18 @@ def get_teams(self):
None
)

def get_topics(self):
"""
:calls: `GET /repos/:owner/:repo/topics <https://developer.github.com/v3/repos/#list-all-topics-for-a-repository>`_
:rtype: list of strings
"""
headers, data = self._requester.requestJsonAndCheck(
"GET",
self.url + "/topics",
headers={'Accept': 'application/vnd.github.mercy-preview+json'}
)
return data['names']

def get_watchers(self):
"""
:calls: `GET /repos/:owner/:repo/watchers <http://developer.github.com/v3/activity/starring>`_
Expand Down Expand Up @@ -2426,6 +2446,22 @@ def protect_branch(self, branch, enabled, enforcement_level=github.GithubObject.
input=post_parameters
)

def replace_topics(self, topics):
"""
:calls: `PUT /repos/:owner/:repo/topics <http://developer.github.com/v3/repos>`_
:param topics: list of strings
:rtype: None
"""
post_parameters = {
'names': topics
}
headers, data = self._requester.requestJsonAndCheck(
"PUT",
self.url + "/topics",
headers={'Accept': 'application/vnd.github.mercy-preview+json'},
input=post_parameters
)

def remove_from_collaborators(self, collaborator):
"""
:calls: `DELETE /repos/:owner/:repo/collaborators/:user <http://developer.github.com/v3/repos/collaborators>`_
Expand Down Expand Up @@ -2564,6 +2600,7 @@ def _initAttributes(self):
self._svn_url = github.GithubObject.NotSet
self._tags_url = github.GithubObject.NotSet
self._teams_url = github.GithubObject.NotSet
self._topics = github.GithubObject.NotSet
self._trees_url = github.GithubObject.NotSet
self._updated_at = github.GithubObject.NotSet
self._url = github.GithubObject.NotSet
Expand Down Expand Up @@ -2709,6 +2746,8 @@ def _useAttributes(self, attributes):
self._teams_url = self._makeStringAttribute(attributes["teams_url"])
if "trees_url" in attributes: # pragma no branch
self._trees_url = self._makeStringAttribute(attributes["trees_url"])
if "topics" in attributes: # pragma no branch
self._topics = self._makeListOfStringsAttribute(attributes["topics"])
if "updated_at" in attributes: # pragma no branch
self._updated_at = self._makeDatetimeAttribute(attributes["updated_at"])
if "url" in attributes: # pragma no branch
Expand Down
11 changes: 11 additions & 0 deletions github/tests/ReplayData/Repository.testGetTopics.txt
@@ -0,0 +1,11 @@
https
GET
api.github.com
None
/repos/jacquev6/PyGithub/topics
{'Authorization': 'Basic login_and_password_removed', 'Accept': 'application/vnd.github.mercy-preview+json', 'User-Agent': 'PyGithub/Python'}
None
200
[('content-length', '30'), ('x-runtime-rack', '0.081855'), ('vary', 'Accept, Authorization, Cookie, X-GitHub-OTP'), ('x-xss-protection', '1; mode=block'), ('x-content-type-options', 'nosniff'), ('etag', '"c34823528b2c41eadf247bcea3b7dabc"'), ('cache-control', 'private, max-age=60, s-maxage=60'), ('referrer-policy', 'origin-when-cross-origin, strict-origin-when-cross-origin'), ('status', '200 OK'), ('x-ratelimit-remaining', '4994'), ('x-github-media-type', 'github.mercy-preview; format=json'), ('access-control-expose-headers', 'ETag, Link, Retry-After, 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', '4C20:5398:FE4A88:26DA323:5AD7F450'), ('date', 'Thu, 19 Apr 2018 01:43:45 GMT'), ('access-control-allow-origin', '*'), ('content-security-policy', "default-src 'none'"), ('strict-transport-security', 'max-age=31536000; includeSubdomains; preload'), ('server', 'GitHub.com'), ('x-ratelimit-limit', '5000'), ('x-frame-options', 'deny'), ('content-type', 'application/json; charset=utf-8'), ('x-ratelimit-reset', '1524105758')]
{"names":["testing","github"]}

11 changes: 11 additions & 0 deletions github/tests/ReplayData/Repository.testReplaceTopics.txt
@@ -0,0 +1,11 @@
https
PUT
api.github.com
None
/repos/jacquev6/PyGithub/topics
{'Content-Type': 'application/json', 'Authorization': 'Basic login_and_password_removed', 'Accept': 'application/vnd.github.mercy-preview+json', 'User-Agent': 'PyGithub/Python'}
{"names": ["github", "testing"]}
200
[('content-length', '30'), ('x-runtime-rack', '0.081855'), ('vary', 'Accept, Authorization, Cookie, X-GitHub-OTP'), ('x-xss-protection', '1; mode=block'), ('x-content-type-options', 'nosniff'), ('etag', '"c34823528b2c41eadf247bcea3b7dabc"'), ('cache-control', 'private, max-age=60, s-maxage=60'), ('referrer-policy', 'origin-when-cross-origin, strict-origin-when-cross-origin'), ('status', '200 OK'), ('x-ratelimit-remaining', '4994'), ('x-github-media-type', 'github.mercy-preview; format=json'), ('access-control-expose-headers', 'ETag, Link, Retry-After, 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', '4C20:5398:FE4A88:26DA323:5AD7F450'), ('date', 'Thu, 19 Apr 2018 01:43:45 GMT'), ('access-control-allow-origin', '*'), ('content-security-policy', "default-src 'none'"), ('strict-transport-security', 'max-age=31536000; includeSubdomains; preload'), ('server', 'GitHub.com'), ('x-ratelimit-limit', '5000'), ('x-frame-options', 'deny'), ('content-type', 'application/json; charset=utf-8'), ('x-ratelimit-reset', '1524105758')]
{"names": ["testing","github"]}

8 changes: 8 additions & 0 deletions github/tests/Repository.py
Expand Up @@ -82,6 +82,7 @@ def testAttributes(self):
self.assertEqual(self.repo.source, None)
self.assertEqual(self.repo.ssh_url, "git@github.com:jacquev6/PyGithub.git")
self.assertEqual(self.repo.svn_url, "https://github.com/jacquev6/PyGithub")
self.assertEqual(self.repo.topics, None)
self.assertEqual(self.repo.updated_at, datetime.datetime(2012, 5, 27, 6, 55, 28))
self.assertEqual(self.repo.url, "https://api.github.com/repos/jacquev6/PyGithub")
self.assertEqual(self.repo.watchers, 15)
Expand Down Expand Up @@ -711,6 +712,13 @@ def testStatistics(self):
def testGetLicense(self):
self.assertEqual(len(self.repo.get_license().content), 47646)

def testGetTopics(self):
topic_list = self.repo.get_topics()
topic = u'github'
self.assertIn(topic, topic_list)

def testReplaceTopics(self):
self.repo.replace_topics(['github', 'testing'])

class LazyRepository(Framework.TestCase):

Expand Down

0 comments on commit c6802b5

Please sign in to comment.