Skip to content

Commit eb1bfad

Browse files
authored
Merge pull request #76 from Matars/GraphMissing
Graph missing
2 parents e7f0961 + 91834da commit eb1bfad

2 files changed

Lines changed: 71 additions & 31 deletions

File tree

src/gitfetch/cli.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import argparse
6+
import os
67
import sys
78
import subprocess
89
from typing import Optional
@@ -45,6 +46,19 @@ def _background_refresh_cache_subprocess(username: str) -> None:
4546
pass
4647

4748

49+
def _debug_enabled() -> bool:
50+
"""Return True when debug mode is enabled via env var."""
51+
value = os.environ.get('DEBUG')
52+
if value is None:
53+
# Backward compatibility with older env var name.
54+
value = os.environ.get('GITFETCH_DEBUG')
55+
56+
if value is None:
57+
return False
58+
59+
return value.strip().lower() not in {'', '0', 'false', 'no', 'off'}
60+
61+
4862
def parse_args() -> argparse.Namespace:
4963
"""Parse command-line arguments."""
5064
parser = argparse.ArgumentParser(
@@ -464,9 +478,8 @@ def main() -> int:
464478
# (useful when users report errors from package builds / other
465479
# environments where the short error message is not enough).
466480
try:
467-
import os
468481
import traceback
469-
if os.environ.get('GITFETCH_DEBUG'):
482+
if _debug_enabled():
470483
traceback.print_exc()
471484
else:
472485
print(f"Error: {e}", file=sys.stderr)

src/gitfetch/fetcher.py

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -542,42 +542,69 @@ def _fetch_contribution_graph(self, username: str) -> list:
542542
Returns:
543543
List of weeks with contribution data
544544
"""
545-
# GraphQL query for contribution calendar (inline username)
546-
query = f'''{{
547-
user(login: "{username}") {{
548-
contributionsCollection(includePrivate: true) {{
549-
contributionCalendar {{
550-
weeks {{
551-
contributionDays {{
552-
contributionCount
553-
date
545+
queries = [
546+
# Preferred query: include private contributions when available.
547+
f'''{{
548+
user(login: "{username}") {{
549+
contributionsCollection(includePrivate: true) {{
550+
contributionCalendar {{
551+
weeks {{
552+
contributionDays {{
553+
contributionCount
554+
date
555+
}}
556+
}}
554557
}}
555558
}}
556559
}}
557-
}}
558-
}}
559-
}}'''
560+
}}''',
561+
# Fallback query for auth/scope combinations where includePrivate
562+
# can fail.
563+
f'''{{
564+
user(login: "{username}") {{
565+
contributionsCollection {{
566+
contributionCalendar {{
567+
weeks {{
568+
contributionDays {{
569+
contributionCount
570+
date
571+
}}
572+
}}
573+
}}
574+
}}
575+
}}
576+
}}''',
577+
]
578+
579+
for query in queries:
580+
try:
581+
result = subprocess.run(
582+
['gh', 'api', 'graphql', '-f', f'query={query}'],
583+
capture_output=True,
584+
text=True,
585+
timeout=30,
586+
env=self._build_env()
587+
)
560588

561-
try:
562-
result = subprocess.run(
563-
['gh', 'api', 'graphql', '-f', f'query={query}'],
564-
capture_output=True,
565-
text=True,
566-
timeout=30,
567-
env=self._build_env()
568-
)
589+
if result.returncode != 0:
590+
continue
569591

570-
if result.returncode != 0:
571-
return []
592+
data = json.loads(result.stdout)
593+
weeks = data.get('data', {}).get('user', {}).get(
594+
'contributionsCollection', {}).get(
595+
'contributionCalendar', {}).get('weeks', None)
572596

573-
data = json.loads(result.stdout)
574-
weeks = data.get('data', {}).get('user', {}).get(
575-
'contributionsCollection', {}).get(
576-
'contributionCalendar', {}).get('weeks', [])
577-
return weeks
597+
if isinstance(weeks, list):
598+
if data.get('errors'):
599+
if weeks:
600+
return weeks
601+
continue
602+
return weeks
578603

579-
except (subprocess.TimeoutExpired, json.JSONDecodeError, KeyError):
580-
return []
604+
except (subprocess.TimeoutExpired, json.JSONDecodeError, KeyError):
605+
continue
606+
607+
return []
581608

582609

583610
class GitLabFetcher(BaseFetcher):

0 commit comments

Comments
 (0)