Skip to content

Commit fb3f28a

Browse files
chore: rename whaction and action to resource_action in CLI
Rename the variables `whaction` and `action` to `resource_action` to improve code-readability.
1 parent 6cdccd9 commit fb3f28a

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

gitlab/cli.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,8 @@ def main() -> None:
323323
if args.fields:
324324
fields = [x.strip() for x in args.fields.split(",")]
325325
debug = args.debug
326-
action = args.whaction
327326
gitlab_resource = args.gitlab_resource
327+
resource_action = args.resource_action
328328

329329
args_dict = vars(args)
330330
# Remove CLI behavior-related args
@@ -334,7 +334,7 @@ def main() -> None:
334334
"verbose",
335335
"debug",
336336
"gitlab_resource",
337-
"whaction",
337+
"resource_action",
338338
"version",
339339
"output",
340340
"fields",
@@ -361,4 +361,6 @@ def main() -> None:
361361
if debug:
362362
gl.enable_debug()
363363

364-
gitlab.v4.cli.run(gl, gitlab_resource, action, args_dict, verbose, output, fields)
364+
gitlab.v4.cli.run(
365+
gl, gitlab_resource, resource_action, args_dict, verbose, output, fields
366+
)

gitlab/v4/cli.py

+21-10
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,18 @@
2929

3030
class GitlabCLI:
3131
def __init__(
32-
self, gl: gitlab.Gitlab, gitlab_resource: str, action: str, args: Dict[str, str]
32+
self,
33+
gl: gitlab.Gitlab,
34+
gitlab_resource: str,
35+
resource_action: str,
36+
args: Dict[str, str],
3337
) -> None:
3438
self.cls: Type[gitlab.base.RESTObject] = cli.gitlab_resource_to_cls(
3539
gitlab_resource, namespace=gitlab.v4.objects
3640
)
3741
self.cls_name = self.cls.__name__
3842
self.gitlab_resource = gitlab_resource.replace("-", "_")
39-
self.action = action.lower()
43+
self.resource_action = resource_action.lower()
4044
self.gl = gl
4145
self.args = args
4246
self.parent_args: Dict[str, Any] = {}
@@ -80,13 +84,13 @@ def _process_from_parent_attrs(self) -> None:
8084
del self.args[key]
8185

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

8892
# Fallback to standard actions (get, list, create, ...)
89-
method = f"do_{self.action}"
93+
method = f"do_{self.resource_action}"
9094
if hasattr(self, method):
9195
return getattr(self, method)()
9296

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

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

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

114-
method_name = self.action.replace("-", "_")
118+
method_name = self.resource_action.replace("-", "_")
115119
return getattr(class_instance, method_name)(**self.args)
116120

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

353357
object_subparsers = object_group.add_subparsers(
354-
title="action", dest="whaction", help="Action to execute."
358+
title="action",
359+
dest="resource_action",
360+
help="Action to execute on the GitLab resource.",
355361
)
356362
_populate_sub_parser_by_class(cls, object_subparsers)
357363
object_subparsers.required = True
@@ -498,13 +504,18 @@ def display_list(
498504
def run(
499505
gl: gitlab.Gitlab,
500506
gitlab_resource: str,
501-
action: str,
507+
resource_action: str,
502508
args: Dict[str, Any],
503509
verbose: bool,
504510
output: str,
505511
fields: List[str],
506512
) -> None:
507-
g_cli = GitlabCLI(gl=gl, gitlab_resource=gitlab_resource, action=action, args=args)
513+
g_cli = GitlabCLI(
514+
gl=gl,
515+
gitlab_resource=gitlab_resource,
516+
resource_action=resource_action,
517+
args=args,
518+
)
508519
data = g_cli.run()
509520

510521
printer: Union[JSONPrinter, LegacyPrinter, YAMLPrinter] = PRINTERS[output]()

tests/unit/test_cli.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def test_v4_parse_args():
126126
parser = cli._get_parser()
127127
args = parser.parse_args(["project", "list"])
128128
assert args.gitlab_resource == "project"
129-
assert args.whaction == "list"
129+
assert args.resource_action == "list"
130130

131131

132132
def test_v4_parser():

0 commit comments

Comments
 (0)