|
| 1 | +# SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <[email protected]> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +name: "Add author to all-contributors" |
| 6 | + |
| 7 | +on: |
| 8 | + pull_request: |
| 9 | + types: [closed] |
| 10 | + |
| 11 | +jobs: |
| 12 | + add-contributor: |
| 13 | + if: github.event.pull_request.merged == true |
| 14 | + runs-on: ubuntu-latest |
| 15 | + |
| 16 | + steps: |
| 17 | + - name: Checkout repository |
| 18 | + |
| 19 | + |
| 20 | + - name: Set up Python |
| 21 | + |
| 22 | + with: |
| 23 | + python-version: "3.x" |
| 24 | + |
| 25 | + - name: Install dependencies |
| 26 | + run: | |
| 27 | + python -m pip install --upgrade pip |
| 28 | + pip install PyGithub |
| 29 | +
|
| 30 | + - name: Check and comment if needed |
| 31 | + env: |
| 32 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 33 | + GITHUB_EVENT_PATH: ${{ github.event_path }} |
| 34 | + GITHUB_REPOSITORY: ${{ github.repository }} |
| 35 | + run: | |
| 36 | + python << 'EOF' |
| 37 | + import os |
| 38 | + import json |
| 39 | + from pathlib import Path |
| 40 | + from github import Github |
| 41 | +
|
| 42 | + # 1) Load environment / event data |
| 43 | + token = os.environ["GITHUB_TOKEN"] |
| 44 | + event_path = os.environ["GITHUB_EVENT_PATH"] |
| 45 | + repo_slug = os.environ["GITHUB_REPOSITORY"] |
| 46 | +
|
| 47 | + with open(event_path, "r", encoding="utf-8") as f: |
| 48 | + event = json.load(f) |
| 49 | +
|
| 50 | + pr_info = event.get("pull_request", {}) |
| 51 | + author = pr_info.get("user", {}).get("login", "").strip() |
| 52 | +
|
| 53 | + # 2) Immediately ignore if author is the All Contributors bot |
| 54 | + if author.lower() == "allcontributors[bot]": |
| 55 | + print("PR authored by allcontributors[bot]; skipping.") |
| 56 | + exit(0) |
| 57 | +
|
| 58 | + # 3) Attempt to read .all-contributorsrc |
| 59 | + contributors_file = Path(".all-contributorsrc") |
| 60 | + contributors_data = {"contributors": []} |
| 61 | + if contributors_file.exists(): |
| 62 | + try: |
| 63 | + contributors_data = json.loads(contributors_file.read_text(encoding="utf-8")) |
| 64 | + except json.JSONDecodeError: |
| 65 | + print("Warning: .all-contributorsrc is invalid JSON; assuming empty list.") |
| 66 | + else: |
| 67 | + print(".all-contributorsrc not found; assuming no contributors yet.") |
| 68 | +
|
| 69 | + # 4) Find if author is present and if they already have "code" |
| 70 | + author_lower = author.lower() |
| 71 | + entry = None |
| 72 | + for c in contributors_data.get("contributors", []): |
| 73 | + if c.get("login", "").lower() == author_lower: |
| 74 | + entry = c |
| 75 | + break |
| 76 | +
|
| 77 | + is_present = entry is not None |
| 78 | + has_code = (is_present and ("code" in entry.get("contributions", []))) |
| 79 | +
|
| 80 | + print(f"Author present? {is_present}") |
| 81 | + print(f"Has 'code' contribution? {has_code}") |
| 82 | +
|
| 83 | + # 5) If missing or missing "code", post a comment via PyGithub |
| 84 | + if (not is_present) or (not has_code): |
| 85 | + # Authenticate and post comment |
| 86 | + gh = Github(token) |
| 87 | + repo = gh.get_repo(repo_slug) |
| 88 | + pr_number = pr_info.get("number") |
| 89 | + comment_body = f"@all-contributors please add @{author} for code" |
| 90 | + repo.get_issue(pr_number).create_comment(body=comment_body) |
| 91 | + print(f"Posted comment to add {author} for code.") |
| 92 | + else: |
| 93 | + print("No action needed; author already credited for code.") |
| 94 | + EOF |
| 95 | +
|
0 commit comments