Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.
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
41 changes: 41 additions & 0 deletions enterprise/internal/insights/discovery/discovery.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package discovery

import (
"context"

"github.com/sourcegraph/sourcegraph/internal/api"
"github.com/sourcegraph/sourcegraph/internal/jsonc"
"github.com/sourcegraph/sourcegraph/schema"
)

// SettingStore is a subset of the API exposed by the database.Settings() store.
type SettingStore interface {
GetLatest(context.Context, api.SettingsSubject) (*api.Settings, error)
}

// Discover uses the given settings store to look for insights in the global user settings.
//
// TODO(slimsag): future: include user/org settings and consider security implications of doing so.
// In the future, this will be expanded to also include insights from users/orgs.
func Discover(ctx context.Context, settingStore SettingStore) ([]*schema.Insight, error) {
// Get latest Global user settings.
subject := api.SettingsSubject{Site: true}
globalSettingsRaw, err := settingStore.GetLatest(ctx, subject)
if err != nil {
return nil, err
}
globalSettings, err := parseUserSettings(globalSettingsRaw)
return globalSettings.Insights, nil
}

func parseUserSettings(settings *api.Settings) (*schema.Settings, error) {
if settings == nil {
// Settings have never been saved for this subject; equivalent to `{}`.
return &schema.Settings{}, nil
}
var v schema.Settings
if err := jsonc.Unmarshal(settings.Contents, &v); err != nil {
return nil, err
}
return &v, nil
}
155 changes: 155 additions & 0 deletions enterprise/internal/insights/discovery/discovery_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package discovery

import (
"context"
"testing"

"github.com/hexops/autogold"

"github.com/sourcegraph/sourcegraph/internal/api"
"github.com/sourcegraph/sourcegraph/schema"
)

var settingsExample = &api.Settings{ID: 1, Contents: `{
"insights": [
{
"title": "fmt usage",
"description": "fmt.Errorf/fmt.Printf usage",
"series": [
{
"label": "fmt.Errorf",
"search": "errorf",
},
{
"label": "printf",
"search": "fmt.Printf",
}
]
},
{
"title": "gitserver usage",
"description": "gitserver exec & close usage",
"series": [
{
"label": "exec",
"search": "gitserver.Exec",
},
{
"label": "close",
"search": "gitserver.Close",
}
]
}
]
}
`}

func TestDiscover(t *testing.T) {
settingStore := NewMockSettingStore()
settingStore.GetLatestFunc.SetDefaultHook(func(ctx context.Context, subject api.SettingsSubject) (*api.Settings, error) {
if !subject.Site { // TODO: future: site is an extremely poor name for "global settings", we should change this.
t.Fatal("expected only to request settings from global user settings")
}
return settingsExample, nil
})
ctx := context.Background()
insights, err := Discover(ctx, settingStore)
if err != nil {
t.Fatal(err)
}
autogold.Want("insights", []*schema.Insight{
{
Description: "fmt.Errorf/fmt.Printf usage",
Series: []*schema.InsightSeries{
{
Label: "fmt.Errorf",
Search: "errorf",
},
{
Label: "printf",
Search: "fmt.Printf",
},
},
Title: "fmt usage",
},
{
Description: "gitserver exec & close usage",
Series: []*schema.InsightSeries{
{
Label: "exec",
Search: "gitserver.Exec",
},
{
Label: "close",
Search: "gitserver.Close",
},
},
Title: "gitserver usage",
},
}).Equal(t, insights)
}

func Test_parseUserSettings(t *testing.T) {
tests := []struct {
name string
input *api.Settings
want autogold.Value
}{
{
name: "nil",
input: nil,
want: autogold.Want("nil", [2]interface{}{&schema.Settings{}, nil}),
},
{
name: "empty",
input: &api.Settings{
Contents: "{}",
},
want: autogold.Want("empty", [2]interface{}{&schema.Settings{}, nil}),
},
{
name: "real",
input: settingsExample,
want: autogold.Want("real", [2]interface{}{
&schema.Settings{Insights: []*schema.Insight{
{
Description: "fmt.Errorf/fmt.Printf usage",
Series: []*schema.InsightSeries{
{
Label: "fmt.Errorf",
Search: "errorf",
},
{
Label: "printf",
Search: "fmt.Printf",
},
},
Title: "fmt usage",
},
{
Description: "gitserver exec & close usage",
Series: []*schema.InsightSeries{
{
Label: "exec",
Search: "gitserver.Exec",
},
{
Label: "close",
Search: "gitserver.Close",
},
},
Title: "gitserver usage",
},
}},
nil,
}),
},
}
for _, tst := range tests {
t.Run(tst.name, func(t *testing.T) {
got, err := parseUserSettings(tst.input)
tst.want.Equal(t, [2]interface{}{got, err})
})
}

}
4 changes: 4 additions & 0 deletions enterprise/internal/insights/discovery/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package discovery

//go:generate env GOBIN=$PWD/.bin GO111MODULE=on go install github.com/efritz/go-mockgen
//go:generate $PWD/.bin/go-mockgen -f github.com/sourcegraph/sourcegraph/enterprise/internal/insights/discovery -i SettingStore -o mock_setting_store.go
151 changes: 151 additions & 0 deletions enterprise/internal/insights/discovery/mock_setting_store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading