Skip to content

Commit

Permalink
refactor to support gitlab version < 13
Browse files Browse the repository at this point in the history
  • Loading branch information
zc2638 committed May 31, 2022
1 parent 338929c commit d8dd88b
Show file tree
Hide file tree
Showing 22 changed files with 804 additions and 776 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

gitlab review-bot

## Preconditions
- Gitlab version 13+

## Quick start

### Config
Expand Down
57 changes: 27 additions & 30 deletions cmd/app/app.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
/*
Copyright © 2021 zc2638 <[email protected]>.
// Copyright © 2021 zc2638 <[email protected]>.
//
// 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.
// See the License for the specific language governing permissions and
// limitations under the License.

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.
See the License for the specific language governing permissions and
limitations under the License.
*/
package app

import (
Expand All @@ -35,27 +34,25 @@ func NewServerCommand() *cobra.Command {
Short: "review bot",
Long: `Review Bot.`,
SilenceUsage: true,
RunE: run,
RunE: func(_ *cobra.Command, _ []string) error {
cfg := global.Environ()
if err := server.ParseConfigWithEnv(cfgFile, cfg, global.EnvPrefix); err != nil {
return err
}
if err := global.InitCfg(cfg); err != nil {
return err
}
s := server.New(&cfg.Server)
s.Handler = handler.New()
fmt.Println("Listen on", s.Addr)
return s.Run(context.Background())
},
}

cfgFilePath := os.Getenv(global.EnvPrefix + "_CONFIG")
if cfgFilePath == "" {
cfgFilePath = "config/config.yaml"
}
cmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", cfgFilePath, "config file (default is $HOME/config.yaml)")
return cmd
}

func run(cmd *cobra.Command, args []string) error {
ctx := context.Background()
cfg, err := global.ParseConfig(cfgFile)
if err != nil {
return err
}
if err := global.InitCfg(cfg); err != nil {
return err
}
s := server.New(&cfg.Server)
s.Handler = handler.New()
fmt.Println("Listen on", s.Addr)
return s.Run(ctx)
}
27 changes: 13 additions & 14 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
/*
Copyright © 2021 zc2638 <[email protected]>.
// Copyright © 2021 zc2638 <[email protected]>.
//
// 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.
// See the License for the specific language governing permissions and
// limitations under the License.

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.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main

import (
Expand Down
63 changes: 13 additions & 50 deletions global/config.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
/*
Copyright © 2021 zc2638 <[email protected]>.
// Copyright © 2021 zc2638 <[email protected]>.
//
// 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.
// See the License for the specific language governing permissions and
// limitations under the License.

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.
See the License for the specific language governing permissions and
limitations under the License.
*/
package global

import (
"fmt"
"os"
"strings"

"github.com/mitchellh/go-homedir"
"github.com/mitchellh/mapstructure"
"github.com/pkgms/go/server"
"github.com/spf13/viper"

"github.com/zc2638/review-bot/pkg/scm"
)
Expand All @@ -48,32 +40,3 @@ func Environ() *Config {
cfg.SCM.Type = "gitlab"
return cfg
}

func ParseConfig(cfgPath string) (*Config, error) {
if cfgPath != "" {
viper.SetConfigFile(cfgPath)
} else {
home, err := homedir.Dir()
if err != nil {
return nil, err
}
viper.AddConfigPath(home)
viper.SetConfigName("config.yaml")
}
viper.SetEnvPrefix(EnvPrefix)
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
viper.AutomaticEnv()
cfg := Environ()
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(*os.PathError); ok {
fmt.Println("Warning: not find config file, use default.")
return cfg, nil
}
return nil, err
}
fmt.Println("Using config file:", viper.ConfigFileUsed())
err := viper.Unmarshal(cfg, func(dc *mapstructure.DecoderConfig) {
dc.TagName = "json"
})
return cfg, err
}
27 changes: 13 additions & 14 deletions global/init.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
/*
Copyright © 2021 zc2638 <[email protected]>.
// Copyright © 2021 zc2638 <[email protected]>.
//
// 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.
// See the License for the specific language governing permissions and
// limitations under the License.

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.
See the License for the specific language governing permissions and
limitations under the License.
*/
package global

import (
Expand Down
8 changes: 3 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,19 @@ go 1.16
require (
github.com/go-chi/chi v1.5.1
github.com/go-chi/cors v1.1.1
github.com/mitchellh/go-homedir v1.1.0
github.com/mitchellh/mapstructure v1.1.2
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pkg/errors v0.8.1
github.com/pkgms/go v0.0.0-20210922085647-58df1ef17e4f
github.com/pkgms/go v0.0.0-20220316065414-13c40cbbea1a
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.1.1
github.com/spf13/viper v1.7.1
github.com/xanzy/go-gitlab v0.54.3
github.com/zc2638/swag v1.1.4
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 // indirect
)

require (
github.com/99nil/go v0.0.0-20210924013233-ebd290da12d6
github.com/golang-jwt/jwt/v4 v4.3.0
github.com/golang/protobuf v1.5.2 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
Expand Down
9 changes: 6 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl
cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs=
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/99nil/go v0.0.0-20210924013233-ebd290da12d6 h1:tU1UpYa5ao+4ht21gk02VCP4EDzupUffWya3xIH/euQ=
github.com/99nil/go v0.0.0-20210924013233-ebd290da12d6/go.mod h1:J/4Ei0TEJTTxM8z9WbenTh6pIElunP1px7yV+61dgP4=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
Expand Down Expand Up @@ -246,8 +248,8 @@ github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkgms/go v0.0.0-20201028070800-899b81726496/go.mod h1:dGRY2Ndm0PkdyhUEsQlaJiI4NVkKSOS5he7UgaULvkE=
github.com/pkgms/go v0.0.0-20210922085647-58df1ef17e4f h1:29MXvUdS/eCRsh08mv+Dy6BDx1YirHdZ3WMyBGitQOQ=
github.com/pkgms/go v0.0.0-20210922085647-58df1ef17e4f/go.mod h1:LbHvgTtlRyyaOV6ax3tx/YB0DZdW8o+EVUeLYZhJlPY=
github.com/pkgms/go v0.0.0-20220316065414-13c40cbbea1a h1:ecwbopPjyxXrRDizIqaaH5S9lxu5YunO56Z+794qa/w=
github.com/pkgms/go v0.0.0-20220316065414-13c40cbbea1a/go.mod h1:LbHvgTtlRyyaOV6ax3tx/YB0DZdW8o+EVUeLYZhJlPY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
Expand Down Expand Up @@ -411,8 +413,9 @@ golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9 h1:SQFwaSi55rU7vdNs9Yr0Z324VNlrF+0wMqRXT4St8ck=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
4 changes: 3 additions & 1 deletion handler/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package handler
import (
"net/http"

"github.com/zc2638/review-bot/global"

"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/cors"
Expand All @@ -41,7 +43,7 @@ func New() http.Handler {
apiDoc.AddEndpointFunc(
home.Register,
)
mux.Post("/webhook", webhook.HandlerEvent())
mux.Post("/webhook", webhook.HandlerEvent(&global.Cfg().SCM, global.SCM()))

apiDoc.RegisterMuxWithData(mux, false)
return mux
Expand Down
29 changes: 13 additions & 16 deletions handler/home/doc.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
/*
Copyright © 2021 zc2638 <[email protected]>.
// Copyright © 2021 zc2638 <[email protected]>.
//
// 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.
// See the License for the specific language governing permissions and
// limitations under the License.

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.
See the License for the specific language governing permissions and
limitations under the License.
*/
package home

import (
Expand All @@ -22,8 +21,6 @@ import (
"github.com/zc2638/swag/swagger"
)

const tag = "home"

func Register(doc *swagger.API) {
doc.AddEndpoint(
endpoint.New(
Expand Down
17 changes: 14 additions & 3 deletions handler/home/home.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
/**
* Created by zc on 2021/1/15.
*/
// Copyright © 2021 zc2638 <[email protected]>.
//
// 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.
// See the License for the specific language governing permissions and
// limitations under the License.

package home

import (
Expand Down
Loading

0 comments on commit d8dd88b

Please sign in to comment.