Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: log with event id during lint process #18

Merged
merged 1 commit into from
Jan 20, 2024
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
11 changes: 6 additions & 5 deletions internal/linters/git-flow/rebase-suggestion/rebase_suggestion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
Expand All @@ -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]**"
Expand All @@ -74,7 +75,7 @@ If you have any questions about this comment, feel free to raise an issue here:
</details>
`

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)
Expand Down
29 changes: 17 additions & 12 deletions internal/linters/go/staticcheck/staticcheck.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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"
Expand All @@ -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
}

Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down
17 changes: 9 additions & 8 deletions internal/linters/linters.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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"
)

Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down