From fb73eb75f900718adb5af82017fb89b14e1185e9 Mon Sep 17 00:00:00 2001 From: Mr_Jing Date: Wed, 6 Aug 2025 12:03:53 +0800 Subject: [PATCH] refactor: simplify GitLab authentication configuration --- docs/docs/installation/gitlab.md | 2 ++ pr_agent/git_providers/gitlab_provider.py | 40 ++++------------------- 2 files changed, 9 insertions(+), 33 deletions(-) diff --git a/docs/docs/installation/gitlab.md b/docs/docs/installation/gitlab.md index 7e587617..da54848e 100644 --- a/docs/docs/installation/gitlab.md +++ b/docs/docs/installation/gitlab.md @@ -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= GITLAB__SHARED_SECRET= GITLAB__URL=https://gitlab.com +GITLAB__AUTH_TYPE=oauth_token # Use "private_token" for older GitLab versions OPENAI__KEY= ``` diff --git a/pr_agent/git_providers/gitlab_provider.py b/pr_agent/git_providers/gitlab_provider.py index e172106f..c75e2a8a 100644 --- a/pr_agent/git_providers/gitlab_provider.py +++ b/pr_agent/git_providers/gitlab_provider.py @@ -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',