From 22f366b540a302036784afc979e28569ede388df Mon Sep 17 00:00:00 2001 From: Vincent Jacques Date: Sun, 10 Nov 2013 15:45:33 -0800 Subject: [PATCH] Accept strings as well as Label objects (#202) This should be more generic, but it's a wig work to do it everywhere. Let's keep that in mind for V2. --- README.rst | 9 +-- github/Issue.py | 18 +++-- github/tests/Issue.py | 20 +++++ ...tAddAndRemoveLabelsWithStringArguments.txt | 77 +++++++++++++++++++ ...tDeleteAndSetLabelsWithStringArguments.txt | 55 +++++++++++++ 5 files changed, 165 insertions(+), 14 deletions(-) create mode 100644 github/tests/ReplayData/Issue.testAddAndRemoveLabelsWithStringArguments.txt create mode 100755 github/tests/ReplayData/Issue.testDeleteAndSetLabelsWithStringArguments.txt diff --git a/README.rst b/README.rst index efaeca89..f5a8592c 100644 --- a/README.rst +++ b/README.rst @@ -16,13 +16,10 @@ Thank you, dear stargazers! Starting today (September 5th, 2013), we now need more than 8 bits to store the number of `stargazers `_! Thank you so much! -`Version 1.20.0 `_ (October 20th, 2013) (First Seattle edition) --------------------------------------------------------------------------------------------------------------------------------------- +`Version 1.21.0 `_ (November ??th, 2013) +--------------------------------------------------------------------------------------------------------------- -* `Implement `_ ``Github.get_hook(name)``. Thank you `klmitch `_ for asking -* In case bad data is returned by Github API v3, `raise `_ an exception only when the user accesses the faulty attribute, not when constructing the object containing this attribute. Thank you `klmitch `_ for asking -* `Fix `_ parameter public/private of ``Repository.edit``. Thank you `daireobroin449 `_ for reporting the issue -* Remove ``Repository.create_download`` and ``NamedUser.create_gist`` as the corrensponding APIs are not documented anymore +* `Accept `__ strings as well as ``Label`` objects in ``Issue.add_to_labels``, ``Issue.remove_from_labels`` and ``Issue.set_labels``. Thank you `acdha `__ for asking Twitter ------- diff --git a/github/Issue.py b/github/Issue.py index c6872ccd..aba747fe 100644 --- a/github/Issue.py +++ b/github/Issue.py @@ -219,11 +219,11 @@ class Issue(github.GithubObject.CompletableGithubObject): def add_to_labels(self, *labels): """ :calls: `POST /repos/:owner/:repo/issues/:number/labels `_ - :param label: :class:`github.Label.Label` + :param label: :class:`github.Label.Label` or string :rtype: None """ - assert all(isinstance(element, github.Label.Label) for element in labels), labels - post_parameters = [label.name for label in labels] + assert all(isinstance(element, (github.Label.Label, str, unicode)) for element in labels), labels + post_parameters = [label.name if isinstance(label, github.Label.Label) else label for label in labels] headers, data = self._requester.requestJsonAndCheck( "POST", self.url + "/labels", @@ -346,13 +346,15 @@ class Issue(github.GithubObject.CompletableGithubObject): def remove_from_labels(self, label): """ :calls: `DELETE /repos/:owner/:repo/issues/:number/labels/:name `_ - :param label: :class:`github.Label.Label` + :param label: :class:`github.Label.Label` or string :rtype: None """ - assert isinstance(label, github.Label.Label), label + assert isinstance(label, (github.Label.Label, str, unicode)), label + if isinstance(label, github.Label.Label): + label = label._identity headers, data = self._requester.requestJsonAndCheck( "DELETE", - self.url + "/labels/" + label._identity + self.url + "/labels/" + label ) def set_labels(self, *labels): @@ -361,8 +363,8 @@ class Issue(github.GithubObject.CompletableGithubObject): :param label: :class:`github.Label.Label` :rtype: None """ - assert all(isinstance(element, github.Label.Label) for element in labels), labels - post_parameters = [label.name for label in labels] + assert all(isinstance(element, (github.Label.Label, str, unicode)) for element in labels), labels + post_parameters = [label.name if isinstance(label, github.Label.Label) else label for label in labels] headers, data = self._requester.requestJsonAndCheck( "PUT", self.url + "/labels", diff --git a/github/tests/Issue.py b/github/tests/Issue.py index 02138ff6..84d1a060 100644 --- a/github/tests/Issue.py +++ b/github/tests/Issue.py @@ -104,6 +104,17 @@ class Issue(Framework.TestCase): self.issue.add_to_labels(bug, question) self.assertListKeyEqual(self.issue.get_labels(), lambda l: l.name, ["Bug", "Project management", "Question"]) + def testAddAndRemoveLabelsWithStringArguments(self): + bug = "Bug" + question = "Question" + self.assertListKeyEqual(self.issue.get_labels(), lambda l: l.name, ["Bug", "Project management", "Question"]) + self.issue.remove_from_labels(bug) + self.assertListKeyEqual(self.issue.get_labels(), lambda l: l.name, ["Project management", "Question"]) + self.issue.remove_from_labels(question) + self.assertListKeyEqual(self.issue.get_labels(), lambda l: l.name, ["Project management"]) + self.issue.add_to_labels(bug, question) + self.assertListKeyEqual(self.issue.get_labels(), lambda l: l.name, ["Bug", "Project management", "Question"]) + def testDeleteAndSetLabels(self): bug = self.repo.get_label("Bug") question = self.repo.get_label("Question") @@ -112,3 +123,12 @@ class Issue(Framework.TestCase): self.assertListKeyEqual(self.issue.get_labels(), None, []) self.issue.set_labels(bug, question) self.assertListKeyEqual(self.issue.get_labels(), lambda l: l.name, ["Bug", "Question"]) + + def testDeleteAndSetLabelsWithStringArguments(self): + bug = "Bug" + question = "Question" + self.assertListKeyEqual(self.issue.get_labels(), lambda l: l.name, ["Bug", "Project management", "Question"]) + self.issue.delete_labels() + self.assertListKeyEqual(self.issue.get_labels(), None, []) + self.issue.set_labels(bug, question) + self.assertListKeyEqual(self.issue.get_labels(), lambda l: l.name, ["Bug", "Question"]) diff --git a/github/tests/ReplayData/Issue.testAddAndRemoveLabelsWithStringArguments.txt b/github/tests/ReplayData/Issue.testAddAndRemoveLabelsWithStringArguments.txt new file mode 100644 index 00000000..34bace89 --- /dev/null +++ b/github/tests/ReplayData/Issue.testAddAndRemoveLabelsWithStringArguments.txt @@ -0,0 +1,77 @@ +https +GET +api.github.com +None +/repos/jacquev6/PyGithub/issues/28/labels +{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'} +null +200 +[('status', '200 OK'), ('x-ratelimit-remaining', '4990'), ('content-length', '335'), ('server', 'nginx/1.0.13'), ('connection', 'keep-alive'), ('x-ratelimit-limit', '5000'), ('etag', '"c9f9beccb03030beaf7b80927da6fef6"'), ('date', 'Sun, 27 May 2012 09:04:03 GMT'), ('content-type', 'application/json; charset=utf-8')] +[{"url":"https://api.github.com/repos/jacquev6/PyGithub/labels/Bug","name":"Bug","color":"e10c02"},{"url":"https://api.github.com/repos/jacquev6/PyGithub/labels/Project+management","name":"Project management","color":"444444"},{"url":"https://api.github.com/repos/jacquev6/PyGithub/labels/Question","name":"Question","color":"02e10c"}] + +https +DELETE +api.github.com +None +/repos/jacquev6/PyGithub/issues/28/labels/Bug +{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'} +null +200 +[('status', '200 OK'), ('x-ratelimit-remaining', '4989'), ('content-length', '237'), ('server', 'nginx/1.0.13'), ('connection', 'keep-alive'), ('x-ratelimit-limit', '5000'), ('etag', '"46cc70bad88a09b559a5e67089005105"'), ('date', 'Sun, 27 May 2012 09:04:03 GMT'), ('content-type', 'application/json; charset=utf-8')] +[{"url":"https://api.github.com/repos/jacquev6/PyGithub/labels/Project+management","name":"Project management","color":"444444"},{"url":"https://api.github.com/repos/jacquev6/PyGithub/labels/Question","name":"Question","color":"02e10c"}] + +https +GET +api.github.com +None +/repos/jacquev6/PyGithub/issues/28/labels +{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'} +null +200 +[('status', '200 OK'), ('x-ratelimit-remaining', '4988'), ('content-length', '237'), ('server', 'nginx/1.0.13'), ('connection', 'keep-alive'), ('x-ratelimit-limit', '5000'), ('etag', '"46cc70bad88a09b559a5e67089005105"'), ('date', 'Sun, 27 May 2012 09:04:04 GMT'), ('content-type', 'application/json; charset=utf-8')] +[{"url":"https://api.github.com/repos/jacquev6/PyGithub/labels/Project+management","name":"Project management","color":"444444"},{"url":"https://api.github.com/repos/jacquev6/PyGithub/labels/Question","name":"Question","color":"02e10c"}] + +https +DELETE +api.github.com +None +/repos/jacquev6/PyGithub/issues/28/labels/Question +{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'} +null +200 +[('status', '200 OK'), ('x-ratelimit-remaining', '4987'), ('content-length', '129'), ('server', 'nginx/1.0.13'), ('connection', 'keep-alive'), ('x-ratelimit-limit', '5000'), ('etag', '"5352ae15c8a5a36c6cace63be9367332"'), ('date', 'Sun, 27 May 2012 09:04:04 GMT'), ('content-type', 'application/json; charset=utf-8')] +[{"url":"https://api.github.com/repos/jacquev6/PyGithub/labels/Project+management","name":"Project management","color":"444444"}] + +https +GET +api.github.com +None +/repos/jacquev6/PyGithub/issues/28/labels +{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'} +null +200 +[('status', '200 OK'), ('x-ratelimit-remaining', '4986'), ('content-length', '129'), ('server', 'nginx/1.0.13'), ('connection', 'keep-alive'), ('x-ratelimit-limit', '5000'), ('etag', '"5352ae15c8a5a36c6cace63be9367332"'), ('date', 'Sun, 27 May 2012 09:04:05 GMT'), ('content-type', 'application/json; charset=utf-8')] +[{"url":"https://api.github.com/repos/jacquev6/PyGithub/labels/Project+management","name":"Project management","color":"444444"}] + +https +POST +api.github.com +None +/repos/jacquev6/PyGithub/issues/28/labels +{'Content-Type': 'application/json', 'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'} +["Bug", "Question"] +200 +[('status', '200 OK'), ('x-ratelimit-remaining', '4985'), ('content-length', '335'), ('server', 'nginx/1.0.13'), ('connection', 'keep-alive'), ('x-ratelimit-limit', '5000'), ('etag', '"d135d74d2ea2159d044676a220d41d3a"'), ('date', 'Sun, 27 May 2012 09:04:06 GMT'), ('content-type', 'application/json; charset=utf-8')] +[{"color":"e10c02","url":"https://api.github.com/repos/jacquev6/PyGithub/labels/Bug","name":"Bug"},{"color":"444444","url":"https://api.github.com/repos/jacquev6/PyGithub/labels/Project+management","name":"Project management"},{"color":"02e10c","url":"https://api.github.com/repos/jacquev6/PyGithub/labels/Question","name":"Question"}] + +https +GET +api.github.com +None +/repos/jacquev6/PyGithub/issues/28/labels +{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'} +null +200 +[('status', '200 OK'), ('x-ratelimit-remaining', '4984'), ('content-length', '335'), ('server', 'nginx/1.0.13'), ('connection', 'keep-alive'), ('x-ratelimit-limit', '5000'), ('etag', '"c9f9beccb03030beaf7b80927da6fef6"'), ('date', 'Sun, 27 May 2012 09:04:06 GMT'), ('content-type', 'application/json; charset=utf-8')] +[{"url":"https://api.github.com/repos/jacquev6/PyGithub/labels/Bug","name":"Bug","color":"e10c02"},{"url":"https://api.github.com/repos/jacquev6/PyGithub/labels/Project+management","name":"Project management","color":"444444"},{"url":"https://api.github.com/repos/jacquev6/PyGithub/labels/Question","name":"Question","color":"02e10c"}] + diff --git a/github/tests/ReplayData/Issue.testDeleteAndSetLabelsWithStringArguments.txt b/github/tests/ReplayData/Issue.testDeleteAndSetLabelsWithStringArguments.txt new file mode 100755 index 00000000..f0201446 --- /dev/null +++ b/github/tests/ReplayData/Issue.testDeleteAndSetLabelsWithStringArguments.txt @@ -0,0 +1,55 @@ +https +GET +api.github.com +None +/repos/jacquev6/PyGithub/issues/28/labels +{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'} +null +200 +[('status', '200 OK'), ('x-ratelimit-remaining', '4972'), ('content-length', '335'), ('server', 'nginx/1.0.13'), ('connection', 'keep-alive'), ('x-ratelimit-limit', '5000'), ('etag', '"d135d74d2ea2159d044676a220d41d3a"'), ('date', 'Sun, 27 May 2012 09:06:39 GMT'), ('content-type', 'application/json; charset=utf-8')] +[{"color":"e10c02","url":"https://api.github.com/repos/jacquev6/PyGithub/labels/Bug","name":"Bug"},{"color":"444444","url":"https://api.github.com/repos/jacquev6/PyGithub/labels/Project+management","name":"Project management"},{"color":"02e10c","url":"https://api.github.com/repos/jacquev6/PyGithub/labels/Question","name":"Question"}] + +https +DELETE +api.github.com +None +/repos/jacquev6/PyGithub/issues/28/labels +{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'} +null +204 +[('status', '204 No Content'), ('x-ratelimit-remaining', '4971'), ('server', 'nginx/1.0.13'), ('connection', 'keep-alive'), ('x-ratelimit-limit', '5000'), ('etag', '"d41d8cd98f00b204e9800998ecf8427e"'), ('date', 'Sun, 27 May 2012 09:06:39 GMT')] + + +https +GET +api.github.com +None +/repos/jacquev6/PyGithub/issues/28/labels +{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'} +null +200 +[('status', '200 OK'), ('x-ratelimit-remaining', '4970'), ('content-length', '2'), ('server', 'nginx/1.0.13'), ('connection', 'keep-alive'), ('x-ratelimit-limit', '5000'), ('etag', '"d751713988987e9331980363e24189ce"'), ('date', 'Sun, 27 May 2012 09:06:40 GMT'), ('content-type', 'application/json; charset=utf-8')] +[] + +https +PUT +api.github.com +None +/repos/jacquev6/PyGithub/issues/28/labels +{'Content-Type': 'application/json', 'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'} +["Bug", "Question"] +200 +[('status', '200 OK'), ('x-ratelimit-remaining', '4969'), ('content-length', '207'), ('server', 'nginx/1.0.13'), ('connection', 'keep-alive'), ('x-ratelimit-limit', '5000'), ('etag', '"1a56634d9c1050a88592ff55ed8adc62"'), ('date', 'Sun, 27 May 2012 09:06:40 GMT'), ('content-type', 'application/json; charset=utf-8')] +[{"url":"https://api.github.com/repos/jacquev6/PyGithub/labels/Bug","name":"Bug","color":"e10c02"},{"url":"https://api.github.com/repos/jacquev6/PyGithub/labels/Question","name":"Question","color":"02e10c"}] + +https +GET +api.github.com +None +/repos/jacquev6/PyGithub/issues/28/labels +{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'} +null +200 +[('status', '200 OK'), ('x-ratelimit-remaining', '4968'), ('content-length', '207'), ('server', 'nginx/1.0.13'), ('connection', 'keep-alive'), ('x-ratelimit-limit', '5000'), ('etag', '"1a56634d9c1050a88592ff55ed8adc62"'), ('date', 'Sun, 27 May 2012 09:06:41 GMT'), ('content-type', 'application/json; charset=utf-8')] +[{"url":"https://api.github.com/repos/jacquev6/PyGithub/labels/Bug","name":"Bug","color":"e10c02"},{"url":"https://api.github.com/repos/jacquev6/PyGithub/labels/Question","name":"Question","color":"02e10c"}] +