增加自动注册git hook路由的方法

This commit is contained in:
白茶清欢 2021-11-11 18:49:09 +08:00
parent 9f07e2521e
commit fc605ce397
2 changed files with 54 additions and 0 deletions

17
git_hook/abstract.go Normal file
View File

@ -0,0 +1,17 @@
// Package git_hook...
//
// Description : git_hook...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-11-11 6:41 下午
package git_hook
import "github.com/gin-gonic/gin"
// IGitHookEventHandler 接口约束, pkg 内部会提供基础实现
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 6:42 下午 2021/11/11
type IGitHookEventHandler func(ctx *gin.Context, hookData *HookData)

37
git_hook/register.go Normal file
View File

@ -0,0 +1,37 @@
// Package git_hook...
//
// Description : git_hook...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-11-11 6:39 下午
package git_hook
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
)
// RegisterGitHookRouter 注册 git hook 回调的路由
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 6:40 下午 2021/11/11
func RegisterGitHookRouter(router *gin.Engine, handler IGitHookEventHandler) error {
if nil == handler {
return errors.New("handler is nil")
}
router.POST("/git/hook/notice", func(ctx *gin.Context) {
var (
hookData HookData
err error
)
if err = ctx.ShouldBindJSON(&hookData); nil != err {
ctx.JSON(http.StatusBadRequest, gin.H{"code": -1, "message": err.Error()})
}
handler(ctx, &hookData)
})
return nil
}