Support labels for PullRequests (#752)

PullRequests can also have labels attached to them, like Issues or
Milestones. Extend PullRequest to also export them if they are present,
as well as adding methods to add, remove and set labels.
This commit is contained in:
Steve Kowalik
2018-04-16 16:43:07 +08:00
committed by Wan Liuyang
parent 1e0150b527
commit a308dc92df
8 changed files with 444 additions and 1 deletions
+79
View File
@@ -17,6 +17,7 @@
# Copyright 2018 Gilad Shefer <gshefer@redhat.com> #
# Copyright 2018 Thibault Jamet <tjamet@users.noreply.github.com> #
# Copyright 2018 sfdye <tsfdye@gmail.com> #
# Copyright 2018 Steve Kowalik <steven@wedontsleep.org> #
# #
# This file is part of PyGithub. #
# http://pygithub.readthedocs.io/ #
@@ -36,6 +37,7 @@
# #
################################################################################
import urllib
import github.GithubObject
import github.PaginatedList
@@ -202,6 +204,14 @@ class PullRequest(github.GithubObject.CompletableGithubObject):
self._completeIfNotSet(self._issue_url)
return self._issue_url.value
@property
def labels(self):
"""
:type: list of :class:`github.Label.Label`
"""
self._completeIfNotSet(self._labels)
return self._labels.value
@property
def merge_commit_sha(self):
"""
@@ -607,6 +617,72 @@ class PullRequest(github.GithubObject.CompletableGithubObject):
list_item='users'
)
def get_labels(self):
"""
:calls: `GET /repos/:owner/:repo/pulls/:number/labels <http://developer.github.com/v3/issues/labels>`_
:rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Label.Label`
"""
return github.PaginatedList.PaginatedList(
github.Label.Label,
self._requester,
self.url + "/labels",
None
)
def add_to_labels(self, *labels):
"""
:calls: `POST /repos/:owner/:repo/pulls/:number/labels <http://developer.github.com/v3/issues/labels>`_
:param label: :class:`github.Label.Label` or string
:rtype: None
"""
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",
input=post_parameters
)
def delete_labels(self):
"""
:calls: `DELETE /repos/:owner/:repo/pulls/:number/labels <http://developer.github.com/v3/issues/labels>`_
:rtype: None
"""
headers, data = self._requester.requestJsonAndCheck(
"DELETE",
self.url + "/labels"
)
def remove_from_labels(self, label):
"""
:calls: `DELETE /repos/:owner/:repo/pulls/:number/labels/:name <http://developer.github.com/v3/issues/labels>`_
:param label: :class:`github.Label.Label` or string
:rtype: None
"""
assert isinstance(label, (github.Label.Label, str, unicode)), label
if isinstance(label, github.Label.Label):
label = label._identity
else:
label = urllib.quote(label)
headers, data = self._requester.requestJsonAndCheck(
"DELETE",
self.url + "/labels/" + label
)
def set_labels(self, *labels):
"""
:calls: `PUT /repos/:owner/:repo/pulls/:number/labels <http://developer.github.com/v3/issues/labels>`_
:param label: :class:`github.Label.Label`
:rtype: None
"""
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",
input=post_parameters
)
def is_merged(self):
"""
:calls: `GET /repos/:owner/:repo/pulls/:number/merge <http://developer.github.com/v3/pulls>`_
@@ -663,6 +739,7 @@ class PullRequest(github.GithubObject.CompletableGithubObject):
self._html_url = github.GithubObject.NotSet
self._id = github.GithubObject.NotSet
self._issue_url = github.GithubObject.NotSet
self._labels = github.GithubObject.NotSet
self._merge_commit_sha = github.GithubObject.NotSet
self._mergeable = github.GithubObject.NotSet
self._mergeable_state = github.GithubObject.NotSet
@@ -723,6 +800,8 @@ class PullRequest(github.GithubObject.CompletableGithubObject):
self._id = self._makeIntAttribute(attributes["id"])
if "issue_url" in attributes: # pragma no branch
self._issue_url = self._makeStringAttribute(attributes["issue_url"])
if "labels" in attributes: # pragma no branch
self._labels = self._makeListOfClassesAttribute(github.Label.Label, attributes["labels"])
if "merge_commit_sha" in attributes: # pragma no branch
self._merge_commit_sha = self._makeStringAttribute(attributes["merge_commit_sha"])
if "mergeable" in attributes: # pragma no branch