11"""Use the GraphQL api to grab issues/PRs that match a query."""
22import datetime
3+ import os
34import sys
45import urllib
56from pathlib import Path
@@ -103,9 +104,20 @@ def get_activity(
103104 # We have just org
104105 search_query = f"user:{ org } "
105106
107+ auth = auth or os .environ .get ("GITHUB_ACCESS_TOKEN" )
108+ if not auth :
109+ raise ValueError (
110+ "Either the environment variable GITHUB_ACCESS_TOKEN or the "
111+ "--auth flag or must be used to pass a Personal Access Token "
112+ "needed by the GitHub API. You can generate a token at "
113+ "https://github.com/settings/tokens/new. Note that while "
114+ "working with a public repository, you don’t need to set any "
115+ "scopes on the token you create."
116+ )
117+
106118 # Figure out dates for our query
107- since_dt , since_is_git_ref = _get_datetime_and_type (org , repo , since )
108- until_dt , until_is_git_ref = _get_datetime_and_type (org , repo , until )
119+ since_dt , since_is_git_ref = _get_datetime_and_type (org , repo , since , auth )
120+ until_dt , until_is_git_ref = _get_datetime_and_type (org , repo , until , auth )
109121 since_dt_str = f"{ since_dt :%Y-%m-%dT%H:%M:%SZ} "
110122 until_dt_str = f"{ until_dt :%Y-%m-%dT%H:%M:%SZ} "
111123
@@ -523,7 +535,7 @@ def _parse_target(target):
523535 return org , repo
524536
525537
526- def _get_datetime_and_type (org , repo , datetime_or_git_ref ):
538+ def _get_datetime_and_type (org , repo , datetime_or_git_ref , auth ):
527539 """Return a datetime object and bool indicating if it is a git reference or
528540 not."""
529541
@@ -534,7 +546,7 @@ def _get_datetime_and_type(org, repo, datetime_or_git_ref):
534546 return (dt , False )
535547
536548 try :
537- dt = _get_datetime_from_git_ref (org , repo , datetime_or_git_ref )
549+ dt = _get_datetime_from_git_ref (org , repo , datetime_or_git_ref , auth )
538550 return (dt , True )
539551 except Exception as ref_error :
540552 try :
@@ -548,10 +560,11 @@ def _get_datetime_and_type(org, repo, datetime_or_git_ref):
548560 )
549561
550562
551- def _get_datetime_from_git_ref (org , repo , ref ):
563+ def _get_datetime_from_git_ref (org , repo , ref , auth ):
552564 """Return a datetime from a git reference."""
553-
554- response = requests .get (f"https://api.github.com/repos/{ org } /{ repo } /commits/{ ref } " )
565+ headers = {"Authorization" : "Bearer %s" % auth }
566+ url = f"https://api.github.com/repos/{ org } /{ repo } /commits/{ ref } "
567+ response = requests .get (url , headers = headers )
555568 response .raise_for_status ()
556569 return dateutil .parser .parse (response .json ()["commit" ]["committer" ]["date" ])
557570
0 commit comments