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