Skip to content
Draft
Show file tree
Hide file tree
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
60 changes: 60 additions & 0 deletions .github/workflows/update-contributors.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Update Contributors

on:
schedule:
# Run on the 1st of every month at 00:00 UTC
- cron: '0 0 1 * *'
workflow_dispatch: # Allow manual triggering

jobs:
update-contributors:
runs-on: ubuntu-latest
permissions:
contents: write # Required to push changes to the repository

steps:
- name: Checkout maintenance repo
uses: actions/checkout@v4
with:
path: maintenance

- name: Checkout apps-android-commons repo
uses: actions/checkout@v4
with:
repository: commons-app/apps-android-commons
token: ${{ secrets.PAT_TOKEN }}
path: apps-android-commons

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.x'

- name: Generate contributors table
id: generate
run: |
set -e
cd maintenance
python populate_contributors.py > /tmp/contributors_table.txt
echo "Generated contributors table"
cat /tmp/contributors_table.txt

- name: Update README with new contributors
run: |
set -e
python maintenance/update_readme.py /tmp/contributors_table.txt apps-android-commons/README.md

- name: Commit and push changes
run: |
cd apps-android-commons
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"

if git diff --quiet; then
echo "No changes to commit"
else
git add README.md
git commit -m "Update contributors list"
git push
echo "Changes pushed successfully"
fi
5 changes: 3 additions & 2 deletions populate_contributors.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import json
import urllib2
import urllib.request

GITHUB_GET_CONTRIBUTORS_API = "https://api.github.com/repos/commons-app/apps-android-commons/contributors"
response = urllib2.urlopen(GITHUB_GET_CONTRIBUTORS_API).read()
req = urllib.request.Request(GITHUB_GET_CONTRIBUTORS_API, headers={'User-Agent': 'Mozilla/5.0'})
response = urllib.request.urlopen(req).read()
response_json = json.loads(response)

# Replace the API call above with the two lines below if your Python is blocked from using the Internet.
Expand Down
68 changes: 68 additions & 0 deletions update_readme.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import re
import sys

def update_readme_with_contributors(contributors_table_path, readme_path):
"""
Update the README file with the new contributors table.

Args:
contributors_table_path: Path to the file containing the new contributors table
readme_path: Path to the README file to update

Returns:
bool: True if the README was updated, False otherwise
"""
# Read the contributors table
try:
with open(contributors_table_path, 'r') as f:
new_table = f.read().strip()
except FileNotFoundError:
print(f"Error: Contributors table file not found: {contributors_table_path}", file=sys.stderr)
sys.exit(1)

# Read the README
try:
with open(readme_path, 'r') as f:
readme_content = f.read()
except FileNotFoundError:
print(f"Error: README file not found: {readme_path}", file=sys.stderr)
sys.exit(1)

# Find the Contributors section and replace it
# Pattern: Find everything between "## Contributors ##" and ".. and [many more]"
pattern = r'(## Contributors ##\s+Thank you all for your work!\s+)(.*?)(\s+\.\. and \[many more\])'

# Check if the pattern matches
if not re.search(pattern, readme_content, flags=re.DOTALL):
print("Error: Could not find Contributors section in README. The README format may have changed.", file=sys.stderr)
print("Expected to find: '## Contributors ##' followed by 'Thank you all for your work!' and '.. and [many more]'", file=sys.stderr)
sys.exit(1)

replacement = r'\1' + new_table + r'\3'
updated_content = re.sub(pattern, replacement, readme_content, flags=re.DOTALL)

# Verify the replacement was successful
if updated_content == readme_content:
print("Warning: No changes were made to the README. The contributors table may be identical.", file=sys.stderr)
return False

# Write the updated README
try:
with open(readme_path, 'w') as f:
f.write(updated_content)
except Exception as e:
print(f"Error: Failed to write updated README: {e}", file=sys.stderr)
sys.exit(1)

print("README updated successfully")
return True

if __name__ == "__main__":
if len(sys.argv) != 3:
print(f"Usage: {sys.argv[0]} <contributors_table_file> <readme_file>", file=sys.stderr)
sys.exit(1)

contributors_table_path = sys.argv[1]
readme_path = sys.argv[2]

update_readme_with_contributors(contributors_table_path, readme_path)