Skip to content

Commit b263d26

Browse files
committed
Enhance total commits calculation by incorporating commits from multiple emails and usernames. Implement a fallback to GraphQL for total commits if the search API fails. Update output messages for clarity.
1 parent 809ac75 commit b263d26

File tree

1 file changed

+72
-25
lines changed

1 file changed

+72
-25
lines changed

github_stats.py

Lines changed: 72 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,11 @@ async def query_rest(self, path: str, params: Optional[Dict] = None) -> Dict:
7777
headers = {
7878
"Authorization": f"token {self.access_token}",
7979
}
80+
81+
# API de busca de commits requer header especial
82+
if "/search/commits" in path:
83+
headers["Accept"] = "application/vnd.github.cloak-preview+json"
84+
8085
if params is None:
8186
params = dict()
8287
if path.startswith("/"):
@@ -773,25 +778,67 @@ async def forks_made(self) -> int:
773778

774779
async def get_all_time_commits(self) -> None:
775780
"""
776-
Get total commits from all years
781+
Get total commits from all time, including commits with different emails
777782
"""
778-
# Não verificar se _total_commits já existe, sempre recalcular
779-
print("Fetching commits from all years...")
783+
print("Fetching total commits...")
784+
total_commits = 0
780785

781-
# Buscar commits de todos os anos
782-
years = (
783-
(await self.queries.query(Queries.contrib_years()))
784-
.get("data", {})
785-
.get("viewer", {})
786-
.get("contributionsCollection", {})
787-
.get("contributionYears", [])
788-
)
786+
# Se temos emails, buscar commits para cada email
787+
if self._emails:
788+
print(f"Using Git emails: {self._emails}")
789+
for email in self._emails:
790+
# Usar a API de busca para contar commits por email
791+
search_url = f"/search/commits"
792+
params = {
793+
"q": f"author-email:{email}",
794+
"per_page": 1
795+
}
796+
797+
result = await self.queries.query_rest(search_url, params)
798+
799+
if result and "total_count" in result:
800+
email_commits = result["total_count"]
801+
total_commits += email_commits
802+
print(f" Email {email}: {email_commits} commits")
789803

790-
total_commits = 0
804+
# Também buscar por username
805+
search_url = f"/search/commits"
806+
params = {
807+
"q": f"author:{self.username}",
808+
"per_page": 1
809+
}
791810

792-
# Para cada ano, buscar o total de commits
793-
for year in years:
794-
query = f"""
811+
result = await self.queries.query_rest(search_url, params)
812+
813+
if result and "total_count" in result:
814+
username_commits = result["total_count"]
815+
print(f" Username {self.username}: {username_commits} commits")
816+
817+
# Se não tínhamos emails, usar apenas commits por username
818+
if not self._emails:
819+
total_commits = username_commits
820+
else:
821+
# Se temos emails, adicionar commits por username que não foram contados
822+
# (isso pode resultar em duplicatas, mas é melhor que perder commits)
823+
if username_commits > total_commits:
824+
total_commits = username_commits
825+
826+
# Se a busca falhou completamente, usar GraphQL como fallback
827+
if total_commits == 0:
828+
print("Search API failed or returned 0, using GraphQL fallback...")
829+
830+
# Buscar commits de todos os anos via GraphQL
831+
years = (
832+
(await self.queries.query(Queries.contrib_years()))
833+
.get("data", {})
834+
.get("viewer", {})
835+
.get("contributionsCollection", {})
836+
.get("contributionYears", [])
837+
)
838+
839+
# Para cada ano, buscar o total de commits
840+
for year in years:
841+
query = f"""
795842
query {{
796843
viewer {{
797844
contributionsCollection(from: "{year}-01-01T00:00:00Z", to: "{int(year) + 1}-01-01T00:00:00Z") {{
@@ -801,18 +848,18 @@ async def get_all_time_commits(self) -> None:
801848
}}
802849
}}
803850
"""
804-
result = await self.queries.query(query)
805-
if result:
806-
contrib = result.get("data", {}).get("viewer", {}).get("contributionsCollection", {})
807-
year_commits = (
808-
contrib.get("totalCommitContributions", 0) +
809-
contrib.get("restrictedContributionsCount", 0)
810-
)
811-
total_commits += year_commits
812-
print(f"Year {year}: {year_commits} commits")
851+
result = await self.queries.query(query)
852+
if result:
853+
contrib = result.get("data", {}).get("viewer", {}).get("contributionsCollection", {})
854+
year_commits = (
855+
contrib.get("totalCommitContributions", 0) +
856+
contrib.get("restrictedContributionsCount", 0)
857+
)
858+
total_commits += year_commits
859+
print(f" Year {year}: {year_commits} commits")
813860

814861
self._total_commits = total_commits
815-
print(f"Total commits from all years: {total_commits}")
862+
print(f"Total commits from all sources: {total_commits}")
816863

817864
@property
818865
async def total_forks(self) -> int:

0 commit comments

Comments
 (0)