# Path: ghtools/exceptions.py
# class GithubError(Exception):
#     pass
# 
# Path: ghtools/identifier.py
# class Identifier(_Identifier):
#     """
# 
#     A simple identifier of an object in the GitHub API, usually created from a
#     string passed in on the command line.
# 
#     Users can reference a repository on a specific GitHub using the form::
# 
#         <github_name>:<org_name>/<repo_name>
# 
#     Where ``github_name`` is optional (defaulting to 'public').
# 
#     The repository can also be omitted in order to refer to an Organisation::
# 
#         <github_name>:<org_name>
# 
#     Again, ``github_name`` is optional.
# 
#     """
# 
#     @classmethod
#     def from_string(cls, identstring):
# 
#         parts = identstring.split(':', 1)
# 
#         if len(parts) == 1:
#             github = DEFAULT_GITHUB
#             remainder = parts[0]
#         else:
#             github, remainder = parts
# 
#         parts = remainder.split('/', 1)
# 
#         if len(parts) == 1:
#             org = parts[0]
#             repo = None
#         else:
#             org, repo = parts
# 
#         return Identifier(github, org, repo)
# 
#     def __str__(self):
#         out = ''
#         if self.github != DEFAULT_GITHUB:
#             out += self.github + ':'
#         out += self.org
#         if self.repo is not None:
#             out += '/{0}'.format(self.repo)
#         return out
# 
# Path: ghtools/util.py
# def make_client(identifier):
#     """
#     Make a GithubAPIClient suitable for interacting with the passed identifier
#     """
# 
#     return GithubAPIClient(nickname=identifier.github)


import itertools
import logging
from ghtools.exceptions import GithubError
from ghtools.identifier import Identifier
from ghtools.util import make_client

    def list_issue_comments(self, issue):
        url = '/repos/{0}/issues/{1}/comments'.format(self.org_repo, issue['number'])
        return self.client.paged_get(url)

    def list_pulls(self, include_closed=False):
        url = '/repos/{0}/pulls?direction=asc&state={1}'
        open_pulls = self.client.paged_get(url.format(self.org_repo, 'open'))

        if include_closed:
            closed_pulls = self.client.paged_get(url.format(self.org_repo, 'closed'))
            return itertools.chain(open_pulls, closed_pulls)
        else:
            return open_pulls

    def close_issue(self, issue):
        url = '/repos/{0}/issues/{1}'.format(self.org_repo, issue['number'])
        res = self.client.patch(url, data={'state': 'closed'})
        return res.json()

    def open_issue(self, issue):
        url = '/repos/{0}/issues/{1}'.format(self.org_repo, issue['number'])
        res = self.client.patch(url, data={'state': 'open'})
        return res.json()

    def set_build_status(self, sha, status):
        url = '/repos/{0}/statuses/{1}'.format(self.org_repo, sha)
        return self.client.post(url, data=status)

    def __str__(self):

# Path: ghtools/exceptions.py
# class GithubError(Exception):
#     pass
# 
# Path: ghtools/identifier.py
# class Identifier(_Identifier):
#     """
# 
#     A simple identifier of an object in the GitHub API, usually created from a
#     string passed in on the command line.
# 
#     Users can reference a repository on a specific GitHub using the form::
# 
#         <github_name>:<org_name>/<repo_name>
# 
#     Where ``github_name`` is optional (defaulting to 'public').
# 
#     The repository can also be omitted in order to refer to an Organisation::
# 
#         <github_name>:<org_name>
# 
#     Again, ``github_name`` is optional.
# 
#     """
# 
#     @classmethod
#     def from_string(cls, identstring):
# 
#         parts = identstring.split(':', 1)
# 
#         if len(parts) == 1:
#             github = DEFAULT_GITHUB
#             remainder = parts[0]
#         else:
#             github, remainder = parts
# 
#         parts = remainder.split('/', 1)
# 
#         if len(parts) == 1:
#             org = parts[0]
#             repo = None
#         else:
#             org, repo = parts
# 
#         return Identifier(github, org, repo)
# 
#     def __str__(self):
#         out = ''
#         if self.github != DEFAULT_GITHUB:
#             out += self.github + ':'
#         out += self.org
#         if self.repo is not None:
#             out += '/{0}'.format(self.repo)
#         return out
# 
# Path: ghtools/util.py
# def make_client(identifier):
#     """
#     Make a GithubAPIClient suitable for interacting with the passed identifier
#     """
# 
#     return GithubAPIClient(nickname=identifier.github)


# Path: ghtools/github/repo.py
import itertools
import logging
from ghtools.exceptions import GithubError
from ghtools.identifier import Identifier
from ghtools.util import make_client


    def list_issue_comments(self, issue):
        url = '/repos/{0}/issues/{1}/comments'.format(self.org_repo, issue['number'])
        return self.client.paged_get(url)

    def list_pulls(self, include_closed=False):
        url = '/repos/{0}/pulls?direction=asc&state={1}'
        open_pulls = self.client.paged_get(url.format(self.org_repo, 'open'))

        if include_closed:
            closed_pulls = self.client.paged_get(url.format(self.org_repo, 'closed'))
            return itertools.chain(open_pulls, closed_pulls)
        else:
            return open_pulls

    def close_issue(self, issue):
        url = '/repos/{0}/issues/{1}'.format(self.org_repo, issue['number'])
        res = self.client.patch(url, data={'state': 'closed'})
        return res.json()

    def open_issue(self, issue):
        url = '/repos/{0}/issues/{1}'.format(self.org_repo, issue['number'])
        res = self.client.patch(url, data={'state': 'open'})
        return res.json()

    def set_build_status(self, sha, status):
        url = '/repos/{0}/statuses/{1}'.format(self.org_repo, sha)
        return self.client.post(url, data=status)

    def __str__(self):

        return '<Repo {0}>'.format(self._ident)