Skip to content

Commit 1289cd6

Browse files
authored
Merge pull request #31 from carlosms/lookout-sdk-v0.6.1
Update lookout-sdk to v0.6.1
2 parents 4f952a1 + 686935c commit 1289cd6

File tree

34 files changed

+1664
-167
lines changed

34 files changed

+1664
-167
lines changed

Gopkg.lock

Lines changed: 11 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939

4040
[[constraint]]
4141
name = "gopkg.in/src-d/lookout-sdk.v0"
42-
version = "0.3.0"
42+
version = "0.6.1"
4343

4444
[[constraint]]
4545
name = "github.com/stretchr/testify"

analyzer.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,31 @@ type Analyzer struct {
2828
var _ pb.AnalyzerServer = &Analyzer{}
2929

3030
// function to convert pb.types.Value to string argument
31-
type argumentConstructor func(v *types.Value) string
31+
type argumentConstructor func(logger log.Logger, v *types.Value) string
3232

3333
// map of linters with options and argument constructors
3434
var lintersOptions = map[string]map[string]argumentConstructor{
3535
"lll": map[string]argumentConstructor{
36-
"maxLen": func(v *types.Value) string {
36+
"maxLen": func(logger log.Logger, v *types.Value) string {
3737
var number int
3838

3939
switch v.GetKind().(type) {
4040
case *types.Value_StringValue:
4141
n, err := strconv.Atoi(v.GetStringValue())
4242
if err != nil {
43-
log.Warningf("wrong type for lll:maxLen argument")
43+
logger.Warningf("wrong type for lll:maxLen argument")
4444
return ""
4545
}
4646
number = n
4747
case *types.Value_NumberValue:
4848
intpart, frac := math.Modf(v.GetNumberValue())
4949
if frac != 0 {
50-
log.Warningf("wrong type for lll:maxLen argument")
50+
logger.Warningf("wrong type for lll:maxLen argument")
5151
return ""
5252
}
5353
number = int(intpart)
5454
default:
55-
log.Warningf("wrong type for lll:maxLen argument")
55+
logger.Warningf("wrong type for lll:maxLen argument")
5656
return ""
5757
}
5858

@@ -67,6 +67,9 @@ var lintersOptions = map[string]map[string]argumentConstructor{
6767

6868
func (a *Analyzer) NotifyReviewEvent(ctx context.Context, e *pb.ReviewEvent) (
6969
*pb.EventResponse, error) {
70+
71+
logger := log.With(log.Fields(pb.GetLogFields(ctx)))
72+
7073
changes, err := a.DataClient.GetChanges(ctx, &pb.ChangesRequest{
7174
Head: &e.Head,
7275
Base: &e.Base,
@@ -76,17 +79,17 @@ func (a *Analyzer) NotifyReviewEvent(ctx context.Context, e *pb.ReviewEvent) (
7679
IncludeLanguages: []string{"go"},
7780
})
7881
if err != nil {
79-
log.Errorf(err, "failed to GetChanges from a DataService")
82+
logger.Errorf(err, "failed to GetChanges from a DataService")
8083
return nil, err
8184
}
8285

8386
tmp, err := ioutil.TempDir("", "gometalint")
8487
if err != nil {
85-
log.Errorf(err, "cannot create tmp dir in %s", os.TempDir())
88+
logger.Errorf(err, "cannot create tmp dir in %s", os.TempDir())
8689
return nil, err
8790
}
8891
defer os.RemoveAll(tmp)
89-
log.Debugf("Saving files to '%s'", tmp)
92+
logger.Debugf("Saving files to '%s'", tmp)
9093

9194
found, saved := 0, 0
9295
for {
@@ -96,7 +99,7 @@ func (a *Analyzer) NotifyReviewEvent(ctx context.Context, e *pb.ReviewEvent) (
9699
}
97100

98101
if err != nil {
99-
log.Errorf(err, "failed to get a file from DataServer")
102+
logger.Errorf(err, "failed to get a file from DataServer")
100103
continue
101104
}
102105

@@ -106,23 +109,23 @@ func (a *Analyzer) NotifyReviewEvent(ctx context.Context, e *pb.ReviewEvent) (
106109

107110
file := change.Head
108111
if err = saveTo(file, tmp); err != nil {
109-
log.Errorf(err, "failed to write file %q", file.Path)
112+
logger.Errorf(err, "failed to write file %q", file.Path)
110113
} else {
111114
saved++
112115
}
113116
found++
114117
}
115118

116119
if saved < found {
117-
log.Warningf("%d/%d Golang files saved. analyzer won't run on non-saved ones", saved, found)
120+
logger.Warningf("%d/%d Golang files saved. analyzer won't run on non-saved ones", saved, found)
118121
}
119122
if saved == 0 {
120-
log.Debugf("no Golang files to work on. skip running gometalinter")
123+
logger.Debugf("no Golang files to work on. skip running gometalinter")
121124
return &pb.EventResponse{AnalyzerVersion: a.Version}, nil
122125
}
123-
log.Debugf("%d Golang files to work on. running gometalinter", saved)
126+
logger.Debugf("%d Golang files to work on. running gometalinter", saved)
124127

125-
withArgs := append(append(a.Args, tmp), a.linterArguments(e.Configuration)...)
128+
withArgs := append(append(a.Args, tmp), a.linterArguments(logger, e.Configuration)...)
126129
comments := RunGometalinter(withArgs)
127130
var allComments []*pb.Comment
128131
for _, comment := range comments {
@@ -134,10 +137,10 @@ func (a *Analyzer) NotifyReviewEvent(ctx context.Context, e *pb.ReviewEvent) (
134137
Text: origPathText,
135138
}
136139
allComments = append(allComments, &newComment)
137-
log.Debugf("Get comment %v", newComment)
140+
logger.Debugf("Get comment %v", newComment)
138141
}
139142

140-
log.Infof("%d comments created", len(allComments))
143+
logger.Infof("%d comments created", len(allComments))
141144
return &pb.EventResponse{
142145
AnalyzerVersion: a.Version,
143146
Comments: allComments,
@@ -189,7 +192,7 @@ func (a *Analyzer) NotifyPushEvent(ctx context.Context, e *pb.PushEvent) (*pb.Ev
189192
return &pb.EventResponse{}, nil
190193
}
191194

192-
func (a *Analyzer) linterArguments(s types.Struct) []string {
195+
func (a *Analyzer) linterArguments(logger log.Logger, s types.Struct) []string {
193196
config := s.GetFields()
194197
if config == nil {
195198
return nil
@@ -232,7 +235,7 @@ func (a *Analyzer) linterArguments(s types.Struct) []string {
232235
}
233236

234237
if !correctLinter {
235-
log.Warningf("unknown linter %s", name)
238+
logger.Warningf("unknown linter %s", name)
236239
continue
237240
}
238241

@@ -243,7 +246,7 @@ func (a *Analyzer) linterArguments(s types.Struct) []string {
243246
continue
244247
}
245248

246-
arg := linterOpts[optionName](optV)
249+
arg := linterOpts[optionName](logger, optV)
247250
if arg != "" {
248251
args = append(args, arg)
249252
}

analyzer_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@ import (
66
types "github.com/gogo/protobuf/types"
77
"github.com/stretchr/testify/assert"
88
"github.com/stretchr/testify/require"
9+
log "gopkg.in/src-d/go-log.v1"
910
"gopkg.in/src-d/lookout-sdk.v0/pb"
1011
)
1112

13+
var logger = log.New(nil)
14+
1215
func TestArgsEmpty(t *testing.T) {
1316
require := require.New(t)
1417

@@ -52,13 +55,13 @@ func TestArgsEmpty(t *testing.T) {
5255

5356
a := Analyzer{}
5457
for i, input := range inputs {
55-
require.Len(a.linterArguments(input), 0, "test case %d; input: %+v", i, input)
58+
require.Len(a.linterArguments(logger, input), 0, "test case %d; input: %+v", i, input)
5659
}
5760
}
5861

5962
func TestArgsCorrect(t *testing.T) {
6063
a := Analyzer{}
61-
require.Equal(t, []string{"--line-length=120"}, a.linterArguments(*pb.ToStruct(map[string]interface{}{
64+
require.Equal(t, []string{"--line-length=120"}, a.linterArguments(logger, *pb.ToStruct(map[string]interface{}{
6265
"linters": []map[string]interface{}{
6366
{
6467
"name": "lll",
@@ -67,7 +70,7 @@ func TestArgsCorrect(t *testing.T) {
6770
},
6871
})))
6972

70-
require.Equal(t, []string{"--line-length=120"}, a.linterArguments(*pb.ToStruct(map[string]interface{}{
73+
require.Equal(t, []string{"--line-length=120"}, a.linterArguments(logger, *pb.ToStruct(map[string]interface{}{
7174
"linters": []map[string]interface{}{
7275
{
7376
"name": "lll",

cmd/gometalint-analyzer/main.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,19 @@ func main() {
6060
return
6161
}
6262

63-
conn, err := pb.DialContext(
63+
logFn := func(fields pb.Fields, format string, args ...interface{}) {
64+
log.With(log.Fields(fields)).Debugf(format, args...)
65+
}
66+
67+
conn, err := pb.DialContextWithInterceptors(
6468
context.Background(),
6569
grpcAddr,
66-
grpc.WithInsecure(),
70+
[]grpc.StreamClientInterceptor{
71+
pb.LogStreamClientInterceptor(logFn),
72+
},
73+
[]grpc.UnaryClientInterceptor{
74+
pb.LogUnaryClientInterceptor(logFn),
75+
},
6776
grpc.WithDefaultCallOptions(grpc.FailFast(false)),
6877
)
6978
if err != nil {
@@ -77,7 +86,14 @@ func main() {
7786
Args: append([]string(nil), os.Args[1:]...),
7887
}
7988

80-
server := grpc.NewServer()
89+
server := pb.NewServerWithInterceptors(
90+
[]grpc.StreamServerInterceptor{
91+
pb.LogStreamServerInterceptor(logFn),
92+
},
93+
[]grpc.UnaryServerInterceptor{
94+
pb.LogUnaryServerInterceptor(logFn),
95+
},
96+
)
8197
pb.RegisterAnalyzerServer(server, analyzer)
8298

8399
analyzerURL := fmt.Sprintf("ipv4://%s:%d", conf.Host, conf.Port)

0 commit comments

Comments
 (0)