Skip to content

Instantly share code, notes, and snippets.

Created March 26, 2023 19:47
Show Gist options
  • Save juandebravo/9ca2ea70d9d38c029809763871f9b1b8 to your computer and use it in GitHub Desktop.
Save juandebravo/9ca2ea70d9d38c029809763871f9b1b8 to your computer and use it in GitHub Desktop.
Chat GPT answer when I asked it to don't use `since` as it does not exist
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 =
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='master')
# 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 >= and <=
if pull_request.merged:
# Get the date the pull request was opened
pr_created_date =
# Get the date the pull request was merged
pr_merged_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] = [time_to_merge]
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, times_to_merge in times_to_merge_per_day.items():
avg_time_to_merge = sum(times_to_merge) / len(times_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)]
# Plot the average times to merge per day as a bar chart
fig, ax = plt.subplots(), avg_times_to_merge_per_day)
# Set the x-axis and y-axis labels
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
# Show the chart
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment