Skip to content

Add GitHub Action to credit new contributors #400

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions .github/workflows/add-contributor.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
# 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