Skip to content

Conversation

@nitesh404240
Copy link

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.

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.
Copy link
Contributor

Copilot AI left a 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_TOKEN as 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();
Copy link

Copilot AI Nov 12, 2025

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, '');
Suggested change
if (match) token = match[1].trim();
if (match) token = match[1].trim().replace(/^["']|["']$/g, '');

Copilot uses AI. Check for mistakes.

if (!token) {
throw new Error("GITHUB_TOKEN environment variable is required.");
const envPath = path.join(process.cwd(), ".env");
Copy link

Copilot AI Nov 12, 2025

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");
Suggested change
const envPath = path.join(process.cwd(), ".env");
const envPath = path.join(getRepoRoot(), ".env");

Copilot uses AI. Check for mistakes.
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);
Copy link

Copilot AI Nov 12, 2025

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);
Suggested change
const match = content.match(/^GITHUB_TOKEN\s*=\s*(.+)$/m);
const match = content.match(/^GITHUB_TOKEN\s*=\s*([^\r\n]+)/m);

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant