Add GitHub Action to credit new contributors #1
Workflow file for this run
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# SPDX-FileCopyrightText: Copyright 2025 Arm Limited and/or its affiliates <[email protected]> | |
# | |
# SPDX-License-Identifier: Apache-2.0 | |
name: "Add author to all-contributors" | |
on: | |
pull_request: | |
types: [closed] | |
jobs: | |
add-contributor: | |
if: github.event.pull_request.merged == true | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout repository | |
uses: actions/[email protected] | |
- name: Set up Python | |
uses: actions/[email protected] | |
with: | |
python-version: "3.x" | |
- name: Install dependencies | |
run: | | |
python -m pip install --upgrade pip | |
pip install PyGithub | |
- name: Check and comment if needed | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
GITHUB_EVENT_PATH: ${{ github.event_path }} | |
GITHUB_REPOSITORY: ${{ github.repository }} | |
run: | | |
python << 'EOF' | |
import os | |
import json | |
from pathlib import Path | |
from github import Github | |
# 1) Load environment / event data | |
token = os.environ["GITHUB_TOKEN"] | |
event_path = os.environ["GITHUB_EVENT_PATH"] | |
repo_slug = os.environ["GITHUB_REPOSITORY"] | |
with open(event_path, "r", encoding="utf-8") as f: | |
event = json.load(f) | |
pr_info = event.get("pull_request", {}) | |
author = pr_info.get("user", {}).get("login", "").strip() | |
# 2) Immediately ignore if author is the All Contributors bot | |
if author.lower() == "allcontributors[bot]": | |
print("PR authored by allcontributors[bot]; skipping.") | |
exit(0) | |
# 3) Attempt to read .all-contributorsrc | |
contributors_file = Path(".all-contributorsrc") | |
contributors_data = {"contributors": []} | |
if contributors_file.exists(): | |
try: | |
contributors_data = json.loads(contributors_file.read_text(encoding="utf-8")) | |
except json.JSONDecodeError: | |
print("Warning: .all-contributorsrc is invalid JSON; assuming empty list.") | |
else: | |
print(".all-contributorsrc not found; assuming no contributors yet.") | |
# 4) Find if author is present and if they already have "code" | |
author_lower = author.lower() | |
entry = None | |
for c in contributors_data.get("contributors", []): | |
if c.get("login", "").lower() == author_lower: | |
entry = c | |
break | |
is_present = entry is not None | |
has_code = (is_present and ("code" in entry.get("contributions", []))) | |
print(f"Author present? {is_present}") | |
print(f"Has 'code' contribution? {has_code}") | |
# 5) If missing or missing "code", post a comment via PyGithub | |
if (not is_present) or (not has_code): | |
# Authenticate and post comment | |
gh = Github(token) | |
repo = gh.get_repo(repo_slug) | |
pr_number = pr_info.get("number") | |
comment_body = f"@all-contributors please add @{author} for code" | |
repo.get_issue(pr_number).create_comment(body=comment_body) | |
print(f"Posted comment to add {author} for code.") | |
else: | |
print("No action needed; author already credited for code.") | |
EOF | |