From 94e4a6cbde9526cde18d6ca85871e271f8e10403 Mon Sep 17 00:00:00 2001 From: Anmol Singh Date: Sat, 25 Feb 2023 01:52:33 +0530 Subject: [PATCH] feat: added ability to generate stats on PR and sent to latest comment on PR --- .github/workflows/review_pr.yml | 54 +++++++++++++++++++++++++++++++++ sources/main.py | 13 ++++++-- sources/manager_download.py | 20 ++++++++++++ sources/manager_environment.py | 1 + 4 files changed, 86 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/review_pr.yml diff --git a/.github/workflows/review_pr.yml b/.github/workflows/review_pr.yml new file mode 100644 index 0000000..d67df3a --- /dev/null +++ b/.github/workflows/review_pr.yml @@ -0,0 +1,54 @@ +name: REVIEW_PULL_REQUEST + +on: + pull_request: + types: [ opened, edited, reopened, synchronize ] + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: ${{ github.ref != 'refs/heads/dev' }} + +jobs: + publish-server-image: + name: Run Test and Review PR + runs-on: ubuntu-latest + + steps: + - name: Checkout ๐Ÿ›Ž๏ธ + uses: actions/checkout@v3 + + - name: Setup Python 3.8 ๐Ÿ + uses: actions/setup-python@v4 + with: + python-version: 3.8 + + - name: Install Dependencies ๐Ÿ“ฅ + run: pip install -r requirements.txt + + - name: Run Current Tests ๐Ÿงช + env: + INPUT_GH_TOKEN: ${{ secrets.INPUT_GITHUB_TOKEN }} + INPUT_WAKATIME_API_KEY: ${{ secrets.WAKATIME_API_KEY }} + PR_NUMBER: ${{ github.event.number }} + INPUT_SHOW_TIMEZONE: true + INPUT_SHOW_PROJECTS: True + INPUT_SHOW_EDITORS: True + INPUT_SHOW_OS: True + INPUT_SHOW_LANGUAGE: True + INPUT_SYMBOL_VERSION: 1 + INPUT_SHOW_LINES_OF_CODE: True + INPUT_SHOW_LOC_CHART: True + INPUT_SHOW_PROFILE_VIEWS: True + INPUT_SHOW_TOTAL_CODE_TIME: True + INPUT_SHOW_SHORT_INFO: True + INPUT_SHOW_COMMIT: True + INPUT_SHOW_DAYS_OF_WEEK: True + INPUT_SHOW_LANGUAGE_PER_REPO: True + INPUT_SHOW_UPDATED_DATE: True + INPUT_COMMIT_BY_ME: True + INPUT_DEBUG_LOGGING: True + DEBUG_RUN: True + run: python3 sources/main.py + + + diff --git a/sources/main.py b/sources/main.py index 9b474d8..8d980b9 100644 --- a/sources/main.py +++ b/sources/main.py @@ -185,8 +185,17 @@ async def main(): init_localization_manager() DBM.i("Managers initialized.") - if GHM.update_readme(await get_stats()): - DBM.g("Readme updated!") + stats = await get_stats() + if not EM.DEBUG_RUN: + if GHM.update_readme(stats): + DBM.g("Readme updated!") + else: + DBM.i("Commenting PR...") + pr_data = await DM.get_remote_graphql("get_pr_id", pr_number=EM.PR_NUMBER) + pr_id = pr_data["data"]["repository"]["pullRequest"]["id"] + await DM.get_remote_graphql("add_pr_comment", pr_id=pr_id, comment=stats) + DBM.g("PR commented!") + DBM.g("Debug run, readme not updated. check the latest comment for the generated stats.") await DM.close_remote_resources() diff --git a/sources/manager_download.py b/sources/manager_download.py index 0505644..a98c06a 100644 --- a/sources/manager_download.py +++ b/sources/manager_download.py @@ -121,6 +121,26 @@ GITHUB_API_QUERIES = { } } """, + # Query to collect current PR ID + # NOTE: Only to be used for PR review not to be used with actual action + "get_pr_id": """ +{ + repository(owner: "anmol098", name: "waka-readme-stats") { + pullRequest(number: $pr_number) { + id + } + } +} + """, + "add_pr_comment": """ +mutation { + addComment(input: {subjectId: "$pr_id", body: "$comment"}) { + subject { + id + } + } +} + """ } diff --git a/sources/manager_environment.py b/sources/manager_environment.py index a38a782..e018075 100644 --- a/sources/manager_environment.py +++ b/sources/manager_environment.py @@ -46,3 +46,4 @@ class EnvironmentManager: DEBUG_LOGGING = getenv("INPUT_DEBUG_LOGGING", "0").lower() in _TRUTHY DEBUG_RUN = getenv("DEBUG_RUN", "False").lower() in _TRUTHY + PR_NUMBER = getenv("PR_NUMBER", "")