Skip to content

Commit 64ae61e

Browse files
nejchmax-wittig
authored andcommitted
feat(snippets): add support for listing all instance snippets
1 parent f13968b commit 64ae61e

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

docs/gl_objects/snippets.rst

+10-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,16 @@ List snippets owned by the current user::
2222

2323
List the public snippets::
2424

25-
public_snippets = gl.snippets.public()
25+
public_snippets = gl.snippets.list_public()
26+
27+
List all snippets::
28+
29+
all_snippets = gl.snippets.list_all()
30+
31+
.. warning::
32+
33+
Only users with the Administrator or Auditor access levels can see all snippets
34+
(both personal and project). See the upstream API documentation for more details.
2635

2736
Get a snippet::
2837

gitlab/v4/objects/snippets.py

+53-3
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,35 @@ class SnippetManager(CRUDMixin, RESTManager):
9090
)
9191

9292
@cli.register_custom_action(cls_names="SnippetManager")
93-
def public(self, **kwargs: Any) -> Union[RESTObjectList, List[RESTObject]]:
94-
"""List all the public snippets.
93+
def list_public(self, **kwargs: Any) -> Union[RESTObjectList, List[RESTObject]]:
94+
"""List all public snippets.
95+
96+
Args:
97+
get_all: If True, return all the items, without pagination
98+
per_page: Number of items to retrieve per request
99+
page: ID of the page to return (starts with page 1)
100+
iterator: If set to True and no pagination option is
101+
defined, return a generator instead of a list
102+
**kwargs: Extra options to send to the server (e.g. sudo)
103+
104+
Raises:
105+
GitlabListError: If the list could not be retrieved
106+
107+
Returns:
108+
The list of snippets, or a generator if `iterator` is True
109+
"""
110+
return self.list(path="/snippets/public", **kwargs)
111+
112+
@cli.register_custom_action(cls_names="SnippetManager")
113+
def list_all(self, **kwargs: Any) -> Union[RESTObjectList, List[RESTObject]]:
114+
"""List all snippets.
95115
96116
Args:
97-
all: If True the returned object will be a list
117+
get_all: If True, return all the items, without pagination
118+
per_page: Number of items to retrieve per request
119+
page: ID of the page to return (starts with page 1)
120+
iterator: If set to True and no pagination option is
121+
defined, return a generator instead of a list
98122
**kwargs: Extra options to send to the server (e.g. sudo)
99123
100124
Raises:
@@ -103,6 +127,32 @@ def public(self, **kwargs: Any) -> Union[RESTObjectList, List[RESTObject]]:
103127
Returns:
104128
A generator for the snippets list
105129
"""
130+
return self.list(path="/snippets/all", **kwargs)
131+
132+
def public(self, **kwargs: Any) -> Union[RESTObjectList, List[RESTObject]]:
133+
"""List all public snippets.
134+
135+
Args:
136+
get_all: If True, return all the items, without pagination
137+
per_page: Number of items to retrieve per request
138+
page: ID of the page to return (starts with page 1)
139+
iterator: If set to True and no pagination option is
140+
defined, return a generator instead of a list
141+
**kwargs: Extra options to send to the server (e.g. sudo)
142+
143+
Raises:
144+
GitlabListError: If the list could not be retrieved
145+
146+
Returns:
147+
The list of snippets, or a generator if `iterator` is True
148+
"""
149+
utils.warn(
150+
message=(
151+
"Gitlab.snippets.public() is deprecated and will be removed in a"
152+
"future major version. Use Gitlab.snippets.list_public() instead."
153+
),
154+
category=DeprecationWarning,
155+
)
106156
return self.list(path="/snippets/public", **kwargs)
107157

108158
def get(self, id: Union[str, int], lazy: bool = False, **kwargs: Any) -> Snippet:

tests/functional/api/test_snippets.py

+7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ def test_snippets(gl):
2323
content = snippet.content()
2424
assert content.decode() == "import gitlab"
2525

26+
all_snippets = gl.snippets.list_all(get_all=True)
27+
public_snippets = gl.snippets.public(get_all=True)
28+
list_public_snippets = gl.snippets.list_public(get_all=True)
29+
assert isinstance(all_snippets, list)
30+
assert isinstance(list_public_snippets, list)
31+
assert public_snippets == list_public_snippets
32+
2633
snippet.delete()
2734

2835

0 commit comments

Comments
 (0)