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
+ _RestObjectBase = gitlab .base .RESTObject
18
+ else :
19
+ _RestObjectBase = object
20
+
11
21
12
- class RepositoryMixin :
22
+ class RepositoryMixin ( _RestObjectBase ) :
13
23
@cli .register_custom_action ("Project" , ("submodule" , "branch" , "commit_sha" ))
14
24
@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 ]:
16
28
"""Update a project submodule
17
29
18
30
Args:
19
31
submodule (str): Full path to the submodule
20
32
branch (str): Name of the branch to commit into
21
33
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)
23
36
24
37
Raises:
25
38
GitlabAuthenticationError: If authentication is not correct
@@ -35,7 +48,9 @@ def update_submodule(self, submodule, branch, commit_sha, **kwargs):
35
48
36
49
@cli .register_custom_action ("Project" , tuple (), ("path" , "ref" , "recursive" ))
37
50
@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 ]]]:
39
54
"""Return a list of files in the repository.
40
55
41
56
Args:
@@ -57,7 +72,7 @@ def repository_tree(self, path="", ref="", recursive=False, **kwargs):
57
72
list: The representation of the tree
58
73
"""
59
74
gl_path = f"/projects/{ self .get_id ()} /repository/tree"
60
- query_data = {"recursive" : recursive }
75
+ query_data : Dict [ str , Any ] = {"recursive" : recursive }
61
76
if path :
62
77
query_data ["path" ] = path
63
78
if ref :
@@ -66,7 +81,9 @@ def repository_tree(self, path="", ref="", recursive=False, **kwargs):
66
81
67
82
@cli .register_custom_action ("Project" , ("sha" ,))
68
83
@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 ]:
70
87
"""Return a file by blob SHA.
71
88
72
89
Args:
@@ -87,8 +104,13 @@ def repository_blob(self, sha, **kwargs):
87
104
@cli .register_custom_action ("Project" , ("sha" ,))
88
105
@exc .on_http_error (exc .GitlabGetError )
89
106
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 ]:
92
114
"""Return the raw file contents for a blob.
93
115
94
116
Args:
@@ -112,11 +134,15 @@ def repository_raw_blob(
112
134
result = self .manager .gitlab .http_get (
113
135
path , streamed = streamed , raw = True , ** kwargs
114
136
)
137
+ if TYPE_CHECKING :
138
+ assert isinstance (result , requests .Response )
115
139
return utils .response_content (result , streamed , action , chunk_size )
116
140
117
141
@cli .register_custom_action ("Project" , ("from_" , "to" ))
118
142
@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 ]:
120
146
"""Return a diff between two branches/commits.
121
147
122
148
Args:
@@ -137,7 +163,9 @@ def repository_compare(self, from_, to, **kwargs):
137
163
138
164
@cli .register_custom_action ("Project" )
139
165
@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 ]]]:
141
169
"""Return a list of contributors for the project.
142
170
143
171
Args:
@@ -161,8 +189,13 @@ def repository_contributors(self, **kwargs):
161
189
@cli .register_custom_action ("Project" , tuple (), ("sha" ,))
162
190
@exc .on_http_error (exc .GitlabListError )
163
191
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 ]:
166
199
"""Return a tarball of the repository.
167
200
168
201
Args:
@@ -189,11 +222,13 @@ def repository_archive(
189
222
result = self .manager .gitlab .http_get (
190
223
path , query_data = query_data , raw = True , streamed = streamed , ** kwargs
191
224
)
225
+ if TYPE_CHECKING :
226
+ assert isinstance (result , requests .Response )
192
227
return utils .response_content (result , streamed , action , chunk_size )
193
228
194
229
@cli .register_custom_action ("Project" )
195
230
@exc .on_http_error (exc .GitlabDeleteError )
196
- def delete_merged_branches (self , ** kwargs ) :
231
+ def delete_merged_branches (self , ** kwargs : Any ) -> None :
197
232
"""Delete merged branches.
198
233
199
234
Args:
0 commit comments