1
+ from typing import Any ,Callable ,cast ,Dict ,Optional ,TYPE_CHECKING ,Union
2
+
3
+ import requests
4
+
1
5
from gitlab import cli
2
6
from gitlab import exceptions as exc
3
7
from gitlab import utils
13
17
class ProjectJob (RefreshMixin ,RESTObject ):
14
18
@cli .register_custom_action ("ProjectJob" )
15
19
@exc .on_http_error (exc .GitlabJobCancelError )
16
- def cancel (self ,** kwargs ) :
20
+ def cancel (self ,** kwargs : Any ) -> Dict [ str , Any ] :
17
21
"""Cancel the job.
18
22
19
23
Args:
@@ -24,11 +28,14 @@ def cancel(self, **kwargs):
24
28
GitlabJobCancelError: If the job could not be canceled
25
29
"""
26
30
path = f"{ self .manager .path } /{ self .get_id ()} /cancel"
27
- return self .manager .gitlab .http_post (path )
31
+ result = self .manager .gitlab .http_post (path )
32
+ if TYPE_CHECKING :
33
+ assert isinstance (result ,dict )
34
+ return result
28
35
29
36
@cli .register_custom_action ("ProjectJob" )
30
37
@exc .on_http_error (exc .GitlabJobRetryError )
31
- def retry (self ,** kwargs ) :
38
+ def retry (self ,** kwargs : Any ) -> Dict [ str , Any ] :
32
39
"""Retry the job.
33
40
34
41
Args:
@@ -39,11 +46,14 @@ def retry(self, **kwargs):
39
46
GitlabJobRetryError: If the job could not be retried
40
47
"""
41
48
path = f"{ self .manager .path } /{ self .get_id ()} /retry"
42
- return self .manager .gitlab .http_post (path )
49
+ result = self .manager .gitlab .http_post (path )
50
+ if TYPE_CHECKING :
51
+ assert isinstance (result ,dict )
52
+ return result
43
53
44
54
@cli .register_custom_action ("ProjectJob" )
45
55
@exc .on_http_error (exc .GitlabJobPlayError )
46
- def play (self ,** kwargs ) :
56
+ def play (self ,** kwargs : Any ) -> None :
47
57
"""Trigger a job explicitly.
48
58
49
59
Args:
@@ -58,7 +68,7 @@ def play(self, **kwargs):
58
68
59
69
@cli .register_custom_action ("ProjectJob" )
60
70
@exc .on_http_error (exc .GitlabJobEraseError )
61
- def erase (self ,** kwargs ) :
71
+ def erase (self ,** kwargs : Any ) -> None :
62
72
"""Erase the job (remove job artifacts and trace).
63
73
64
74
Args:
@@ -73,7 +83,7 @@ def erase(self, **kwargs):
73
83
74
84
@cli .register_custom_action ("ProjectJob" )
75
85
@exc .on_http_error (exc .GitlabCreateError )
76
- def keep_artifacts (self ,** kwargs ) :
86
+ def keep_artifacts (self ,** kwargs : Any ) -> None :
77
87
"""Prevent artifacts from being deleted when expiration is set.
78
88
79
89
Args:
@@ -88,7 +98,7 @@ def keep_artifacts(self, **kwargs):
88
98
89
99
@cli .register_custom_action ("ProjectJob" )
90
100
@exc .on_http_error (exc .GitlabCreateError )
91
- def delete_artifacts (self ,** kwargs ) :
101
+ def delete_artifacts (self ,** kwargs : Any ) -> None :
92
102
"""Delete artifacts of a job.
93
103
94
104
Args:
@@ -103,7 +113,13 @@ def delete_artifacts(self, **kwargs):
103
113
104
114
@cli .register_custom_action ("ProjectJob" )
105
115
@exc .on_http_error (exc .GitlabGetError )
106
- def artifacts (self ,streamed = False ,action = None ,chunk_size = 1024 ,** kwargs ):
116
+ def artifacts (
117
+ self ,
118
+ streamed :bool = False ,
119
+ action :Optional [Callable [...,Any ]]= None ,
120
+ chunk_size :int = 1024 ,
121
+ ** kwargs :Any ,
122
+ )-> Optional [bytes ]:
107
123
"""Get the job artifacts.
108
124
109
125
Args:
@@ -120,17 +136,26 @@ def artifacts(self, streamed=False, action=None, chunk_size=1024, **kwargs):
120
136
GitlabGetError: If the artifacts could not be retrieved
121
137
122
138
Returns:
123
- str : The artifacts if `streamed` is False, None otherwise.
139
+ bytes : The artifacts if `streamed` is False, None otherwise.
124
140
"""
125
141
path = f"{ self .manager .path } /{ self .get_id ()} /artifacts"
126
142
result = self .manager .gitlab .http_get (
127
143
path ,streamed = streamed ,raw = True ,** kwargs
128
144
)
145
+ if TYPE_CHECKING :
146
+ assert isinstance (result ,requests .Response )
129
147
return utils .response_content (result ,streamed ,action ,chunk_size )
130
148
131
149
@cli .register_custom_action ("ProjectJob" )
132
150
@exc .on_http_error (exc .GitlabGetError )
133
- def artifact (self ,path ,streamed = False ,action = None ,chunk_size = 1024 ,** kwargs ):
151
+ def artifact (
152
+ self ,
153
+ path :str ,
154
+ streamed :bool = False ,
155
+ action :Optional [Callable [...,Any ]]= None ,
156
+ chunk_size :int = 1024 ,
157
+ ** kwargs :Any ,
158
+ )-> Optional [bytes ]:
134
159
"""Get a single artifact file from within the job's artifacts archive.
135
160
136
161
Args:
@@ -148,17 +173,25 @@ def artifact(self, path, streamed=False, action=None, chunk_size=1024, **kwargs)
148
173
GitlabGetError: If the artifacts could not be retrieved
149
174
150
175
Returns:
151
- str : The artifacts if `streamed` is False, None otherwise.
176
+ bytes : The artifacts if `streamed` is False, None otherwise.
152
177
"""
153
178
path = f"{ self .manager .path } /{ self .get_id ()} /artifacts/{ path } "
154
179
result = self .manager .gitlab .http_get (
155
180
path ,streamed = streamed ,raw = True ,** kwargs
156
181
)
182
+ if TYPE_CHECKING :
183
+ assert isinstance (result ,requests .Response )
157
184
return utils .response_content (result ,streamed ,action ,chunk_size )
158
185
159
186
@cli .register_custom_action ("ProjectJob" )
160
187
@exc .on_http_error (exc .GitlabGetError )
161
- def trace (self ,streamed = False ,action = None ,chunk_size = 1024 ,** kwargs ):
188
+ def trace (
189
+ self ,
190
+ streamed :bool = False ,
191
+ action :Optional [Callable [...,Any ]]= None ,
192
+ chunk_size :int = 1024 ,
193
+ ** kwargs :Any ,
194
+ )-> Dict [str ,Any ]:
162
195
"""Get the job trace.
163
196
164
197
Args:
@@ -181,10 +214,18 @@ def trace(self, streamed=False, action=None, chunk_size=1024, **kwargs):
181
214
result = self .manager .gitlab .http_get (
182
215
path ,streamed = streamed ,raw = True ,** kwargs
183
216
)
184
- return utils .response_content (result ,streamed ,action ,chunk_size )
217
+ if TYPE_CHECKING :
218
+ assert isinstance (result ,requests .Response )
219
+ return_value = utils .response_content (result ,streamed ,action ,chunk_size )
220
+ if TYPE_CHECKING :
221
+ assert isinstance (return_value ,dict )
222
+ return return_value
185
223
186
224
187
225
class ProjectJobManager (RetrieveMixin ,RESTManager ):
188
226
_path = "/projects/{project_id}/jobs"
189
227
_obj_cls = ProjectJob
190
228
_from_parent_attrs = {"project_id" :"id" }
229
+
230
+ def get (self ,id :Union [str ,int ],lazy :bool = False ,** kwargs :Any )-> ProjectJob :
231
+ return cast (ProjectJob ,super ().get (id = id ,lazy = lazy ,** kwargs ))