Skip to content

Commit 349dc59

Browse files
committed
Refactor total commits calculation to fetch data from all contribution years via GraphQL. Remove email and username specific commit counting logic, simplifying the process and improving accuracy. Update output messages for clarity.
1 parent b263d26 commit 349dc59

File tree

1 file changed

+27
-71
lines changed

1 file changed

+27
-71
lines changed

github_stats.py

Lines changed: 27 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,6 @@ async def get_summary_stats(self) -> None:
383383
self._prs = viewer.get("pullRequests", {}).get("totalCount", 0)
384384
self._issues = viewer.get("issues", {}).get("totalCount", 0)
385385
contributions = viewer.get("contributionsCollection", {})
386-
self._total_commits = (
387-
contributions.get("totalCommitContributions", 0)
388-
+ contributions.get("restrictedContributionsCount", 0)
389-
)
390386

391387
async def get_stats(self) -> None:
392388
"""
@@ -778,67 +774,25 @@ async def forks_made(self) -> int:
778774

779775
async def get_all_time_commits(self) -> None:
780776
"""
781-
Get total commits from all time, including commits with different emails
777+
Get total commits from all years via GraphQL
782778
"""
783-
print("Fetching total commits...")
784-
total_commits = 0
785-
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")
803-
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-
}
779+
print("Fetching total commits from all years...")
810780

811-
result = await self.queries.query_rest(search_url, params)
781+
# Buscar todos os anos de contribuição
782+
years = (
783+
(await self.queries.query(Queries.contrib_years()))
784+
.get("data", {})
785+
.get("viewer", {})
786+
.get("contributionsCollection", {})
787+
.get("contributionYears", [])
788+
)
812789

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
790+
print(f"Found contribution years: {years}")
791+
total_commits = 0
825792

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"""
793+
# Para cada ano, buscar o total de commits
794+
for year in years:
795+
query = f"""
842796
query {{
843797
viewer {{
844798
contributionsCollection(from: "{year}-01-01T00:00:00Z", to: "{int(year) + 1}-01-01T00:00:00Z") {{
@@ -848,18 +802,20 @@ async def get_all_time_commits(self) -> None:
848802
}}
849803
}}
850804
"""
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")
805+
result = await self.queries.query(query)
806+
if result and "data" in result:
807+
contrib = result.get("data", {}).get("viewer", {}).get("contributionsCollection", {})
808+
year_commits = (
809+
contrib.get("totalCommitContributions", 0) +
810+
contrib.get("restrictedContributionsCount", 0)
811+
)
812+
total_commits += year_commits
813+
print(f" Year {year}: {year_commits} commits")
814+
else:
815+
print(f" Year {year}: Failed to fetch data")
860816

861817
self._total_commits = total_commits
862-
print(f"Total commits from all sources: {total_commits}")
818+
print(f"Total commits from all years: {total_commits}")
863819

864820
@property
865821
async def total_forks(self) -> int:

0 commit comments

Comments
 (0)