1+ from typing import Any ,Callable ,cast ,Dict ,Optional ,TYPE_CHECKING ,Union
2+
3+ import requests
4+
15from gitlab import cli
26from gitlab import exceptions as exc
37from gitlab import utils
1317class ProjectJob (RefreshMixin ,RESTObject ):
1418@cli .register_custom_action ("ProjectJob" )
1519@exc .on_http_error (exc .GitlabJobCancelError )
16- def cancel (self ,** kwargs ) :
20+ def cancel (self ,** kwargs : Any ) -> Dict [ str , Any ] :
1721"""Cancel the job.
1822
1923 Args:
@@ -24,11 +28,14 @@ def cancel(self, **kwargs):
2428 GitlabJobCancelError: If the job could not be canceled
2529 """
2630path = 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
2835
2936@cli .register_custom_action ("ProjectJob" )
3037@exc .on_http_error (exc .GitlabJobRetryError )
31- def retry (self ,** kwargs ) :
38+ def retry (self ,** kwargs : Any ) -> Dict [ str , Any ] :
3239"""Retry the job.
3340
3441 Args:
@@ -39,11 +46,14 @@ def retry(self, **kwargs):
3946 GitlabJobRetryError: If the job could not be retried
4047 """
4148path = 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
4353
4454@cli .register_custom_action ("ProjectJob" )
4555@exc .on_http_error (exc .GitlabJobPlayError )
46- def play (self ,** kwargs ) :
56+ def play (self ,** kwargs : Any ) -> None :
4757"""Trigger a job explicitly.
4858
4959 Args:
@@ -58,7 +68,7 @@ def play(self, **kwargs):
5868
5969@cli .register_custom_action ("ProjectJob" )
6070@exc .on_http_error (exc .GitlabJobEraseError )
61- def erase (self ,** kwargs ) :
71+ def erase (self ,** kwargs : Any ) -> None :
6272"""Erase the job (remove job artifacts and trace).
6373
6474 Args:
@@ -73,7 +83,7 @@ def erase(self, **kwargs):
7383
7484@cli .register_custom_action ("ProjectJob" )
7585@exc .on_http_error (exc .GitlabCreateError )
76- def keep_artifacts (self ,** kwargs ) :
86+ def keep_artifacts (self ,** kwargs : Any ) -> None :
7787"""Prevent artifacts from being deleted when expiration is set.
7888
7989 Args:
@@ -88,7 +98,7 @@ def keep_artifacts(self, **kwargs):
8898
8999@cli .register_custom_action ("ProjectJob" )
90100@exc .on_http_error (exc .GitlabCreateError )
91- def delete_artifacts (self ,** kwargs ) :
101+ def delete_artifacts (self ,** kwargs : Any ) -> None :
92102"""Delete artifacts of a job.
93103
94104 Args:
@@ -103,7 +113,13 @@ def delete_artifacts(self, **kwargs):
103113
104114@cli .register_custom_action ("ProjectJob" )
105115@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 ]:
107123"""Get the job artifacts.
108124
109125 Args:
@@ -120,17 +136,26 @@ def artifacts(self, streamed=False, action=None, chunk_size=1024, **kwargs):
120136 GitlabGetError: If the artifacts could not be retrieved
121137
122138 Returns:
123- str : The artifacts if `streamed` is False, None otherwise.
139+ bytes : The artifacts if `streamed` is False, None otherwise.
124140 """
125141path = f"{ self .manager .path } /{ self .get_id ()} /artifacts"
126142result = self .manager .gitlab .http_get (
127143path ,streamed = streamed ,raw = True ,** kwargs
128144 )
145+ if TYPE_CHECKING :
146+ assert isinstance (result ,requests .Response )
129147return utils .response_content (result ,streamed ,action ,chunk_size )
130148
131149@cli .register_custom_action ("ProjectJob" )
132150@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 ]:
134159"""Get a single artifact file from within the job's artifacts archive.
135160
136161 Args:
@@ -148,17 +173,25 @@ def artifact(self, path, streamed=False, action=None, chunk_size=1024, **kwargs)
148173 GitlabGetError: If the artifacts could not be retrieved
149174
150175 Returns:
151- str : The artifacts if `streamed` is False, None otherwise.
176+ bytes : The artifacts if `streamed` is False, None otherwise.
152177 """
153178path = f"{ self .manager .path } /{ self .get_id ()} /artifacts/{ path } "
154179result = self .manager .gitlab .http_get (
155180path ,streamed = streamed ,raw = True ,** kwargs
156181 )
182+ if TYPE_CHECKING :
183+ assert isinstance (result ,requests .Response )
157184return utils .response_content (result ,streamed ,action ,chunk_size )
158185
159186@cli .register_custom_action ("ProjectJob" )
160187@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 ]:
162195"""Get the job trace.
163196
164197 Args:
@@ -181,10 +214,18 @@ def trace(self, streamed=False, action=None, chunk_size=1024, **kwargs):
181214result = self .manager .gitlab .http_get (
182215path ,streamed = streamed ,raw = True ,** kwargs
183216 )
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
185223
186224
187225class ProjectJobManager (RetrieveMixin ,RESTManager ):
188226_path = "/projects/{project_id}/jobs"
189227_obj_cls = ProjectJob
190228_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 ))