Skip to content

Commit

Permalink
Auto contributors will now try to merge with main before pushing
Browse files Browse the repository at this point in the history
  • Loading branch information
uchendui committed Sep 27, 2023
1 parent 103b17e commit 4fe14b9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 40 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/auto-add-contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,10 @@ jobs:
python -m pip install --upgrade pip
pip install -r .github/workflows/contributors/requirements.txt
- name: Retrieving all contributors from the main branch
- name: Auto add contributor job has been triggered
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 main)
echo "The contributors are: $PUSHED_CONTRIBUTORS"
- name: Running Python Script to Update .all-contributorsrc
run: |
echo "Running Python Script to Update .all-contributorsrc"
Expand Down
81 changes: 45 additions & 36 deletions .github/workflows/contributors/update_contributors.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,17 @@
import os

from absl import app
from github import Github
import requests

CONTRIBUTORS_FILE = '.all-contributorsrc'

EXCLUDED_USERS = ['web-flow', 'github-actions[bot]', 'mrdragonbear']
EXCLUDED_USERS = {'web-flow', 'github-actions[bot]', 'mrdragonbear'}

OWNER = "harvard-edge"
REPO = "cs249r_book"
BRANCH = "main"


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"]

Expand All @@ -39,35 +24,59 @@ def main(_):
res = requests.get(web_address, headers=headers)

print(web_address)
user_to_name_dict = dict()
users_from_api = []

# 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)
for node in data:
user_full_name = None
username = None
if node['commit']:
commit = node['commit']
if commit['author']:
author = commit['author']
user_full_name = author['name']
elif commit['committer']:
committer = commit['committer']
user_full_name = committer['name']
if node['committer']:
committer = node['committer']
username = committer['login']
if node['author']:
author = node['author']
if author['login']:
username = author['login']

assert user_full_name is not None, 'User full name should not be None'
assert username is not None, 'Username should not be None'

user_to_name_dict[username] = user_full_name
users_from_api.append(username)

users_from_api = set(users_from_api)
print('Users pulled from API: ', users_from_api)

with open(CONTRIBUTORS_FILE, 'r') as contrib_file:
contributors_data = json.load(contrib_file)
user_to_name_dict = dict()
contributors = contributors_data['contributors']
existing_contributor_data = json.load(contrib_file)
existing_contributors = existing_contributor_data['contributors']

existing_contributor_logins = []
for existing_contributor in existing_contributors:
user_to_name_dict[existing_contributor['login']] = existing_contributor['name']
existing_contributor_logins.append(existing_contributor['login'])
existing_contributor_logins_set = set(existing_contributor_logins)
print('Existing contributors: ', existing_contributor_logins_set)

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)
# All contributors in the file should be in the API
assert existing_contributor_logins_set.issubset(
users_from_api), 'All contributors in the .all-contributorsrc file should be pulled using the API'

# Perform the set subtraction
# result = usernames_set - contributor_logins_set
result = contributor_logins_set - set(EXCLUDED_USERS)
new_contributor_logins = users_from_api - existing_contributor_logins_set
print('New contributors: ', new_contributor_logins)

print('New contributors: ', result)
result = users_from_api - EXCLUDED_USERS

final_result = dict(
projectName=REPO,
Expand Down

0 comments on commit 4fe14b9

Please sign in to comment.