|
| 1 | +# SPDX-FileCopyrightText: 2022 Alec Delaney, for Adafruit Industries |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +""" |
| 6 | +Checks for the latest releases in the Community bundle based |
| 7 | +on the automated release. |
| 8 | +
|
| 9 | +* Author(s): Alec Delaney |
| 10 | +""" |
| 11 | + |
| 12 | +import datetime |
| 13 | +import logging |
| 14 | +import os |
| 15 | +import time |
| 16 | +from typing import Tuple, Set |
| 17 | +from typing_extensions import TypeAlias |
| 18 | + |
| 19 | +import github as pygithub |
| 20 | +import parse |
| 21 | + |
| 22 | +GH_INTERFACE = pygithub.Github(os.environ.get("ADABOT_GITHUB_ACCESS_TOKEN")) |
| 23 | + |
| 24 | +RepoResult: TypeAlias = Tuple[str, str] |
| 25 | +"""(Submodule Name, Full Repo Name)""" |
| 26 | + |
| 27 | + |
| 28 | +def get_community_bundle_updates() -> Tuple[Set[RepoResult], Set[RepoResult]]: |
| 29 | + """ |
| 30 | + Get the updates to the Community Bundle. |
| 31 | +
|
| 32 | + Returns new and updated libraries |
| 33 | + """ |
| 34 | + while True: |
| 35 | + try: |
| 36 | + repository = GH_INTERFACE.get_repo( |
| 37 | + "adafruit/CircuitPython_Community_Bundle" |
| 38 | + ) |
| 39 | + seven_days_ago = datetime.datetime.now() - datetime.timedelta(days=7) |
| 40 | + recent_releases = [ |
| 41 | + release |
| 42 | + for release in repository.get_releases() |
| 43 | + if release.created_at > seven_days_ago |
| 44 | + ] |
| 45 | + new_libs = set() |
| 46 | + updated_libs = set() |
| 47 | + for recent_release in recent_releases: |
| 48 | + relevant_lines = [ |
| 49 | + line |
| 50 | + for line in recent_release.body.split("\n") |
| 51 | + if line.startswith("Updated libraries") |
| 52 | + or line.startswith("New libraries:") |
| 53 | + ] |
| 54 | + for relevant_line in relevant_lines: |
| 55 | + lib_components = [ |
| 56 | + x.strip(",") for x in relevant_line.split(" ")[2:] |
| 57 | + ] |
| 58 | + for lib in lib_components: |
| 59 | + comps = parse.parse("[{name:S}]({link_comp:S})", lib) |
| 60 | + link: str = parse.search( |
| 61 | + "{link:S}/releases", comps["link_comp"] |
| 62 | + )["link"] |
| 63 | + full_name = parse.search( |
| 64 | + "https://github.com/{full_name:S}", link |
| 65 | + )["full_name"] |
| 66 | + if relevant_line.startswith("Updated libraries"): |
| 67 | + updated_libs.add((full_name, link)) |
| 68 | + else: |
| 69 | + new_libs.add((full_name, link)) |
| 70 | + return (new_libs, updated_libs) |
| 71 | + |
| 72 | + except pygithub.RateLimitExceededException: |
| 73 | + core_rate_limit_reset = GH_INTERFACE.get_rate_limit().core.reset |
| 74 | + sleep_time = core_rate_limit_reset - datetime.datetime.now() |
| 75 | + logging.warning("Rate Limit will reset at: %s", core_rate_limit_reset) |
| 76 | + time.sleep(sleep_time.seconds) |
| 77 | + continue |
| 78 | + except pygithub.GithubException: |
| 79 | + # Secrets may not be available or error occurred - just skip |
| 80 | + return (set(), set()) |
| 81 | + |
| 82 | + |
| 83 | +if __name__ == "__main__": |
| 84 | + results = get_community_bundle_updates() |
| 85 | + for new_lib in results[0]: |
| 86 | + print(f"New libraries: {new_lib[0]} { {new_lib[1]} }") |
| 87 | + for updated_lib in results[1]: |
| 88 | + print(f"New libraries: {updated_lib[0]} { {updated_lib[1]} }") |
0 commit comments