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 zhouxiaoliang committed Oct 30, 2024
2 parents 2006e1f + c69e64a commit 49ecb83
Show file tree
Hide file tree
Showing 23 changed files with 514 additions and 315 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# go lint tool dependencies `go list` `gofmt`
FROM golang:1.23.0-alpine3.20
# image from qiniu registry, origin: https://hub.docker.com/_/golang
FROM aslan-spock-register.qiniu.io/qa/golang:1.23.0-alpine3.20

# if you want to install other tools, please add them here.
# Do not install unnecessary tools to reduce image size.
Expand Down
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ This approach helps PR authors avoid searching for issues in lengthy console log
- [Configuration](#configuration)
- [Adjusting Execution Commands](#adjusting-execution-commands)
- [Disabling a Linter](#disabling-a-linter)
- [Cloning multiple repositories](#cloning-multiple-repositories)
- [Executing Linters via Docker](#executing-linters-via-docker)
- [Executing Linters via Kubernetes](#executing-linters-via-kubernetes)
- [Reviewbot Operational Flow](#reviewbot-operational-flow)
Expand Down Expand Up @@ -163,6 +164,22 @@ qbox/net-gslb:

This configuration means that the `golangci-lint` check is disabled for the `qbox/net-gslb` repository.

### Cloning multiple repositories

By default, Reviewbot clones the repository where the event occurs. However, in some scenarios, we might want to clone multiple repositories, and customizing the cloning path.

For example:

```yaml
qbox/net-gslb:
refs:
- org: "qbox"
repo: "net-gslb"
pathAlias: "src/qiniu.com/net-gslb"
- org: "qbox"
repo: "kodo"
```

### Executing Linters via Docker

By default, Reviewbot uses locally installed linters for checks. However, in some scenarios, we might want to use Docker images to execute linters, such as:
Expand Down
17 changes: 17 additions & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Reviewbot 帮助你快速搭建一个自托管的代码分析和代码审查服
- [配置](#配置)
- [调整执行命令](#调整执行命令)
- [关闭 Linter](#关闭-Linter)
- [克隆多个仓库](#克隆多个仓库)
- [通过 Docker 执行 linter](#通过-Docker-执行-linter)
- [在 Kubernetes 集群中执行 linter](#在-Kubernetes-集群中执行-linter)
- [Reviewbot 运行流程](#reviewbot-运行流程)
Expand Down Expand Up @@ -156,6 +157,22 @@ qbox/net-gslb:
enable: false
```

### 克隆多个仓库

默认情况下,`Reviewbot` 会克隆当前事件发生的仓库,但有些场景下,我们可能希望克隆多个仓库,甚至指定不同的路径别名。

比如:

```yaml
qbox/net-gslb:
refs:
- org: "qbox"
repo: "net-gslb"
pathAlias: "src/qiniu.com/net-gslb"
- org: "qbox"
repo: "kodo"
```

### 通过 Docker 执行 linter

默认情况下,`Reviewbot` 会使用本地安装的 linter 来执行检查,但有些场景下,我们可能希望使用 Docker 镜像来执行 linter,比如:
Expand Down
25 changes: 25 additions & 0 deletions config/.image/go1.20.14.gocilint.1.61.0.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# go lint tool dependencies `go list` `gofmt`
FROM golang:1.20.14-alpine3.19

# if you want to install other tools, please add them here.
# Do not install unnecessary tools to reduce image size.
RUN set -eux \
apk update && \
apk --no-cache add ca-certificates git openssh yarn libpcap-dev curl bash build-base

RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b /usr/local/bin v1.61.0

WORKDIR /

# SSH config
RUN mkdir -p /root/.ssh && chown -R root /root/.ssh/ && chgrp -R root /root/.ssh/ \
&& git config --global url."[email protected]:".insteadOf https://github.com/ \
&& git config --global url."git://".insteadOf https://
COPY deploy/config /root/.ssh/config
COPY deploy/github-known-hosts /github_known_hosts

# set go proxy and private repo
RUN go env -w GOPROXY=https://goproxy.cn,direct \
&& go env -w GOPRIVATE=github.com/qbox,qiniu.com

EXPOSE 8888
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ FROM golang:1.22.3-alpine3.20
# Do not install unnecessary tools to reduce image size.
RUN set -eux \
apk update && \
apk --no-cache add ca-certificates git openssh yarn libpcap-dev curl openjdk11 bash build-base && \
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b /usr/local/bin v1.59.1
apk --no-cache add ca-certificates git openssh yarn libpcap-dev curl openjdk11 bash build-base

RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b /usr/local/bin v1.59.1

WORKDIR /

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ FROM golang:1.23.2-alpine3.20
# Do not install unnecessary tools to reduce image size.
RUN set -eux \
apk update && \
apk --no-cache add ca-certificates git openssh yarn libpcap-dev curl openjdk11 bash build-base && \
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b /usr/local/bin v1.61.0
apk --no-cache add ca-certificates git openssh yarn libpcap-dev curl openjdk11 bash build-base

RUN curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b /usr/local/bin v1.61.0

WORKDIR /

Expand Down
94 changes: 86 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,21 @@ type Config struct {
// * "org/repo": {"extraRefs":{org:xxx, repo:xxx, path_alias:github.com/repo }, "golangci-lint": {"enable": true, "workDir": "", "command": "golangci-lint", "args": ["run", "--config", ".golangci.yml"], "reportFormat": "github_checks"}}
// * "org": {"extraRefs":{org:xxx, repo:xxx, path_alias:github.com/repo }, "golangci-lint": {"enable": true, "workDir": "", "command": "golangci-lint", "args": ["run", "--config", ".golangci.yml"], "reportFormat": "github_checks"}}
CustomConfig map[string]RepoConfig `json:"customConfig,omitempty"`

// IssueReferences is the issue references config.
// key is the linter name.
// value is the issue references config.
IssueReferences map[string][]IssueReference `json:"issueReferences,omitempty"`
// compiledIssueReferences is the compiled issue references config.
compiledIssueReferences map[string][]CompiledIssueReference
}

type RepoConfig struct {
// ExtraRefs are auxiliary repositories that
// need to be cloned, determined from config
ExtraRefs []Refs `json:"extraRefs,omitempty"`
Linters map[string]Linter `json:"linters,omitempty"`
// Refs are repositories that need to be cloned.
// The main repository is cloned by default and does not need to be specified here if not specified.
// extra refs must be specified.
Refs []Refs `json:"refs,omitempty"`
Linters map[string]Linter `json:"linters,omitempty"`
}

type Refs struct {
Expand Down Expand Up @@ -110,6 +118,19 @@ type KubernetesAsRunner struct {
// The destination directory will be created via mounting a emptyDir volume in the pod.
CopySSHKeyToPod string `json:"copySSHKeyToPod,omitempty"`
}

type IssueReference struct {
// Pattern is the regex pattern to match the issue message.
Pattern string `json:"pattern"`
// URL is the url of the issue reference.
URL string `json:"url"`
}

type CompiledIssueReference struct {
Pattern *regexp.Regexp
URL string
}

type Linter struct {
// Name is the linter name.
Name string
Expand Down Expand Up @@ -147,7 +168,7 @@ type Linter struct {
// Note:
// * github_check_run only support on Github Apps, not support on Github OAuth Apps or authenticated users.
GithubReportFormat GithubReportType `json:"githubReportType,omitempty"`
GitlabReportFormat GitlabReportType `json:"githubReportType,omitempty"`
GitlabReportFormat GitlabReportType `json:"gitlabReportType,omitempty"`

// ConfigPath is the path of the linter config file.
// If not empty, use the config to run the linter.
Expand All @@ -159,11 +180,14 @@ 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)
}

var (
ErrEmptyRepoOrOrg = errors.New("empty repo or org")
)

// NewConfig returns a new Config.
func NewConfig(conf string) (Config, error) {
var c Config
Expand All @@ -181,6 +205,12 @@ func NewConfig(conf string) (Config, error) {
if err := c.parseCloneURLs(); err != nil {
return c, err
}
if err := c.validateRefs(); err != nil {
return c, err
}
if err := c.parseIssueReferences(); err != nil {
return c, err
}

// set default value
if c.GlobalDefaultConfig.GithubReportType == "" {
Expand Down Expand Up @@ -266,6 +296,17 @@ func (c Config) GetLinterConfig(org, repo, ln string) Linter {
return linter
}

// GetCompiledIssueReferences returns the compiled issue references config for the given linter name.
func (c Config) GetCompiledIssueReferences(linterName string) []CompiledIssueReference {
if c.compiledIssueReferences == nil {
return nil
}
if refs, ok := c.compiledIssueReferences[linterName]; ok {
return refs
}
return nil
}

func applyCustomConfig(legacy, custom Linter) Linter {
if custom.Enable != nil {
legacy.Enable = custom.Enable
Expand Down Expand Up @@ -385,7 +426,7 @@ func (c *Config) parseCloneURLs() error {
re := regexp.MustCompile(`^(?:git@|https://)?([^:/]+)[:/]{1}(.*?)/(.*?)\.git$`)

for orgRepo, refConfig := range c.CustomConfig {
for k, ref := range refConfig.ExtraRefs {
for k, ref := range refConfig.Refs {
if ref.CloneURL == "" {
continue
}
Expand All @@ -399,8 +440,45 @@ func (c *Config) parseCloneURLs() error {
return nil
}

func (c *Config) validateRefs() error {
for orgRepo, refConfig := range c.CustomConfig {
for _, ref := range refConfig.Refs {
if ref.PathAlias != "" && (ref.Repo == "" || ref.Org == "") {
log.Errorf("invalid ref: %v for org/repo: %s", ref, orgRepo)
return ErrEmptyRepoOrOrg
}
}
}

return nil
}

func (c *Config) parseIssueReferences() error {
if c.IssueReferences == nil {
return nil
}

c.compiledIssueReferences = make(map[string][]CompiledIssueReference)

for linterName, issueReferences := range c.IssueReferences {
for _, ref := range issueReferences {
re, err := regexp.Compile(ref.Pattern)
if err != nil {
return err
}

c.compiledIssueReferences[linterName] = append(c.compiledIssueReferences[linterName], CompiledIssueReference{
Pattern: re,
URL: ref.URL,
})
}
}

return nil
}

func (c *Config) parseAndUpdateCloneURL(re *regexp.Regexp, orgRepo string, k int) error {
ref := &c.CustomConfig[orgRepo].ExtraRefs[k]
ref := &c.CustomConfig[orgRepo].Refs[k]
matches := re.FindStringSubmatch(ref.CloneURL)
if len(matches) != 4 {
log.Errorf("failed to parse CloneURL, please check the format of %s", ref.CloneURL)
Expand Down
57 changes: 42 additions & 15 deletions config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ globalDefaultConfig: # global default settings, will be overridden by qbox org a
golangcilintConfig: "config/linters-config/.golangci.yml" # golangci-lint config file to use
copySSHKeyToContainer: "/root/.ssh/id_rsa"


customConfig: # custom config for specific orgs or repos
goplus:
linters:
Expand All @@ -24,22 +23,22 @@ customConfig: # custom config for specific orgs or repos
enable: false

qbox/zrs:
extraRefs:
refs:
- org: qbox
repo: kodo
linters:
gomodcheck:
enable: false
qbox/kodoe-manager:
extraRefs:
refs:
- org: qbox
repo: kodo
qbox/configcenter:
extraRefs:
refs:
- org: qbox
repo: kodo
qbox/logverse:
extraRefs:
refs:
- org: qbox
repo: kodo

Expand All @@ -49,7 +48,7 @@ customConfig: # custom config for specific orgs or repos
enable: true
dockerAsRunner:
# kodo only use go1.22.+, see kodo/logservice/logupload/version/check_test.go
image: "aslan-spock-register.qiniu.io/reviewbot/base:golangci-lint.1.59.1"
image: "aslan-spock-register.qiniu.io/reviewbot/base:go1.22.3-gocilint.1.59.1"
command:
- "/bin/sh"
- "-c"
Expand All @@ -64,7 +63,7 @@ customConfig: # custom config for specific orgs or repos
golangci-lint run --timeout=10m0s --allow-parallel-runners=true --print-issued-lines=false --out-format=line-number >> $ARTIFACT/lint.log 2>&1
qbox/kodo-ops:
extraRefs:
refs:
- org: qbox
repo: kodo
linters:
Expand Down Expand Up @@ -112,12 +111,40 @@ customConfig: # custom config for specific orgs or repos
go mod tidy
CGO_ENABLED=1 golangci-lint run --timeout=10m0s --allow-parallel-runners=true --print-issued-lines=false --out-format=line-number >> $ARTIFACT/lint.log 2>&1
dockerAsRunner:
image: "aslan-spock-register.qiniu.io/reviewbot/base:golangci-lint.1.59.1"
image: "aslan-spock-register.qiniu.io/reviewbot/base:go1.22.3-gocilint.1.59.1"

qiniu/reviewbot:
linters:
golangci-lint:
enable: true
kubernetesAsRunner:
image: "aslan-spock-register.qiniu.io/reviewbot/base:go1.23.2-gocilint.1.61.0"
namespace: "reviewbot"

qbox/ffmpeg:
refs:
- org: qbox
repo: ffmpeg
pathAlias: "src/qbox.com/ffmpeg"

qbox/net-schedule:
linters:
golangci-lint:
dockerAsRunner:
image: "aslan-spock-register.qiniu.io/reviewbot/base:go1.20.14-gocilint.1.61.0"
qbox/net-pcdn:
refs:
- org: qbox
repo: kodo

# qiniu/reviewbot:
# linters:
# golangci-lint:
# enable: true
# kubernetesAsRunner:
# image: "aslan-spock-register.qiniu.io/reviewbot/base:golangci-lint.1.61.0"
# namespace: "reviewbot"
issueReferences:
golangci-lint:
- pattern: '^ST1003: .+ \(stylecheck\)$'
url: "https://github.com/qiniu/reviewbot/issues/398"
- pattern: '^do not define dynamic errors, use wrapped static errors instead:.*\(err113\)$'
url: "https://github.com/qiniu/reviewbot/issues/418"
- pattern: '^found a struct that contains a context.Context field \(containedctx\)$'
url: "https://github.com/qiniu/reviewbot/issues/420"
shellcheck:
- pattern: '^SC2086: .+ \(shellcheck\)$'
url: "https://github.com/qiniu/reviewbot/issues/413"
Loading

0 comments on commit 49ecb83

Please sign in to comment.