From c28471ad93d1aee87572c548fd945375c38e8a68 Mon Sep 17 00:00:00 2001 From: Ikechukwu Uchendu Date: Wed, 27 Sep 2023 09:41:22 -0400 Subject: [PATCH] Auto contributors will now try to merge with main before pushing --- .github/workflows/auto-add-contributors.yml | 5 +- .../contributors/update_contributors.py | 59 ++++++++----------- 2 files changed, 26 insertions(+), 38 deletions(-) diff --git a/.github/workflows/auto-add-contributors.yml b/.github/workflows/auto-add-contributors.yml index 756bb6a4..280f9bed 100644 --- a/.github/workflows/auto-add-contributors.yml +++ b/.github/workflows/auto-add-contributors.yml @@ -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" diff --git a/.github/workflows/contributors/update_contributors.py b/.github/workflows/contributors/update_contributors.py index 2acae10a..9f856645 100644 --- a/.github/workflows/contributors/update_contributors.py +++ b/.github/workflows/contributors/update_contributors.py @@ -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"] @@ -39,35 +24,41 @@ def main(_): res = requests.get(web_address, headers=headers) print(web_address) + user_to_name_dict = dict() # 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) + users_from_api = [] + for commit in data: + if commit['committer']: + committer = commit['committer'] + users_from_api.append(committer['login']) + user_to_name_dict[committer['login']] = committer['name'] + 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,