From f1a8774f2d37329b103af8c0adf405c45392381e Mon Sep 17 00:00:00 2001 From: Ikechukwu Uchendu Date: Thu, 21 Sep 2023 14:57:25 -0400 Subject: [PATCH] Added code to automatically update contributors --- .all-contributorsrc | 83 +++++++------- .github/workflows/auto-add-contributors.yml | 61 ++++++++++ .../workflows/contributors/requirements.txt | 3 + .../contributors/update_contributors.py | 104 ++++++++++++++++++ README.md | 7 +- contributors.qmd | 7 +- 6 files changed, 216 insertions(+), 49 deletions(-) create mode 100644 .github/workflows/auto-add-contributors.yml create mode 100644 .github/workflows/contributors/requirements.txt create mode 100644 .github/workflows/contributors/update_contributors.py diff --git a/.all-contributorsrc b/.all-contributorsrc index 2677f391..5ed68d23 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -1,42 +1,43 @@ { - "projectName": "cs249r_book", - "projectOwner": "harvard-edge", - "files": [ - "contributors.qmd", "README.md" - ], - "contributors": [ - { - "login": "jveejay", - "name": "Vijay Janapa Reddi", - "avatar_url": "https://avatars.githubusercontent.com/jveejay", - "profile": "https://github.com/jveejay", - "contributions": [ - "doc" - ] - }, - { - "login": "mpstewart1", - "name": "Matthew Stewart", - "avatar_url": "https://avatars.githubusercontent.com/mpstewart1", - "profile": "https://github.com/mpstewart1", - "contributions": [ - "doc" - ] - }, - { - "login": "uchendui", - "name": "Ikechukwu Uchendu", - "avatar_url": "https://avatars.githubusercontent.com/uchendui", - "profile": "https://github.com/uchendui", - "contributions": [ - "doc" - ] - } - ], - "repoType": "github", - "contributorsPerLine": 7, - "repoHost": "https://github.com", - "commitConvention": "angular", - "skipCi": true, - "commitType": "docs" -} + "projectName": "cs249r_book", + "projectOwner": "harvard-edge", + "files": [ + "contributors.qmd", + "README.md" + ], + "contributors": [ + { + "login": "jveejay", + "name": "Vijay Janapa Reddi", + "avatar_url": "https://avatars.githubusercontent.com/jveejay", + "profile": "https://github.com/jveejay", + "contributions": [ + "doc" + ] + }, + { + "login": "uchendui", + "name": "Ikechukwu Uchendu", + "avatar_url": "https://avatars.githubusercontent.com/uchendui", + "profile": "https://github.com/uchendui", + "contributions": [ + "doc" + ] + }, + { + "login": "mpstewart1", + "name": "Matthew Stewart", + "avatar_url": "https://avatars.githubusercontent.com/mpstewart1", + "profile": "https://github.com/mpstewart1", + "contributions": [ + "doc" + ] + } + ], + "repoType": "github", + "contributorsPerLine": 7, + "repoHost": "https=//github.com", + "commitConvention": "angular", + "skipCi": true, + "commitType": "docs" +} \ No newline at end of file diff --git a/.github/workflows/auto-add-contributors.yml b/.github/workflows/auto-add-contributors.yml new file mode 100644 index 00000000..ae3654ce --- /dev/null +++ b/.github/workflows/auto-add-contributors.yml @@ -0,0 +1,61 @@ +name: Automatically Add Contributors +run-name: ${{ github.actor }} is updating TinyML book contributors +on: + push: + branches: + auto_contributor + +jobs: + update-contributors: + runs-on: ubuntu-latest + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Setup Python3.11 + uses: actions/setup-python@v4 + with: + python-version: 3.11 + + - name: Installing GitHub CLI + run: | + sudo apt install -y gh jq nodejs npm + - name: Install python packages + run: | + python -m pip install --upgrade pip + pip install -r .github/workflows/contributors/requirements.txt + + - name: Retrieving contributors for commit ${{ github.sha }} + run: | + echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event." + echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!" + echo "🔎 Getting all contributors from the ${{ github.ref }} branch." + PUSHED_CONTRIBUTORS=$(git shortlog -sne --no-merges ${{ github.event.before }}..${{ github.sha }}) + echo "The contributors are: $PUSHED_CONTRIBUTORS" + - name: Running Python Script to Update .all-contributorsrc + run: | + echo "Running Python Script to Update .all-contributorsrc" + python .github/workflows/contributors/update_contributors.py + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Committing changes to same branch + run: | + echo "Committing changes to same branch" + git config --global user.email "github-actions[bot]@users.noreply.github.com" + git config --global user.name "github-actions[bot]" + git add .all-contributorsrc + git commit -m " Update contributors from github action" + git push + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Using all-contributors CLI to update files + run: | + echo "Using all-contributors CLI to update files" + npm i -D all-contributors-cli + npx all-contributors generate + git config --global user.email "github-actions[bot]@users.noreply.github.com" + git config --global user.name "github-actions[bot]" + git add -u + git commit -m "Update readme and contributors.qmd with contributors" + git push \ No newline at end of file diff --git a/.github/workflows/contributors/requirements.txt b/.github/workflows/contributors/requirements.txt new file mode 100644 index 00000000..ee1e1596 --- /dev/null +++ b/.github/workflows/contributors/requirements.txt @@ -0,0 +1,3 @@ +absl-py +pandas +PyGithub \ No newline at end of file diff --git a/.github/workflows/contributors/update_contributors.py b/.github/workflows/contributors/update_contributors.py new file mode 100644 index 00000000..6032034b --- /dev/null +++ b/.github/workflows/contributors/update_contributors.py @@ -0,0 +1,104 @@ +import json +import os + +from absl import app +from github import Github +import requests + +CONTRIBUTORS_TMP_FILE = 'CONTRIBUTORS_TEMP.txt' +CONTRIBUTORS_FILE = '.all-contributorsrc' + +EXCLUDED_USERS = ['web-flow'] + +OWNER = "harvard-edge" +REPO = "cs249r_book" +BRANCH = "auto_contributor" + + +def split_name_email(s): + parts = s.rsplit(' ', 1) + return parts[0], parts[1][1:-1] # Removing angle brackets from email + + +def get_github_username(token, email): + g = Github(token) + users = g.search_users(email) + for user in users: + # Assuming the first user returned with the matching email is the correct user + return user.login + return None + + +def main(_): + token = os.environ["GH_TOKEN"] + + headers = { + "Authorization": f"token {token}" + } + + web_address = f'https://api.github.com/repos/{OWNER}/{REPO}/commits?sha={BRANCH}&per_page=100' + res = requests.get(web_address, headers=headers) + + print(web_address) + + # Check if the request was successful + if res.status_code == 200: + # Parse the JSON response + data = res.json() + + # Extract the 'login' attribute for each committer + usernames = [commit['committer']['login'] for commit in data if commit['committer']] + + # Print unique usernames + for username in sorted(set(usernames)): + print(username) + + with open(CONTRIBUTORS_FILE, 'r') as contrib_file: + contributors_data = json.load(contrib_file) + user_to_name_dict = dict() + contributors = contributors_data['contributors'] + + contributor_logins = [] + for contrib in contributors: + user_to_name_dict[contrib['login']] = contrib['name'] + contributor_logins.append(contrib['login']) + contributor_logins_set = set(contributor_logins) + + # Perform the set subtraction + # result = usernames_set - contributor_logins_set + result = contributor_logins_set - set(EXCLUDED_USERS) + + print('New contributors: ', result) + + final_result = dict( + projectName=REPO, + projectOwner=OWNER, + files=["contributors.qmd", "README.md"], + contributors=[dict(login=user, + name=user_to_name_dict[user], + avatar_url=f'https://avatars.githubusercontent.com/{user}', + profile=f'https://github.com/{user}', + contributions=['doc'], ) for + user in result], + + repoType='github', + contributorsPerLine=7, + repoHost="https=//github.com", + commitConvention='angular', + skipCi=True, + commitType="docs" + ) + + print(final_result) + json_string = json.dumps(final_result, + indent=4) # The indent parameter is optional, but it formats the output to be more readable + print(json_string) + + with open(CONTRIBUTORS_FILE, 'w') as contrib_file: + contrib_file.write(json_string) + else: + print(f"Failed with status code: {res.status_code}") + + +if __name__ == '__main__': + app.run(main) diff --git a/README.md b/README.md index e8a69b10..9acee064 100644 --- a/README.md +++ b/README.md @@ -66,13 +66,12 @@ quarto render - - - - + + +
Vijay Janapa Reddi
Vijay Janapa Reddi

📖
Matthew Stewart
Matthew Stewart

📖
LBF38
Ikechukwu Uchendu

📖
Vijay Janapa Reddi
Vijay Janapa Reddi

📖
Ikechukwu Uchendu
Ikechukwu Uchendu

📖
Matthew Stewart
Matthew Stewart

📖
diff --git a/contributors.qmd b/contributors.qmd index 25541316..85a7433c 100644 --- a/contributors.qmd +++ b/contributors.qmd @@ -5,13 +5,12 @@ We extend our heartfelt gratitude to the diverse group of individuals who have s - - - - + + +
Vijay Janapa Reddi
Vijay Janapa Reddi

📖
Matthew Stewart
Matthew Stewart

📖
LBF38
Ikechukwu Uchendu

📖
Vijay Janapa Reddi
Vijay Janapa Reddi

📖
Ikechukwu Uchendu
Ikechukwu Uchendu

📖
Matthew Stewart
Matthew Stewart

📖