33
44Currently this module only contains repository-related methods for projects.
55"""
6+ from typing import Any , Callable , Dict , List , Optional , TYPE_CHECKING , Union
67
8+ import requests
9+
10+ import gitlab
711from gitlab import cli
812from gitlab import exceptions as exc
913from gitlab import utils
1014
15+ if TYPE_CHECKING :
16+ # When running mypy we use these as the base classes
17+ _RestObjectBase = gitlab .base .RESTObject
18+ else :
19+ _RestObjectBase = object
20+
1121
12- class RepositoryMixin :
22+ class RepositoryMixin ( _RestObjectBase ) :
1323 @cli .register_custom_action ("Project" , ("submodule" , "branch" , "commit_sha" ))
1424 @exc .on_http_error (exc .GitlabUpdateError )
15- def update_submodule (self , submodule , branch , commit_sha , ** kwargs ):
25+ def update_submodule (
26+ self , submodule : str , branch : str , commit_sha : str , ** kwargs : Any
27+ ) -> Union [Dict [str , Any ], requests .Response ]:
1628 """Update a project submodule
1729
1830 Args:
1931 submodule (str): Full path to the submodule
2032 branch (str): Name of the branch to commit into
2133 commit_sha (str): Full commit SHA to update the submodule to
22- commit_message (str): Commit message. If no message is provided, a default one will be set (optional)
34+ commit_message (str): Commit message. If no message is provided, a
35+ default one will be set (optional)
2336
2437 Raises:
2538 GitlabAuthenticationError: If authentication is not correct
@@ -35,7 +48,9 @@ def update_submodule(self, submodule, branch, commit_sha, **kwargs):
3548
3649 @cli .register_custom_action ("Project" , tuple (), ("path" , "ref" , "recursive" ))
3750 @exc .on_http_error (exc .GitlabGetError )
38- def repository_tree (self , path = "" , ref = "" , recursive = False , ** kwargs ):
51+ def repository_tree (
52+ self , path : str = "" , ref : str = "" , recursive : bool = False , ** kwargs : Any
53+ ) -> Union [gitlab .client .GitlabList , List [Dict [str , Any ]]]:
3954 """Return a list of files in the repository.
4055
4156 Args:
@@ -57,7 +72,7 @@ def repository_tree(self, path="", ref="", recursive=False, **kwargs):
5772 list: The representation of the tree
5873 """
5974 gl_path = f"/projects/{ self .get_id ()} /repository/tree"
60- query_data = {"recursive" : recursive }
75+ query_data : Dict [ str , Any ] = {"recursive" : recursive }
6176 if path :
6277 query_data ["path" ] = path
6378 if ref :
@@ -66,7 +81,9 @@ def repository_tree(self, path="", ref="", recursive=False, **kwargs):
6681
6782 @cli .register_custom_action ("Project" , ("sha" ,))
6883 @exc .on_http_error (exc .GitlabGetError )
69- def repository_blob (self , sha , ** kwargs ):
84+ def repository_blob (
85+ self , sha : str , ** kwargs : Any
86+ ) -> Union [Dict [str , Any ], requests .Response ]:
7087 """Return a file by blob SHA.
7188
7289 Args:
@@ -87,8 +104,13 @@ def repository_blob(self, sha, **kwargs):
87104 @cli .register_custom_action ("Project" , ("sha" ,))
88105 @exc .on_http_error (exc .GitlabGetError )
89106 def repository_raw_blob (
90- self , sha , streamed = False , action = None , chunk_size = 1024 , ** kwargs
91- ):
107+ self ,
108+ sha : str ,
109+ streamed : bool = False ,
110+ action : Optional [Callable [..., Any ]] = None ,
111+ chunk_size : int = 1024 ,
112+ ** kwargs : Any ,
113+ ) -> Optional [bytes ]:
92114 """Return the raw file contents for a blob.
93115
94116 Args:
@@ -112,11 +134,15 @@ def repository_raw_blob(
112134 result = self .manager .gitlab .http_get (
113135 path , streamed = streamed , raw = True , ** kwargs
114136 )
137+ if TYPE_CHECKING :
138+ assert isinstance (result , requests .Response )
115139 return utils .response_content (result , streamed , action , chunk_size )
116140
117141 @cli .register_custom_action ("Project" , ("from_" , "to" ))
118142 @exc .on_http_error (exc .GitlabGetError )
119- def repository_compare (self , from_ , to , ** kwargs ):
143+ def repository_compare (
144+ self , from_ : str , to : str , ** kwargs : Any
145+ ) -> Union [Dict [str , Any ], requests .Response ]:
120146 """Return a diff between two branches/commits.
121147
122148 Args:
@@ -137,7 +163,9 @@ def repository_compare(self, from_, to, **kwargs):
137163
138164 @cli .register_custom_action ("Project" )
139165 @exc .on_http_error (exc .GitlabGetError )
140- def repository_contributors (self , ** kwargs ):
166+ def repository_contributors (
167+ self , ** kwargs : Any
168+ ) -> Union [gitlab .client .GitlabList , List [Dict [str , Any ]]]:
141169 """Return a list of contributors for the project.
142170
143171 Args:
@@ -161,8 +189,13 @@ def repository_contributors(self, **kwargs):
161189 @cli .register_custom_action ("Project" , tuple (), ("sha" ,))
162190 @exc .on_http_error (exc .GitlabListError )
163191 def repository_archive (
164- self , sha = None , streamed = False , action = None , chunk_size = 1024 , ** kwargs
165- ):
192+ self ,
193+ sha : str = None ,
194+ streamed : bool = False ,
195+ action : Optional [Callable [..., Any ]] = None ,
196+ chunk_size : int = 1024 ,
197+ ** kwargs : Any ,
198+ ) -> Optional [bytes ]:
166199 """Return a tarball of the repository.
167200
168201 Args:
@@ -189,11 +222,13 @@ def repository_archive(
189222 result = self .manager .gitlab .http_get (
190223 path , query_data = query_data , raw = True , streamed = streamed , ** kwargs
191224 )
225+ if TYPE_CHECKING :
226+ assert isinstance (result , requests .Response )
192227 return utils .response_content (result , streamed , action , chunk_size )
193228
194229 @cli .register_custom_action ("Project" )
195230 @exc .on_http_error (exc .GitlabDeleteError )
196- def delete_merged_branches (self , ** kwargs ) :
231+ def delete_merged_branches (self , ** kwargs : Any ) -> None :
197232 """Delete merged branches.
198233
199234 Args:
0 commit comments