diff --git a/.gitlab/merge_request_templates/Bugfix.md b/.gitlab/merge_request_templates/Bugfix.md new file mode 100644 index 0000000..83e09d7 --- /dev/null +++ b/.gitlab/merge_request_templates/Bugfix.md @@ -0,0 +1,16 @@ +功能分类: + +/kind bugfix + +提交内容的简短描述: + + +> + + +对于reviewer有没有需要特别注意的: + + +> + + \ No newline at end of file diff --git a/.gitlab/merge_request_templates/CICD.md b/.gitlab/merge_request_templates/CICD.md new file mode 100644 index 0000000..50ba76a --- /dev/null +++ b/.gitlab/merge_request_templates/CICD.md @@ -0,0 +1,16 @@ +功能分类: + +/kind ci + +提交内容的简短描述: + + +> + + +对于reviewer有没有需要特别注意的: + + +> + + \ No newline at end of file diff --git a/.gitlab/merge_request_templates/Cleanup.md b/.gitlab/merge_request_templates/Cleanup.md new file mode 100644 index 0000000..b7b3989 --- /dev/null +++ b/.gitlab/merge_request_templates/Cleanup.md @@ -0,0 +1,16 @@ +功能分类: + +/kind cleanup + +提交内容的简短描述: + + +> + + +对于reviewer有没有需要特别注意的: + + +> + + \ No newline at end of file diff --git a/.gitlab/merge_request_templates/Docs.md b/.gitlab/merge_request_templates/Docs.md new file mode 100644 index 0000000..96751e3 --- /dev/null +++ b/.gitlab/merge_request_templates/Docs.md @@ -0,0 +1,16 @@ +功能分类: + +/kind docs + +提交内容的简短描述: + + +> + + +对于reviewer有没有需要特别注意的: + + +> + + \ No newline at end of file diff --git a/.gitlab/merge_request_templates/Feature.md b/.gitlab/merge_request_templates/Feature.md new file mode 100644 index 0000000..1e6c9f2 --- /dev/null +++ b/.gitlab/merge_request_templates/Feature.md @@ -0,0 +1,16 @@ +功能分类: + +/kind feature + +提交内容的简短描述: + + +> + + +对于reviewer有没有需要特别注意的: + + +> + + \ No newline at end of file diff --git a/.gitlab/merge_request_templates/Merge.md b/.gitlab/merge_request_templates/Merge.md new file mode 100644 index 0000000..9bf4bd0 --- /dev/null +++ b/.gitlab/merge_request_templates/Merge.md @@ -0,0 +1,16 @@ +功能分类: + +/kind merge + +提交内容的简短描述: + + +> + + +对于reviewer有没有需要特别注意的: + + +> + + \ No newline at end of file diff --git a/.gitlab/merge_request_templates/Performance.md b/.gitlab/merge_request_templates/Performance.md new file mode 100644 index 0000000..952559a --- /dev/null +++ b/.gitlab/merge_request_templates/Performance.md @@ -0,0 +1,16 @@ +功能分类: + +/kind pref + +提交内容的简短描述: + + +> + + +对于reviewer有没有需要特别注意的: + + +> + + \ No newline at end of file diff --git a/.gitlab/merge_request_templates/Refactor.md b/.gitlab/merge_request_templates/Refactor.md new file mode 100644 index 0000000..9f9f0c4 --- /dev/null +++ b/.gitlab/merge_request_templates/Refactor.md @@ -0,0 +1,16 @@ +功能分类: + +/kind refactor + +提交内容的简短描述: + + +> + + +对于reviewer有没有需要特别注意的: + + +> + + \ No newline at end of file diff --git a/.gitlab/merge_request_templates/Style.md b/.gitlab/merge_request_templates/Style.md new file mode 100644 index 0000000..4ff5dbd --- /dev/null +++ b/.gitlab/merge_request_templates/Style.md @@ -0,0 +1,16 @@ +功能分类: + +/kind style + +提交内容的简短描述: + + +> + + +对于reviewer有没有需要特别注意的: + + +> + + \ No newline at end of file diff --git a/.gitlab/merge_request_templates/Test.md b/.gitlab/merge_request_templates/Test.md new file mode 100644 index 0000000..7b36e9e --- /dev/null +++ b/.gitlab/merge_request_templates/Test.md @@ -0,0 +1,16 @@ +功能分类: + +/kind test + +提交内容的简短描述: + + +> + + +对于reviewer有没有需要特别注意的: + + +> + + \ No newline at end of file diff --git a/.gitlab/review.yml b/.gitlab/review.yml new file mode 100644 index 0000000..d937799 --- /dev/null +++ b/.gitlab/review.yml @@ -0,0 +1,33 @@ +# can use /lgtm +reviewers: + - reviewer1 + - reviewer2 + +# can use /approve +approvers: + - approver1 + - approver2 + +# merge request settings +pullrequest: + # The merge information is mainly based on the title of PR + # otherwise it is mainly based on the content of in PR description template + squash_with_title: true + +# custom label settings +custom_labels: + # Operation instructions in comments + - order: /kind cleanup + # Label name associated with the instruction + name: kind/cleanup + # Automatically add prefix for merged submission information + short: cleanup + # Label background color + color: #33a3dc + # Label description + description: "kind: cleanup code" + + - order: /area scheduler + name: area/scheduler + color: #96582a + description: "area: scheduler service code area" \ No newline at end of file diff --git a/README.md b/README.md index fb2a1de..cb81aa2 100644 --- a/README.md +++ b/README.md @@ -4,34 +4,15 @@ gitlab review-bot -## Quick start +## Settings -### Config +### Step 1: Create Private Token -change config file in `config/config.yaml` +![](./docs/image/private-token.png) -``` -scm: - host: https://gitlab.com - token: - secret: -``` +### Step 2: Deploy Review Bot -| Configuration Item | Environment Variable | Description | -|:------------------:|:--------------------:|:------------------------------:| -| scm.host | BOT_SCM_HOST | source code management address | -| scm.token | BOT_SCM_TOKEN | private token | -| scm.secret | BOT_SCM_SECRET | webhook secret | - -### Run Local - -``` -go run github.com/zc2638/review-bot/cmd -c config/config.yaml -``` - -### Run Docker - -``` +```shell docker run -d -p 2640:2640 \ -e BOT_SCM_HOST=https://gitlab.com \ -e BOT_SCM_TOKEN= \ @@ -39,29 +20,33 @@ docker run -d -p 2640:2640 \ zc2638/review-bot:latest ``` -## Note +- `https://gitlab.com` replace with the actual gitlab server address +- `` replace with the token generated by step1 +- `` replace with a custom password for the webhook, which can be a random string -### Api Document -`GET /swagger-ui` +### Step 3: Generate Webhook Secret -### Generate Webhook Secret +1. Directly access the interface to request to obtain the secret key -`GET /secret?namespace=repo&name=test` +`GET /secret?namespace=zc&name=test` -### Pull Request Template +2. Visit Online API Doc `http:///swagger/ui` -- Download at url `GET /download?type=gitlab` -- Unzip and move the directory `gitlab` to `.gitlab` in your project -- Modify configuration file `review.yml` +![](./docs/image/webhook-secret.png) + +### Step 4: Add Project Webhook -### Setting +![](./docs/image/webhook.png) - add webhook to associated project, URL is `http:///webhook` - the `review-bot` user must have your project permissions - webhook must set sufficient permissions(e.g. `Comments`、`Confidential Comments`、`Pull request events`) -### Custom Config -the configuration file path is in `.gitlab/review.yml` +### Step 5: Add Project Config + +Please add the `.gitlab/review.yml` configuration file to the default branch of the project repository. +You can refer to the [`.gitlab` directory](./.gitlab) settings of this project. + ```yaml # can use /lgtm reviewers: @@ -72,12 +57,16 @@ reviewers: approvers: - approver1 - approver2 + +# merge request settings pullrequest: # The merge information is mainly based on the title of PR # otherwise it is mainly based on the content of in PR description template squash_with_title: true + +# custom label settings custom_labels: - # Operation instructions in comments + # Operation instructions in comments - order: /kind cleanup # Label name associated with the instruction name: kind/cleanup @@ -92,4 +81,51 @@ custom_labels: name: area/scheduler color: #96582a description: "area: scheduler service code area" -``` \ No newline at end of file +``` + +### Step 6 (optional): Add Merge Request Template + +- Download at url `GET /download?type=gitlab` +- Unzip and move the directory `gitlab` to `.gitlab` in your project + +You can refer to the [`.gitlab` directory](./.gitlab) settings of this project. + +**Please Enjoy it** + +## Deploy + +### Local + +``` +go run github.com/zc2638/review-bot/cmd -c config/config.yaml +``` + +### Docker + +``` +docker run -d -p 2640:2640 \ + -e BOT_SCM_HOST=https://gitlab.com \ + -e BOT_SCM_TOKEN= \ + -e BOT_SCM_SECRET= \ + zc2638/review-bot:latest +``` + +## Config + +change config file in `config/config.yaml` + +``` +server: + port: 2640 +scm: + host: https://gitlab.com + token: + secret: +``` + +| Configuration Item | Environment Variable | Description | +|:------------------:|:--------------------:|:------------------------------:| +| server.port | BOT_SERVER_PORT | bot server listening port | +| scm.host | BOT_SCM_HOST | source code management address | +| scm.token | BOT_SCM_TOKEN | private token | +| scm.secret | BOT_SCM_SECRET | webhook secret | diff --git a/config/config.yaml b/config/config.yaml index a856d5d..7da2936 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -1,3 +1,5 @@ +server: + port: 2640 scm: host: https://gitlab.com token: diff --git a/docs/image/private-token.png b/docs/image/private-token.png new file mode 100644 index 0000000..dfcfe21 Binary files /dev/null and b/docs/image/private-token.png differ diff --git a/docs/image/webhook-secret.png b/docs/image/webhook-secret.png new file mode 100644 index 0000000..75c514c Binary files /dev/null and b/docs/image/webhook-secret.png differ diff --git a/docs/image/webhook.png b/docs/image/webhook.png new file mode 100644 index 0000000..fb8b7bc Binary files /dev/null and b/docs/image/webhook.png differ diff --git a/go.mod b/go.mod index 099e323..2b2e9d3 100644 --- a/go.mod +++ b/go.mod @@ -11,17 +11,23 @@ require ( github.com/sirupsen/logrus v1.8.1 github.com/spf13/cobra v1.1.1 github.com/xanzy/go-gitlab v0.54.3 - github.com/zc2638/swag v1.1.4 + github.com/zc2638/swag v1.2.2-0.20220524015808-e1aadea752de golang.org/x/sync v0.0.0-20210220032951-036812b2e83c - golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 // indirect + golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect ) require ( github.com/99nil/go v0.0.0-20210924013233-ebd290da12d6 + github.com/gin-gonic/gin v1.6.3 // indirect github.com/golang-jwt/jwt/v4 v4.3.0 github.com/golang/protobuf v1.5.2 // indirect + github.com/gorilla/mux v1.8.0 // indirect github.com/hashicorp/go-cleanhttp v0.5.2 // indirect github.com/hashicorp/go-retryablehttp v0.7.0 // indirect + github.com/julienschmidt/httprouter v1.3.0 // indirect + github.com/labstack/echo v3.3.10+incompatible // indirect + github.com/labstack/gommon v0.3.0 // indirect + golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 // indirect golang.org/x/net v0.0.0-20220107192237-5cfca573fb4d // indirect golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11 // indirect diff --git a/go.sum b/go.sum index 39a2d96..9443189 100644 --- a/go.sum +++ b/go.sum @@ -317,6 +317,10 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/zc2638/swag v1.1.4 h1:ISkwVftIlLE4/nAlJURd7wuyv2rQhSgg925wmVjaY+o= github.com/zc2638/swag v1.1.4/go.mod h1:GhhUB84sxe70w3ar6PUMtcuyjptXzDPlzk34tHRTr3Y= +github.com/zc2638/swag v1.2.1 h1:TRXi2KAZFldOJvhYgDuGZzYY4+iR5QlklsJIi08+Ekk= +github.com/zc2638/swag v1.2.1/go.mod h1:HnfRlwQMD7sRSvyeXkVHrIsp7Om9fxjnLqalBuz8xH8= +github.com/zc2638/swag v1.2.2-0.20220524015808-e1aadea752de h1:FRB/b5j/ieZBv+/3wPtFBy5TOt2CG1Re8G9j+kQH/l4= +github.com/zc2638/swag v1.2.2-0.20220524015808-e1aadea752de/go.mod h1:HnfRlwQMD7sRSvyeXkVHrIsp7Om9fxjnLqalBuz8xH8= go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= @@ -456,6 +460,8 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20211124211545-fe61309f8881 h1:TyHqChC80pFkXWraUUf6RuB5IqFdQieMLwwCJokV2pc= golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k= +golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= diff --git a/handler/api.go b/handler/api.go index c6b8863..bb8dcbe 100644 --- a/handler/api.go +++ b/handler/api.go @@ -18,12 +18,13 @@ package handler import ( "net/http" - "github.com/zc2638/review-bot/global" + "github.com/zc2638/swag/option" "github.com/go-chi/chi" "github.com/go-chi/chi/middleware" "github.com/go-chi/cors" + "github.com/zc2638/review-bot/global" "github.com/zc2638/review-bot/handler/home" "github.com/zc2638/review-bot/handler/webhook" "github.com/zc2638/swag" @@ -38,13 +39,15 @@ func New() http.Handler { ) apiDoc := swag.New( - swag.Title("Review Bot API Doc"), - ) - apiDoc.AddEndpointFunc( - home.Register, + option.Title("Review Bot API Doc"), ) - mux.Post("/webhook", webhook.HandlerEvent(&global.Cfg().SCM, global.SCM())) + apiDoc.AddEndpointFunc(home.Register) + apiDoc.Walk(func(path string, e *swag.Endpoint) { + mux.Method(e.Method, path, e.Handler.(http.Handler)) + }) - apiDoc.RegisterMuxWithData(mux, false) + mux.Post("/webhook", webhook.HandlerEvent(&global.Cfg().SCM, global.SCM())) + mux.Handle("/swagger/json", apiDoc.Handler()) + mux.Mount("/swagger/ui", swag.UIHandler("/swagger/ui", "/swagger/json", true)) return mux } diff --git a/handler/home/doc.go b/handler/home/doc.go index a3c3c51..03dd124 100644 --- a/handler/home/doc.go +++ b/handler/home/doc.go @@ -17,11 +17,14 @@ package home import ( "net/http" + "github.com/zc2638/swag/types" + + "github.com/zc2638/swag" + "github.com/zc2638/swag/endpoint" - "github.com/zc2638/swag/swagger" ) -func Register(doc *swagger.API) { +func Register(doc *swag.API) { doc.AddEndpoint( endpoint.New( http.MethodGet, "/", @@ -33,8 +36,8 @@ func Register(doc *swagger.API) { http.MethodGet, "/secret", endpoint.Handler(secret()), endpoint.Summary("生成webhook密钥"), - endpoint.Query("namespace", "string", "仓库中间名称", true), - endpoint.Query("name", "string", "仓库名称", true), + endpoint.Query("namespace", types.String, "仓库中间名称", true), + endpoint.Query("name", types.String, "仓库名称", true), endpoint.ResponseSuccess(), endpoint.NoSecurity(), ), @@ -42,7 +45,7 @@ func Register(doc *swagger.API) { http.MethodGet, "/download", endpoint.Handler(download("")), endpoint.Summary("模板文件下载"), - endpoint.QueryDefault("type", "string", "版本系统类型", "gitlab", true), + endpoint.QueryDefault("type", types.String, "版本系统类型", "gitlab", true), endpoint.ResponseSuccess(), endpoint.NoSecurity(), ),