From 83e7c1835477d5fd639ebc0a614ed425fa06cab0 Mon Sep 17 00:00:00 2001 From: Ramana-Raja Date: Mon, 14 Apr 2025 21:59:32 +0530 Subject: [PATCH 1/5] added ai_spam --- .github/utilities/ai_spam.py | 28 +++++++++++++++++++++++ .github/workflows/ai_spam.yml | 42 +++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 .github/utilities/ai_spam.py create mode 100644 .github/workflows/ai_spam.yml diff --git a/.github/utilities/ai_spam.py b/.github/utilities/ai_spam.py new file mode 100644 index 0000000000..ef013cdd8d --- /dev/null +++ b/.github/utilities/ai_spam.py @@ -0,0 +1,28 @@ +"""Script for handling AI Spam label on pull requests. + +Triggered when AI Spam label is added to a PR, +it adds a comment and closes the PR. +""" + +import json +import os +from github import Github + +context_dict = json.loads(os.getenv("CONTEXT_GITHUB")) + +repo_name = context_dict["repository"] +g = Github(os.getenv("GITHUB_TOKEN")) +repo = g.get_repo(repo_name) +pr_number = context_dict["event"]["pull_request"]["number"] +pr = repo.get_pull(pr_number) +label_name = context_dict["event"]["label"]["name"] + +if label_name == 'AI Spam': + comment_body = ( + f"This pull request has been flagged with the **AI Spam** label.\n\n" + f"This PR is being closed." + ) + pr.create_issue_comment(comment_body) + pr.edit(state="closed") + + diff --git a/.github/workflows/ai_spam.yml b/.github/workflows/ai_spam.yml new file mode 100644 index 0000000000..89c4126703 --- /dev/null +++ b/.github/workflows/ai_spam.yml @@ -0,0 +1,42 @@ +name: AI Spam Detection On PR + +on: + pull_request: + types: [labeled] + +concurrency: + group: ${{ github.workflow }}-${{ github.event.pull_request.number }} + cancel-in-progress: true + +jobs: + ai-spam-present: + if: ${{ github.event.label.name == 'AI Spam' }} + runs-on: ubuntu-24.04 + + steps: + - name: Create app token + uses: actions/create-github-app-token@v1 + id: app-token + with: + app-id: ${{ vars.PR_APP_ID }} + private-key: ${{ secrets.PR_APP_KEY }} + + - name: Checkout main + uses: actions/checkout@v4 + with: + sparse-checkout: .github/utilities + + - name: Setup Python 3.10 + uses: actions/setup-python@v5 + with: + python-version: "3.10" + + - name: Install PyGithub + run: pip install -Uq PyGithub + + - name: Process AI Spam + id: handle_spam + run: python .github/utilities/ai_spam.py + env: + CONTEXT_GITHUB: ${{ toJson(github) }} + GITHUB_TOKEN: ${{ steps.app-token.outputs.token }} From 446e20d1d5cc52e561a8a42455b80c6c0f335809 Mon Sep 17 00:00:00 2001 From: Ramana-Raja <83065061+Ramana-Raja@users.noreply.github.com> Date: Mon, 14 Apr 2025 16:50:26 +0000 Subject: [PATCH 2/5] Automatic `pre-commit` fixes --- .github/utilities/ai_spam.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/.github/utilities/ai_spam.py b/.github/utilities/ai_spam.py index ef013cdd8d..9b33da8515 100644 --- a/.github/utilities/ai_spam.py +++ b/.github/utilities/ai_spam.py @@ -6,6 +6,7 @@ import json import os + from github import Github context_dict = json.loads(os.getenv("CONTEXT_GITHUB")) @@ -17,12 +18,10 @@ pr = repo.get_pull(pr_number) label_name = context_dict["event"]["label"]["name"] -if label_name == 'AI Spam': +if label_name == "AI Spam": comment_body = ( - f"This pull request has been flagged with the **AI Spam** label.\n\n" - f"This PR is being closed." + f"This pull request has been flagged with the **AI Spam** label.\n\n" + f"This PR is being closed." ) pr.create_issue_comment(comment_body) pr.edit(state="closed") - - From 1a543074b9da1bb3cded582ca8d49a027bd9d191 Mon Sep 17 00:00:00 2001 From: Ramana-Raja Date: Mon, 14 Apr 2025 22:22:39 +0530 Subject: [PATCH 3/5] fixed comment text --- .github/utilities/ai_spam.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/utilities/ai_spam.py b/.github/utilities/ai_spam.py index ef013cdd8d..a0404383b7 100644 --- a/.github/utilities/ai_spam.py +++ b/.github/utilities/ai_spam.py @@ -19,8 +19,8 @@ if label_name == 'AI Spam': comment_body = ( - f"This pull request has been flagged with the **AI Spam** label.\n\n" - f"This PR is being closed." + "This pull request has been flagged with the **AI Spam** label.\n\n" + "This PR is being closed." ) pr.create_issue_comment(comment_body) pr.edit(state="closed") From 340ea8cb7fa959ea59ef6718a9a1ec534043be0e Mon Sep 17 00:00:00 2001 From: Ramana-Raja Date: Mon, 14 Apr 2025 22:23:16 +0530 Subject: [PATCH 4/5] fixed comment text --- .github/utilities/ai_spam.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/utilities/ai_spam.py b/.github/utilities/ai_spam.py index 9b33da8515..0113aaad08 100644 --- a/.github/utilities/ai_spam.py +++ b/.github/utilities/ai_spam.py @@ -20,8 +20,8 @@ if label_name == "AI Spam": comment_body = ( - f"This pull request has been flagged with the **AI Spam** label.\n\n" - f"This PR is being closed." + "This pull request has been flagged with the **AI Spam** label.\n\n" + "This PR is being closed." ) pr.create_issue_comment(comment_body) pr.edit(state="closed") From f3437e307c01c9b3172f7d07d9e5504ffc1c4f50 Mon Sep 17 00:00:00 2001 From: Ramana-Raja Date: Mon, 14 Apr 2025 22:27:02 +0530 Subject: [PATCH 5/5] updated to python 3.11 --- .github/workflows/ai_spam.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ai_spam.yml b/.github/workflows/ai_spam.yml index 89c4126703..f09a1cff2d 100644 --- a/.github/workflows/ai_spam.yml +++ b/.github/workflows/ai_spam.yml @@ -26,10 +26,10 @@ jobs: with: sparse-checkout: .github/utilities - - name: Setup Python 3.10 + - name: Setup Python 3.11 uses: actions/setup-python@v5 with: - python-version: "3.10" + python-version: "3.11" - name: Install PyGithub run: pip install -Uq PyGithub