8000 feat(cli): allow skipping initial auth calls · python-gitlab/python-gitlab@001e596 · GitHub
[go: up one dir, main page]

Skip to content

Commit 001e596

Browse files
committed
feat(cli): allow skipping initial auth calls
1 parent 6d4bffb commit 001e596

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

gitlab/cli.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,16 @@ def _get_base_parser(add_help: bool = True) -> argparse.ArgumentParser:
282282
help=("GitLab CI job token [env var: CI_JOB_TOKEN]"),
283283
required=False,
284284
)
285+
parser.add_argument(
286+
"--skip-login",
287+
help=(
288+
"Skip initial authenticated API call to the current user endpoint. "
289+
"This may be useful when invoking the CLI in scripts. "
290+
"[env var: GITLAB_SKIP_LOGIN]"
291+
),
292+
action="store_true",
293+
default=os.getenv("GITLAB_SKIP_LOGIN"),
294+
)
285295
return parser
286296

287297

@@ -368,6 +378,7 @@ def main() -> None:
368378
debug = args.debug
369379
gitlab_resource = args.gitlab_resource
370380
resource_action = args.resource_action
381+
skip_login = args.skip_login
371382

372383
args_dict = vars(args)
373384
# Remove CLI behavior-related args
@@ -390,6 +401,7 @@ def main() -> None:
390401
"private_token",
391402
"oauth_token",
392403
"job_token",
404+
"skip_login",
393405
):
394406
args_dict.pop(item)
395407
args_dict = {k: _parse_value(v) for k, v in args_dict.items() if v is not None}
@@ -398,7 +410,7 @@ def main() -> None:
398410
gl = gitlab.Gitlab.merge_config(vars(options), gitlab_id, config_files)
399411
if debug:
400412
gl.enable_debug()
401-
if gl.private_token or gl.oauth_token:
413+
if not skip_login and (gl.private_token or gl.oauth_token):
402414
gl.auth()
403415
except Exception as e:
404416
die(str(e))

tests/functional/cli/conftest.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ def resp_get_project():
3535
}
3636

3737

38+
@pytest.fixture
39+
def resp_current_user():
40+
return {
41+
"method": responses.GET,
42+
"url": f"{DEFAULT_URL}/api/v4/user",
43+
"json": {"username": "name", "id": 1},
44+
"content_type": "application/json",
45+
"status": 200,
46+
}
47+
48+
3849
@pytest.fixture
3950
def resp_delete_registry_tags_in_bulk():
4051
return {

tests/functional/cli/test_cli.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,22 @@ def test_uses_ci_job_token(monkeypatch, script_runner, resp_get_project):
9090
assert ret.success
9191

9292

93+
@pytest.mark.script_launch_mode("inprocess")
94+
@responses.activate
95+
def test_does_not_auth_on_skip_login(
96+
monkeypatch, script_runner, resp_get_project, resp_current_user
97+
):
98+
monkeypatch.setenv("GITLAB_PRIVATE_TOKEN", PRIVATE_TOKEN)
99+
monkeypatch.setattr(config, "_DEFAULT_FILES", [])
100+
101+
resp_user = responses.add(**resp_current_user)
102+
resp_project = responses.add(**resp_get_project)
103+
ret = script_runner.run(["gitlab", "--skip-login", "project", "get", "--id", "1"])
104+
assert ret.success
105+
assert resp_user.call_count == 0
106+
assert resp_project.call_count == 1
107+
108+
93109
@pytest.mark.script_launch_mode("inprocess")
94110
@responses.activate
95111
def test_private_token_overrides_job_token(

0 commit comments

Comments
 (0)
0