Skip to content

Commit

Permalink
Merge pull request #466 from snyk/fix/snyk-learn-for-all-ecosystems
Browse files Browse the repository at this point in the history
fix: associate learn lessons with all ecosystems [IDE-203]
  • Loading branch information
bastiandoetsch authored Mar 14, 2024
2 parents cd9e82b + 49b483d commit f60b494
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
15 changes: 8 additions & 7 deletions infrastructure/learn/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ type Lesson struct {
Cves []string `json:"cves"`
Cwes []string `json:"cwes"`
Description string `json:"description"`
Ecosystem string `json:"ecosystem"`
Ecosystems []string `json:"ecosystems"`
Rules []string `json:"rules"`
Slug string `json:"slug"`
Published bool `json:"published"`
Expand Down Expand Up @@ -204,13 +204,14 @@ func (s *serviceImpl) updateCaches(lessons []Lesson) {
lessonsForRule = append(lessonsForRule, lesson)
s.lessonsByRuleCache.Set(lesson.Slug, lessonsForRule, expiration)
}

lessonsForEcosystem, exists := s.lessonsByEcosystemCache.Get(lesson.Ecosystem)
if !exists {
lessonsForEcosystem = []Lesson{}
for _, ecosystem := range lesson.Ecosystems {
lessonsForEcosystem, exists := s.lessonsByEcosystemCache.Get(ecosystem)
if !exists {
lessonsForEcosystem = []Lesson{}
}
lessonsForEcosystem = append(lessonsForEcosystem, lesson)
s.lessonsByEcosystemCache.Set(ecosystem, lessonsForEcosystem, expiration)
}
lessonsForEcosystem = append(lessonsForEcosystem, lesson)
s.lessonsByEcosystemCache.Set(lesson.Ecosystem, lessonsForEcosystem, expiration)
}
}

Expand Down
25 changes: 19 additions & 6 deletions infrastructure/learn/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,19 @@ func Test_GetLesson(t *testing.T) {
assert.NoError(t, err)
assert.Empty(t, lesson)
})
t.Run("Code security - lesson returned", func(t *testing.T) {
t.Run("Code security - lesson returned - javascript", func(t *testing.T) {
params := getRealCodeLookupParams()

lesson, err := cut.GetLesson(params.Ecosystem, params.Rule, params.CWEs, params.CVEs, snyk.CodeSecurityVulnerability)
checkLesson(t, cut, params)
})

assert.NoError(t, err)
assert.NotEmpty(t, lesson)
assert.Contains(t, lesson.Cwes, params.CWEs[0])
assert.Equal(t, lesson.Ecosystem, params.Ecosystem)
t.Run("Code security - lesson returned - java", func(t *testing.T) {
params := getRealCodeLookupParams()
params.Ecosystem = "java"

checkLesson(t, cut, params)
})

t.Run("Code quality - no lessons returned", func(t *testing.T) {
params := getRealCodeLookupParams()

Expand All @@ -100,3 +103,13 @@ func Test_GetLesson(t *testing.T) {
assert.Empty(t, lesson)
})
}

func checkLesson(t *testing.T, cut Service, params LessonLookupParams) {
t.Helper()
lesson, err := cut.GetLesson(params.Ecosystem, params.Rule, params.CWEs, params.CVEs, snyk.CodeSecurityVulnerability)

assert.NoError(t, err)
assert.NotEmpty(t, lesson)
assert.Contains(t, lesson.Cwes, params.CWEs[0])
assert.Contains(t, lesson.Ecosystems, params.Ecosystem)
}

0 comments on commit f60b494

Please sign in to comment.