Commit 001e5967 authored by Nejc Habjan's avatar Nejc Habjan Committed by Nejc Habjan
Browse files

feat(cli): allow skipping initial auth calls

parent 6d4bffb5
Loading
Loading
Loading
Loading
+13 −1
Original line number Diff line number Diff line
@@ -282,6 +282,16 @@ def _get_base_parser(add_help: bool = True) -> argparse.ArgumentParser:
        help=("GitLab CI job token [env var: CI_JOB_TOKEN]"),
        required=False,
    )
    parser.add_argument(
        "--skip-login",
        help=(
            "Skip initial authenticated API call to the current user endpoint. "
            "This may  be useful when invoking the CLI in scripts. "
            "[env var: GITLAB_SKIP_LOGIN]"
        ),
        action="store_true",
        default=os.getenv("GITLAB_SKIP_LOGIN"),
    )
    return parser


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

    args_dict = vars(args)
    # Remove CLI behavior-related args
@@ -390,6 +401,7 @@ def main() -> None:
        "private_token",
        "oauth_token",
        "job_token",
        "skip_login",
    ):
        args_dict.pop(item)
    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:
        gl = gitlab.Gitlab.merge_config(vars(options), gitlab_id, config_files)
        if debug:
            gl.enable_debug()
        if gl.private_token or gl.oauth_token:
        if not skip_login and (gl.private_token or gl.oauth_token):
            gl.auth()
    except Exception as e:
        die(str(e))
+11 −0
Original line number Diff line number Diff line
@@ -35,6 +35,17 @@ def resp_get_project():
    }


@pytest.fixture
def resp_current_user():
    return {
        "method": responses.GET,
        "url": f"{DEFAULT_URL}/api/v4/user",
        "json": {"username": "name", "id": 1},
        "content_type": "application/json",
        "status": 200,
    }


@pytest.fixture
def resp_delete_registry_tags_in_bulk():
    return {
+16 −0
Original line number Diff line number Diff line
@@ -90,6 +90,22 @@ def test_uses_ci_job_token(monkeypatch, script_runner, resp_get_project):
    assert ret.success


@pytest.mark.script_launch_mode("inprocess")
@responses.activate
def test_does_not_auth_on_skip_login(
    monkeypatch, script_runner, resp_get_project, resp_current_user
):
    monkeypatch.setenv("GITLAB_PRIVATE_TOKEN", PRIVATE_TOKEN)
    monkeypatch.setattr(config, "_DEFAULT_FILES", [])

    resp_user = responses.add(**resp_current_user)
    resp_project = responses.add(**resp_get_project)
    ret = script_runner.run(["gitlab", "--skip-login", "project", "get", "--id", "1"])
    assert ret.success
    assert resp_user.call_count == 0
    assert resp_project.call_count == 1


@pytest.mark.script_launch_mode("inprocess")
@responses.activate
def test_private_token_overrides_job_token(