Skip to content

Commit 1ed154c

Browse files
committed
chore(api): move repository endpoints into separate module
1 parent 5bc158d commit 1ed154c

File tree

2 files changed

+208
-197
lines changed

2 files changed

+208
-197
lines changed

gitlab/v4/objects/projects.py

+2-197
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
)
4848
from .push_rules import ProjectPushRulesManager
4949
from .releases import ProjectReleaseManager
50+
from .repositories import RepositoryMixin
5051
from .runners import ProjectRunnerManager
5152
from .services import ProjectServiceManager
5253
from .snippets import ProjectSnippetManager
@@ -100,7 +101,7 @@ class GroupProjectManager(ListMixin, RESTManager):
100101
)
101102

102103

103-
class Project(RefreshMixin, SaveMixin, ObjectDeleteMixin, RESTObject):
104+
class Project(RefreshMixin, SaveMixin, ObjectDeleteMixin, RepositoryMixin, RESTObject):
104105
_short_print_attr = "path"
105106
_managers = (
106107
("access_tokens", "ProjectAccessTokenManager"),
@@ -154,187 +155,6 @@ class Project(RefreshMixin, SaveMixin, ObjectDeleteMixin, RESTObject):
154155
("deploytokens", "ProjectDeployTokenManager"),
155156
)
156157

157-
@cli.register_custom_action("Project", ("submodule", "branch", "commit_sha"))
158-
@exc.on_http_error(exc.GitlabUpdateError)
159-
def update_submodule(self, submodule, branch, commit_sha, **kwargs):
160-
"""Update a project submodule
161-
162-
Args:
163-
submodule (str): Full path to the submodule
164-
branch (str): Name of the branch to commit into
165-
commit_sha (str): Full commit SHA to update the submodule to
166-
commit_message (str): Commit message. If no message is provided, a default one will be set (optional)
167-
168-
Raises:
169-
GitlabAuthenticationError: If authentication is not correct
170-
GitlabPutError: If the submodule could not be updated
171-
"""
172-
173-
submodule = submodule.replace("/", "%2F") # .replace('.', '%2E')
174-
path = "/projects/%s/repository/submodules/%s" % (self.get_id(), submodule)
175-
data = {"branch": branch, "commit_sha": commit_sha}
176-
if "commit_message" in kwargs:
177-
data["commit_message"] = kwargs["commit_message"]
178-
return self.manager.gitlab.http_put(path, post_data=data)
179-
180-
@cli.register_custom_action("Project", tuple(), ("path", "ref", "recursive"))
181-
@exc.on_http_error(exc.GitlabGetError)
182-
def repository_tree(self, path="", ref="", recursive=False, **kwargs):
183-
"""Return a list of files in the repository.
184-
185-
Args:
186-
path (str): Path of the top folder (/ by default)
187-
ref (str): Reference to a commit or branch
188-
recursive (bool): Whether to get the tree recursively
189-
all (bool): If True, return all the items, without pagination
190-
per_page (int): Number of items to retrieve per request
191-
page (int): ID of the page to return (starts with page 1)
192-
as_list (bool): If set to False and no pagination option is
193-
defined, return a generator instead of a list
194-
**kwargs: Extra options to send to the server (e.g. sudo)
195-
196-
Raises:
197-
GitlabAuthenticationError: If authentication is not correct
198-
GitlabGetError: If the server failed to perform the request
199-
200-
Returns:
201-
list: The representation of the tree
202-
"""
203-
gl_path = "/projects/%s/repository/tree" % self.get_id()
204-
query_data = {"recursive": recursive}
205-
if path:
206-
query_data["path"] = path
207-
if ref:
208-
query_data["ref"] = ref
209-
return self.manager.gitlab.http_list(gl_path, query_data=query_data, **kwargs)
210-
211-
@cli.register_custom_action("Project", ("sha",))
212-
@exc.on_http_error(exc.GitlabGetError)
213-
def repository_blob(self, sha, **kwargs):
214-
"""Return a file by blob SHA.
215-
216-
Args:
217-
sha(str): ID of the blob
218-
**kwargs: Extra options to send to the server (e.g. sudo)
219-
220-
Raises:
221-
GitlabAuthenticationError: If authentication is not correct
222-
GitlabGetError: If the server failed to perform the request
223-
224-
Returns:
225-
dict: The blob content and metadata
226-
"""
227-
228-
path = "/projects/%s/repository/blobs/%s" % (self.get_id(), sha)
229-
return self.manager.gitlab.http_get(path, **kwargs)
230-
231-
@cli.register_custom_action("Project", ("sha",))
232-
@exc.on_http_error(exc.GitlabGetError)
233-
def repository_raw_blob(
234-
self, sha, streamed=False, action=None, chunk_size=1024, **kwargs
235-
):
236-
"""Return the raw file contents for a blob.
237-
238-
Args:
239-
sha(str): ID of the blob
240-
streamed (bool): If True the data will be processed by chunks of
241-
`chunk_size` and each chunk is passed to `action` for
242-
treatment
243-
action (callable): Callable responsible of dealing with chunk of
244-
data
245-
chunk_size (int): Size of each chunk
246-
**kwargs: Extra options to send to the server (e.g. sudo)
247-
248-
Raises:
249-
GitlabAuthenticationError: If authentication is not correct
250-
GitlabGetError: If the server failed to perform the request
251-
252-
Returns:
253-
str: The blob content if streamed is False, None otherwise
254-
"""
255-
path = "/projects/%s/repository/blobs/%s/raw" % (self.get_id(), sha)
256-
result = self.manager.gitlab.http_get(
257-
path, streamed=streamed, raw=True, **kwargs
258-
)
259-
return utils.response_content(result, streamed, action, chunk_size)
260-
261-
@cli.register_custom_action("Project", ("from_", "to"))
262-
@exc.on_http_error(exc.GitlabGetError)
263-
def repository_compare(self, from_, to, **kwargs):
264-
"""Return a diff between two branches/commits.
265-
266-
Args:
267-
from_(str): Source branch/SHA
268-
to(str): Destination branch/SHA
269-
**kwargs: Extra options to send to the server (e.g. sudo)
270-
271-
Raises:
272-
GitlabAuthenticationError: If authentication is not correct
273-
GitlabGetError: If the server failed to perform the request
274-
275-
Returns:
276-
str: The diff
277-
"""
278-
path = "/projects/%s/repository/compare" % self.get_id()
279-
query_data = {"from": from_, "to": to}
280-
return self.manager.gitlab.http_get(path, query_data=query_data, **kwargs)
281-
282-
@cli.register_custom_action("Project")
283-
@exc.on_http_error(exc.GitlabGetError)
284-
def repository_contributors(self, **kwargs):
285-
"""Return a list of contributors for the project.
286-
287-
Args:
288-
all (bool): If True, return all the items, without pagination
289-
per_page (int): Number of items to retrieve per request
290-
page (int): ID of the page to return (starts with page 1)
291-
as_list (bool): If set to False and no pagination option is
292-
defined, return a generator instead of a list
293-
**kwargs: Extra options to send to the server (e.g. sudo)
294-
295-
Raises:
296-
GitlabAuthenticationError: If authentication is not correct
297-
GitlabGetError: If the server failed to perform the request
298-
299-
Returns:
300-
list: The contributors
301-
"""
302-
path = "/projects/%s/repository/contributors" % self.get_id()
303-
return self.manager.gitlab.http_list(path, **kwargs)
304-
305-
@cli.register_custom_action("Project", tuple(), ("sha",))
306-
@exc.on_http_error(exc.GitlabListError)
307-
def repository_archive(
308-
self, sha=None, streamed=False, action=None, chunk_size=1024, **kwargs
309-
):
310-
"""Return a tarball of the repository.
311-
312-
Args:
313-
sha (str): ID of the commit (default branch by default)
314-
streamed (bool): If True the data will be processed by chunks of
315-
`chunk_size` and each chunk is passed to `action` for
316-
treatment
317-
action (callable): Callable responsible of dealing with chunk of
318-
data
319-
chunk_size (int): Size of each chunk
320-
**kwargs: Extra options to send to the server (e.g. sudo)
321-
322-
Raises:
323-
GitlabAuthenticationError: If authentication is not correct
324-
GitlabListError: If the server failed to perform the request
325-
326-
Returns:
327-
str: The binary data of the archive
328-
"""
329-
path = "/projects/%s/repository/archive" % self.get_id()
330-
query_data = {}
331-
if sha:
332-
query_data["sha"] = sha
333-
result = self.manager.gitlab.http_get(
334-
path, query_data=query_data, raw=True, streamed=streamed, **kwargs
335-
)
336-
return utils.response_content(result, streamed, action, chunk_size)
337-
338158
@cli.register_custom_action("Project", ("forked_from_id",))
339159
@exc.on_http_error(exc.GitlabCreateError)
340160
def create_fork_relation(self, forked_from_id, **kwargs):
@@ -366,21 +186,6 @@ def delete_fork_relation(self, **kwargs):
366186
path = "/projects/%s/fork" % self.get_id()
367187
self.manager.gitlab.http_delete(path, **kwargs)
368188

369-
@cli.register_custom_action("Project")
370-
@exc.on_http_error(exc.GitlabDeleteError)
371-
def delete_merged_branches(self, **kwargs):
372-
"""Delete merged branches.
373-
374-
Args:
375-
**kwargs: Extra options to send to the server (e.g. sudo)
376-
377-
Raises:
378-
GitlabAuthenticationError: If authentication is not correct
379-
GitlabDeleteError: If the server failed to perform the request
380-
"""
381-
path = "/projects/%s/repository/merged_branches" % self.get_id()
382-
self.manager.gitlab.http_delete(path, **kwargs)
383-
384189
@cli.register_custom_action("Project")
385190
@exc.on_http_error(exc.GitlabGetError)
386191
def languages(self, **kwargs):

0 commit comments

Comments
 (0)