From b5d8fe88670ec58c33a99a05755df104755e27f0 Mon Sep 17 00:00:00 2001 From: Navarone Feekery <13634519+navarone-feekery@users.noreply.github.com> Date: Wed, 24 Apr 2024 17:56:08 +0200 Subject: [PATCH] Add action workflow for community issues (#2434) --- .github/workflows/label-community-issues.yml | 29 +++++++++++++++ .../scripts/label_community_issues.py | 36 +++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 .github/workflows/label-community-issues.yml create mode 100644 .github/workflows/scripts/label_community_issues.py diff --git a/.github/workflows/label-community-issues.yml b/.github/workflows/label-community-issues.yml new file mode 100644 index 000000000..303bb4491 --- /dev/null +++ b/.github/workflows/label-community-issues.yml @@ -0,0 +1,29 @@ +name: Label Community Issues + +on: + issues: + types: [opened] + +jobs: + run-python-script: + runs-on: ubuntu-latest + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.10' + + - name: Install dependencies + run: python3 -m pip install aiohttp gidgethub + + - name: Run Python script + run: python .github/workflows/scripts/label_community_issues.py + env: + ACTOR: ${{ github.actor }} + NUMBER: ${{ github.event.issue.number }} + REPO: ${{ github.repository }} + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/scripts/label_community_issues.py b/.github/workflows/scripts/label_community_issues.py new file mode 100644 index 000000000..18e8cee30 --- /dev/null +++ b/.github/workflows/scripts/label_community_issues.py @@ -0,0 +1,36 @@ +#!/usr/bin/env python + +import aiohttp +import asyncio +import os +from gidgethub.aiohttp import GitHubAPI +from gidgethub import BadRequest + +ACTOR = os.getenv("ACTOR") +NUMBER = os.getenv("NUMBER") +REPO = os.getenv("REPO") +GITHUB_TOKEN = os.getenv("GITHUB_TOKEN") + +LABELS = ["community-driven", "needs-triage"] + +async def main(): + async with aiohttp.ClientSession() as session: + gh = GitHubAPI(session, requester="", base_url="https://api.github.com", oauth_token=GITHUB_TOKEN) + + print("********") + print(f"ACTOR: {ACTOR}") + print(f"NUMBER: {NUMBER}") + print(f"REPO: {REPO}") + print("********") + + try: + # this API returns a None response, but will raise if the user isn't a collaborator + await gh.getitem(f"/repos/{REPO}/collaborators/{ACTOR}") + print("User is a collaborator, not applying labels.") + except BadRequest as e: + # if this fails we want it to be noisy, so no try/except + print("User is not a collaborator, applying labels...") + await gh.post(f"/repos/{REPO}/issues/{NUMBER}/labels", data={"labels": LABELS}) + +if __name__ == "__main__": + asyncio.run(main())