From fc605ce397727c0c73d185c165d7808525268765 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 11 Nov 2021 18:49:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=87=AA=E5=8A=A8=E6=B3=A8?= =?UTF-8?q?=E5=86=8Cgit=20hook=E8=B7=AF=E7=94=B1=E7=9A=84=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- git_hook/abstract.go | 17 +++++++++++++++++ git_hook/register.go | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 git_hook/abstract.go create mode 100644 git_hook/register.go diff --git a/git_hook/abstract.go b/git_hook/abstract.go new file mode 100644 index 0000000..4f3ee73 --- /dev/null +++ b/git_hook/abstract.go @@ -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) diff --git a/git_hook/register.go b/git_hook/register.go new file mode 100644 index 0000000..6678d90 --- /dev/null +++ b/git_hook/register.go @@ -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 +}