-
Notifications
You must be signed in to change notification settings - Fork 0
Add GitHub token authentication for fetching pull requests #68
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
Conversation
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 adds GitHub token authentication support to improve the reliability of fetching pull request references in the Git utilities module. The authentication is implemented conditionally based on the presence of a GITHUB_TOKEN environment variable.
origin.set_url( | ||
( | ||
f"https://x-access-token:{os.getenv('GITHUB_TOKEN')}" | ||
f"@github.com/{origin.url.split('/')[-2]}/" | ||
f"{origin.url.split('/')[-1]}" | ||
) | ||
) | ||
|
||
# Equivalent to: git fetch origin refs/pull/xx/merge | ||
origin.fetch(current_ref) |
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.
Modifying the origin URL permanently could expose the token in subsequent operations and logs. Consider using a temporary authentication method or creating a separate remote for authenticated operations.
origin.set_url( | |
( | |
f"https://x-access-token:{os.getenv('GITHUB_TOKEN')}" | |
f"@github.com/{origin.url.split('/')[-2]}/" | |
f"{origin.url.split('/')[-1]}" | |
) | |
) | |
# Equivalent to: git fetch origin refs/pull/xx/merge | |
origin.fetch(current_ref) | |
temp_url = ( | |
f"https://x-access-token:{os.getenv('GITHUB_TOKEN')}" | |
f"@github.com/{origin.url.split('/')[-2]}/" | |
f"{origin.url.split('/')[-1]}" | |
) | |
self.logger.debug(f"Using temporary URL for fetch: {temp_url}") | |
# Equivalent to: git fetch <temp_url> refs/pull/xx/merge | |
self.repo.git.fetch(temp_url, current_ref) |
Copilot uses AI. Check for mistakes.
f"@github.com/{origin.url.split('/')[-2]}/" | ||
f"{origin.url.split('/')[-1]}" |
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.
URL parsing using string splitting is brittle and may fail for different URL formats (SSH, different protocols). Consider using urllib.parse or a more robust URL parsing method.
f"@github.com/{origin.url.split('/')[-2]}/" | |
f"{origin.url.split('/')[-1]}" | |
f"@{parsed_url.netloc}/{'/'.join(path_parts)}" |
Copilot uses AI. Check for mistakes.
if os.getenv("GITHUB_TOKEN"): | ||
self.logger.debug( | ||
"Setting up GitHub token authentication for fetch" | ||
) | ||
origin.set_url( | ||
( | ||
f"https://x-access-token:{os.getenv('GITHUB_TOKEN')}" |
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 GitHub token is retrieved twice with os.getenv('GITHUB_TOKEN'), but the second call could return None if the environment variable was removed between calls. Store the token in a variable after the initial check.
if os.getenv("GITHUB_TOKEN"): | |
self.logger.debug( | |
"Setting up GitHub token authentication for fetch" | |
) | |
origin.set_url( | |
( | |
f"https://x-access-token:{os.getenv('GITHUB_TOKEN')}" | |
github_token = os.getenv("GITHUB_TOKEN") | |
if github_token: | |
self.logger.debug( | |
"Setting up GitHub token authentication for fetch" | |
) | |
origin.set_url( | |
( | |
f"https://x-access-token:{github_token}" |
Copilot uses AI. Check for mistakes.
No description provided.