Skip to content

Instantly share code, notes, and snippets.

@juandebravo
Created March 26, 2023 19:34
Show Gist options
  • Save juandebravo/9c23aec131deef9f04877ba403f64d46 to your computer and use it in GitHub Desktop.
Save juandebravo/9c23aec131deef9f04877ba403f64d46 to your computer and use it in GitHub Desktop.
Chat GPT answer when I asked it to plot daily information about Pull Requests opened in a github repo during the last 6 months
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 created in the specified date range
pull_requests = repo.get_pulls(state='all', sort='created', direction='desc', base='master', since=start_date, until=end_date)
# 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:
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, times_to_merge in times_to_merge_per_day.items():
avg_time_to_merge = sum(times_to_merge) / len(times_to_merge)
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)]
# 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