-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add workflow to update contributors list in README.md (#30)
* add workflow to update contributors list in README.md * Update contributors * Updated workflow to trigger only if commits to main * Add check to skip commit if no changes --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
78c7950
commit 88a4445
Showing
3 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
name: Update Contributors | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
workflow_dispatch: # Allows manual trigger of the workflow | ||
|
||
jobs: | ||
update-contributors: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v2 | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: '3.10' | ||
|
||
- name: Install dependencies | ||
run: pip install requests | ||
|
||
- name: Update README.md with contributors | ||
run: python update_contributors.py | ||
|
||
- name: Check for changes and commit | ||
run: | | ||
if git diff --quiet; then | ||
echo "No changes to commit"; | ||
else | ||
git config --global user.name 'github-actions[bot]' | ||
git config --global user.email '41898282+github-actions[bot]@users.noreply.github.com' | ||
git add README.md | ||
git commit -m 'Update contributors' | ||
git push | ||
fi |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import os | ||
import requests | ||
|
||
REPO_OWNER = "pythonindia" | ||
REPO_NAME = "inpycon2024" | ||
API_URL = f"https://api.github.com/repos/{REPO_OWNER}/{REPO_NAME}/contributors" | ||
|
||
def get_contributors(): | ||
response = requests.get(API_URL) | ||
response.raise_for_status() | ||
return response.json() | ||
|
||
def get_contributor_details(username): | ||
url = f"https://api.github.com/users/{username}" | ||
response = requests.get(url) | ||
response.raise_for_status() | ||
return response.json() | ||
|
||
def update_readme(contributors): | ||
readme_path = "./README.md" | ||
with open(readme_path, "r") as file: | ||
readme_content = file.read() | ||
|
||
start_marker = "<!-- CONTRIBUTORS_START -->" | ||
end_marker = "<!-- CONTRIBUTORS_END -->" | ||
start = readme_content.index(start_marker) + len(start_marker) | ||
end = readme_content.index(end_marker) | ||
|
||
rows_per_column = 6 | ||
new_contributors_section = "<table>\n <tr>" | ||
for i, contributor in enumerate(contributors): | ||
contributor_details = get_contributor_details(contributor['login']) | ||
name = contributor_details['name'] or contributor_details['login'] | ||
avatar_url = contributor_details['avatar_url'] | ||
html_url = contributor_details['html_url'] | ||
|
||
new_contributors_section += f"""\n <td align="center"><a href="{html_url}"><img src="{avatar_url}" width="100px" alt="{name}"/><br /><sub><b>{name}</b></sub></a></td>""" | ||
|
||
if (i + 1) % rows_per_column == 0: | ||
new_contributors_section += "\n </tr>\n <tr>" | ||
|
||
# Close the last table row | ||
new_contributors_section += "\n </tr>\n</table>" | ||
new_readme_content = f"{readme_content[:start]}\n{new_contributors_section}\n{readme_content[end:]}" | ||
|
||
with open(readme_path, "w") as file: | ||
file.write(new_readme_content) | ||
|
||
def main(): | ||
contributors = get_contributors() | ||
update_readme(contributors) | ||
|
||
if __name__ == "__main__": | ||
main() |