From f80f623ea26602d5db8948fdf2965c31be519681 Mon Sep 17 00:00:00 2001 From: Linus Karlsson Date: Thu, 22 Aug 2024 13:15:00 +0200 Subject: [PATCH] Print details url also when queue time is long --- internal/scan/scanner.go | 3 ++- internal/scan/scanner_test.go | 15 ++++++++++++++- internal/upload/batch.go | 13 +++++++++---- internal/upload/batch_test.go | 2 +- internal/upload/result.go | 2 ++ internal/upload/uploader_test.go | 2 +- 6 files changed, 29 insertions(+), 8 deletions(-) diff --git a/internal/scan/scanner.go b/internal/scan/scanner.go index d55958c5..4ee931aa 100644 --- a/internal/scan/scanner.go +++ b/internal/scan/scanner.go @@ -117,8 +117,9 @@ func (dScanner *DebrickedScanner) Scan(o IOptions) error { return dScanner.handleScanError(err, dOptions.PassOnTimeOut) } - if result == nil { + if result.LongQueue { fmt.Println("Progress polling terminated due to long scan times. Please try again later") + fmt.Printf("For full details, visit: %s\n\n", color.BlueString(result.DetailsUrl)) return nil } diff --git a/internal/scan/scanner_test.go b/internal/scan/scanner_test.go index f95f23ad..ea9798cc 100644 --- a/internal/scan/scanner_test.go +++ b/internal/scan/scanner_test.go @@ -260,7 +260,7 @@ func TestScanEmptyResult(t *testing.T) { addMockedFinishResponse(clientMock, http.StatusNoContent) addMockedStatusResponse(clientMock, http.StatusOK, 50) // Create mocked scan result response, 201 is returned when the queue time are too long - addMockedStatusResponse(clientMock, http.StatusCreated, 0) + addMockedQueueTooLongStatusResponse(clientMock) scanner := makeScanner(clientMock, nil, nil) path := testdataNpm @@ -297,6 +297,11 @@ func TestScanEmptyResult(t *testing.T) { assert.NoError(t, err, "failed to assert that scan ran without errors") assert.True(t, existsMessageInCMDOutput, "failed to assert that scan ran without errors") + + existsMessageInCMDOutputDetails := strings.Contains( + string(out), + "http://localhost:8888/app/en/repository/13/commit/37") + assert.True(t, existsMessageInCMDOutputDetails, "failed to assert that long queue scan contain detailed url") } func TestScanInCiWithPathSet(t *testing.T) { @@ -789,6 +794,14 @@ func addMockedStatusResponse(clientMock *testdata.DebClientMock, statusCode int, clientMock.AddMockUriResponse("/api/1.0/open/ci/upload/status", finishMockRes) } +func addMockedQueueTooLongStatusResponse(clientMock *testdata.DebClientMock) { + finishMockRes := testdata.MockResponse{ + StatusCode: http.StatusCreated, + ResponseBody: io.NopCloser(strings.NewReader(`{"message": "Queue too long","detailsUrl":"http://localhost:8888/app/en/repository/13/commit/37"}`)), + } + clientMock.AddMockUriResponse("/api/1.0/open/ci/upload/status", finishMockRes) +} + func resetWd(t *testing.T, wd string) { err := os.Chdir(wd) if err != nil { diff --git a/internal/upload/batch.go b/internal/upload/batch.go index a5c7eacf..e22b76ef 100644 --- a/internal/upload/batch.go +++ b/internal/upload/batch.go @@ -224,18 +224,23 @@ func (uploadBatch *uploadBatch) wait() (*UploadResult, error) { if err != nil { return nil, err } + status, err := newUploadStatus(res) + if err != nil { + return nil, err + } if res.StatusCode == http.StatusCreated { err := bar.Finish() if err != nil { return resultStatus, err } + resultStatus = &UploadResult{ + DetailsUrl: status.DetailsUrl, + LongQueue: true, + } + return resultStatus, PollingTerminatedErr } - status, err := newUploadStatus(res) - if err != nil { - return nil, err - } err = bar.Set(status.Progress) if err != nil { return nil, err diff --git a/internal/upload/batch_test.go b/internal/upload/batch_test.go index eaa2f633..f9392977 100644 --- a/internal/upload/batch_test.go +++ b/internal/upload/batch_test.go @@ -77,7 +77,7 @@ func TestWaitWithPollingTerminatedError(t *testing.T) { uploadResult, err := batch.wait() - assert.Nil(t, uploadResult) + assert.True(t, uploadResult.LongQueue) assert.ErrorIs(t, err, PollingTerminatedErr) } diff --git a/internal/upload/result.go b/internal/upload/result.go index e58026ad..5d6a9408 100644 --- a/internal/upload/result.go +++ b/internal/upload/result.go @@ -10,6 +10,7 @@ type UploadResult struct { AutomationsAction string `json:"automationsAction"` AutomationRules []automation.Rule `json:"automationRules"` DetailsUrl string `json:"detailsUrl"` + LongQueue bool } func newUploadResult(status *uploadStatus) *UploadResult { @@ -19,5 +20,6 @@ func newUploadResult(status *uploadStatus) *UploadResult { status.AutomationsAction, status.AutomationRules, status.DetailsUrl, + false, } } diff --git a/internal/upload/uploader_test.go b/internal/upload/uploader_test.go index cccfa747..5d3fb970 100644 --- a/internal/upload/uploader_test.go +++ b/internal/upload/uploader_test.go @@ -85,7 +85,7 @@ func TestUploadPollingError(t *testing.T) { result, err := uploader.Upload(uploaderOptions) assert.NoError(t, err) - assert.Nil(t, result) + assert.True(t, result.LongQueue) } type debClientMock struct{}