3
3
4
4
Currently this module only contains repository-related methods for projects.
5
5
"""
6
+ from typing import Any , Callable , Dict , List , Optional , TYPE_CHECKING , Union
6
7
8
+ import requests
9
+
10
+ import gitlab
7
11
from gitlab import cli
8
12
from gitlab import exceptions as exc
9
13
from gitlab import utils
10
14
15
+ if TYPE_CHECKING :
16
+ # When running mypy we use these as the base classes
17
+ _RestManagerBase = gitlab .base .RESTManager
18
+ _RestObjectBase = gitlab .base .RESTObject
19
+ else :
20
+ _RestManagerBase = object
21
+ _RestObjectBase = object
22
+
11
23
12
- class RepositoryMixin :
24
+ class RepositoryMixin ( _RestObjectBase ) :
13
25
@cli .register_custom_action ("Project" , ("submodule" , "branch" , "commit_sha" ))
14
26
@exc .on_http_error (exc .GitlabUpdateError )
15
- def update_submodule (self , submodule , branch , commit_sha , ** kwargs ):
27
+ def update_submodule (
28
+ self , submodule : str , branch : str , commit_sha : str , ** kwargs : Any
29
+ ) -> Union [Dict [str , Any ], requests .Response ]:
16
30
"""Update a project submodule
17
31
18
32
Args:
19
33
submodule (str): Full path to the submodule
20
34
branch (str): Name of the branch to commit into
21
35
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)
36
+ commit_message (str): Commit message. If no message is provided, a
37
+ default one will be set (optional)
23
38
24
39
Raises:
25
40
GitlabAuthenticationError: If authentication is not correct
@@ -35,7 +50,9 @@ def update_submodule(self, submodule, branch, commit_sha, **kwargs):
35
50
36
51
@cli .register_custom_action ("Project" , tuple (), ("path" , "ref" , "recursive" ))
37
52
@exc .on_http_error (exc .GitlabGetError )
38
- def repository_tree (self , path = "" , ref = "" , recursive = False , ** kwargs ):
53
+ def repository_tree (
54
+ self , path : str = "" , ref : str = "" , recursive : bool = False , ** kwargs : Any
55
+ ) -> Union [gitlab .client .GitlabList , List [Dict [str , Any ]]]:
39
56
"""Return a list of files in the repository.
40
57
41
58
Args:
@@ -57,7 +74,7 @@ def repository_tree(self, path="", ref="", recursive=False, **kwargs):
57
74
list: The representation of the tree
58
75
"""
59
76
gl_path = f"/projects/{ self .get_id ()} /repository/tree"
60
- query_data = {"recursive" : recursive }
77
+ query_data : Dict [ str , Any ] = {"recursive" : recursive }
61
78
if path :
62
79
query_data ["path" ] = path
63
80
if ref :
@@ -66,7 +83,9 @@ def repository_tree(self, path="", ref="", recursive=False, **kwargs):
66
83
67
84
@cli .register_custom_action ("Project" , ("sha" ,))
68
85
@exc .on_http_error (exc .GitlabGetError )
69
- def repository_blob (self , sha , ** kwargs ):
86
+ def repository_blob (
87
+ self , sha : str , ** kwargs : Any
88
+ ) -> Union [Dict [str , Any ], requests .Response ]:
70
89
"""Return a file by blob SHA.
71
90
72
91
Args:
@@ -87,8 +106,13 @@ def repository_blob(self, sha, **kwargs):
87
106
@cli .register_custom_action ("Project" , ("sha" ,))
88
107
@exc .on_http_error (exc .GitlabGetError )
89
108
def repository_raw_blob (
90
- self , sha , streamed = False , action = None , chunk_size = 1024 , ** kwargs
91
- ):
109
+ self ,
110
+ sha : str ,
111
+ streamed : bool = False ,
112
+ action : Optional [Callable [..., Any ]] = None ,
113
+ chunk_size : int = 1024 ,
114
+ ** kwargs : Any ,
115
+ ) -> Optional [bytes ]:
92
116
"""Return the raw file contents for a blob.
93
117
94
118
Args:
@@ -112,11 +136,15 @@ def repository_raw_blob(
112
136
result = self .manager .gitlab .http_get (
113
137
path , streamed = streamed , raw = True , ** kwargs
114
138
)
139
+ if TYPE_CHECKING :
140
+ assert isinstance (result , requests .Response )
115
141
return utils .response_content (result , streamed , action , chunk_size )
116
142
117
143
@cli .register_custom_action ("Project" , ("from_" , "to" ))
118
144
@exc .on_http_error (exc .GitlabGetError )
119
- def repository_compare (self , from_ , to , ** kwargs ):
145
+ def repository_compare (
146
+ self , from_ : str , to : str , ** kwargs : Any
147
+ ) -> Union [Dict [str , Any ], requests .Response ]:
120
148
"""Return a diff between two branches/commits.
121
149
122
150
Args:
@@ -137,7 +165,9 @@ def repository_compare(self, from_, to, **kwargs):
137
165
138
166
@cli .register_custom_action ("Project" )
139
167
@exc .on_http_error (exc .GitlabGetError )
140
- def repository_contributors (self , ** kwargs ):
168
+ def repository_contributors (
169
+ self , ** kwargs : Any
170
+ ) -> Union [gitlab .client .GitlabList , List [Dict [str , Any ]]]:
141
171
"""Return a list of contributors for the project.
142
172
143
173
Args:
@@ -161,8 +191,13 @@ def repository_contributors(self, **kwargs):
161
191
@cli .register_custom_action ("Project" , tuple (), ("sha" ,))
162
192
@exc .on_http_error (exc .GitlabListError )
163
193
def repository_archive (
164
- self , sha = None , streamed = False , action = None , chunk_size = 1024 , ** kwargs
165
- ):
194
+ self ,
195
+ sha : str = None ,
196
+ streamed : bool = False ,
197
+ action : Optional [Callable [..., Any ]] = None ,
198
+ chunk_size : int = 1024 ,
199
+ ** kwargs : Any ,
200
+ ) -> Optional [bytes ]:
166
201
"""Return a tarball of the repository.
167
202
168
203
Args:
@@ -189,11 +224,13 @@ def repository_archive(
189
224
result = self .manager .gitlab .http_get (
190
225
path , query_data = query_data , raw = True , streamed = streamed , ** kwargs
191
226
)
227
+ if TYPE_CHECKING :
228
+ assert isinstance (result , requests .Response )
192
229
return utils .response_content (result , streamed , action , chunk_size )
193
230
194
231
@cli .register_custom_action ("Project" )
195
232
@exc .on_http_error (exc .GitlabDeleteError )
196
- def delete_merged_branches (self , ** kwargs ) :
233
+ def delete_merged_branches (self , ** kwargs : Any ) -> None :
197
234
"""Delete merged branches.
198
235
199
236
Args:
0 commit comments