Skip to content

Commit

Permalink
chore: align common practice
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlJi committed Nov 20, 2024
1 parent 9473eab commit 4d425a5
Show file tree
Hide file tree
Showing 3 changed files with 350 additions and 458 deletions.
33 changes: 26 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,17 @@ type Config struct {
// compiledIssueReferences is the compiled issue references config.
compiledIssueReferences map[string][]CompiledIssueReference

CustomLinters map[string]CustomLinters `json:"customLinters,omitempty"`
// CustomLinters is the custom linters config.
// key is the linter name.
// value is the custom linter config.
// the custom linter config will be used as the default config for the linter.
// it can be overridden by CustomRepos linter config for the specific repo.
CustomLinters map[string]CustomLinter `json:"customLinters,omitempty"`
}

type CustomLinters struct {
type CustomLinter struct {
Linter
// Languages is the languages of the linter.
Languages []string `json:"languages,omitempty"`
}

Expand Down Expand Up @@ -200,6 +206,7 @@ var (
ErrEmptyRepoOrOrg = errors.New("empty repo or org")
ErrIssueReferenceMustInReviewbotRepo = errors.New("issue reference must in reviewbot repo")
ErrInvalidIssueNumber = errors.New("invalid issue number")
ErrCustomLinterConfig = errors.New("custom linter must specify at least one language")
)

// NewConfig returns a new Config.
Expand All @@ -215,7 +222,9 @@ func NewConfig(conf string) (Config, error) {
}

// ============ validate and update the config ============

if err := c.validateCustomLinters(); err != nil {
return c, err
}
if err := c.parseCloneURLs(); err != nil {
return c, err
}
Expand Down Expand Up @@ -262,18 +271,18 @@ func NewConfig(conf string) (Config, error) {
return c, nil
}

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

Expand Down Expand Up @@ -390,7 +399,7 @@ func applyCustomConfig(legacy Linter, custom Linter) Linter {
return legacy
}

func applyCustomLintersConfig(legacy Linter, custom CustomLinters) Linter {
func applyCustomLintersConfig(legacy Linter, custom CustomLinter) Linter {
// Convert CustomizedExtraLinter to Linter for reuse
tempLinter := Linter{
Command: custom.Command,
Expand Down Expand Up @@ -554,3 +563,13 @@ func (c *Config) parseAndUpdateCloneURL(re *regexp.Regexp, orgRepo string, k int

return nil
}

func (c Config) validateCustomLinters() error {
for name, linter := range c.CustomLinters {
if len(linter.Languages) == 0 {
log.Errorf("custom linter %s must specify at least one language", name)
return ErrCustomLinterConfig
}
}
return nil
}
17 changes: 10 additions & 7 deletions internal/linters/providergitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,16 @@ func NewGitlabProvider(gitlabClient *gitlab.Client, gitClient gitv2.ClientFactor
}, nil
}

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
func reportFormatMatCheck(gc *gitlab.Client, reportFormat config.ReportType) (reportType config.ReportType) {
// gitlab version below 10.8 not support discussion resource api.
// see https://gitlab.com/gitlab-org/gitlab-foss/-/blob/v10.8.7/CHANGELOG.md
// gitlab reportFormat not config:
// version=>10.8: GitlabCommentAndDiscussion, version<10.8: GitlabComment
// gitlab reportFormat 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)
log.Errorf("Failed to get version: %v,response is %v", e, r)
return config.Quiet
}
v1, _ := version.NewVersion(v.Version)
Expand Down Expand Up @@ -231,8 +234,8 @@ 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.ReportType)
switch reportformat {
reportFormat := reportFormatMatCheck(g.GitLabClient, a.LinterConfig.ReportType)
switch reportFormat {
case config.GitlabCommentAndDiscussion:
// list MR comments
var pid = g.MergeRequestEvent.ObjectAttributes.TargetProjectID
Expand Down
Loading

0 comments on commit 4d425a5

Please sign in to comment.