Skip to content

Commit

Permalink
fix main conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
never112 authored and never112 committed Nov 13, 2024
1 parent 4d3625c commit 52ec7e8
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 74 deletions.
41 changes: 24 additions & 17 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type Refs struct {
}

type GlobalConfig struct {
// GithubReportType is the format of the report, will be used if linterConfig.GithubReportFormat is empty.
// GithubReportType/GitlabReportType is the format of the report, will be used if linterConfig.ReportFormat is empty.
// e.g. "github_checks", "github_pr_review"
GithubReportType ReportType `json:"githubReportType,omitempty"`
GitlabReportType ReportType `json:"gitlabReportType,omitempty"`
Expand Down Expand Up @@ -173,8 +173,7 @@ type Linter struct {
// github_pr_review: https://developer.github.com/v3/pulls/reviews/#create-a-pull-request-review
// Note:
// * github_check_run only support on Github Apps, not support on Github OAuth Apps or authenticated users.
GithubReportFormat ReportType `json:"githubReportType,omitempty"`
GitlabReportFormat ReportType `json:"gitlabReportType,omitempty"`
ReportFormat ReportType `json:"githubReportType,omitempty"`

// ConfigPath is the path of the linter config file.
// If not empty, use the config to run the linter.
Expand All @@ -187,7 +186,7 @@ type Linter struct {
func (l Linter) String() string {
return fmt.Sprintf(
"Linter{Enable: %v, DockerAsRunner: %v, Workspace: %v, WorkDir: %v, Command: %v, Args: %v, ReportFormat: %v, ConfigPath: %v}",
*l.Enable, l.DockerAsRunner, l.Workspace, l.WorkDir, l.Command, l.Args, l.GithubReportFormat, l.ConfigPath)
*l.Enable, l.DockerAsRunner, l.Workspace, l.WorkDir, l.Command, l.Args, l.ReportFormat, l.ConfigPath)
}

var (
Expand Down Expand Up @@ -256,15 +255,19 @@ func NewConfig(conf string) (Config, error) {
return c, nil
}

func (c Config) GetLinterConfig(org, repo, ln string) Linter {
func (c Config) GetLinterConfig(org, repo, ln string, repotype RepoType) Linter {
linter := Linter{
Enable: boolPtr(true),
GithubReportFormat: c.GlobalDefaultConfig.GithubReportType,
GitlabReportFormat: c.GlobalDefaultConfig.GitlabReportType,
Modifier: NewBaseModifier(),
Name: ln,
Org: org,
Repo: repo,
Enable: boolPtr(true),
Modifier: NewBaseModifier(),
Name: ln,
Org: org,
Repo: repo,
}
if repotype == GitLab {
linter.ReportFormat = c.GlobalDefaultConfig.GithubReportType
}
if repotype == GitHub {
linter.ReportFormat = c.GlobalDefaultConfig.GithubReportType
}

// set golangci-lint config path if exists
Expand Down Expand Up @@ -332,11 +335,8 @@ func applyCustomConfig(legacy, custom Linter) Linter {
legacy.Args = custom.Args
}

if custom.GithubReportFormat != "" {
legacy.GithubReportFormat = custom.GithubReportFormat
}
if custom.GitlabReportFormat != "" {
legacy.GitlabReportFormat = custom.GitlabReportFormat
if custom.ReportFormat != "" {
legacy.ReportFormat = custom.ReportFormat
}

if custom.ConfigPath != "" {
Expand Down Expand Up @@ -392,6 +392,13 @@ const (
Quiet ReportType = "quiet"
)

type RepoType string

const (
GitLab RepoType = "gitlab"
GitHub RepoType = "github"
)

func boolPtr(b bool) *bool {
return &b
}
Expand Down
2 changes: 1 addition & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ customConfig: # custom config for specific orgs or repos

for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
got := c.GetLinterConfig(tc.org, tc.repo, tc.linter)
got := c.GetLinterConfig(tc.org, tc.repo, tc.linter, GitHub)
if *got.Enable != *tc.want.Enable {
t.Errorf("expected %v, got %v", *tc.want.Enable, *got.Enable)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/linters/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type Agent struct {
func (a *Agent) ApplyIssueReferences(ctx context.Context, lintResults map[string][]LinterOutput) {
log := lintersutil.FromContext(ctx)
var msgFormat string
format := a.LinterConfig.GithubReportFormat
format := a.LinterConfig.ReportFormat
switch format {
case config.GithubCheckRuns:
msgFormat = "%s\nmore info: %s"
Expand Down
2 changes: 1 addition & 1 deletion internal/linters/go/gofmt/gofmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func gofmtHandler(ctx context.Context, a linters.Agent) error {

// Since GitHub's check run feature does not have the suggestion functionality, GitHub PR review is fixed used to display gofmt reports.
// Details: https://github.com/qiniu/reviewbot/issues/166
a.LinterConfig.GithubReportFormat = config.GithubPRReview
a.LinterConfig.ReportFormat = config.GithubPRReview

executor, err := NewgofmtExecutor(a.LinterConfig.WorkDir)
if err != nil {
Expand Down
24 changes: 12 additions & 12 deletions internal/linters/linters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,10 @@ func TestExecRun(t *testing.T) {
id: "case1 - without ARTIFACT",
input: Agent{
LinterConfig: config.Linter{
Enable: &tp,
Command: []string{"/bin/bash", "-c", "--"},
Args: []string{"echo file:line:column:message"},
GithubReportFormat: config.Quiet,
Enable: &tp,
Command: []string{"/bin/bash", "-c", "--"},
Args: []string{"echo file:line:column:message"},
ReportFormat: config.Quiet,
},
},
output: []byte("file:line:column:message\n"),
Expand All @@ -202,10 +202,10 @@ func TestExecRun(t *testing.T) {
id: "case2 - with ARTIFACT",
input: Agent{
LinterConfig: config.Linter{
Enable: &tp,
Command: []string{"/bin/bash", "-c", "--"},
Args: []string{"echo file2:6:7:message >> $ARTIFACT/golangci-lint.log 2>&1"},
GithubReportFormat: config.Quiet,
Enable: &tp,
Command: []string{"/bin/bash", "-c", "--"},
Args: []string{"echo file2:6:7:message >> $ARTIFACT/golangci-lint.log 2>&1"},
ReportFormat: config.Quiet,
},
},
output: []byte("file2:6:7:message\n"),
Expand All @@ -215,10 +215,10 @@ func TestExecRun(t *testing.T) {
id: "case2 - with multi files under ARTIFACT",
input: Agent{
LinterConfig: config.Linter{
Enable: &tp,
Command: []string{"/bin/bash", "-c", "--"},
Args: []string{"echo file2:6:7:message >> $ARTIFACT/golangci-lint.log 2>&1 ;echo file3:6:7:message >> $ARTIFACT/golangci-lint.log 2>&1"},
GithubReportFormat: config.Quiet,
Enable: &tp,
Command: []string{"/bin/bash", "-c", "--"},
Args: []string{"echo file2:6:7:message >> $ARTIFACT/golangci-lint.log 2>&1 ;echo file3:6:7:message >> $ARTIFACT/golangci-lint.log 2>&1"},
ReportFormat: config.Quiet,
},
},
output: []byte("file2:6:7:message\nfile3:6:7:message\n"),
Expand Down
4 changes: 2 additions & 2 deletions internal/linters/providergithub.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func (g *GithubProvider) Report(ctx context.Context, a Agent, lintResults map[st
num := a.Provider.GetCodeReviewInfo().Number
orgRepo := fmt.Sprintf("%s/%s", org, repo)

switch a.LinterConfig.GithubReportFormat {
switch a.LinterConfig.ReportFormat {
case config.GithubCheckRuns:
ch, err := g.CreateGithubChecks(ctx, a, lintResults)
if err != nil {
Expand Down Expand Up @@ -319,7 +319,7 @@ func (g *GithubProvider) Report(ctx context.Context, a Agent, lintResults map[st
case config.Quiet:
return nil
default:
log.Errorf("unsupported report format: %v", a.LinterConfig.GithubReportFormat)
log.Errorf("unsupported report format: %v", a.LinterConfig.ReportFormat)
}

return nil
Expand Down
22 changes: 14 additions & 8 deletions internal/linters/providergitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,25 @@ func NewGitlabProvider(gitlabClient *gitlab.Client, gitClient gitv2.ClientFactor

func ReportFormMatCheck(gc *gitlab.Client, reportFormat config.ReportType) (reportType config.ReportType) {
// gitlab verion below 10.8 not support discussion resource api
// gitlab reportformart not config: version=>10.8: GitlabCommentAndDiscussion, version<10.8: GitlabComment
// gitlab reportformart config: version=>10.8: use config, version<10.8: GitlabComment
v, r, e := gc.Version.GetVersion()
if e != nil {
log.Fatalf("Failed to get version: %v,response is %v", e, r)
return config.Quiet
}
v1, _ := version.NewVersion(v.Version)
v2, _ := version.NewVersion("10.8")
if reportFormat != "" {
if v1.LessThan(v2) {
return config.GitlabComment
}
return reportFormat
}
if v1.LessThan(v2) {
return config.GitlabComment
}
return reportFormat
return config.GitlabCommentAndDiscussion
}

func (g *GitlabProvider) HandleComments(ctx context.Context, outputs map[string][]LinterOutput) error {
Expand All @@ -222,7 +230,7 @@ func (g *GitlabProvider) Report(ctx context.Context, a Agent, lintResults map[st
repo := a.Provider.GetCodeReviewInfo().Repo
num := a.Provider.GetCodeReviewInfo().Number
orgRepo := fmt.Sprintf("%s/%s", org, repo)
reportformat := ReportFormMatCheck(g.GitLabClient, a.LinterConfig.GitlabReportFormat)
reportformat := ReportFormMatCheck(g.GitLabClient, a.LinterConfig.ReportFormat)
switch reportformat {

Check warning on line 234 in internal/linters/providergitlab.go

View check run for this annotation

qiniu-x / golangci-lint

internal/linters/providergitlab.go#L234

missing cases in switch of type config.ReportType: config.GithubCheckRuns, config.GithubPRReview, config.GithubMixType (exhaustive)
case config.GitlabCommentAndDiscussion:
// list MR comments
Expand Down Expand Up @@ -317,7 +325,7 @@ func (g *GitlabProvider) Report(ctx context.Context, a Agent, lintResults map[st
case config.Quiet:
return nil
default:
log.Errorf("unsupported report format: %v", a.LinterConfig.GitlabReportFormat)
log.Errorf("unsupported report format: %v", a.LinterConfig.ReportFormat)
}
return nil
}
Expand All @@ -335,9 +343,8 @@ func CreateGitLabCommentsReport(ctx context.Context, gc *gitlab.Client, outputs
var errormessage string
var tabletop10 string
tabletop10 = "\n| FilePath | Line |ErrorMessage |\n"
tabletop10 = tabletop10 + "| -------- | -------- | -------- |\n"
var top10 int64
top10 = 0
tabletop10 += "| -------- | -------- | -------- |\n"
var top10 = 0
var totalerrorscount int
totalerrorscount = 0
// for combine the linter result
Expand All @@ -349,9 +356,8 @@ func CreateGitLabCommentsReport(ctx context.Context, gc *gitlab.Client, outputs
if top10 < 10 {
tabletop10 += "|" + outputmessage.File + "|" + strconv.Itoa(outputmessage.Line) + "|" + outputmessage.Message + "|\n"
}
top10 += 1
top10++
}

}
message = fmt.Sprintf("[**%s**] check failed❌ , %v files exist errors,%v errors found. This is [the detailed log](%s).<br>Top 10 errors as below:\r\n"+tabletop10+"\n%s", lintername, len(outputs), totalerrorscount, logurl, comentDetailHeader+errormessage+"<br>"+commentDetail)
} else {
Expand Down
7 changes: 1 addition & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,7 @@ func main() {
opt := gitv2.ClientFactoryOpts{
CacheDirBase: github.String(o.codeCacheDir),
Persist: github.Bool(true),
UseSSH: github.Bool(false),
Host: o.gitLabHost,
Username: func() (string, error) { return "oauth2", nil },
Token: func(v string) (string, error) {
return o.gitLabAccessToken, nil
},
UseSSH: github.Bool(true),
}
v2, err := gitv2.NewClientFactory(opt.Apply)
if err != nil {
Expand Down
56 changes: 30 additions & 26 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,20 +465,37 @@ func (s *Server) gitlabHandle(ctx context.Context, event *gitlab.MergeEvent) err
}
}()

r, err := s.gitClientFactory.ClientForWithRepoOpts(org, repo, gitv2.RepoOpts{
opt := gitv2.ClientFactoryOpts{
CacheDirBase: github.String(s.repoCacheDir),
Persist: github.Bool(true),
UseSSH: github.Bool(false),
Host: s.gitLabHost,
Username: func() (string, error) { return "oauth2", nil },
Token: func(v string) (string, error) {
return s.gitLabAccessToken, nil
},
}
v2, errapp := gitv2.NewClientFactory(opt.Apply)
s.gitClientFactory = v2

r, errclone := s.gitClientFactory.ClientForWithRepoOpts(org, repo, gitv2.RepoOpts{
CopyTo: defaultWorkDir + "/" + repo,
})
if err != nil {
if errapp != nil {
log.Errorf("failed to create git client: %v", err)
return err
return errapp
}
if err := r.Checkout(latestCommit); err != nil {
log.Errorf("failed to checkout pull request %d: %v", num, err)
return err
if errclone != nil {
log.Errorf("failed to clone code repo : %v", err)
return errclone
}
if errcheckout := r.Checkout(latestCommit); err != nil {
log.Errorf("failed to checkout pull request %d: %v", num, errcheckout)
return errcheckout
}
gitModulesFile := path.Join(r.Directory(), ".gitmodules")
_, err = os.Stat(gitModulesFile)
if err == nil {
_, errpro := os.Stat(gitModulesFile)
if errpro == nil {
log.Info("git pull submodule in progress")
cmd := exec.Command("git", "submodule", "update", "--init", "--recursive")
cmd.Dir = r.Directory()
Expand All @@ -490,9 +507,9 @@ func (s *Server) gitlabHandle(ctx context.Context, event *gitlab.MergeEvent) err
} else {
log.Infof("no .gitmodules file in repo %s", repo)
}

for name, fn := range linters.TotalPullRequestHandlers() {

Check warning on line 510 in server.go

View check run for this annotation

qiniu-x / golangci-lint

server.go#L510

empty-lines: extra empty line at the start of a block (revive)
linterConfig := s.config.GetLinterConfig(org, repo, name)

linterConfig := s.config.GetLinterConfig(org, repo, name, config.GitLab)

// skip linter if it is disabled
if linterConfig.Enable != nil && !*linterConfig.Enable {
Expand All @@ -506,21 +523,8 @@ func (s *Server) gitlabHandle(ctx context.Context, event *gitlab.MergeEvent) err
}

log.Infof("[%s] config on repo %v: %+v", name, orgRepo, linterConfig)
opt := gitv2.ClientFactoryOpts{
CacheDirBase: github.String(s.repoCacheDir),
Persist: github.Bool(true),
UseSSH: github.Bool(false),
Host: s.gitLabHost,
Username: func() (string, error) { return "oauth2", nil },
Token: func(v string) (string, error) {
return s.gitLabAccessToken, nil
},
}
v2new, err := gitv2.NewClientFactory(opt.Apply)
if err != nil {
log.Fatalf("failed to create git client factory: %v", err)
}
gitlabProvider, err := linters.NewGitlabProvider(s.GitLabClient(), v2new, mergeRequestAffectedFiles, *event)

gitlabProvider, err := linters.NewGitlabProvider(s.GitLabClient(), v2, mergeRequestAffectedFiles, *event)

if err != nil {
log.Errorf("failed to create provider: %v", err)
Expand Down Expand Up @@ -634,7 +638,7 @@ func (s *Server) handle(ctx context.Context, event *github.PullRequestEvent) err
}()

for name, fn := range linters.TotalPullRequestHandlers() {
linterConfig := s.config.GetLinterConfig(org, repo, name)
linterConfig := s.config.GetLinterConfig(org, repo, name, config.GitHub)
linterConfig.Number = num
linterConfig.Workspace = workspace
// skip linter if it is disabled
Expand Down

0 comments on commit 52ec7e8

Please sign in to comment.