From db09a1deedadfe6fa05041b0a635d416c0d1cb29 Mon Sep 17 00:00:00 2001 From: pseusys Date: Sun, 26 Feb 2023 09:48:03 +0100 Subject: [PATCH] stats commits pushed on behalf of bot --- .github/workflows/review_pr.yml | 1 + sources/manager_download.py | 20 ++++++++++++-------- sources/manager_environment.py | 2 ++ sources/manager_github.py | 12 ++++++------ 4 files changed, 21 insertions(+), 14 deletions(-) diff --git a/.github/workflows/review_pr.yml b/.github/workflows/review_pr.yml index e3b8b95..a09185c 100644 --- a/.github/workflows/review_pr.yml +++ b/.github/workflows/review_pr.yml @@ -55,4 +55,5 @@ jobs: INPUT_DEBUG_LOGGING: True # This is for testing purpose only not for production DEBUG_RUN: True # This is for testing purpose only not for production PR_NUMBER: ${{ github.event.number }} # This is for testing purpose only not for production + CURRENT_GITHUB_ACTION_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This is for testing purpose only not for production run: python3 sources/main.py diff --git a/sources/manager_download.py b/sources/manager_download.py index 62c2ed4..6208fc4 100644 --- a/sources/manager_download.py +++ b/sources/manager_download.py @@ -227,14 +227,15 @@ class DownloadManager: return await DownloadManager._get_remote_resource(resource, safe_load) @staticmethod - async def _fetch_graphql_query(query: str, **kwargs) -> Dict: + async def _fetch_graphql_query(query: str, use_github_action: bool = False, **kwargs) -> Dict: """ Execute GitHub GraphQL API simple query. :param query: Dynamic query identifier. + :param use_github_action: Use GitHub actions bot auth token instead of current user. :param kwargs: Parameters for substitution of variables in dynamic query. :return: Response JSON dictionary. """ - headers = {"Authorization": f"Bearer {EM.GH_TOKEN}"} + headers = {"Authorization": f"Bearer {EM.GH_TOKEN if not use_github_action else EM.CURRENT_GITHUB_ACTION_TOKEN}"} res = await DownloadManager._client.post( "https://api.github.com/graphql", json={"query": Template(GITHUB_API_QUERIES[query]).substitute(kwargs)}, headers=headers ) @@ -267,19 +268,21 @@ class DownloadManager: return list(), dict(hasNextPage=False) @staticmethod - async def _fetch_graphql_paginated(query: str, **kwargs) -> Dict: + async def _fetch_graphql_paginated(query: str, use_github_action: bool = False, **kwargs) -> Dict: """ Execute GitHub GraphQL API paginated query. Queries 100 new results each time until no more results are left. Merges result list into single query, clears pagination-related info. :param query: Dynamic query identifier. + :param use_github_action: Use GitHub actions bot auth token instead of current user. :param kwargs: Parameters for substitution of variables in dynamic query. :return: Response JSON dictionary. """ - initial_query_response = await DownloadManager._fetch_graphql_query(query, **kwargs, pagination="first: 100") + initial_query_response = await DownloadManager._fetch_graphql_query(query, use_github_action, **kwargs, pagination="first: 100") page_list, page_info = DownloadManager._find_pagination_and_data_list(initial_query_response) while page_info["hasNextPage"]: - query_response = await DownloadManager._fetch_graphql_query(query, **kwargs, pagination=f'first: 100, after: "{page_info["endCursor"]}"') + pagination = f'first: 100, after: "{page_info["endCursor"]}"' + query_response = await DownloadManager._fetch_graphql_query(query, use_github_action, **kwargs, pagination=pagination) new_page_list, page_info = DownloadManager._find_pagination_and_data_list(query_response) page_list += new_page_list _, page_info = DownloadManager._find_pagination_and_data_list(initial_query_response) @@ -287,7 +290,7 @@ class DownloadManager: return initial_query_response @staticmethod - async def get_remote_graphql(query: str, **kwargs) -> Dict: + async def get_remote_graphql(query: str, use_github_action: bool = False, **kwargs) -> Dict: """ Execute GitHub GraphQL API query. The queries are defined in `GITHUB_API_QUERIES`, all parameters should be passed as kwargs. @@ -295,15 +298,16 @@ class DownloadManager: Merges paginated sub-queries if pagination is required for the query. Parse and return response as JSON. :param query: Dynamic query identifier. + :param use_github_action: Use GitHub actions bot auth token instead of current user. :param kwargs: Parameters for substitution of variables in dynamic query. :return: Response JSON dictionary. """ key = f"{query}_{md5(dumps(kwargs, sort_keys=True).encode('utf-8')).digest()}" if key not in DownloadManager._REMOTE_RESOURCES_CACHE: if "$pagination" in GITHUB_API_QUERIES[query]: - res = await DownloadManager._fetch_graphql_paginated(query, **kwargs) + res = await DownloadManager._fetch_graphql_paginated(query, use_github_action, **kwargs) else: - res = await DownloadManager._fetch_graphql_query(query, **kwargs) + res = await DownloadManager._fetch_graphql_query(query, use_github_action, **kwargs) DownloadManager._REMOTE_RESOURCES_CACHE[key] = res else: res = DownloadManager._REMOTE_RESOURCES_CACHE[key] diff --git a/sources/manager_environment.py b/sources/manager_environment.py index 53c543c..d98fb07 100644 --- a/sources/manager_environment.py +++ b/sources/manager_environment.py @@ -46,4 +46,6 @@ class EnvironmentManager: DEBUG_LOGGING = getenv("INPUT_DEBUG_LOGGING", "0").lower() in _TRUTHY DEBUG_RUN = getenv("DEBUG_RUN", "False").lower() in _TRUTHY + PR_NUMBER = int(getenv("PR_NUMBER", "0")) + CURRENT_GITHUB_ACTION_TOKEN = getenv("CURRENT_GITHUB_ACTION_TOKEN", "") diff --git a/sources/manager_github.py b/sources/manager_github.py index 45ed276..c757dae 100644 --- a/sources/manager_github.py +++ b/sources/manager_github.py @@ -18,7 +18,6 @@ def init_github_manager(): class GitHubManager: - _GITHUB: Github USER: AuthenticatedUser REPO: Repository _README: ContentFile @@ -37,9 +36,9 @@ class GitHubManager: - README.md file of this repo. - Parsed contents of the file. """ - GitHubManager._GITHUB = Github(EM.GH_TOKEN) - GitHubManager.USER = GitHubManager._GITHUB.get_user() - GitHubManager.REPO = GitHubManager._GITHUB.get_repo(f"{GitHubManager.USER.login}/{GitHubManager.USER.login}") + github = Github(EM.GH_TOKEN) + GitHubManager.USER = github.get_user() + GitHubManager.REPO = github.get_repo(f"{GitHubManager.USER.login}/{GitHubManager.USER.login}") GitHubManager._README = GitHubManager.REPO.get_readme() GitHubManager._README_CONTENTS = str(b64decode(GitHubManager._README.content), "utf-8") @@ -113,9 +112,10 @@ class GitHubManager: prefix = "README stats current output:" DBM.i("Commenting PR...") - pull_request = GitHubManager._GITHUB.get_repo("anmol098/waka-readme-stats").get_pull(EM.PR_NUMBER) + github = Github(EM.CURRENT_GITHUB_ACTION_TOKEN) + pull_request = github.get_repo("anmol098/waka-readme-stats").get_pull(EM.PR_NUMBER) for comment in [ic for ic in pull_request.get_issue_comments() if ic.body.startswith(prefix)]: - await DM.get_remote_graphql("hide_outdated_comment", id=comment.id) + await DM.get_remote_graphql("hide_outdated_comment", use_github_action=True, id=comment.id) pull_request.create_issue_comment(f"{prefix}\n\n{stats}") DBM.g("PR commented!")