|
16 | 16 | # along with this program. If not, see <http://www.gnu.org/licenses/>.
|
17 | 17 | """Wrapper for the GitLab API."""
|
18 | 18 |
|
| 19 | +importos |
19 | 20 | importtime
|
| 21 | +fromargparseimportNamespace |
20 | 22 | fromtypingimportAny,cast,Dict,List,Optional,Tuple,TYPE_CHECKING,Union
|
21 | 23 |
|
22 | 24 | importrequests
|
@@ -256,6 +258,87 @@ def from_config(
|
256 | 258 | retry_transient_errors=config.retry_transient_errors,
|
257 | 259 | )
|
258 | 260 |
|
| 261 | +@classmethod |
| 262 | +defmerge_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 | +orconfig.url |
| 297 | +oros.getenv("CI_SERVER_URL") |
| 298 | +orgitlab.const.DEFAULT_URL |
| 299 | + ) |
| 300 | +private_token,oauth_token,job_token=cls._get_auth_from_env(options,config) |
| 301 | + |
| 302 | +returncls( |
| 303 | +url=url, |
| 304 | +private_token=private_token, |
| 305 | +oauth_token=oauth_token, |
| 306 | +job_token=job_token, |
| 307 | +ssl_verify=options.ssl_verifyorconfig.ssl_verify, |
| 308 | +timeout=options.timeoutorconfig.timeout, |
| 309 | +api_version=options.api_versionorconfig.api_version, |
| 310 | +per_page=options.per_pageorconfig.per_page, |
| 311 | +pagination=options.paginationorconfig.pagination, |
| 312 | +order_by=options.order_byorconfig.order_by, |
| 313 | +user_agent=options.user_agentorconfig.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_tokenorconfig.private_token |
| 330 | +oauth_token=options.oauth_tokenorconfig.oauth_token |
| 331 | +job_token=options.job_tokenorconfig.job_tokenoros.getenv("CI_JOB_TOKEN") |
| 332 | + |
| 333 | +ifprivate_token: |
| 334 | +return (private_token,None,None) |
| 335 | +ifoauth_token: |
| 336 | +return (None,oauth_token,None) |
| 337 | +ifjob_token: |
| 338 | +return (None,None,job_token) |
| 339 | + |
| 340 | +return (None,None,None) |
| 341 | + |
259 | 342 | defauth(self)->None:
|
260 | 343 | """Performs an authentication using private token.
|
261 | 344 |
|
|