diff --git a/.github/workflows/update-contributors.yml b/.github/workflows/update-contributors.yml new file mode 100644 index 0000000..d15989b --- /dev/null +++ b/.github/workflows/update-contributors.yml @@ -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 diff --git a/populate_contributors.py b/populate_contributors.py index a8e880f..0458a41 100644 --- a/populate_contributors.py +++ b/populate_contributors.py @@ -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. diff --git a/update_readme.py b/update_readme.py new file mode 100644 index 0000000..3cce63e --- /dev/null +++ b/update_readme.py @@ -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]} ", 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)