Skip to content

Commit 2240e3c

Browse files
Update print_github_reviews.py to read token from ~/.github_token (#1752)
If GITHUB_TOKEN environment variable is not set and --token argument is not provided, the script will now attempt to read the GitHub token from the `~/.github_token` file. The order of precedence for token retrieval is: 1. --token command-line argument 2. GITHUB_TOKEN environment variable 3. ~/.github_token file Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent df805b2 commit 2240e3c

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

scripts/print_github_reviews.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ def parse_repo_url(url_string):
254254
"--token",
255255
type=str,
256256
default=os.environ.get("GITHUB_TOKEN"),
257-
help="GitHub token. Can also be set via GITHUB_TOKEN env var."
257+
help="GitHub token. Can also be set via GITHUB_TOKEN env var or from ~/.github_token."
258258
)
259259
parser.add_argument(
260260
"--context-lines",
@@ -289,9 +289,23 @@ def parse_repo_url(url_string):
289289
latest_line_comment_activity_dt = None
290290
processed_comments_count = 0
291291

292-
if not args.token:
293-
sys.stderr.write(f"Error: GitHub token not provided. Set GITHUB_TOKEN or use --token.{error_suffix}\n")
292+
token = args.token
293+
if not token:
294+
try:
295+
with open(os.path.expanduser("~/.github_token"), "r") as f:
296+
token = f.read().strip()
297+
if token:
298+
sys.stderr.write("Using token from ~/.github_token\n")
299+
except FileNotFoundError:
300+
pass # File not found is fine, we'll check token next
301+
except Exception as e:
302+
sys.stderr.write(f"Warning: Could not read ~/.github_token: {e}\n")
303+
304+
305+
if not token:
306+
sys.stderr.write(f"Error: GitHub token not provided. Set GITHUB_TOKEN, use --token, or place it in ~/.github_token.{error_suffix}\n")
294307
sys.exit(1)
308+
args.token = token # Ensure args.token is populated for the rest of the script
295309

296310
final_owner = None
297311
final_repo = None

0 commit comments

Comments
 (0)