Skip to content

Instantly share code, notes, and snippets.

@toopay
Created March 1, 2017 08:10
Show Gist options
  • Save toopay/378309852704e4efde11cfdc85523f30 to your computer and use it in GitHub Desktop.
Save toopay/378309852704e4efde11cfdc85523f30 to your computer and use it in GitHub Desktop.
PyGithub extension
"""
project.py
~~~~~~~~
Extending Github repo class to allow project access
:author: Taufan Aditya
"""
import github
import github.GithubObject
import github.PaginatedList
class Github(github.MainClass.Github):
"""
This is the extended mainclass to support project API
"""
def get_repo(self, full_name_or_id, lazy=True):
"""
:calls: `GET /repos/:owner/:repo <http://developer.github.com/v3/repos>`_ or `GET /repositories/:id <http://developer.github.com/v3/repos>`_
:rtype: :class:`github.Repository.Repository`
"""
assert isinstance(full_name_or_id, (str, unicode, int, long)), full_name_or_id
url_base = "/repositories/" if isinstance(full_name_or_id, int) or isinstance(full_name_or_id, long) else "/repos/"
url = "%s%s" % (url_base, full_name_or_id)
if lazy:
return Repository(self.__requester, {}, {"url": url}, completed=False)
headers, data = self.__requester.requestJsonAndCheck(
"GET",
"%s%s" % (url_base, full_name_or_id)
)
return Repository(self.__requester, headers, data, completed=True)
class Repository(github.Repository.Repository):
"""
This is the extended Repository class to support project API
"""
def get_projects(self):
"""
:calls: `GET /repos/:owner/:repo/projects <https://developer.github.com/v3/projects/>`_
:rtype: :class:`github.PaginatedList.PaginatedList` of :class:`Project`
"""
return github.PaginatedList.PaginatedList(
Project,
self._requester,
self.url + "/projects",
None,
{'Accept': 'application/vnd.github.inertia-preview+json'}
)
class Project(github.GithubObject.CompletableGithubObject):
"""
This class represents Project as returned for example by https://developer.github.com/v3/projects/
"""
def __repr__(self):
return self.get__repr__({"id": self._id.value, "name": self._name.value})
@property
def creator(self):
"""
:type: :class:`github.NamedUser.NamedUser`
"""
self._completeIfNotSet(self._creator)
return self._creator.value
@property
def body(self):
"""
:type: string
"""
self._completeIfNotSet(self._body)
return self._body.value
@property
def created_at(self):
"""
:type: datetime.datetime
"""
self._completeIfNotSet(self._created_at)
return self._created_at.value
@property
def owner_url(self):
"""
:type: string
"""
self._completeIfNotSet(self._owner_url)
return self._owner_url.value
@property
def url(self):
"""
:type: string
"""
self._completeIfNotSet(self._url)
return self._url.value
@property
def id(self):
"""
:type: integer
"""
self._completeIfNotSet(self._id)
return self._id.value
@property
def name(self):
"""
:type: string
"""
self._completeIfNotSet(self._name)
return self._name.value
@property
def _identity(self):
return self.id
def get_columns(self):
"""
:calls: `GET /projects/:id/columns <https://developer.github.com/v3/projects/columns/>`_
:rtype: :class:`github.PaginatedList.PaginatedList` of :class:`Column`
"""
return github.PaginatedList.PaginatedList(
Column,
self._requester,
self.url + "/columns",
None,
{'Accept': 'application/vnd.github.inertia-preview+json'}
)
def _initAttributes(self):
self._id = github.GithubObject.NotSet
self._creator = github.GithubObject.NotSet
self._body = github.GithubObject.NotSet
self._created_at = github.GithubObject.NotSet
self._owner_url = github.GithubObject.NotSet
self._number = github.GithubObject.NotSet
self._name = github.GithubObject.NotSet
self._url = github.GithubObject.NotSet
def _useAttributes(self, attributes):
if "creator" in attributes: # pragma no branch
self._creator = self._makeClassAttribute(github.NamedUser.NamedUser, attributes["creator"])
if "body" in attributes: # pragma no branch
self._body = self._makeStringAttribute(attributes["body"])
if "created_at" in attributes: # pragma no branch
self._created_at = self._makeDatetimeAttribute(attributes["created_at"])
if "owner_url" in attributes: # pragma no branch
self._owner_url = self._makeStringAttribute(attributes["owner_url"])
if "id" in attributes: # pragma no branch
self._id = self._makeIntAttribute(attributes["id"])
if "number" in attributes: # pragma no branch
self._number = self._makeIntAttribute(attributes["number"])
if "name" in attributes: # pragma no branch
self._name = self._makeStringAttribute(attributes["name"])
if "url" in attributes: # pragma no branch
self._url = self._makeStringAttribute(attributes["url"])
class Column(github.GithubObject.CompletableGithubObject):
"""
This class represents Project column as returned for example by https://developer.github.com/v3/projects/:number/columns
"""
def __repr__(self):
return self.get__repr__({"id": self._id.value, "name": self._name.value})
@property
def created_at(self):
"""
:type: datetime.datetime
"""
self._completeIfNotSet(self._created_at)
return self._created_at.value
@property
def project_url(self):
"""
:type: string
"""
self._completeIfNotSet(self._project_url)
return self._project_url.value
@property
def id(self):
"""
:type: integer
"""
self._completeIfNotSet(self._id)
return self._id.value
@property
def name(self):
"""
:type: string
"""
self._completeIfNotSet(self._name)
return self._name.value
@property
def _identity(self):
return self.id
def get_cards(self):
"""
:calls: `GET /projects/:number/columns <https://developer.github.com/v3/projects/columns/cards>`_
:rtype: :class:`github.PaginatedList.PaginatedList` of :class:`Card`
"""
return github.PaginatedList.PaginatedList(
Column,
self._requester,
self.url + "/cards",
None,
{'Accept': 'application/vnd.github.inertia-preview+json'}
)
def _initAttributes(self):
self._id = github.GithubObject.NotSet
self._project_url = github.GithubObject.NotSet
self._created_at = github.GithubObject.NotSet
self._name = github.GithubObject.NotSet
def _useAttributes(self, attributes):
if "created_at" in attributes: # pragma no branch
self._created_at = self._makeDatetimeAttribute(attributes["created_at"])
if "project_url" in attributes: # pragma no branch
self._project_url = self._makeStringAttribute(attributes["project_url"])
if "id" in attributes: # pragma no branch
self._id = self._makeIntAttribute(attributes["id"])
if "name" in attributes: # pragma no branch
self._name = self._makeStringAttribute(attributes["name"])
class Card(github.GithubObject.CompletableGithubObject):
"""
This class represents Project card as returned for example by https://developer.github.com/v3/projects/columns/:id/cards
"""
def __repr__(self):
return self.get__repr__({"id": self._id.value, "note": self._note.value})
@property
def created_at(self):
"""
:type: datetime.datetime
"""
self._completeIfNotSet(self._created_at)
return self._created_at.value
@property
def content_url(self):
"""
:type: string
"""
self._completeIfNotSet(self._content_url)
return self._content_url.value
@property
def column_url(self):
"""
:type: string
"""
self._completeIfNotSet(self._column_url)
return self._column_url.value
@property
def id(self):
"""
:type: integer
"""
self._completeIfNotSet(self._id)
return self._id.value
@property
def note(self):
"""
:type: string
"""
self._completeIfNotSet(self._note)
return self._note.value
@property
def _identity(self):
return self.id
def get_issue(self):
"""
:calls: `GET /repos/:owner/:repo/issues/:number <http://developer.github.com/v3/issues>`_
:param number: integer
:rtype: :class:`github.Issue.Issue`
"""
headers, data = self._requester.requestJsonAndCheck(
"GET",
self.content_url
)
return github.Issue.Issue(self._requester, headers, data, completed=True)
def _initAttributes(self):
self._id = github.GithubObject.NotSet
self._content_url = github.GithubObject.NotSet
self._column_url = github.GithubObject.NotSet
self._created_at = github.GithubObject.NotSet
self._note = github.GithubObject.NotSet
def _useAttributes(self, attributes):
if "created_at" in attributes: # pragma no branch
self._created_at = self._makeDatetimeAttribute(attributes["created_at"])
if "column_url" in attributes: # pragma no branch
self._column_url = self._makeStringAttribute(attributes["column_url"])
if "content_url" in attributes: # pragma no branch
self._content_url = self._makeStringAttribute(attributes["content_url"])
if "id" in attributes: # pragma no branch
self._id = self._makeIntAttribute(attributes["id"])
if "note" in attributes: # pragma no branch
self._note = self._makeStringAttribute(attributes["note"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment