Skip to content

Update README with Contributors #71

Update README with Contributors

Update README with Contributors #71

Workflow file for this run

#!/usr/bin/env python3
name: Update README with Contributors
on:
schedule:
- cron: '0 0 * * *' # Runs daily at midnight (UTC)
workflow_dispatch:
jobs:
update-readme:
runs-on: ubuntu-latest
steps:
- name: Checkout the repository
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install dependencies
run: |
pip install requests
- name: Fetch contributors and update README
env:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
run: |
import requests
import os
import re
from pathlib import Path
# Get repository details
repo_details = os.getenv('Battery-Capacity-Prediction-Using-Regression').split('/')
repo_owner = repo_details[0]
repo_name = repo_details[1]
api_url = f'https://api.github.com/repos/{repo_owner}/{repo_name}/contributors'
headers = {'Authorization': f'token {os.getenv("GH_TOKEN")}'}
# Fetch contributors
response = requests.get(api_url, headers=headers)
if response.status_code != 200:
raise Exception(f"Failed to fetch contributors: {response.status_code} - {response.text}")
contributors = response.json()
# Generate contributor section
contributors_section = '<!-- readme: contributors -start -->\n'
for contributor in contributors:
username = contributor['login']
avatar_url = contributor['avatar_url']
profile_url = f'https://github.com/{username}'
contributors_section += f'![{username}]({avatar_url}?s=50) [{username}]({profile_url})\n'
contributors_section += '<!-- readme: contributors -end -->\n'
# Read and update the README file
readme_path = Path('README.md')
if not readme_path.exists():
raise FileNotFoundError("README.md not found.")
with readme_path.open('r') as file:
readme_content = file.read()
if '<!-- readme: contributors -start -->' in readme_content:
readme_content = re.sub(
r'<!-- readme: contributors -start -->.*?<!-- readme: contributors -end -->',
contributors_section,
readme_content,
flags=re.DOTALL
)
else:
introduction_section = '## 1. Introduction\n'
insert_position = readme_content.find(introduction_section) + len(introduction_section)
if insert_position == -1:
raise ValueError("Introduction section not found.")
readme_content = (
readme_content[:insert_position] +
'\n' + contributors_section +
readme_content[insert_position:]
)
with readme_path.open('w') as file:
file.write(readme_content)
- name: Commit and push changes
run: |
git config --global user.name 'github-actions'
git config --global user.email '[email protected]'
git add README.md
git commit -m 'Update README with contributors'
git push