Skip to content

Commit 3f0a1d5

Browse files
committed
Refactor forks calculation to include both received and made forks. Remove deprecated user_forked_repos method and streamline the fetching of user-made forks with pagination support. Ensure total commits are always recalculated for accuracy.
1 parent 244c540 commit 3f0a1d5

File tree

1 file changed

+65
-52
lines changed

1 file changed

+65
-52
lines changed

github_stats.py

Lines changed: 65 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -296,31 +296,6 @@ def all_contribs(cls, years: List[str]) -> str:
296296
}}
297297
"""
298298

299-
@staticmethod
300-
def user_forked_repos() -> str:
301-
"""
302-
:return: GraphQL query to get repositories forked by the user
303-
"""
304-
return """
305-
query {
306-
viewer {
307-
repositories(first: 100, isFork: true, ownerAffiliations: OWNER) {
308-
totalCount
309-
pageInfo {
310-
hasNextPage
311-
endCursor
312-
}
313-
nodes {
314-
nameWithOwner
315-
parent {
316-
nameWithOwner
317-
}
318-
}
319-
}
320-
}
321-
}
322-
"""
323-
324299

325300
class Stats(object):
326301
"""
@@ -529,13 +504,22 @@ async def stargazers(self) -> int:
529504
@property
530505
async def forks(self) -> int:
531506
"""
532-
:return: total number of forks on user's repos
507+
:return: total number of forks on user's repos + forks made by user
533508
"""
534-
if self._forks is not None:
535-
return self._forks
536-
await self.get_summary_stats()
509+
# Primeiro, obter forks recebidos
510+
if self._forks is None:
511+
await self.get_summary_stats()
537512
assert self._forks is not None
538-
return self._forks
513+
forks_received = self._forks
514+
515+
# Depois, obter forks feitos
516+
forks_made = await self.forks_made
517+
518+
# Retornar a soma total
519+
total = forks_received + forks_made
520+
print(f"Total forks: {forks_received} received + {forks_made} made = {total}")
521+
522+
return total
539523

540524
@property
541525
async def languages(self) -> Dict:
@@ -689,9 +673,9 @@ async def total_commits(self) -> int:
689673
"""
690674
Get the total number of commits made by the user from all years.
691675
"""
692-
if self._total_commits is not None:
693-
return self._total_commits
694-
await self.get_all_time_commits()
676+
# Sempre recalcular se for None para garantir contagem de todos os anos
677+
if self._total_commits is None:
678+
await self.get_all_time_commits()
695679
assert self._total_commits is not None
696680
return self._total_commits
697681

@@ -725,20 +709,55 @@ async def get_user_forks(self) -> None:
725709
return
726710

727711
print("Fetching forks made by user...")
728-
raw_results = await self.queries.query(Queries.user_forked_repos())
729712

730-
if raw_results is None:
731-
self._forks_made = 0
732-
return
713+
total_forks = 0
714+
cursor = None
715+
716+
while True:
717+
query = f"""
718+
query {{
719+
viewer {{
720+
repositories(first: 100, isFork: true, ownerAffiliations: OWNER, after: {"null" if cursor is None else '"' + cursor + '"'}) {{
721+
totalCount
722+
pageInfo {{
723+
hasNextPage
724+
endCursor
725+
}}
726+
nodes {{
727+
nameWithOwner
728+
parent {{
729+
nameWithOwner
730+
}}
731+
}}
732+
}}
733+
}}
734+
}}
735+
"""
736+
raw_results = await self.queries.query(query)
733737

734-
viewer = raw_results.get("data", {}).get("viewer", {})
735-
if not viewer:
736-
self._forks_made = 0
737-
return
738+
if raw_results is None:
739+
self._forks_made = 0
740+
return
741+
742+
viewer = raw_results.get("data", {}).get("viewer", {})
743+
if not viewer:
744+
self._forks_made = 0
745+
return
746+
747+
repositories = viewer.get("repositories", {})
738748

739-
repositories = viewer.get("repositories", {})
740-
self._forks_made = repositories.get("totalCount", 0)
741-
749+
# Na primeira página, pegamos o totalCount
750+
if cursor is None:
751+
total_forks = repositories.get("totalCount", 0)
752+
753+
# Verificar se há mais páginas
754+
page_info = repositories.get("pageInfo", {})
755+
if page_info.get("hasNextPage"):
756+
cursor = page_info.get("endCursor")
757+
else:
758+
break
759+
760+
self._forks_made = total_forks
742761
print(f"Found {self._forks_made} forks made by user")
743762

744763
@property
@@ -756,16 +775,10 @@ async def get_all_time_commits(self) -> None:
756775
"""
757776
Get total commits from all years
758777
"""
759-
if self._total_commits is not None:
760-
return
761-
778+
# Não verificar se _total_commits já existe, sempre recalcular
762779
print("Fetching commits from all years...")
763780

764-
# Primeiro, pegar os commits do contributionsCollection (ano atual)
765-
await self.get_summary_stats()
766-
current_year_commits = self._total_commits or 0
767-
768-
# Depois, buscar commits de todos os anos
781+
# Buscar commits de todos os anos
769782
years = (
770783
(await self.queries.query(Queries.contrib_years()))
771784
.get("data", {})

0 commit comments

Comments
 (0)