git-hook/git_hook.go

114 lines
2.7 KiB
Go
Raw Normal View History

2021-10-13 11:23:52 +08:00
// Package git_hook...
//
// Description : git_hook...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-10-13 11:11 上午
package main
import (
2021-11-12 17:18:40 +08:00
"encoding/json"
"time"
2021-11-12 17:18:40 +08:00
2021-11-12 17:35:03 +08:00
"git.zhangdeman.cn/zhangdeman/gopkg/cmd"
2021-11-12 17:18:40 +08:00
"git.zhangdeman.cn/zhangdeman/git-hook/define"
2021-11-12 11:08:11 +08:00
"git.zhangdeman.cn/zhangdeman/gopkg/git_hook"
2021-11-12 17:18:40 +08:00
"git.zhangdeman.cn/zhangdeman/gopkg/util"
2021-10-13 11:23:52 +08:00
"github.com/gin-gonic/gin"
)
2021-11-12 17:18:40 +08:00
var (
// CLIParamConfig ...
CLIParamConfig map[string]string
// TaskList 任务列表
TaskList []define.Task
)
2021-10-13 11:23:52 +08:00
func main() {
2021-11-12 17:18:40 +08:00
parseCLIParam()
go func() {
// 初始化配置
byteData, err := util.ReadFileContent(CLIParamConfig["config_path"] + "/task.json")
2021-11-12 17:18:40 +08:00
if nil != err {
panic("任务配置文件读取失败 : " + err.Error())
}
if err = json.Unmarshal(byteData, &TaskList); nil != err {
panic("任务配置文件解析失败 : " + err.Error())
}
// 每分钟自动更新一次任务
time.Sleep(60 * time.Second)
}()
2021-11-12 17:18:40 +08:00
2021-10-13 11:23:52 +08:00
router := gin.Default()
2021-11-12 11:08:11 +08:00
// 注册回调路由
2021-11-12 11:43:40 +08:00
_ = git_hook.RegisterGitHookRouter(router, func(ctx *gin.Context, hookData *git_hook.HookData) *git_hook.ResponseData {
2021-11-12 17:35:03 +08:00
// 检测仓库是否建立了相关任务
taskConfig, exist := getTaskConfig(hookData.Repository.Name)
if !exist {
// 没有配置相关任务
return &git_hook.ResponseData{
Code: 0,
Message: "成功接收到web hook通知, " + hookData.Repository.Name + " 没有配置任何任务, 不做处理",
Data: hookData,
}
}
2021-11-12 17:18:40 +08:00
if hookData.Ref != "refs/heads/master" {
// 不是master分支, 不触发相应逻辑
return &git_hook.ResponseData{
Code: 0,
Message: "成功接收到web hook通知, 非master分支,不做任何处理",
Data: hookData,
}
}
2021-11-12 17:35:03 +08:00
taskRunResult := cmd.Execute(cmd.Config{
WorkDir: taskConfig.WorkDir,
Command: taskConfig.Command,
Script: taskConfig.Script,
ParameterList: taskConfig.Parameter,
})
if nil != taskRunResult.Err {
return &git_hook.ResponseData{
Code: -1,
Message: "task任务执行失败 : " + taskRunResult.Err.Error(),
Data: gin.H{
"command": taskRunResult.ExecuteCommand,
"output": string(taskRunResult.Output),
},
}
}
2021-11-12 11:43:40 +08:00
return &git_hook.ResponseData{
Code: 0,
Message: "成功接收到web hook通知",
2021-11-12 17:35:03 +08:00
Data: gin.H{
"command": taskRunResult.ExecuteCommand,
"output": string(taskRunResult.Output),
},
2021-11-12 11:43:40 +08:00
}
2021-11-12 11:08:11 +08:00
})
_ = router.Run(":12590")
2021-10-13 11:23:52 +08:00
}
2021-11-12 17:18:40 +08:00
func parseCLIParam() {
paramList := []string{"config_path"}
CLIParamConfig = util.ParseCLIParameter(paramList)
}
2021-11-12 17:35:03 +08:00
func getTaskConfig(taskFlag string) (define.Task, bool) {
for _, item := range TaskList {
if item.Flag == taskFlag {
return item, true
}
}
return define.Task{}, false
}