-
Notifications
You must be signed in to change notification settings - Fork 663
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?
Conversation
Enhanced getGitHubToken to improve flexibility and local development support. - Added fallback to GH_TOKEN environment variable - Added ability to read GITHUB_TOKEN from a .env file in the project root - Improved error message when no token is found This update ensures authentication works in both CI environments and local setups without requiring manual environment variable configuration.
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.
Pull Request Overview
This PR enhances the getGitHubToken() utility function to provide more flexible authentication token resolution for local development and various CI environments.
Key Changes:
- Added support for
GH_TOKENas an alternative environment variable - Implemented .env file reading as a fallback when environment variables are not set
- Improved error messaging to guide users on all available authentication options
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (existsSync(envPath)) { | ||
| const content = readFileSync(envPath, "utf-8"); | ||
| const match = content.match(/^GITHUB_TOKEN\s*=\s*(.+)$/m); | ||
| if (match) token = match[1].trim(); |
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, ''); |
|
|
||
| if (!token) { | ||
| throw new Error("GITHUB_TOKEN environment variable is required."); | ||
| const envPath = path.join(process.cwd(), ".env"); |
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.
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 using getRepoRoot() instead to ensure the .env file is consistently read from the repository root, regardless of the working directory:
const envPath = path.join(getRepoRoot(), ".env");| const envPath = path.join(process.cwd(), ".env"); | |
| const envPath = path.join(getRepoRoot(), ".env"); |
| 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); |
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 .+ is greedy and will capture trailing whitespace and newlines. While .trim() handles leading/trailing whitespace, it's better to use a more specific pattern. Consider using [^\r\n]+ or \S+ to capture only non-whitespace or non-newline characters:
const match = content.match(/^GITHUB_TOKEN\s*=\s*(\S+)/m);| const match = content.match(/^GITHUB_TOKEN\s*=\s*(.+)$/m); | |
| const match = content.match(/^GITHUB_TOKEN\s*=\s*([^\r\n]+)/m); |
Added fallback logic in getGitHubToken() to improve reliability:
Supports GH_TOKEN as an alternative when GITHUB_TOKEN is not set.
Reads .env file for GITHUB_TOKEN if both environment variables are missing.
Provides more informative error messages for missing authentication tokens.
This change improves developer experience when running locally or in CI environments without a preset GITHUB_TOKEN.