Skip to content

Instantly share code, notes, and snippets.

@juandebravo
Created March 26, 2023 20:15
Show Gist options
  • Save juandebravo/33ba2ff6bf9c827f12d243f6e4cdce1a to your computer and use it in GitHub Desktop.
Save juandebravo/33ba2ff6bf9c827f12d243f6e4cdce1a to your computer and use it in GitHub Desktop.
chat GTP Get information about Pull Requests now with hours to get merged
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).total_seconds() / 3600
# 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 dates in the specified date range
date_list = [start_date + datetime.timedelta(days=x) for x in range(date_range+1)]
# 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)
# 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 (hours)')
# Set the chart title
ax.set_title('Average Time to Merge for Pull Requests Opened in the Last {} Days'.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment