diff --git a/internal/linters/git-flow/rebase-suggestion/rebase_suggestion.go b/internal/linters/git-flow/rebase-suggestion/rebase_suggestion.go index 9a961f40..f83cbf04 100644 --- a/internal/linters/git-flow/rebase-suggestion/rebase_suggestion.go +++ b/internal/linters/git-flow/rebase-suggestion/rebase_suggestion.go @@ -24,10 +24,11 @@ import ( "strings" "text/template" - "github.com/reviewbot/config" - "github.com/reviewbot/internal/linters" "github.com/google/go-github/v57/github" "github.com/qiniu/x/log" + "github.com/qiniu/x/xlog" + "github.com/reviewbot/config" + "github.com/reviewbot/internal/linters" ) var lintName = "rebase-suggestion" @@ -36,7 +37,7 @@ func init() { linters.RegisterCommentHandler(lintName, rebaseSuggestionHandler) } -func rebaseSuggestionHandler(linterConfig config.Linter, agent linters.Agent, event github.PullRequestEvent) error { +func rebaseSuggestionHandler(log *xlog.Logger, linterConfig config.Linter, agent linters.Agent, event github.PullRequestEvent) error { var ( org = event.GetRepo().GetOwner().GetLogin() repo = event.GetRepo().GetName() @@ -53,7 +54,7 @@ func rebaseSuggestionHandler(linterConfig config.Linter, agent linters.Agent, ev return err } - return handle(context.Background(), agent, org, repo, number, preFilterCommits, existedComments) + return handle(context.Background(), log, agent, org, repo, number, preFilterCommits, existedComments) } var rebaseSuggestionFlag = "**[REBASE SUGGESTION]**" @@ -74,7 +75,7 @@ If you have any questions about this comment, feel free to raise an issue here: ` -func handle(ctx context.Context, agent linters.Agent, org, repo string, number int, prefilterCommits []*github.RepositoryCommit, existedComments []*github.IssueComment) error { +func handle(ctx context.Context, log *xlog.Logger, agent linters.Agent, org, repo string, number int, prefilterCommits []*github.RepositoryCommit, existedComments []*github.IssueComment) error { var commitMessages []string for _, commit := range prefilterCommits { commitMessages = append(commitMessages, *commit.Commit.Message) diff --git a/internal/linters/go/staticcheck/staticcheck.go b/internal/linters/go/staticcheck/staticcheck.go index aa3665ed..95a986f0 100644 --- a/internal/linters/go/staticcheck/staticcheck.go +++ b/internal/linters/go/staticcheck/staticcheck.go @@ -1,12 +1,12 @@ /* Copyright 2024 Qiniu Cloud (qiniu.com). - + Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - + http://www.apache.org/licenses/LICENSE-2.0 - + Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -23,10 +23,11 @@ import ( "strconv" "strings" - "github.com/reviewbot/config" - "github.com/reviewbot/internal/linters" "github.com/google/go-github/v57/github" "github.com/qiniu/x/log" + "github.com/qiniu/x/xlog" + "github.com/reviewbot/config" + "github.com/reviewbot/internal/linters" ) var lintName = "staticcheck" @@ -35,19 +36,22 @@ func init() { linters.RegisterCodeReviewHandler(lintName, staticcheckHandler) } -func staticcheckHandler(linterConfig config.Linter, agent linters.Agent, event github.PullRequestEvent) (map[string][]linters.LinterOutput, error) { +func staticcheckHandler(log *xlog.Logger, linterConfig config.Linter, agent linters.Agent, event github.PullRequestEvent) (map[string][]linters.LinterOutput, error) { executor, err := NewStaticcheckExecutor(linterConfig.WorkDir) if err != nil { + log.Errorf("init staticcheck executor failed: %v", err) return nil, err } - output, err := executor.Run(linterConfig.Args...) + output, err := executor.Run(log, linterConfig.Args...) if err != nil { + log.Errorf("staticcheck run failed: %v", err) return nil, err } - parsedOutput, err := executor.Parse(output) + parsedOutput, err := executor.Parse(log, output) if err != nil { + log.Errorf("staticcheck parse output failed: %v", err) return nil, err } @@ -67,6 +71,7 @@ type Staticcheck struct { // NewStaticcheckExecutor returns a new executor that knows how to execute staticcheck commands // TODO: with config func NewStaticcheckExecutor(dir string) (linters.Linter, error) { + log.Infof("staticcheck executor init") g, err := exec.LookPath("staticcheck") if err != nil { return nil, err @@ -82,7 +87,7 @@ func NewStaticcheckExecutor(dir string) (linters.Linter, error) { }, nil } -func (e *Staticcheck) Run(args ...string) ([]byte, error) { +func (e *Staticcheck) Run(log *xlog.Logger, args ...string) ([]byte, error) { b, err := e.execute(e.dir, e.staticcheck, args...) if err != nil { log.Errorf("staticcheck run with status: %v, mark and continue", err) @@ -93,8 +98,8 @@ func (e *Staticcheck) Run(args ...string) ([]byte, error) { return b, nil } -func (e *Staticcheck) Parse(output []byte) (map[string][]linters.LinterOutput, error) { - return formatStaticcheckOutput(output) +func (e *Staticcheck) Parse(log *xlog.Logger, output []byte) (map[string][]linters.LinterOutput, error) { + return formatStaticcheckOutput(log, output) } // formatStaticcheckOutput formats the output of staticcheck @@ -104,7 +109,7 @@ func (e *Staticcheck) Parse(output []byte) (map[string][]linters.LinterOutput, e // domain/repo/image.go:70:7: receiver name should be a reflection of its identity; don't use generic names such as "this" or "self" (ST1006) // // output: map[file][]linters.LinterOutput -func formatStaticcheckOutput(output []byte) (map[string][]linters.LinterOutput, error) { +func formatStaticcheckOutput(log *xlog.Logger, output []byte) (map[string][]linters.LinterOutput, error) { lines := strings.Split(string(output), "\n") var result = make(map[string][]linters.LinterOutput) diff --git a/internal/linters/linters.go b/internal/linters/linters.go index 421267e5..d4a16fee 100644 --- a/internal/linters/linters.go +++ b/internal/linters/linters.go @@ -1,12 +1,12 @@ /* Copyright 2024 Qiniu Cloud (qiniu.com). - + Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - + http://www.apache.org/licenses/LICENSE-2.0 - + Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,8 +17,9 @@ package linters import ( - "github.com/reviewbot/config" "github.com/google/go-github/v57/github" + "github.com/qiniu/x/xlog" + "github.com/reviewbot/config" gitv2 "k8s.io/test-infra/prow/git/v2" ) @@ -28,7 +29,7 @@ var ( ) // CommentHandlerFunc knows how to comment on a PR. -type CommentHandlerFunc func(config.Linter, Agent, github.PullRequestEvent) error +type CommentHandlerFunc func(*xlog.Logger, config.Linter, Agent, github.PullRequestEvent) error // RegisterCommentHandler registers a CommentHandlerFunc for the given linter name. func RegisterCommentHandler(name string, handler CommentHandlerFunc) { @@ -54,7 +55,7 @@ func TotalCommentHandlers() map[string]CommentHandlerFunc { } // CodeReviewHandlerFunc knows how to code review on a PR. -type CodeReviewHandlerFunc func(config.Linter, Agent, github.PullRequestEvent) (map[string][]LinterOutput, error) +type CodeReviewHandlerFunc func(*xlog.Logger, config.Linter, Agent, github.PullRequestEvent) (map[string][]LinterOutput, error) // RegisterCodeReviewHandler registers a CodeReviewHandlerFunc for the given linter name. func RegisterCodeReviewHandler(name string, handler CodeReviewHandlerFunc) { @@ -74,9 +75,9 @@ func TotalCodeReviewHandlers() map[string]CodeReviewHandlerFunc { // Linter knows how to execute linters. type Linter interface { // Run executes a linter command. - Run(args ...string) ([]byte, error) + Run(log *xlog.Logger, args ...string) ([]byte, error) // Parse parses the output of a linter command. - Parse(output []byte) (map[string][]LinterOutput, error) + Parse(log *xlog.Logger, output []byte) (map[string][]LinterOutput, error) } type LinterOutput struct { diff --git a/server.go b/server.go index c7e8932a..2bdd35b5 100644 --- a/server.go +++ b/server.go @@ -21,10 +21,10 @@ import ( "fmt" "net/http" - "github.com/reviewbot/config" - "github.com/reviewbot/internal/linters" "github.com/google/go-github/v57/github" "github.com/qiniu/x/xlog" + "github.com/reviewbot/config" + "github.com/reviewbot/internal/linters" gitv2 "k8s.io/test-infra/prow/git/v2" ) @@ -124,7 +124,7 @@ func (s *Server) handle(log *xlog.Logger, ctx context.Context, event *github.Pul log.Infof("running %s on repo %v with config %v", name, fmt.Sprintf("%s/%s", org, repo), lingerConfig) - lintResults, err := fn(lingerConfig, linters.Agent{}, *event) + lintResults, err := fn(log, lingerConfig, linters.Agent{}, *event) if err != nil { log.Errorf("failed to run linter: %v", err) return err @@ -161,7 +161,7 @@ func (s *Server) handle(log *xlog.Logger, ctx context.Context, event *github.Pul log.Infof("running %s on repo %v with config %v", name, fmt.Sprintf("%s/%s", org, repo), lingerConfig) agent := linters.NewAgent(s.gc, s.gitClientFactory, s.config) - if err := fn(lingerConfig, agent, *event); err != nil { + if err := fn(log, lingerConfig, agent, *event); err != nil { log.Errorf("failed to run linter: %v", err) return err }