Created
September 14, 2024 18:23
-
-
Save adrianlungu/8a27b44c61800aecc8925661817f1bb3 to your computer and use it in GitHub Desktop.
Python script to retrieve a list of Git commits and calculate a total number of hours using time durations in the commit message
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import re | |
# Example usage: | |
# commit_messages = [ | |
# "Fixed bug [1.5h]", | |
# "Added feature [1h 30m]", | |
# "Refactored code [10m]", | |
# "Implemented new algorithm [5h]" | |
# ] | |
# | |
# time_durations = extract_time_durations(commit_messages) | |
# total_time = calculate_total_time(time_durations) | |
# | |
# print(f"Total time duration: {total_time} minutes") | |
def extract_time_durations(commits): | |
pattern = r'\[(\d+(?:\.\d+)?h)?\s*(\d+m)?\]' | |
time_durations = [] | |
for commit in commits: | |
print(commit["commit"]["message"]) | |
match = re.search(pattern, commit["commit"]["message"]) | |
if match: | |
hours = 0 | |
minutes = 0 | |
if match.group(1): | |
hours = float(match.group(1).replace('h', '')) | |
if match.group(2): | |
minutes = int(match.group(2).replace('m', '')) | |
time_durations.append(hours * 60 + minutes) | |
return time_durations | |
def calculate_total_time(time_durations): | |
return sum(time_durations) | |
# Replace with your access token and repository owner/name | |
access_token = "" | |
repo_owner = "" | |
repo_name = "" | |
start_commit_hash = "" # "" | |
# end_commit_hash = "your_commit_hash" | |
start_commit_timestamp = "2024-07-17T00:00:00Z" # in ISO 8601: YYYY-MM-DDTHH:MM:SSZ | |
# Set API endpoint and headers | |
endpoint = f"https://api.github.com/repos/{repo_owner}/{repo_name}/commits?sha={start_commit_hash}&since={start_commit_timestamp}" | |
headers = {"Authorization": f"Bearer {access_token}"} | |
# Fetch commit data | |
response = requests.get(endpoint, headers=headers) | |
commits = response.json() | |
# Filter commits starting from the desired commit hash | |
# filtered_commits = [commit for commit in commits if commit["sha"] == start_commit_hash or commit["sha"] > start_commit_hash] | |
time_durations = extract_time_durations(commits) | |
total_time = calculate_total_time(time_durations) | |
print(f"Total time duration: {total_time} minutes, {total_time / 60} hours. {len(commits)} commits.") | |
# Iterate over the filtered commits | |
# for commit in commits: | |
# print(commit["sha"], commit["commit"]["message"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment