refactor: simplify GitLab authentication configuration

This commit is contained in:
Mr_Jing 2025-08-06 12:03:53 +08:00
parent dd6f56915b
commit fb73eb75f9
2 changed files with 9 additions and 33 deletions

View file

@ -67,6 +67,7 @@ git clone https://github.com/qodo-ai/pr-agent.git
2. In the secrets file/variables:
- Set your AI model key in the respective section
- In the [gitlab] section, set `personal_access_token` (with token from step 2) and `shared_secret` (with secret from step 3)
- **Authentication type**: Set `auth_type` to `"private_token"` for older GitLab versions (e.g., 11.x) or private deployments. Default is `"oauth_token"` for gitlab.com and newer versions.
6. Build a Docker image for the app and optionally push it to a Docker repository. We'll use Dockerhub as an example:
@ -82,6 +83,7 @@ CONFIG__GIT_PROVIDER=gitlab
GITLAB__PERSONAL_ACCESS_TOKEN=<personal_access_token>
GITLAB__SHARED_SECRET=<shared_secret>
GITLAB__URL=https://gitlab.com
GITLAB__AUTH_TYPE=oauth_token # Use "private_token" for older GitLab versions
OPENAI__KEY=<your_openai_api_key>
```

View file

@ -35,8 +35,13 @@ class GitLabProvider(GitProvider):
gitlab_access_token = get_settings().get("GITLAB.PERSONAL_ACCESS_TOKEN", None)
if not gitlab_access_token:
raise ValueError("GitLab personal access token is not set in the config file")
# Use encapsulated method to determine authentication method
auth_method = self._get_auth_method(gitlab_url)
# Authentication method selection via configuration
auth_method = get_settings().get("GITLAB.AUTH_TYPE", "oauth_token")
# Basic validation of authentication type
if auth_method not in ["oauth_token", "private_token"]:
raise ValueError(f"Unsupported GITLAB.AUTH_TYPE: '{auth_method}'. "
f"Must be 'oauth_token' or 'private_token'.")
# Create GitLab instance based on authentication method
try:
@ -66,37 +71,6 @@ class GitLabProvider(GitProvider):
r"^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@[ ]?(.*)")
self.incremental = incremental
def _get_auth_method(self, gitlab_url: str) -> str:
"""
Determine the authentication method for a GitLab instance.
Args:
gitlab_url: URL of the GitLab instance
Returns:
Authentication method: "oauth_token" or "private_token"
"""
# Check for explicit configuration override first
explicit_auth_type = get_settings().get("GITLAB.AUTH_TYPE", None)
if explicit_auth_type:
# Validate the explicit authentication type
if explicit_auth_type not in ["oauth_token", "private_token"]:
raise ValueError(f"Unsupported GITLAB.AUTH_TYPE: '{explicit_auth_type}'. "
f"Must be 'oauth_token' or 'private_token'.")
return explicit_auth_type
# Default strategy: Use precise hostname matching for gitlab.com and gitlab.io
try:
parsed_url = urlparse(gitlab_url)
hostname = parsed_url.hostname
if hostname and (hostname == "gitlab.com" or hostname == "gitlab.io" or
hostname.endswith(".gitlab.com") or hostname.endswith(".gitlab.io")):
return "oauth_token"
except Exception:
# If URL parsing fails, fall back to private_token for safety
pass
return "private_token"
def is_supported(self, capability: str) -> bool:
if capability in ['get_issue_comments', 'create_inline_comment', 'publish_inline_comments',