-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The GitProviderFactory class manages the creation of the GitProvider based on input and default logic. Signed-off-by: Jennifer Power <[email protected]>
- Loading branch information
Showing
1 changed file
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# Copyright (c) 2024 Red Hat, Inc. | ||
|
||
import logging | ||
from typing import Optional | ||
|
||
from trestlebot.github import GitHub, is_github_actions | ||
from trestlebot.gitlab import GitLab, get_gitlab_root_url, is_gitlab_ci | ||
from trestlebot.provider import GitProvider | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class GitProviderFactory: | ||
"""Factory class for creating Git provider objects""" | ||
|
||
@staticmethod | ||
def provider_factory( | ||
access_token: str, type: str = "", server_url: str = "" | ||
) -> GitProvider: | ||
""" | ||
Factory class for creating Git provider objects | ||
Args: | ||
access_token: Access token for the Git provider | ||
type: Type of Git provider. Supported values are "github" or "gitlab" | ||
server_url: URL of the Git provider server | ||
Returns: | ||
a GitProvider object | ||
Notes: | ||
If type is not provided, the method will attempt to detect the Git provider from the | ||
environment. | ||
Raises: | ||
ValueError: If the server URL is provided for GitHub provider | ||
RuntimeError: If the Git provider cannot be detected | ||
""" | ||
|
||
git_provider: Optional[GitProvider] = None | ||
|
||
if type == "github": | ||
logger.debug("Creating GitHub provider") | ||
if server_url and server_url != "https://github.com": | ||
raise ValueError("GitHub provider does not support custom server URLs") | ||
git_provider = GitHub(access_token=access_token) | ||
elif type == "gitlab": | ||
logger.debug("Creating GitLab provider") | ||
git_provider = GitLab(api_token=access_token, server_url=server_url) | ||
else: | ||
logger.debug( | ||
"No type or server_url provided." | ||
"Detecting Git provider from environment." | ||
) | ||
git_provider = GitProviderFactory._detect_from_environment(access_token) | ||
|
||
if git_provider is None: | ||
raise RuntimeError("Could not detect Git provider from environment") | ||
|
||
return git_provider | ||
|
||
@staticmethod | ||
def _detect_from_environment(access_token: str) -> Optional[GitProvider]: | ||
"""Detect the Git provider from the environment""" | ||
git_provider: Optional[GitProvider] = None | ||
if is_github_actions(): | ||
logging.debug("Detected GitHub Actions environment") | ||
git_provider = GitHub(access_token=access_token) | ||
elif is_gitlab_ci(): | ||
logging.debug("Detected GitLab CI environment") | ||
server_api_url = get_gitlab_root_url() | ||
git_provider = GitLab(api_token=access_token, server_url=server_api_url) | ||
return git_provider |