-
Notifications
You must be signed in to change notification settings - Fork 664
feat(utils): add GH_TOKEN and .env fallback support for getGitHubToken #1437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -20,11 +20,25 @@ export function getRepoRoot(): string { | |||||
| throw new Error("Could not find project root"); | ||||||
| } | ||||||
|
|
||||||
| export function getGitHubToken() { | ||||||
| const token = process.env.GITHUB_TOKEN; | ||||||
| export function getGitHubToken(): string { | ||||||
| let token = process.env.GITHUB_TOKEN || process.env.GH_TOKEN; | ||||||
|
|
||||||
| if (!token) { | ||||||
| throw new Error("GITHUB_TOKEN environment variable is required."); | ||||||
| const envPath = path.join(process.cwd(), ".env"); | ||||||
| if (existsSync(envPath)) { | ||||||
| const content = readFileSync(envPath, "utf-8"); | ||||||
| const match = content.match(/^GITHUB_TOKEN\s*=\s*(.+)$/m); | ||||||
|
||||||
| const match = content.match(/^GITHUB_TOKEN\s*=\s*(.+)$/m); | |
| const match = content.match(/^GITHUB_TOKEN\s*=\s*([^\r\n]+)/m); |
Copilot
AI
Nov 12, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The regex pattern /^GITHUB_TOKEN\s*=\s*(.+)$/m will capture tokens containing quotes. For example, if the .env file contains GITHUB_TOKEN="token123", the captured token will be "token123" (with quotes). Consider also stripping quotes from the matched value.
Suggested fix:
if (match) token = match[1].trim().replace(/^["']|["']$/g, '');| if (match) token = match[1].trim(); | |
| if (match) token = match[1].trim().replace(/^["']|["']$/g, ''); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
process.cwd()for the .env file path may not align with the project root when the script is executed from a different directory. Consider usinggetRepoRoot()instead to ensure the .env file is consistently read from the repository root, regardless of the working directory: