improve: enhance GitLab provider error handling and attribute access safety

- Add try-catch block around GitLab instance creation for better error handling
- Use getattr() for safer attribute access in _prepare_clone_url_with_token method
- Improve authentication failure debugging with clearer error messages
This commit is contained in:
Mr_Jing 2025-08-04 10:35:52 +08:00
parent 2d858a43be
commit dd6f56915b

View file

@ -39,6 +39,7 @@ class GitLabProvider(GitProvider):
auth_method = self._get_auth_method(gitlab_url)
# Create GitLab instance based on authentication method
try:
if auth_method == "oauth_token":
self.gl = gitlab.Gitlab(
url=gitlab_url,
@ -49,6 +50,9 @@ class GitLabProvider(GitProvider):
url=gitlab_url,
private_token=gitlab_access_token
)
except Exception as e:
get_logger().error(f"Failed to create GitLab instance: {e}")
raise ValueError(f"Unable to authenticate with GitLab: {e}")
self.max_comment_chars = 65000
self.id_project = None
self.id_mr = None
@ -719,7 +723,7 @@ class GitLabProvider(GitProvider):
get_logger().error(f"Repo URL: {repo_url_to_clone} is not a valid gitlab URL.")
return None
(scheme, base_url) = repo_url_to_clone.split("gitlab.")
access_token = self.gl.oauth_token or self.gl.private_token
access_token = getattr(self.gl, 'oauth_token', None) or getattr(self.gl, 'private_token', None)
if not all([scheme, access_token, base_url]):
get_logger().error(f"Either no access token found, or repo URL: {repo_url_to_clone} "
f"is missing prefix: {scheme} and/or base URL: {base_url}.")