za3karia commited on
Commit
a2bfbc7
·
verified ·
1 Parent(s): e60de73

Update functions.py

Browse files
Files changed (1) hide show
  1. functions.py +54 -0
functions.py CHANGED
@@ -54,3 +54,57 @@ def get_repo_branches(repo_name, updated_token=None):
54
  except requests.exceptions.RequestException as e:
55
  print(f"Failed to fetch branches: {e}")
56
  return [] # Return an empty list in case of failure
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
  except requests.exceptions.RequestException as e:
55
  print(f"Failed to fetch branches: {e}")
56
  return [] # Return an empty list in case of failure
57
+ # Function to calculate the start date based on the period
58
+ def get_start_date(period):
59
+ if period == "month":
60
+ return datetime.now() - timedelta(days=30)
61
+ elif period == "3 months":
62
+ return datetime.now() - timedelta(days=30*3)
63
+ elif period == "6 months":
64
+ return datetime.now() - timedelta(days=30*6)
65
+ elif period == "year":
66
+ return datetime.now() - timedelta(days=365)
67
+ else:
68
+ raise ValueError("Invalid period specified")
69
+
70
+ # Function to fetch commits by a contributor in a given period
71
+ def get_contributor_commits(repo_name, contributor, period, updated_token=None):
72
+ """
73
+ Fetch all commits made by a specific contributor within a specified period for a given repository.
74
+
75
+ Parameters:
76
+ - repo_name: str - The full name of the repository (e.g., "owner/repo").
77
+ - contributor: str - The GitHub username of the contributor.
78
+ - period: str - The period for which to fetch commits ("month", "3 months", "6 months", "year").
79
+ - updated_token: str - Optional. A GitHub token for authentication.
80
+
81
+ Returns:
82
+ - A list of commits made by the specified contributor in the given period.
83
+ """
84
+ headers = get_headers(updated_token)
85
+ start_date = get_start_date(period)
86
+ commits = []
87
+
88
+ # Assuming the repository might have multiple branches, we start by fetching branches
89
+ branches_url = f"{GITHUB_API}/repos/{repo_name}/branches"
90
+ try:
91
+ branches_response = requests.get(branches_url, headers=headers)
92
+ branches_response.raise_for_status()
93
+ branches = [branch['name'] for branch in branches_response.json()]
94
+
95
+ # Fetch commits for each branch
96
+ for branch in branches:
97
+ commits_url = f"{GITHUB_API}/repos/{repo_name}/commits"
98
+ params = {
99
+ "sha": branch,
100
+ "since": start_date.isoformat(),
101
+ "author": contributor
102
+ }
103
+ commits_response = requests.get(commits_url, headers=headers, params=params)
104
+ commits_response.raise_for_status()
105
+ branch_commits = commits_response.json()
106
+ commits.extend(branch_commits)
107
+ except requests.exceptions.RequestException as e:
108
+ print(f"Failed to fetch commits: {e}")
109
+
110
+ return commits