Skip to content

Commit

Permalink
Added code to automatically update contributors
Browse files Browse the repository at this point in the history
  • Loading branch information
uchendui committed Sep 22, 2023
1 parent dc26c68 commit f1a8774
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 49 deletions.
83 changes: 42 additions & 41 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
@@ -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"
}
61 changes: 61 additions & 0 deletions .github/workflows/auto-add-contributors.yml
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions .github/workflows/contributors/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
absl-py
pandas
PyGithub
104 changes: 104 additions & 0 deletions .github/workflows/contributors/update_contributors.py
Original file line number Diff line number Diff line change
@@ -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)
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,12 @@ quarto render
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
<tbody>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/jveejay"><img src="https://avatars.githubusercontent.com/jveejay" width="100px;" alt="Vijay Janapa Reddi"/><br /><sub><b>Vijay Janapa Reddi</b></sub></a><br /><a href="https://github.com/all-contributors/all-contributors/commits?author=jveejay" title="Documentation">πŸ“–</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/mpstewart1"><img src="https://avatars.githubusercontent.com/mpstewart1" width="100px;" alt="Matthew Stewart"/><br /><sub><b>Matthew Stewart</b></sub></a><br /><a href="https://github.com/all-contributors/all-contributors/commits?author=mpstewart1" title="Documentation">πŸ“–</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/uchendui"><img src="https://avatars.githubusercontent.com/uchendui?v=4?s=100" width="100px;" alt="LBF38"/><br /><sub><b>Ikechukwu Uchendu</b></sub></a><br /><a href="https://github.com/all-contributors/all-contributors/commits?author=uchendui" title="Documentation">πŸ“–</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/jveejay"><img src="https://avatars.githubusercontent.com/jveejay?s=100" width="100px;" alt="Vijay Janapa Reddi"/><br /><sub><b>Vijay Janapa Reddi</b></sub></a><br /><a href="https=//github.com/harvard-edge/cs249r_book/commits?author=jveejay" title="Documentation">πŸ“–</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/uchendui"><img src="https://avatars.githubusercontent.com/uchendui?s=100" width="100px;" alt="Ikechukwu Uchendu"/><br /><sub><b>Ikechukwu Uchendu</b></sub></a><br /><a href="https=//github.com/harvard-edge/cs249r_book/commits?author=uchendui" title="Documentation">πŸ“–</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/mpstewart1"><img src="https://avatars.githubusercontent.com/mpstewart1?s=100" width="100px;" alt="Matthew Stewart"/><br /><sub><b>Matthew Stewart</b></sub></a><br /><a href="https=//github.com/harvard-edge/cs249r_book/commits?author=mpstewart1" title="Documentation">πŸ“–</a></td>
</tr>
</tbody>
</table>
Expand Down
7 changes: 3 additions & 4 deletions contributors.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ We extend our heartfelt gratitude to the diverse group of individuals who have s
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->

<table>
<tbody>
<tr>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/jveejay"><img src="https://avatars.githubusercontent.com/jveejay" width="100px;" alt="Vijay Janapa Reddi"/><br /><sub><b>Vijay Janapa Reddi</b></sub></a><br /><a href="https://github.com/all-contributors/all-contributors/commits?author=jveejay" title="Documentation">πŸ“–</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/mpstewart1"><img src="https://avatars.githubusercontent.com/mpstewart1" width="100px;" alt="Matthew Stewart"/><br /><sub><b>Matthew Stewart</b></sub></a><br /><a href="https://github.com/all-contributors/all-contributors/commits?author=mpstewart1" title="Documentation">πŸ“–</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/uchendui"><img src="https://avatars.githubusercontent.com/uchendui?v=4?s=100" width="100px;" alt="LBF38"/><br /><sub><b>Ikechukwu Uchendu</b></sub></a><br /><a href="https://github.com/all-contributors/all-contributors/commits?author=uchendui" title="Documentation">πŸ“–</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/jveejay"><img src="https://avatars.githubusercontent.com/jveejay?s=100" width="100px;" alt="Vijay Janapa Reddi"/><br /><sub><b>Vijay Janapa Reddi</b></sub></a><br /><a href="https=//github.com/harvard-edge/cs249r_book/commits?author=jveejay" title="Documentation">πŸ“–</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/uchendui"><img src="https://avatars.githubusercontent.com/uchendui?s=100" width="100px;" alt="Ikechukwu Uchendu"/><br /><sub><b>Ikechukwu Uchendu</b></sub></a><br /><a href="https=//github.com/harvard-edge/cs249r_book/commits?author=uchendui" title="Documentation">πŸ“–</a></td>
<td align="center" valign="top" width="14.28%"><a href="https://github.com/mpstewart1"><img src="https://avatars.githubusercontent.com/mpstewart1?s=100" width="100px;" alt="Matthew Stewart"/><br /><sub><b>Matthew Stewart</b></sub></a><br /><a href="https=//github.com/harvard-edge/cs249r_book/commits?author=mpstewart1" title="Documentation">πŸ“–</a></td>
</tr>
</tbody>
</table>
Expand Down

0 comments on commit f1a8774

Please sign in to comment.