Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions gitlab/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ def main() -> None:
if args.fields:
fields = [x.strip() for x in args.fields.split(",")]
debug = args.debug
action = args.whaction
gitlab_resource = args.gitlab_resource
resource_action = args.resource_action

args_dict = vars(args)
# Remove CLI behavior-related args
Expand All @@ -334,7 +334,7 @@ def main() -> None:
"verbose",
"debug",
"gitlab_resource",
"whaction",
"resource_action",
"version",
"output",
"fields",
Expand All @@ -361,4 +361,6 @@ def main() -> None:
if debug:
gl.enable_debug()

gitlab.v4.cli.run(gl, gitlab_resource, action, args_dict, verbose, output, fields)
gitlab.v4.cli.run(
gl, gitlab_resource, resource_action, args_dict, verbose, output, fields
)
31 changes: 21 additions & 10 deletions gitlab/v4/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,18 @@

class GitlabCLI:
def __init__(
self, gl: gitlab.Gitlab, gitlab_resource: str, action: str, args: Dict[str, str]
self,
gl: gitlab.Gitlab,
gitlab_resource: str,
resource_action: str,
args: Dict[str, str],
) -> None:
self.cls: Type[gitlab.base.RESTObject] = cli.gitlab_resource_to_cls(
gitlab_resource, namespace=gitlab.v4.objects
)
self.cls_name = self.cls.__name__
self.gitlab_resource = gitlab_resource.replace("-", "_")
self.action = action.lower()
self.resource_action = resource_action.lower()
self.gl = gl
self.args = args
self.parent_args: Dict[str, Any] = {}
Expand Down Expand Up @@ -80,13 +84,13 @@ def _process_from_parent_attrs(self) -> None:
del self.args[key]

def run(self) -> Any:
# Check for a method that matches object + action
method = f"do_{self.gitlab_resource}_{self.action}"
# Check for a method that matches gitlab_resource + action
method = f"do_{self.gitlab_resource}_{self.resource_action}"
if hasattr(self, method):
return getattr(self, method)()

# Fallback to standard actions (get, list, create, ...)
method = f"do_{self.action}"
method = f"do_{self.resource_action}"
if hasattr(self, method):
return getattr(self, method)()

Expand All @@ -95,7 +99,7 @@ def run(self) -> Any:

def do_custom(self) -> Any:
class_instance: Union[gitlab.base.RESTManager, gitlab.base.RESTObject]
in_obj = cli.custom_actions[self.cls_name][self.action][2]
in_obj = cli.custom_actions[self.cls_name][self.resource_action][2]

# Get the object (lazy), then act
if in_obj:
Expand All @@ -111,7 +115,7 @@ def do_custom(self) -> Any:
else:
class_instance = self.mgr

method_name = self.action.replace("-", "_")
method_name = self.resource_action.replace("-", "_")
return getattr(class_instance, method_name)(**self.args)

def do_project_export_download(self) -> None:
Expand Down Expand Up @@ -351,7 +355,9 @@ def extend_parser(parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
object_group = subparsers.add_parser(arg_name)

object_subparsers = object_group.add_subparsers(
title="action", dest="whaction", help="Action to execute."
title="action",
dest="resource_action",
help="Action to execute on the GitLab resource.",
)
_populate_sub_parser_by_class(cls, object_subparsers)
object_subparsers.required = True
Expand Down Expand Up @@ -498,13 +504,18 @@ def display_list(
def run(
gl: gitlab.Gitlab,
gitlab_resource: str,
action: str,
resource_action: str,
args: Dict[str, Any],
verbose: bool,
output: str,
fields: List[str],
) -> None:
g_cli = GitlabCLI(gl=gl, gitlab_resource=gitlab_resource, action=action, args=args)
g_cli = GitlabCLI(
gl=gl,
gitlab_resource=gitlab_resource,
resource_action=resource_action,
args=args,
)
data = g_cli.run()

printer: Union[JSONPrinter, LegacyPrinter, YAMLPrinter] = PRINTERS[output]()
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def test_v4_parse_args():
parser = cli._get_parser()
args = parser.parse_args(["project", "list"])
assert args.gitlab_resource == "project"
assert args.whaction == "list"
assert args.resource_action == "list"


def test_v4_parser():
Expand Down