|
16 | 16 | # along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17 | 17 | """Wrapper for the GitLab API."""
|
18 | 18 |
|
| 19 | +import os |
19 | 20 | import time
|
| 21 | +from argparse import Namespace |
20 | 22 | from typing import Any, cast, Dict, List, Optional, Tuple, TYPE_CHECKING, Union
|
21 | 23 |
|
22 | 24 | import requests
|
@@ -256,6 +258,87 @@ def from_config(
|
256 | 258 | retry_transient_errors=config.retry_transient_errors,
|
257 | 259 | )
|
258 | 260 |
|
| 261 | + @classmethod |
| 262 | + def merge_config( |
| 263 | + cls, |
| 264 | + options: Namespace, |
| 265 | + gitlab_id: Optional[str] = None, |
| 266 | + config_files: Optional[List[str]] = None, |
| 267 | + ) -> "Gitlab": |
| 268 | + """Create a Gitlab connection by merging configuration with |
| 269 | + the following precedence: |
| 270 | +
|
| 271 | + 1. Explicitly provided CLI arguments, |
| 272 | + 2. Environment variables, |
| 273 | + 3. Configuration files: |
| 274 | + a. explicitly defined config files: |
| 275 | + i. via the `--config-file` CLI argument, |
| 276 | + ii. via the `PYTHON_GITLAB_CFG` environment variable, |
| 277 | + b. user-specific config file, |
| 278 | + c. system-level config file, |
| 279 | + 4. Environment variables always present in CI (CI_SERVER_URL, CI_JOB_TOKEN). |
| 280 | +
|
| 281 | + Args: |
| 282 | + options list[str]: List of options provided via the CLI. |
| 283 | + gitlab_id (str): ID of the configuration section. |
| 284 | + config_files list[str]: List of paths to configuration files. |
| 285 | + Returns: |
| 286 | + (gitlab.Gitlab): A Gitlab connection. |
| 287 | +
|
| 288 | + Raises: |
| 289 | + gitlab.config.GitlabDataError: If the configuration is not correct. |
| 290 | + """ |
| 291 | + config = gitlab.config.GitlabConfigParser( |
| 292 | + gitlab_id=gitlab_id, config_files=config_files |
| 293 | + ) |
| 294 | + url = ( |
| 295 | + options.url |
| 296 | + or config.url |
| 297 | + or os.getenv("CI_SERVER_URL") |
| 298 | + or gitlab.const.DEFAULT_URL |
| 299 | + ) |
| 300 | + private_token, oauth_token, job_token = cls._get_auth_from_env(options, config) |
| 301 | + |
| 302 | + return cls( |
| 303 | + url=url, |
| 304 | + private_token=private_token, |
| 305 | + oauth_token=oauth_token, |
| 306 | + job_token=job_token, |
| 307 | + ssl_verify=options.ssl_verify or config.ssl_verify, |
| 308 | + timeout=options.timeout or config.timeout, |
| 309 | + api_version=options.api_version or config.api_version, |
| 310 | + per_page=options.per_page or config.per_page, |
| 311 | + pagination=options.pagination or config.pagination, |
| 312 | + order_by=options.order_by or config.order_by, |
| 313 | + user_agent=options.user_agent or config.user_agent, |
| 314 | + ) |
| 315 | + |
| 316 | + @staticmethod |
| 317 | + def _get_auth_from_env( |
| 318 | + options: Namespace, config: gitlab.config.GitlabConfigParser |
| 319 | + ) -> Tuple: |
| 320 | + """ |
| 321 | + Return a tuple where at most one of 3 token types ever has a value. |
| 322 | + Since multiple types of tokens may be present in the environment, |
| 323 | + options, or config files, this precedence ensures we don't |
| 324 | + inadvertently cause errors when initializing the client. |
| 325 | +
|
| 326 | + This is especially relevant when executed in CI where user and |
| 327 | + CI-provided values are both available. |
| 328 | + """ |
| 329 | + private_token = options.private_token or config.private_token |
| 330 | + oauth_token = options.oauth_token or config.oauth_token |
| 331 | + job_token = options.job_token or config.job_token or os.getenv("CI_JOB_TOKEN") |
| 332 | + |
| 333 | + if private_token: |
| 334 | + return (private_token, None, None) |
| 335 | + if oauth_token: |
| 336 | + return (None, oauth_token, None) |
| 337 | + if job_token: |
| 338 | + return (None, None, job_token) |
| 339 | + |
| 340 | + return (None, None, None) |
| 341 | + |
259 | 342 | def auth(self) -> None:
|
260 | 343 | """Performs an authentication using private token.
|
261 | 344 |
|
|
0 commit comments