Skip to content

Commit 22f366b

Browse files
committed
Accept strings as well as Label objects (PyGithub#202)
This should be more generic, but it's a wig work to do it everywhere. Let's keep that in mind for V2.
1 parent 54f718a commit 22f366b

5 files changed

Lines changed: 165 additions & 14 deletions

File tree

README.rst

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@ Thank you, dear stargazers!
1616

1717
Starting today (September 5th, 2013), we now need more than 8 bits to store the number of `stargazers <https://github.com/jacquev6/PyGithub/stargazers>`_! Thank you so much!
1818

19-
`Version 1.20.0 <https://github.com/jacquev6/PyGithub/issues?milestone=32&state=closed>`_ (October 20th, 2013) (First Seattle edition)
20-
--------------------------------------------------------------------------------------------------------------------------------------
19+
`Version 1.21.0 <https://github.com/jacquev6/PyGithub/issues?milestone=33&state=closed>`_ (November ??th, 2013)
20+
---------------------------------------------------------------------------------------------------------------
2121

22-
* `Implement <https://github.com/jacquev6/PyGithub/issues/196>`_ ``Github.get_hook(name)``. Thank you `klmitch <https://github.com/klmitch>`_ for asking
23-
* In case bad data is returned by Github API v3, `raise <https://github.com/jacquev6/PyGithub/issues/195>`_ an exception only when the user accesses the faulty attribute, not when constructing the object containing this attribute. Thank you `klmitch <https://github.com/klmitch>`_ for asking
24-
* `Fix <https://github.com/jacquev6/PyGithub/issues/199>`_ parameter public/private of ``Repository.edit``. Thank you `daireobroin449 <https://github.com/daireobroin449>`_ for reporting the issue
25-
* Remove ``Repository.create_download`` and ``NamedUser.create_gist`` as the corrensponding APIs are not documented anymore
22+
* `Accept <https://github.com/jacquev6/PyGithub/issues/202>`__ strings as well as ``Label`` objects in ``Issue.add_to_labels``, ``Issue.remove_from_labels`` and ``Issue.set_labels``. Thank you `acdha <https://github.com/acdha>`__ for asking
2623

2724
Twitter
2825
-------

github/Issue.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -219,11 +219,11 @@ def user(self):
219219
def add_to_labels(self, *labels):
220220
"""
221221
:calls: `POST /repos/:owner/:repo/issues/:number/labels <http://developer.github.com/v3/issues/labels>`_
222-
:param label: :class:`github.Label.Label`
222+
:param label: :class:`github.Label.Label` or string
223223
:rtype: None
224224
"""
225-
assert all(isinstance(element, github.Label.Label) for element in labels), labels
226-
post_parameters = [label.name for label in labels]
225+
assert all(isinstance(element, (github.Label.Label, str, unicode)) for element in labels), labels
226+
post_parameters = [label.name if isinstance(label, github.Label.Label) else label for label in labels]
227227
headers, data = self._requester.requestJsonAndCheck(
228228
"POST",
229229
self.url + "/labels",
@@ -346,13 +346,15 @@ def get_labels(self):
346346
def remove_from_labels(self, label):
347347
"""
348348
:calls: `DELETE /repos/:owner/:repo/issues/:number/labels/:name <http://developer.github.com/v3/issues/labels>`_
349-
:param label: :class:`github.Label.Label`
349+
:param label: :class:`github.Label.Label` or string
350350
:rtype: None
351351
"""
352-
assert isinstance(label, github.Label.Label), label
352+
assert isinstance(label, (github.Label.Label, str, unicode)), label
353+
if isinstance(label, github.Label.Label):
354+
label = label._identity
353355
headers, data = self._requester.requestJsonAndCheck(
354356
"DELETE",
355-
self.url + "/labels/" + label._identity
357+
self.url + "/labels/" + label
356358
)
357359

358360
def set_labels(self, *labels):
@@ -361,8 +363,8 @@ def set_labels(self, *labels):
361363
:param label: :class:`github.Label.Label`
362364
:rtype: None
363365
"""
364-
assert all(isinstance(element, github.Label.Label) for element in labels), labels
365-
post_parameters = [label.name for label in labels]
366+
assert all(isinstance(element, (github.Label.Label, str, unicode)) for element in labels), labels
367+
post_parameters = [label.name if isinstance(label, github.Label.Label) else label for label in labels]
366368
headers, data = self._requester.requestJsonAndCheck(
367369
"PUT",
368370
self.url + "/labels",

github/tests/Issue.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,17 @@ def testAddAndRemoveLabels(self):
104104
self.issue.add_to_labels(bug, question)
105105
self.assertListKeyEqual(self.issue.get_labels(), lambda l: l.name, ["Bug", "Project management", "Question"])
106106

107+
def testAddAndRemoveLabelsWithStringArguments(self):
108+
bug = "Bug"
109+
question = "Question"
110+
self.assertListKeyEqual(self.issue.get_labels(), lambda l: l.name, ["Bug", "Project management", "Question"])
111+
self.issue.remove_from_labels(bug)
112+
self.assertListKeyEqual(self.issue.get_labels(), lambda l: l.name, ["Project management", "Question"])
113+
self.issue.remove_from_labels(question)
114+
self.assertListKeyEqual(self.issue.get_labels(), lambda l: l.name, ["Project management"])
115+
self.issue.add_to_labels(bug, question)
116+
self.assertListKeyEqual(self.issue.get_labels(), lambda l: l.name, ["Bug", "Project management", "Question"])
117+
107118
def testDeleteAndSetLabels(self):
108119
bug = self.repo.get_label("Bug")
109120
question = self.repo.get_label("Question")
@@ -112,3 +123,12 @@ def testDeleteAndSetLabels(self):
112123
self.assertListKeyEqual(self.issue.get_labels(), None, [])
113124
self.issue.set_labels(bug, question)
114125
self.assertListKeyEqual(self.issue.get_labels(), lambda l: l.name, ["Bug", "Question"])
126+
127+
def testDeleteAndSetLabelsWithStringArguments(self):
128+
bug = "Bug"
129+
question = "Question"
130+
self.assertListKeyEqual(self.issue.get_labels(), lambda l: l.name, ["Bug", "Project management", "Question"])
131+
self.issue.delete_labels()
132+
self.assertListKeyEqual(self.issue.get_labels(), None, [])
133+
self.issue.set_labels(bug, question)
134+
self.assertListKeyEqual(self.issue.get_labels(), lambda l: l.name, ["Bug", "Question"])
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
https
2+
GET
3+
api.github.com
4+
None
5+
/repos/jacquev6/PyGithub/issues/28/labels
6+
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
7+
null
8+
200
9+
[('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')]
10+
[{"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"}]
11+
12+
https
13+
DELETE
14+
api.github.com
15+
None
16+
/repos/jacquev6/PyGithub/issues/28/labels/Bug
17+
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
18+
null
19+
200
20+
[('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')]
21+
[{"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"}]
22+
23+
https
24+
GET
25+
api.github.com
26+
None
27+
/repos/jacquev6/PyGithub/issues/28/labels
28+
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
29+
null
30+
200
31+
[('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')]
32+
[{"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"}]
33+
34+
https
35+
DELETE
36+
api.github.com
37+
None
38+
/repos/jacquev6/PyGithub/issues/28/labels/Question
39+
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
40+
null
41+
200
42+
[('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')]
43+
[{"url":"https://api.github.com/repos/jacquev6/PyGithub/labels/Project+management","name":"Project management","color":"444444"}]
44+
45+
https
46+
GET
47+
api.github.com
48+
None
49+
/repos/jacquev6/PyGithub/issues/28/labels
50+
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
51+
null
52+
200
53+
[('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')]
54+
[{"url":"https://api.github.com/repos/jacquev6/PyGithub/labels/Project+management","name":"Project management","color":"444444"}]
55+
56+
https
57+
POST
58+
api.github.com
59+
None
60+
/repos/jacquev6/PyGithub/issues/28/labels
61+
{'Content-Type': 'application/json', 'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
62+
["Bug", "Question"]
63+
200
64+
[('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')]
65+
[{"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"}]
66+
67+
https
68+
GET
69+
api.github.com
70+
None
71+
/repos/jacquev6/PyGithub/issues/28/labels
72+
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
73+
null
74+
200
75+
[('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')]
76+
[{"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"}]
77+
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
https
2+
GET
3+
api.github.com
4+
None
5+
/repos/jacquev6/PyGithub/issues/28/labels
6+
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
7+
null
8+
200
9+
[('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')]
10+
[{"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"}]
11+
12+
https
13+
DELETE
14+
api.github.com
15+
None
16+
/repos/jacquev6/PyGithub/issues/28/labels
17+
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
18+
null
19+
204
20+
[('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')]
21+
22+
23+
https
24+
GET
25+
api.github.com
26+
None
27+
/repos/jacquev6/PyGithub/issues/28/labels
28+
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
29+
null
30+
200
31+
[('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')]
32+
[]
33+
34+
https
35+
PUT
36+
api.github.com
37+
None
38+
/repos/jacquev6/PyGithub/issues/28/labels
39+
{'Content-Type': 'application/json', 'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
40+
["Bug", "Question"]
41+
200
42+
[('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')]
43+
[{"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"}]
44+
45+
https
46+
GET
47+
api.github.com
48+
None
49+
/repos/jacquev6/PyGithub/issues/28/labels
50+
{'Authorization': 'Basic login_and_password_removed', 'User-Agent': 'PyGithub/Python'}
51+
null
52+
200
53+
[('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')]
54+
[{"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"}]
55+

0 commit comments

Comments
 (0)