aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/cgitize/github.py
blob: 02057c98363080b4b3377a0e3dad238fbb34b288 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# Copyright (c) 2021 Egor Tensin <egor@tensin.name>
# This file is part of the "cgitize" project.
# For details, see https://github.com/egor-tensin/cgitize.
# Distributed under the MIT License.

import logging

from github import Github, GithubException

from cgitize.repo import Repo


class GitHub:
    def __init__(self, username, token):
        self._username = username
        self._impl = Github(token)

    def get_repo(self, repo):
        try:
            return self._impl.get_repo(repo.id)
        except GithubException:
            logging.error("Couldn't fetch repository: %s", repo.id)
            raise

    def get_user_repos(self, user):
        try:
            if user.name == self._username:
                # To get private repositories, get_user() must be called
                # without arguments:
                return self._impl.get_user().get_repos(affiliation='owner')
            else:
                return self._impl.get_user(user.name).get_repos()
        except GithubException:
            logging.error("Couldn't fetch user repositories: %s", user.name)
            raise

    def get_org_repos(self, org):
        try:
            return self._impl.get_organization(org.name).get_repos()
        except GithubException:
            logging.error("Couldn't fetch organization repositories: %s", org.name)
            raise

    @staticmethod
    def convert_repo(repo, *args, **kwargs):
        return Repo.from_github(repo, *args, **kwargs)