Skip to content

Instantly share code, notes, and snippets.

@juandebravo
Last active March 26, 2023 20:01
Show Gist options
  • Save juandebravo/b49db9d84e9803e780bf1741dd2e8c04 to your computer and use it in GitHub Desktop.
Save juandebravo/b49db9d84e9803e780bf1741dd2e8c04 to your computer and use it in GitHub Desktop.
Chat Gpt fixes an issue with matplotlib (again!)
from github import Github
import datetime
import matplotlib.pyplot as plt
# Access the GitHub API using a personal access token
g = Github("<personal_access_token>")
# Replace <repository_owner> and <repository_name> with the owner and name of the repository
repo = g.get_repo("<repository_owner>/<repository_name>")
# Set the date range to retrieve pull requests for
date_range = 180 # Number of days in the date range
end_date = datetime.datetime.now()
start_date = end_date - datetime.timedelta(days=date_range)
# Get all pull requests for the repository
pull_requests = repo.get_pulls(state='all', sort='created', direction='desc', base='main')
# Create a dictionary to hold the times to merge for each day
times_to_merge_per_day = {}
# Iterate over each pull request and calculate the time to merge
for pull_request in pull_requests:
# Check if the pull request was created within the specified date range
if pull_request.created_at.date() >= start_date.date() and pull_request.created_at.date() <= end_date.date():
if pull_request.merged:
# Get the date the pull request was opened
pr_created_date = pull_request.created_at.date()
# Get the date the pull request was merged
pr_merged_date = pull_request.merged_at.date()
# Calculate the time between the pull request being opened and it being merged
time_to_merge = (pr_merged_date - pr_created_date).days
# Add the time to merge to the corresponding day in the dictionary
if pr_created_date in times_to_merge_per_day:
times_to_merge_per_day[pr_created_date].append(time_to_merge)
else:
times_to_merge_per_day[pr_created_date] = [time_to_merge]
else:
print("Pull Request #{} - Not merged yet".format(pull_request.number))
# Create a list of the average times to merge for each day
avg_times_to_merge_per_day = []
for date in date_list:
if date in times_to_merge_per_day:
times_to_merge = times_to_merge_per_day[date]
avg_time_to_merge = sum(times_to_merge) / len(times_to_merge)
else:
avg_time_to_merge = 0
avg_times_to_merge_per_day.append(avg_time_to_merge)
# Create a list of the dates in the specified date range
date_list = [start_date + datetime.timedelta(days=x) for x in range(date_range+1)]
# Check if there are any pull requests merged during the specified date range
if not avg_times_to_merge_per_day:
print("There are no pull requests merged during the specified date range.")
else:
# Plot the average times to merge per day as a bar chart
fig, ax = plt.subplots()
ax.bar(date_list, avg_times_to_merge_per_day)
# Set the x-axis and y-axis labels
ax.set_xlabel('Date')
ax.set_ylabel('Average Time to Merge (days)')
# Set the chart title
ax.set_title('Average Time to Merge for Pull Requests Opened in the Last {} Days'.format(date_range))
# Rotate the x-axis tick labels by 45 degrees for readability
plt.xticks(rotation=45)
# Show the chart
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment