Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions scripts/docs/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
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.
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.
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(
`Missing GitHub authentication token.
Please set one of the following:
- GITHUB_TOKEN or GH_TOKEN environment variable
- Or add GITHUB_TOKEN to a .env file in the project root`
);
}

return token;
Expand Down
Loading