Skip to content

Commit

Permalink
Added different print statements to debug infinite run.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sammybams committed Sep 25, 2024
1 parent 7706a43 commit c2eb31c
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions update_leaderboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from collections import defaultdict
import os

print("Updating the GitHub Leaderboard...")

def initialize_api():
# Define your GitHub repository and authentication token
repository_owner = "mlsanigeria"
Expand All @@ -29,26 +31,32 @@ def initialize_api():

all_prs = []
while True:
print(f"Fetching PRs, page: {params['page']}") # Debug: print current page number
response = requests.get(api_url, params=params, headers=headers)

# Check if the request was successful (HTTP status code 200)
if response.status_code == 200:
# Parse the JSON response
pull_requests = response.json()
print("Pull requests: ", pull_requests)
if len(pull_requests) == 0:
# No more PRs to fetch, break out of the loop
print(f"Fetched {len(pull_requests)} PRs") # Debug: number of PRs fetched

if not pull_requests:
print("No more PRs to fetch. Exiting loop.") # Debug: No more PRs
break
else:
# Extend the list of all PRs with the current page's PRs
all_prs.extend(pull_requests)
# Move to the next page
params['page'] += 1
# Send a GET request to the GitHub API
else:
print(f"Error fetching PRs: {response.status_code}") # Debug: print error code
break

print(f"Total PRs fetched: {len(all_prs)}") # Debug: total PRs fetched
return all_prs


def get_sorted_pr():

print("Fetching and sorting pull requests...") # Debug: process start message
response = initialize_api()

# Create a dictionary to track the count of merged pull requests by each user
Expand All @@ -65,20 +73,21 @@ def get_sorted_pr():

# Check if the user is exempted
if pr_by in exempt:
print(f"Skipping exempt user: {pr_by}") # Debug: Skipping user
continue
else:
# Increment the count of merged pull requests for this user
merged_prs_count_by_user[pr_by] += 1
avi[pr_by] = pr['user']['avatar_url']
# Print the sorted list of users and their merged pull request counts
# sorted_users = sorted(merged_prs_count_by_user.items(), key=lambda x: x[1], reverse=True)
sorted_users = sorted(merged_prs_count_by_user.items(), key=lambda x: (-x[1], x[0].lower()))
print(f"User: {pr_by}, Merged PRs: {merged_prs_count_by_user[pr_by]}") # Debug: user PR count

# Sort the users by the number of merged pull requests in descending order
sorted_users = sorted(merged_prs_count_by_user.items(), key=lambda x: (-x[1], x[0].lower()))
print(f"Total contributors: {len(sorted_users)}") # Debug: total contributors count
return sorted_users, avi

def leaderboard_data():
# Calculate the leaderboard data
print("Generating leaderboard data...") # Debug: process start message
sorted_users, avi = get_sorted_pr()
leaderboard_data = []
rank = 1
Expand All @@ -91,6 +100,7 @@ def leaderboard_data():
2: "🥈", # Silver Medal
3: "🥉", # Bronze Medal
}

for user, count in sorted_users:
if count == last_count:
rank -= 1
Expand All @@ -101,16 +111,12 @@ def leaderboard_data():
rank += 1
pos += 1

print(f"Generated leaderboard data for {len(leaderboard_data)} contributors.") # Debug: leaderboard count
return leaderboard_data



leaderboard_data = leaderboard_data()





# Generate the Markdown content for the leaderboard
leaderboard_content = """
# GitHub Leaderboard
Expand All @@ -137,12 +143,13 @@ def leaderboard_data():
# Write the Markdown content to LEADERBOARD.md
with open("LEADERBOARD.md", "w") as readme_file:
readme_file.write(leaderboard_content)
print("successfully updated LEADERBOARD.md")
print("Successfully updated LEADERBOARD.md") # Debug: Success message


# filter only the top 10 contributors
# Filter only the top 10 contributors
max_position = 10
filtered_data = [contributor for contributor in leaderboard_data if contributor['position'] <= max_position]
print(f"Filtered top {max_position} contributors.") # Debug: Filtered top contributors

# Generate the Markdown content for the README
readme_content = """
Expand Down Expand Up @@ -175,6 +182,7 @@ def update_readme_section(readme_path, section_start, section_end, new_content):
bool: True if the section was successfully updated, False otherwise.
"""
try:
print(f"Updating README section from {section_start} to {section_end}") # Debug: update message
# Open and read the README file
with open(readme_path, 'r') as file:
readme_contents = file.read()
Expand All @@ -185,6 +193,7 @@ def update_readme_section(readme_path, section_start, section_end, new_content):

if section_start_index == -1 or section_end_index == -1 or section_start_index >= section_end_index:
# Section not found or invalid markers, return False
print("Section markers not found or invalid.") # Debug: Section not found
return False

# Update the section content
Expand All @@ -198,12 +207,11 @@ def update_readme_section(readme_path, section_start, section_end, new_content):
# Write the updated contents back to the file
with open(readme_path, 'w') as file:
file.write(updated_readme_contents)

print("Section updated successfully.") # Debug: Section updated
return True # Section updated successfully

except Exception as e:
# Handle exceptions
print(f"An error occurred: {e}")
print(f"An error occurred: {e}") # Debug: Exception message
return False

readme_path = 'README.md'
Expand All @@ -212,9 +220,6 @@ def update_readme_section(readme_path, section_start, section_end, new_content):
new_content = readme_content

if update_readme_section(readme_path, section_start, section_end, new_content):
print("Section updated successfully.")
print("README section updated successfully.")
else:
print("Section update failed.")



print

0 comments on commit c2eb31c

Please sign in to comment.