From 912f62285e58bd755d4ac31ce1e8182662c6e361 Mon Sep 17 00:00:00 2001 From: "Kay-Uwe (Kiwi) Lorenz" Date: Sat, 6 Mar 2021 11:44:45 +0100 Subject: [PATCH 01/11] fix: make scopes work with multiple scope-names --- gitlab/types.py | 16 ++++++++++++++++ gitlab/v4/objects/__init__.py | 6 +++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/gitlab/types.py b/gitlab/types.py index e07d078e1..06482b2a0 100644 --- a/gitlab/types.py +++ b/gitlab/types.py @@ -45,6 +45,22 @@ def get_for_api(self): return ",".join(self._value) +class ScopesListAttribute(ListAttribute): + def set_from_cli(self, cli_value): + if not cli_value.strip(): + self._value = [] + else: + self._value = [item.strip() for item in cli_value.split(",")] + + + def get_for_api(self): + # Do not comma-split single value passed as string + if isinstance(self._value, str): + return [self._value] + + return self._value + + class LowercaseStringAttribute(GitlabAttribute): def get_for_api(self): return str(self._value).lower() diff --git a/gitlab/v4/objects/__init__.py b/gitlab/v4/objects/__init__.py index edeff044e..7cfd41e0b 100644 --- a/gitlab/v4/objects/__init__.py +++ b/gitlab/v4/objects/__init__.py @@ -229,7 +229,7 @@ class UserIdentityProviderManager(DeleteMixin, RESTManager): class UserImpersonationToken(ObjectDeleteMixin, RESTObject): - pass + _short_print_attr = "token" class UserImpersonationTokenManager(NoUpdateMixin, RESTManager): @@ -238,6 +238,7 @@ class UserImpersonationTokenManager(NoUpdateMixin, RESTManager): _from_parent_attrs = {"user_id": "id"} _create_attrs = (("name", "scopes"), ("expires_at",)) _list_filters = ("state",) + _types = {"scopes": types.ScopesListAttribute} class UserMembership(RESTObject): @@ -734,6 +735,7 @@ class ProjectDeployTokenManager(ListMixin, CreateMixin, DeleteMixin, RESTManager "username", ), ) + _types = {"scopes": types.ScopesListAttribute} class GroupDeployToken(ObjectDeleteMixin, RESTObject): @@ -754,6 +756,7 @@ class GroupDeployTokenManager(ListMixin, CreateMixin, DeleteMixin, RESTManager): "username", ), ) + _types = {"scopes": types.ScopesListAttribute} class NotificationSettings(SaveMixin, RESTObject): @@ -5786,3 +5789,4 @@ class ApplicationManager(ListMixin, CreateMixin, DeleteMixin, RESTManager): _path = "/applications" _obj_cls = Application _create_attrs = (("name", "redirect_uri", "scopes"), ("confidential",)) + _types = {"scopes": types.ScopesListAttribute} From 02012ef400f1680c0dc0d44288226434ac0f6ef0 Mon Sep 17 00:00:00 2001 From: "Kay-Uwe (Kiwi) Lorenz" Date: Sat, 6 Mar 2021 11:53:57 +0100 Subject: [PATCH 02/11] chore: make lint happy --- gitlab/types.py | 1 - 1 file changed, 1 deletion(-) diff --git a/gitlab/types.py b/gitlab/types.py index 06482b2a0..225cef5ac 100644 --- a/gitlab/types.py +++ b/gitlab/types.py @@ -52,7 +52,6 @@ def set_from_cli(self, cli_value): else: self._value = [item.strip() for item in cli_value.split(",")] - def get_for_api(self): # Do not comma-split single value passed as string if isinstance(self._value, str): From 85757d80daee687b29432f3dbe51d43923aea757 Mon Sep 17 00:00:00 2001 From: "Kay-Uwe (Kiwi) Lorenz" Date: Sun, 7 Mar 2021 13:10:44 +0100 Subject: [PATCH 03/11] fix: not re-implement inherited functions in ScopesListAttribute, add _types to objects --- gitlab/types.py | 7 +------ gitlab/v4/objects/applications.py | 3 +++ gitlab/v4/objects/deploy_tokens.py | 4 +++- gitlab/v4/objects/users.py | 2 ++ 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/gitlab/types.py b/gitlab/types.py index 225cef5ac..54dc503a4 100644 --- a/gitlab/types.py +++ b/gitlab/types.py @@ -46,14 +46,9 @@ def get_for_api(self): class ScopesListAttribute(ListAttribute): - def set_from_cli(self, cli_value): - if not cli_value.strip(): - self._value = [] - else: - self._value = [item.strip() for item in cli_value.split(",")] def get_for_api(self): - # Do not comma-split single value passed as string + # scopes are expected to be a list of strings if isinstance(self._value, str): return [self._value] diff --git a/gitlab/v4/objects/applications.py b/gitlab/v4/objects/applications.py index ddb9d234d..2b56168e0 100644 --- a/gitlab/v4/objects/applications.py +++ b/gitlab/v4/objects/applications.py @@ -1,5 +1,6 @@ from gitlab.base import RESTManager, RESTObject from gitlab.mixins import CreateMixin, DeleteMixin, ListMixin, ObjectDeleteMixin +from gitlab.types import ScopesListAttribute __all__ = [ "Application", @@ -16,3 +17,5 @@ class ApplicationManager(ListMixin, CreateMixin, DeleteMixin, RESTManager): _path = "/applications" _obj_cls = Application _create_attrs = (("name", "redirect_uri", "scopes"), ("confidential",)) + + _types = {"scopes": ScopesListAttribute} diff --git a/gitlab/v4/objects/deploy_tokens.py b/gitlab/v4/objects/deploy_tokens.py index b9d0bad7d..e45085117 100644 --- a/gitlab/v4/objects/deploy_tokens.py +++ b/gitlab/v4/objects/deploy_tokens.py @@ -1,6 +1,6 @@ from gitlab.base import RESTManager, RESTObject from gitlab.mixins import CreateMixin, DeleteMixin, ListMixin, ObjectDeleteMixin - +from gitlab.types import ScopesListAttribute __all__ = [ "DeployToken", @@ -39,6 +39,7 @@ class GroupDeployTokenManager(ListMixin, CreateMixin, DeleteMixin, RESTManager): "username", ), ) + _types = {"scopes": ScopesListAttribute} class ProjectDeployToken(ObjectDeleteMixin, RESTObject): @@ -59,3 +60,4 @@ class ProjectDeployTokenManager(ListMixin, CreateMixin, DeleteMixin, RESTManager "username", ), ) + _types = {"scopes": ScopesListAttribute} diff --git a/gitlab/v4/objects/users.py b/gitlab/v4/objects/users.py index 4f14e86b3..317a6459d 100644 --- a/gitlab/v4/objects/users.py +++ b/gitlab/v4/objects/users.py @@ -13,6 +13,7 @@ SaveMixin, UpdateMixin, ) +from gitlab.types import ScopesListAttribute from .custom_attributes import UserCustomAttributeManager from .events import UserEventManager @@ -406,6 +407,7 @@ class UserImpersonationTokenManager(NoUpdateMixin, RESTManager): _from_parent_attrs = {"user_id": "id"} _create_attrs = (("name", "scopes"), ("expires_at",)) _list_filters = ("state",) + _types = {"scopes": ScopesListAttribute} class UserMembership(RESTObject): From f14f91b8e4f13d354342370aee1c843814183410 Mon Sep 17 00:00:00 2001 From: "Kay-Uwe (Kiwi) Lorenz" Date: Sun, 7 Mar 2021 13:30:17 +0100 Subject: [PATCH 04/11] chore: add test for token scopes --- tools/functional/cli/test_cli_v4.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tools/functional/cli/test_cli_v4.py b/tools/functional/cli/test_cli_v4.py index a63c1b1b5..a0c66633f 100644 --- a/tools/functional/cli/test_cli_v4.py +++ b/tools/functional/cli/test_cli_v4.py @@ -560,12 +560,10 @@ def test_create_project_with_values_from_file(gitlab_cli, tmpdir): assert ret.success assert description in ret.stdout - -def test_create_project_deploy_token(gitlab_cli, project): +def do_test_create_project_deploy_token(gitlab_cli, project, scopes): name = "project-token" username = "root" expires_at = "2021-09-09" - scopes = "read_registry" cmd = [ "-v", @@ -590,6 +588,11 @@ def test_create_project_deploy_token(gitlab_cli, project): assert expires_at in ret.stdout assert scopes in ret.stdout +def test_create_project_deploy_token_one_scope(gitlab_cli, project, scopes): + do_test_create_project_deploy_token(gitlab_cli, project, scopes="read_registry") + +def test_create_project_deploy_token_many_scopes(gitlab_cli, project, scopes): + do_test_create_project_deploy_token(gitlab_cli, project, scopes="read_registry,read_repository") def test_list_all_deploy_tokens(gitlab_cli, deploy_token): cmd = ["-v", "deploy-token", "list"] From 9350067333d3ea3a95fd499c153ea57e8f5711b0 Mon Sep 17 00:00:00 2001 From: "Kay-Uwe (Kiwi) Lorenz" Date: Sun, 7 Mar 2021 13:32:53 +0100 Subject: [PATCH 05/11] chore: make lint happy --- tools/functional/cli/test_cli_v4.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tools/functional/cli/test_cli_v4.py b/tools/functional/cli/test_cli_v4.py index a0c66633f..773de4f39 100644 --- a/tools/functional/cli/test_cli_v4.py +++ b/tools/functional/cli/test_cli_v4.py @@ -588,11 +588,16 @@ def do_test_create_project_deploy_token(gitlab_cli, project, scopes): assert expires_at in ret.stdout assert scopes in ret.stdout + def test_create_project_deploy_token_one_scope(gitlab_cli, project, scopes): do_test_create_project_deploy_token(gitlab_cli, project, scopes="read_registry") + def test_create_project_deploy_token_many_scopes(gitlab_cli, project, scopes): - do_test_create_project_deploy_token(gitlab_cli, project, scopes="read_registry,read_repository") + do_test_create_project_deploy_token( + gitlab_cli, project, scopes="read_registry,read_repository" + ) + def test_list_all_deploy_tokens(gitlab_cli, deploy_token): cmd = ["-v", "deploy-token", "list"] From d1de30b88673349221e2b943cdf0f82ab57866fd Mon Sep 17 00:00:00 2001 From: "Kay-Uwe (Kiwi) Lorenz" Date: Sun, 7 Mar 2021 13:34:38 +0100 Subject: [PATCH 06/11] chore: make lint happy --- tools/functional/cli/test_cli_v4.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/functional/cli/test_cli_v4.py b/tools/functional/cli/test_cli_v4.py index 773de4f39..cb8d9f259 100644 --- a/tools/functional/cli/test_cli_v4.py +++ b/tools/functional/cli/test_cli_v4.py @@ -560,6 +560,7 @@ def test_create_project_with_values_from_file(gitlab_cli, tmpdir): assert ret.success assert description in ret.stdout + def do_test_create_project_deploy_token(gitlab_cli, project, scopes): name = "project-token" username = "root" From 7443cd8d4a64650b69db883ff3a6ad858227926c Mon Sep 17 00:00:00 2001 From: "Kay-Uwe (Kiwi) Lorenz" Date: Sun, 7 Mar 2021 14:16:30 +0100 Subject: [PATCH 07/11] chore: make lint happy --- gitlab/types.py | 1 - 1 file changed, 1 deletion(-) diff --git a/gitlab/types.py b/gitlab/types.py index 54dc503a4..cb1740ab7 100644 --- a/gitlab/types.py +++ b/gitlab/types.py @@ -46,7 +46,6 @@ def get_for_api(self): class ScopesListAttribute(ListAttribute): - def get_for_api(self): # scopes are expected to be a list of strings if isinstance(self._value, str): From fe6aef3a5de96181b7d0809795a517586c633415 Mon Sep 17 00:00:00 2001 From: "Kay-Uwe (Kiwi) Lorenz" Date: Sun, 7 Mar 2021 14:30:54 +0100 Subject: [PATCH 08/11] fix: token scopes tests --- tools/functional/cli/test_cli_v4.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/functional/cli/test_cli_v4.py b/tools/functional/cli/test_cli_v4.py index cb8d9f259..3e301cb03 100644 --- a/tools/functional/cli/test_cli_v4.py +++ b/tools/functional/cli/test_cli_v4.py @@ -590,11 +590,11 @@ def do_test_create_project_deploy_token(gitlab_cli, project, scopes): assert scopes in ret.stdout -def test_create_project_deploy_token_one_scope(gitlab_cli, project, scopes): +def test_create_project_deploy_token_one_scope(gitlab_cli, project): do_test_create_project_deploy_token(gitlab_cli, project, scopes="read_registry") -def test_create_project_deploy_token_many_scopes(gitlab_cli, project, scopes): +def test_create_project_deploy_token_many_scopes(gitlab_cli, project): do_test_create_project_deploy_token( gitlab_cli, project, scopes="read_registry,read_repository" ) From 9c49cc807578430cb8d9f30f130a260d3e8137c0 Mon Sep 17 00:00:00 2001 From: "Kay-Uwe (Kiwi) Lorenz" Date: Sun, 7 Mar 2021 15:46:09 +0100 Subject: [PATCH 09/11] fix: token scopes tests --- tools/functional/cli/test_cli_v4.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tools/functional/cli/test_cli_v4.py b/tools/functional/cli/test_cli_v4.py index 3e301cb03..c599730b6 100644 --- a/tools/functional/cli/test_cli_v4.py +++ b/tools/functional/cli/test_cli_v4.py @@ -561,7 +561,7 @@ def test_create_project_with_values_from_file(gitlab_cli, tmpdir): assert description in ret.stdout -def do_test_create_project_deploy_token(gitlab_cli, project, scopes): +def do_test_create_project_deploy_token(gitlab_cli, project, scopes, expected_scopes): name = "project-token" username = "root" expires_at = "2021-09-09" @@ -591,12 +591,18 @@ def do_test_create_project_deploy_token(gitlab_cli, project, scopes): def test_create_project_deploy_token_one_scope(gitlab_cli, project): - do_test_create_project_deploy_token(gitlab_cli, project, scopes="read_registry") + scopes = "read_registry" + expected_scopes = "['read_registry']" + do_test_create_project_deploy_token( + gitlab_cli, project, scopes, expected_scopes + ) def test_create_project_deploy_token_many_scopes(gitlab_cli, project): + scopes = "read_registry,read_repository" + expected_scopes = "['read_repository', 'read_registry']" do_test_create_project_deploy_token( - gitlab_cli, project, scopes="read_registry,read_repository" + gitlab_cli, project, scopes, expected_scopes ) From 0d1dca551ff2a6f9c09904e96d8822c6442b291b Mon Sep 17 00:00:00 2001 From: "Kay-Uwe (Kiwi) Lorenz" Date: Sun, 7 Mar 2021 16:00:08 +0100 Subject: [PATCH 10/11] chore: make lint happy --- tools/functional/cli/test_cli_v4.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/tools/functional/cli/test_cli_v4.py b/tools/functional/cli/test_cli_v4.py index c599730b6..147f2013d 100644 --- a/tools/functional/cli/test_cli_v4.py +++ b/tools/functional/cli/test_cli_v4.py @@ -593,17 +593,13 @@ def do_test_create_project_deploy_token(gitlab_cli, project, scopes, expected_sc def test_create_project_deploy_token_one_scope(gitlab_cli, project): scopes = "read_registry" expected_scopes = "['read_registry']" - do_test_create_project_deploy_token( - gitlab_cli, project, scopes, expected_scopes - ) + do_test_create_project_deploy_token(gitlab_cli, project, scopes, expected_scopes) def test_create_project_deploy_token_many_scopes(gitlab_cli, project): scopes = "read_registry,read_repository" expected_scopes = "['read_repository', 'read_registry']" - do_test_create_project_deploy_token( - gitlab_cli, project, scopes, expected_scopes - ) + do_test_create_project_deploy_token(gitlab_cli, project, scopes, expected_scopes) def test_list_all_deploy_tokens(gitlab_cli, deploy_token): From 3826ea8ce0b31ff7f97e48b9a8618b5788ce9b51 Mon Sep 17 00:00:00 2001 From: "Kay-Uwe (Kiwi) Lorenz" Date: Sun, 7 Mar 2021 16:35:06 +0100 Subject: [PATCH 11/11] fix: expected scopes in deploy_token test --- tools/functional/cli/test_cli_v4.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/functional/cli/test_cli_v4.py b/tools/functional/cli/test_cli_v4.py index 147f2013d..4cba64823 100644 --- a/tools/functional/cli/test_cli_v4.py +++ b/tools/functional/cli/test_cli_v4.py @@ -587,7 +587,7 @@ def do_test_create_project_deploy_token(gitlab_cli, project, scopes, expected_sc assert name in ret.stdout assert username in ret.stdout assert expires_at in ret.stdout - assert scopes in ret.stdout + assert expected_scopes in ret.stdout def test_create_project_deploy_token_one_scope(gitlab_cli, project):