Skip to content

Commit 08b6dfb

Browse files
authored
MAINT: Add check for installed gh cli (#72)
1 parent 2851438 commit 08b6dfb

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

github_activity/github_activity.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -111,24 +111,35 @@ def get_activity(
111111

112112
if not auth:
113113
if "GITHUB_ACCESS_TOKEN" in os.environ:
114+
# Access token is stored in a local environment variable so just use this
114115
print("Using GH access token stored in `GITHUB_ACCESS_TOKEN`.")
115116
auth = os.environ.get("GITHUB_ACCESS_TOKEN")
116117
else:
117-
out = run(shlex.split("gh auth status -t"), capture_output=True)
118-
lines = [ii for ii in out.stderr.decode().split("\n") if "Token:" in ii]
119-
if lines:
120-
print("Using GH access token stored via GH CLI.")
121-
auth = lines[0].split(": ")[-1].strip()
122-
else:
123-
raise ValueError(
124-
"Either the environment variable GITHUB_ACCESS_TOKEN or the "
125-
"--auth flag or must be used to pass a Personal Access Token "
126-
"needed by the GitHub API. You can generate a token at "
127-
"https://github.com/settings/tokens/new. Note that while "
128-
"working with a public repository, you don’t need to set any "
129-
"scopes on the token you create. Alternatively, you may log-in "
130-
"via the GitHub CLI (`gh auth login`)."
118+
# Attempt to use the gh cli if installed
119+
try:
120+
out = run(shlex.split("gh auth status -t"), capture_output=True)
121+
lines = [ii for ii in out.stderr.decode().split("\n") if "Token:" in ii]
122+
if lines:
123+
print("Using GH access token stored via GH CLI.")
124+
auth = lines[0].split(": ")[-1].strip()
125+
except FileNotFoundError:
126+
print(
127+
(
128+
"gh cli not found, so will not use it for auth. To download, "
129+
"see https://cli.github.com/"
130+
)
131131
)
132+
# We can't use this without auth because we hit rate limits immediately
133+
if not auth:
134+
raise ValueError(
135+
"Either the environment variable GITHUB_ACCESS_TOKEN or the "
136+
"--auth flag or must be used to pass a Personal Access Token "
137+
"needed by the GitHub API. You can generate a token at "
138+
"https://github.com/settings/tokens/new. Note that while "
139+
"working with a public repository, you don’t need to set any "
140+
"scopes on the token you create. Alternatively, you may log-in "
141+
"via the GitHub CLI (`gh auth login`)."
142+
)
132143

133144
# Figure out dates for our query
134145
since_dt, since_is_git_ref = _get_datetime_and_type(org, repo, since, auth)

0 commit comments

Comments
 (0)