Esempio n. 1
0
    async def get_org_repos(self, org: str, page=1):
        """Retrun a list of AIOGithubRepository objects."""
        from aiogithubapi import AIOGithubRepository

        url = f"{BASE_URL}/orgs/{org}/repos?page={str(page)}"
        params = {"per_page": 100}
        headers = self.headers
        headers["Accept"] = "application/vnd.github.mercy-preview+json"

        await self.get_ratelimit()
        if self.ratelimits.remaining is not None and self.ratelimits.remaining == 0:
            raise AIOGitHubRatelimit("GitHub Ratelimit error")

        async with async_timeout.timeout(20, loop=get_event_loop()):
            response = await self.session.get(url, headers=headers, params=params)
            if response.status not in GOOD_HTTP_CODES:
                raise AIOGitHubException(f"GitHub returned {response.status} for {url}")

            response = await response.json()

            if not isinstance(response, list):
                if response["message"] == "Bad credentials":
                    raise AIOGitHubAuthentication("Access token is not valid!")
                else:
                    raise AIOGitHubException(response["message"])

            repositories = []

            for repository in response:
                repositories.append(
                    AIOGithubRepository(repository, self.token, self.session)
                )

        return repositories
Esempio n. 2
0
    async def get_repo(self, repo: str):
        """Retrun AIOGithubRepository object."""
        from aiogithubapi import AIOGithubRepository

        url = f"{BASE_URL}/repos/{repo}"

        headers = self.headers
        headers["Accept"] = "application/vnd.github.mercy-preview+json"

        await self.get_ratelimit()
        if self.ratelimits.remaining is not None and self.ratelimits.remaining == 0:
            raise AIOGitHubRatelimit("GitHub Ratelimit error")

        async with async_timeout.timeout(20, loop=get_event_loop()):
            response = await self.session.get(url, headers=headers)
            if response.status not in GOOD_HTTP_CODES:
                raise AIOGitHubException(f"GitHub returned {response.status} for {url}")
            response = await response.json()

            if response.get("message"):
                if response["message"] == "Bad credentials":
                    raise AIOGitHubAuthentication("Access token is not valid!")
                else:
                    raise AIOGitHubException(response["message"])

        return AIOGithubRepository(response, self.token, self.session)
Esempio n. 3
0
async def load_hacs_repository():
    """Load HACS repositroy."""
    hacs = get_hacs()
    from .const import VERSION
    from aiogithubapi import (
        AIOGitHubAuthentication,
        AIOGitHubException,
        AIOGitHubRatelimit,
    )

    try:
        repository = hacs.get_by_name("hacs/integration")
        if repository is None:
            await register_repository("hacs/integration", "integration")
            repository = hacs.get_by_name("hacs/integration")
        if repository is None:
            raise AIOGitHubException("Unknown error")
        repository.status.installed = True
        repository.versions.installed = VERSION
        repository.status.new = False
        hacs.repo = repository.repository_object
        hacs.data_repo = await get_repository(hacs.session,
                                              hacs.configuration.token,
                                              "hacs/default")
    except (
            AIOGitHubException,
            AIOGitHubRatelimit,
            AIOGitHubAuthentication,
    ) as exception:
        hacs.logger.critical(f"[{exception}] - Could not load HACS!")
        return False
    return True
Esempio n. 4
0
    async def render_markdown(self, content: str):
        """Retrun AIOGithubRepository object."""
        url = f"{BASE_URL}/markdown/raw"
        headers = self.headers
        headers["Content-Type"] = "text/plain"

        await self.get_ratelimit()
        if self.ratelimits.remaining is not None and self.ratelimits.remaining == 0:
            raise AIOGitHubRatelimit("GitHub Ratelimit error")

        async with async_timeout.timeout(20, loop=get_event_loop()):
            response = await self.session.post(url, headers=headers, data=content)
            if response.status not in GOOD_HTTP_CODES:
                raise AIOGitHubException(f"GitHub returned {response.status} for {url}")
            response = await response.text()

            if isinstance(response, dict):
                if response.get("message"):
                    if response["message"] == "Bad credentials":
                        raise AIOGitHubAuthentication("Access token is not valid!")
                    else:
                        raise AIOGitHubException(response["message"])

        return response
Esempio n. 5
0
async def load_hacs_repository(hacs):
    """Load HACS repositroy."""
    try:
        await hacs().register_repository("custom-components/hacs", "integration")
        repository = hacs().get_by_name("custom-components/hacs")
        if repository is None:
            raise AIOGitHubException("Unknown error")
        repository.status.installed = True
        repository.versions.installed = const.VERSION
        repository.status.new = False
        hacs.repo = repository.repository_object
    except (
        AIOGitHubException,
        AIOGitHubRatelimit,
        AIOGitHubAuthentication,
    ) as exception:
        hacs.logger.critical(f"[{exception}] - Could not load HACS!")
        return False
    return True
Esempio n. 6
0
    async def get_ratelimit(self):
        """Retrun a list of AIOGithubRepository objects."""
        url = f"{BASE_URL}/rate_limit"
        headers = self.headers

        async with async_timeout.timeout(5, loop=get_event_loop()):
            response = await self.session.get(url, headers=headers)

            # Handle bad status codes
            if response.status not in GOOD_HTTP_CODES:
                raise AIOGitHubException(f"GitHub returned {response.status} for {url}")

            # Convert response to json
            response = await response.json()

            # Ready AIOGithubRateLimits object
            ratelimit = AIOGithubRateLimits(response)
            self.ratelimits = ratelimit

        return self.ratelimits
Esempio n. 7
0
async def load_new_hacs_repository(hacs):
    """Load HACS repositroy."""
    try:
        repository = hacs().get_by_name("hacs/integration")
        if repository is None:
            await hacs().register_repository("hacs/integration", "integration")
            repository = hacs().get_by_name("hacs/integration")
        if repository is None:
            raise AIOGitHubException("Unknown error")
        repository.status.installed = True
        repository.versions.installed = const.VERSION
        repository.status.new = False
        hacs.repo = repository.repository_object
        hacs.data_repo = await hacs().github.get_repo("hacs/default")
    except (
        AIOGitHubException,
        AIOGitHubRatelimit,
        AIOGitHubAuthentication,
    ) as exception:
        hacs.logger.debug(f"[{exception}] - Could not load HACS!")
        # TODO: After move, enable critical
        # hacs.logger.critical(f"[{exception}] - Could not load HACS!")
        return False
    return True