Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions internal/model/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ type RecordUpdateJobUnknownError struct {
ErrorDetails map[string]any `json:"error-details" yaml:"error-details"`
}

type RecordUpdateJobWarning struct {
WarnType string `json:"warn-type" yaml:"warn-type"`
WarnTitle string `json:"warn-title" yaml:"warn-title"`
WarnDescription string `json:"warn-description" yaml:"warn-description"`
}

type IncrementMetric struct {
Metric string `json:"metric" yaml:"metric"`
Tags map[string]any `json:"tags" yaml:"tags"`
Expand Down
11 changes: 11 additions & 0 deletions internal/server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ func decodeWrapper(kind string, data []byte) (actual *model.UpdateWrapper, err e
actual.Data, err = decode[model.RecordUpdateJobError](data)
case "record_update_job_unknown_error":
actual.Data, err = decode[model.RecordUpdateJobUnknownError](data)
case "record_update_job_warning":
actual.Data, err = decode[model.RecordUpdateJobWarning](data)
case "increment_metric":
actual.Data, err = decode[model.IncrementMetric](data)
default:
Expand Down Expand Up @@ -305,6 +307,8 @@ func compare(expect, actual *model.UpdateWrapper) error {
return compareRecordUpdateJobError(v, actual.Data.(model.RecordUpdateJobError))
case model.RecordUpdateJobUnknownError:
return compareRecordUpdateJobUnknownError(v, actual.Data.(model.RecordUpdateJobUnknownError))
case model.RecordUpdateJobWarning:
return compareRecordUpdateJobWarning(v, actual.Data.(model.RecordUpdateJobWarning))
case []model.RecordEcosystemMeta:
return compareRecordEcosystemMeta(v, actual.Data.([]model.RecordEcosystemMeta))
default:
Expand Down Expand Up @@ -384,6 +388,13 @@ func compareRecordUpdateJobUnknownError(expect, actual model.RecordUpdateJobUnkn
return unexpectedBody("record_update_job_unknown_error")
}

func compareRecordUpdateJobWarning(expect, actual model.RecordUpdateJobWarning) error {
if reflect.DeepEqual(expect, actual) {
return nil
}
return unexpectedBody("record_update_job_warning")
}

func compareRecordEcosystemMeta(expect, actual []model.RecordEcosystemMeta) error {
if reflect.DeepEqual(expect, actual) {
return nil
Expand Down
55 changes: 55 additions & 0 deletions internal/server/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,61 @@ func TestAPI_compareRecordEcosystemMeta(t *testing.T) {
})
}

func TestAPI_compareRecordUpdateJobWarning(t *testing.T) {
t.Run("matching warning", func(t *testing.T) {
warning := model.RecordUpdateJobWarning{
WarnType: "warn_once",
WarnTitle: "something to know",
WarnDescription: "more details here",
}
if err := compareRecordUpdateJobWarning(warning, warning); err != nil {
t.Errorf("expected no error, got %v", err)
}
})

t.Run("mismatched warning", func(t *testing.T) {
expect := model.RecordUpdateJobWarning{WarnType: "warn_once", WarnTitle: "title-a", WarnDescription: "desc-a"}
actual := model.RecordUpdateJobWarning{WarnType: "warn_once", WarnTitle: "title-b", WarnDescription: "desc-b"}
if err := compareRecordUpdateJobWarning(expect, actual); err == nil {
t.Error("expected error for mismatched warning")
}
})

t.Run("compare via compare function", func(t *testing.T) {
warning := model.RecordUpdateJobWarning{
WarnType: "warn_once",
WarnTitle: "something to know",
WarnDescription: "more details here",
}
expectWrapper := &model.UpdateWrapper{Data: warning}
actualWrapper := &model.UpdateWrapper{Data: warning}
if err := compare(expectWrapper, actualWrapper); err != nil {
t.Errorf("expected no error from compare, got %v", err)
}
})

t.Run("decodeWrapper round-trip", func(t *testing.T) {
payload := []byte(`{"data": {"warn-type": "warn_once", "warn-title": "something to know", "warn-description": "more details here"}}`)
wrapper, err := decodeWrapper("record_update_job_warning", payload)
if err != nil {
t.Fatalf("unexpected decode error: %v", err)
}
warning, ok := wrapper.Data.(model.RecordUpdateJobWarning)
if !ok {
t.Fatalf("expected RecordUpdateJobWarning, got %T", wrapper.Data)
}
if warning.WarnType != "warn_once" {
t.Errorf("expected warn-type 'warn_once', got '%s'", warning.WarnType)
}
if warning.WarnTitle != "something to know" {
t.Errorf("expected warn-title 'something to know', got '%s'", warning.WarnTitle)
}
if warning.WarnDescription != "more details here" {
t.Errorf("expected warn-description 'more details here', got '%s'", warning.WarnDescription)
}
})
}

func TestAPI_compareDependencySubmissionRequest(t *testing.T) {
t.Run("ignores detector version", func(t *testing.T) {
expect := model.DependencySubmissionRequest{
Expand Down
Loading