Skip to content
This repository was archived by the owner on Sep 17, 2024. It is now read-only.

Commit 6ee6b7a

Browse files
authored
Allow downloading artifacts from project specific locations (#3622)
* Allow downloading snapshots from projects. Remove use of non-stdlib http clients. * Fix broken retries and JSON parsing. * Add mising locks, clean up logs. * Clean up more logs, add more missing locks. * Improve all log messages. * Fix tests. * Fix lint errors.
1 parent 3cca085 commit 6ee6b7a

4 files changed

Lines changed: 181 additions & 177 deletions

File tree

internal/utils/utils.go

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package utils
66

77
import (
8+
"fmt"
89
"io"
910
"math/rand"
1011
"net/http"
@@ -53,11 +54,7 @@ func DownloadFile(downloadRequest *DownloadRequest) error {
5354
tempParentDir := filepath.Join(os.TempDir(), uuid.NewString())
5455
err := internalio.MkdirAll(tempParentDir)
5556
if err != nil {
56-
log.WithFields(log.Fields{
57-
"error": err,
58-
"path": tempParentDir,
59-
}).Error("Error creating directory")
60-
return err
57+
return fmt.Errorf("creating directory: %w", err)
6158
}
6259
filePath = filepath.Join(tempParentDir, uuid.NewString())
6360
downloadRequest.DownloadPath = filePath
@@ -67,11 +64,7 @@ func DownloadFile(downloadRequest *DownloadRequest) error {
6764

6865
tempFile, err := os.Create(filePath)
6966
if err != nil {
70-
log.WithFields(log.Fields{
71-
"error": err,
72-
"url": downloadRequest.URL,
73-
}).Error("Error creating file")
74-
return err
67+
return fmt.Errorf("creating file: %w", err)
7568
}
7669
defer tempFile.Close()
7770

@@ -83,36 +76,19 @@ func DownloadFile(downloadRequest *DownloadRequest) error {
8376
download := func() error {
8477
resp, err := http.Get(downloadRequest.URL)
8578
if err != nil {
86-
log.WithFields(log.Fields{
87-
"elapsedTime": exp.GetElapsedTime(),
88-
"error": err,
89-
"path": downloadRequest.UnsanitizedFilePath,
90-
"retry": retryCount,
91-
"url": downloadRequest.URL,
92-
}).Warn("Could not download the file")
93-
9479
retryCount++
95-
96-
return err
80+
return fmt.Errorf("downloading file %s: %w", downloadRequest.URL, err)
9781
}
9882

99-
log.WithFields(log.Fields{
100-
"elapsedTime": exp.GetElapsedTime(),
101-
"retries": retryCount,
102-
"path": downloadRequest.UnsanitizedFilePath,
103-
"url": downloadRequest.URL,
104-
}).Trace("File downloaded")
83+
if resp != nil && resp.StatusCode == http.StatusNotFound {
84+
return backoff.Permanent(fmt.Errorf("%s not found", downloadRequest.URL))
85+
}
10586

10687
fileReader = resp.Body
10788

10889
return nil
10990
}
11091

111-
log.WithFields(log.Fields{
112-
"url": downloadRequest.URL,
113-
"path": downloadRequest.UnsanitizedFilePath,
114-
}).Trace("Downloading file")
115-
11692
err = backoff.Retry(download, exp)
11793
if err != nil {
11894
return err
@@ -121,13 +97,7 @@ func DownloadFile(downloadRequest *DownloadRequest) error {
12197

12298
_, err = io.Copy(tempFile, fileReader)
12399
if err != nil {
124-
log.WithFields(log.Fields{
125-
"error": err,
126-
"url": downloadRequest.URL,
127-
"path": downloadRequest.UnsanitizedFilePath,
128-
}).Error("Could not write file")
129-
130-
return err
100+
return fmt.Errorf("writing file %s: %w", tempFile.Name(), err)
131101
}
132102

133103
_ = os.Chmod(tempFile.Name(), 0666)

0 commit comments

Comments
 (0)