Skip to content

Commit f1c9278

Browse files
authored
Capture update_job warnings (#622)
1 parent c2aab4e commit f1c9278

3 files changed

Lines changed: 72 additions & 0 deletions

File tree

internal/model/update.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,12 @@ type RecordUpdateJobUnknownError struct {
7979
ErrorDetails map[string]any `json:"error-details" yaml:"error-details"`
8080
}
8181

82+
type RecordUpdateJobWarning struct {
83+
WarnType string `json:"warn-type" yaml:"warn-type"`
84+
WarnTitle string `json:"warn-title" yaml:"warn-title"`
85+
WarnDescription string `json:"warn-description" yaml:"warn-description"`
86+
}
87+
8288
type IncrementMetric struct {
8389
Metric string `json:"metric" yaml:"metric"`
8490
Tags map[string]any `json:"tags" yaml:"tags"`

internal/server/api.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,8 @@ func decodeWrapper(kind string, data []byte) (actual *model.UpdateWrapper, err e
246246
actual.Data, err = decode[model.RecordUpdateJobError](data)
247247
case "record_update_job_unknown_error":
248248
actual.Data, err = decode[model.RecordUpdateJobUnknownError](data)
249+
case "record_update_job_warning":
250+
actual.Data, err = decode[model.RecordUpdateJobWarning](data)
249251
case "increment_metric":
250252
actual.Data, err = decode[model.IncrementMetric](data)
251253
default:
@@ -305,6 +307,8 @@ func compare(expect, actual *model.UpdateWrapper) error {
305307
return compareRecordUpdateJobError(v, actual.Data.(model.RecordUpdateJobError))
306308
case model.RecordUpdateJobUnknownError:
307309
return compareRecordUpdateJobUnknownError(v, actual.Data.(model.RecordUpdateJobUnknownError))
310+
case model.RecordUpdateJobWarning:
311+
return compareRecordUpdateJobWarning(v, actual.Data.(model.RecordUpdateJobWarning))
308312
case []model.RecordEcosystemMeta:
309313
return compareRecordEcosystemMeta(v, actual.Data.([]model.RecordEcosystemMeta))
310314
default:
@@ -384,6 +388,13 @@ func compareRecordUpdateJobUnknownError(expect, actual model.RecordUpdateJobUnkn
384388
return unexpectedBody("record_update_job_unknown_error")
385389
}
386390

391+
func compareRecordUpdateJobWarning(expect, actual model.RecordUpdateJobWarning) error {
392+
if reflect.DeepEqual(expect, actual) {
393+
return nil
394+
}
395+
return unexpectedBody("record_update_job_warning")
396+
}
397+
387398
func compareRecordEcosystemMeta(expect, actual []model.RecordEcosystemMeta) error {
388399
if reflect.DeepEqual(expect, actual) {
389400
return nil

internal/server/api_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,61 @@ func TestAPI_compareRecordEcosystemMeta(t *testing.T) {
181181
})
182182
}
183183

184+
func TestAPI_compareRecordUpdateJobWarning(t *testing.T) {
185+
t.Run("matching warning", func(t *testing.T) {
186+
warning := model.RecordUpdateJobWarning{
187+
WarnType: "warn_once",
188+
WarnTitle: "something to know",
189+
WarnDescription: "more details here",
190+
}
191+
if err := compareRecordUpdateJobWarning(warning, warning); err != nil {
192+
t.Errorf("expected no error, got %v", err)
193+
}
194+
})
195+
196+
t.Run("mismatched warning", func(t *testing.T) {
197+
expect := model.RecordUpdateJobWarning{WarnType: "warn_once", WarnTitle: "title-a", WarnDescription: "desc-a"}
198+
actual := model.RecordUpdateJobWarning{WarnType: "warn_once", WarnTitle: "title-b", WarnDescription: "desc-b"}
199+
if err := compareRecordUpdateJobWarning(expect, actual); err == nil {
200+
t.Error("expected error for mismatched warning")
201+
}
202+
})
203+
204+
t.Run("compare via compare function", func(t *testing.T) {
205+
warning := model.RecordUpdateJobWarning{
206+
WarnType: "warn_once",
207+
WarnTitle: "something to know",
208+
WarnDescription: "more details here",
209+
}
210+
expectWrapper := &model.UpdateWrapper{Data: warning}
211+
actualWrapper := &model.UpdateWrapper{Data: warning}
212+
if err := compare(expectWrapper, actualWrapper); err != nil {
213+
t.Errorf("expected no error from compare, got %v", err)
214+
}
215+
})
216+
217+
t.Run("decodeWrapper round-trip", func(t *testing.T) {
218+
payload := []byte(`{"data": {"warn-type": "warn_once", "warn-title": "something to know", "warn-description": "more details here"}}`)
219+
wrapper, err := decodeWrapper("record_update_job_warning", payload)
220+
if err != nil {
221+
t.Fatalf("unexpected decode error: %v", err)
222+
}
223+
warning, ok := wrapper.Data.(model.RecordUpdateJobWarning)
224+
if !ok {
225+
t.Fatalf("expected RecordUpdateJobWarning, got %T", wrapper.Data)
226+
}
227+
if warning.WarnType != "warn_once" {
228+
t.Errorf("expected warn-type 'warn_once', got '%s'", warning.WarnType)
229+
}
230+
if warning.WarnTitle != "something to know" {
231+
t.Errorf("expected warn-title 'something to know', got '%s'", warning.WarnTitle)
232+
}
233+
if warning.WarnDescription != "more details here" {
234+
t.Errorf("expected warn-description 'more details here', got '%s'", warning.WarnDescription)
235+
}
236+
})
237+
}
238+
184239
func TestAPI_compareDependencySubmissionRequest(t *testing.T) {
185240
t.Run("ignores detector version", func(t *testing.T) {
186241
expect := model.DependencySubmissionRequest{

0 commit comments

Comments
 (0)