Skip to content
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

Update contributors.json list weekly #3881

Merged
merged 1 commit into from
Oct 5, 2024
Merged
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
37 changes: 37 additions & 0 deletions .github/workflows/update-contributors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Automatically update contributors.json

on:
schedule:
- cron: '0 0 * * 1' # Every Monday @ 00:00
# Allow manual trigger
workflow_dispatch:

jobs:
update-contributors:
runs-on: ubuntu-latest

permissions:
# Give the default GITHUB_TOKEN write permission to commit and push the
# added or changed files to the repository.
contents: write

steps:
- uses: actions/checkout@v4.2.0

- name: Setup Python
uses: actions/setup-python@v5.2.0

- name: Get contributors
run: "python get_developers.py"

- name: Copy generated contributors.json to its correct place
run: "mv contributors.json composeApp/src/androidMain/res/raw/contributors.json"

- name: Get current date
id: date
run: echo "::set-output name=date::$(date +'%m-%d-%Y')"

- name: Push changes
uses: stefanzweifel/git-auto-commit-action@v5.0.1
with:
commit_message: "updated ${{ steps.date.outputs.date }}"
101 changes: 101 additions & 0 deletions get_developers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import re
import requests
import json



contributors: list[dict]


needed_keys = ['login', 'id', 'avatar_url', 'html_url', 'contributions']
def filter_keys(entry: dict):
"""
Deletes keys that are not defined in 'needed_keys'.
The list may be appended in the future
"""
for key in list(entry.keys()):
if not key in needed_keys:
del entry[key]


contributors_url: str = 'https://api.github.com/repos/fast4x/rimusic/contributors'
def fetch_contributors() -> None:
"""
Gets all the contributors of this repository with Github API.
"""
global contributors

response = requests.get(contributors_url)
# Convert response from Github API to list of entries
contributors = json.loads(response.text)

# Filter out unused keys to save space
for entry in contributors:
filter_keys(entry)


filename: str = 'README.md'
section: str = '### **Developer / Designer that contribute:**'
hyperlink_pattern = r'^-\s\[.*]\((https?://github.com/.*)\)$'
def registered_devs() -> list[str]:
"""
Devs that are recognized in README.md
Returns a list of urls
"""
results: list[str] = []

with open(filename, 'r') as file:
found_header: bool = False
for line in file.readlines():

if found_header:
match = re.match(hyperlink_pattern, line)

if match:
results.append(match.group(1))
else:
# Stop when there's no more matches
break

if not found_header and line.startswith(section):
found_header = True

return results


if __name__ == '__main__':
fetch_contributors()
if len(contributors) == 0:
print(f'Could not fetch contributors')

registered = registered_devs()

devs: dict = {}
for entry in contributors:
dev_url: str = entry['html_url']

if dev_url in registered:
devs[dev_url] = entry

github_id_pattern = r'https?://github.com/(.*)'
for dev_url in registered:
if not dev_url in list(devs.keys()):
match = re.match(github_id_pattern, dev_url)
if not match:
continue

response = requests.get(f'https://api.github.com/users/{match.group(1)}')
if response.status_code != 200:
print(f'Failed to get developer\'s information. Skipping...')
print(response.text)
continue

dev_json = json.loads(response.text)
filter_keys(dev_json)

devs[dev_url] = dev_json

with open('contributors.json', 'w') as file:
file.write(json.dumps(list(devs.values()), indent=2))