Example #1
0
class GitHubConnector(object):

    def __init__(self):

        self.git_hub = GitHub(settings.GITHUB_USER, settings.GITHUB_PASS)
        self.orgs = list(self.git_hub.iter_orgs())

        # As of 10/3/2013, the default rate limit was 5000/hr.
        # Should your code loop infinitely, this exception will
        # leave enough requests to debug the problem without
        # having to wait an hour.
        if self.git_hub.ratelimit_remaining < 500:
            raise Exception('You have only 500 GitHub requests left for this hour')

    def get_issues_for_repo(self, repo_name=None, github_id=None, repo_tuple=None):
        repos = self.git_hub.iter_all_repos()
        for repo in repos:
            if repo_name and repo_name == repo.name:
                return list(repo.iter_issues())
            elif github_id and github_id == repo.id:
                return list(repo.iter_issues())
            elif repo_tuple and repo_tuple[1] == repo.name:
                return list(repo.iter_issues())

    def get_all_issues(self):
        issues = []
        for org in self.orgs:
            org_repos = org.iter_repos()
            for repo in org_repos:
                issues += list(repo.iter_issues(state='open'))
                issues += list(repo.iter_issues(state='closed'))
        return issues

    def get_repos_for_org(self, org_login=None, org_id=None):
        for org in self.orgs:
            org_json = org.to_json()
            if org_json["login"] == org_login or org_json["id"] == org_id:
                return list(org.iter_repos())

    def get_all_repos(self):
        repos = []
        for org in self.orgs:
            org_repos = org.iter_repos()
            for repo in org_repos:
                repos.append(repo)
        return repos
Example #2
0
class Github:
    def __init__(self, _ask_credentials=None, _ask_2fa=None):
        self.last_error = None

        self._ask_credentials = _ask_credentials
        self._ask_2fa = _ask_2fa

        self.gh = GitHub(token=self._get_authorization_token())
        self.user = self.gh.user()

    def _get_authorization_token(self):
        if not config.has('github', 'token') or \
           not config.get('github', 'token'):
            if not self._ask_credentials:
                raise RuntimeError('Github Token is not set in the '
                                   'configuration and no function was set to '
                                   'ask for credentials')

            token = self._gen_authorization_token()
            config.set('github', 'token', token)
            config.save()

        return config.get('github', 'token')

    def _gen_authorization_token(self, counter=0, creds=None):
        """
        This function creates the authorization token for AerisCloud.
        If an existing token exists for this computer, it adds a #N counter
        next to the name.
        """
        if creds:
            user, pwd = creds['user'], creds['pwd']
        else:
            (user, pwd) = self._ask_credentials()

        note = 'AerisCloud on %s' % (node())
        if counter > 0:
            note += ' #%d' % counter

        try:
            auth = authorize(user, pwd, ['repo', 'read:org'], note=note,
                             two_factor_callback=self._ask_2fa)
            return auth.token
        except GitHubError as e:
            if not e.errors or e.errors[0]['code'] != 'already_exists':
                raise

            # token exists, increment counter
            counter += 1
            return self._gen_authorization_token(counter=counter,
                                                 creds={'user': user,
                                                        'pwd': pwd})

    def get_organizations(self):
        return [org for org in self.gh.iter_orgs()]

    def get_repo(self, name, fork=False):
        """
        Find a repository in the available ogranizations, if fork is set to
        True it will try forking it to the user's account
        """
        # check on the user
        repo = self.gh.repository(self.user.login, name)

        if repo:
            return repo, None

        if not config.has('github', 'organizations'):
            return False, None

        # then on configured organization
        organizations = config.get('github', 'organizations').split(',')

        for org in organizations:
            repo = self.gh.repository(org, name)
            if repo:
                break

        if not repo:
            return False, None

        if not fork:
            return repo, None

        return repo.create_fork(), repo