@@ -9,9 +9,12 @@ import (
9
9
"github.com/diggerhq/digger/libs/terraform_utils"
10
10
"io"
11
11
"log"
12
+ "mime"
13
+ "mime/multipart"
12
14
"net/http"
13
15
"net/url"
14
16
"os"
17
+ "path"
15
18
"path/filepath"
16
19
"time"
17
20
)
@@ -31,6 +34,14 @@ func (n NoopApi) ReportProjectJobStatus(repo string, projectName string, jobId s
31
34
return nil , nil
32
35
}
33
36
37
+ func (n NoopApi ) UploadJobArtefact (zipLocation string ) (* int , * string , error ) {
38
+ return nil , nil , nil
39
+ }
40
+
41
+ func (n NoopApi ) DownloadJobArtefact (downloadTo string ) (* string , error ) {
42
+ return nil , nil
43
+ }
44
+
34
45
type DiggerApi struct {
35
46
DiggerHost string
36
47
AuthToken string
@@ -187,6 +198,135 @@ func (d DiggerApi) ReportProjectJobStatus(repo string, projectName string, jobId
187
198
return & response , nil
188
199
}
189
200
201
+ func (d DiggerApi ) UploadJobArtefact (zipLocation string ) (* int , * string , error ) {
202
+ u , err := url .Parse (d .DiggerHost )
203
+ if err != nil {
204
+ return nil , nil , err
205
+ }
206
+ u .Path = path .Join (u .Path , "job_artefacts" )
207
+ url := u .String ()
208
+ filePath := zipLocation
209
+
210
+ // Open the file
211
+ file , err := os .Open (filePath )
212
+ if err != nil {
213
+ fmt .Println ("Error opening file:" , err )
214
+ return nil , nil , fmt .Errorf ("Error opening file:" , err )
215
+ }
216
+ defer file .Close ()
217
+
218
+ // Create a buffer to store our request body as bytes
219
+ var requestBody bytes.Buffer
220
+
221
+ // Create a multipart writer
222
+ multipartWriter := multipart .NewWriter (& requestBody )
223
+
224
+ // Create a form file writer for our file field
225
+ fileWriter , err := multipartWriter .CreateFormFile ("file" , filepath .Base (filePath ))
226
+ if err != nil {
227
+ fmt .Println ("Error creating form file:" , err )
228
+ return nil , nil , fmt .Errorf ("Error creating form file:" , err )
229
+ }
230
+
231
+ // Copy the file content to the form file writer
232
+ _ , err = io .Copy (fileWriter , file )
233
+ if err != nil {
234
+ fmt .Println ("Error copying file content:" , err )
235
+ return nil , nil , fmt .Errorf ("Error copying file content:" , err )
236
+ }
237
+
238
+ // Close the multipart writer to finalize the request body
239
+ multipartWriter .Close ()
240
+
241
+ // Create a new HTTP request
242
+ req , err := http .NewRequest ("PUT" , url , & requestBody )
243
+ if err != nil {
244
+ fmt .Println ("Error creating request:" , err )
245
+ return nil , nil , fmt .Errorf ("Error creating request:" , err )
246
+ }
247
+
248
+ // Set the content type header
249
+ req .Header .Set ("Content-Type" , multipartWriter .FormDataContentType ())
250
+ req .Header .Set ("Authorization" , fmt .Sprintf ("Bearer %v" , d .AuthToken ))
251
+
252
+ // Send the request
253
+ client := & http.Client {}
254
+ resp , err := client .Do (req )
255
+ if err != nil {
256
+ fmt .Println ("Error sending request:" , err )
257
+ return nil , nil , fmt .Errorf ("Error sending request:" , err )
258
+ }
259
+ defer resp .Body .Close ()
260
+
261
+ // Read and print the response
262
+ body , err := io .ReadAll (resp .Body )
263
+ if err != nil {
264
+ fmt .Println ("Error reading response:" , err )
265
+ return nil , nil , fmt .Errorf ("Error reading response: %v" , err )
266
+ }
267
+
268
+ b := string (body )
269
+ return & resp .StatusCode , & b , nil
270
+ }
271
+
272
+ func getFilename (resp * http.Response ) string {
273
+ // Check the Content-Disposition header
274
+ if cd := resp .Header .Get ("Content-Disposition" ); cd != "" {
275
+ if _ , params , err := mime .ParseMediaType (cd ); err == nil {
276
+ if filename , ok := params ["filename" ]; ok {
277
+ return filename
278
+ }
279
+ }
280
+ }
281
+ // Fallback to the last part of the URL path
282
+ return path .Base (resp .Request .URL .Path )
283
+ }
284
+
285
+ func (d DiggerApi ) DownloadJobArtefact (downloadTo string ) (* string , error ) {
286
+ // Download the zip file
287
+ url , err := url .JoinPath (d .DiggerHost , "job_artefacts" )
288
+ if err != nil {
289
+ log .Printf ("failed to create url: %v" , err )
290
+ return nil , err
291
+ }
292
+
293
+ // Create a new HTTP request
294
+ req , err := http .NewRequest ("GET" , url , nil )
295
+ if err != nil {
296
+ fmt .Println ("Error creating request:" , err )
297
+ return nil , fmt .Errorf ("Error creating request:" , err )
298
+ }
299
+
300
+ // Set the content type header
301
+ req .Header .Set ("Authorization" , fmt .Sprintf ("Bearer %v" , d .AuthToken ))
302
+
303
+ // Send the request
304
+ client := & http.Client {}
305
+ resp , err := client .Do (req )
306
+ if err != nil {
307
+ return nil , fmt .Errorf ("failed to download zip: %w" , err )
308
+ }
309
+ defer resp .Body .Close ()
310
+
311
+ // Create a temporary file to store the zip
312
+ tempZipFile , err := os .Create (path .Join (downloadTo , getFilename (resp )))
313
+ if err != nil {
314
+ return nil , fmt .Errorf ("failed to create zip file: %w" , err )
315
+ }
316
+ defer tempZipFile .Close ()
317
+
318
+ // Copy the downloaded content to the temporary file
319
+ _ , err = io .Copy (tempZipFile , resp .Body )
320
+ if err != nil {
321
+ return nil , fmt .Errorf ("failed to save zip content: %w" , err )
322
+ }
323
+
324
+ // note that fileName include absolute path to the zip file
325
+ fileName := tempZipFile .Name ()
326
+ return & fileName , nil
327
+
328
+ }
329
+
190
330
func NewBackendApi (hostName string , authToken string ) Api {
191
331
var backendApi Api
192
332
if os .Getenv ("NO_BACKEND" ) == "true" {
0 commit comments