forked from lifei6671/go-git-webhook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.go
112 lines (91 loc) · 2.47 KB
/
webhook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package models
import (
"time"
"errors"
"github.com/astaxie/beego/orm"
"github.com/lifei6671/go-git-webhook/modules/hash"
)
// WebHook对象
type WebHook struct {
WebHookId int `orm:"pk;auto;unique;column(web_hook_id)" json:"web_hook_id"`
RepositoryName string `orm:"size(255);column(repo_name)" json:"repository_name"`
BranchName string `orm:"size(255);column(branch_name)" json:"branch_name"`
Tag string `orm:"size(1000);column(tag)" json:"tag"`
Shell string `orm:"size(1000);column(shell)" json:"shell"`
Status int `orm:"type(int);column(status);default(0)" json:"status"`
Key string `orm:"size(255);column(key);unique" json:"key"`
Secure string `orm:"size(255);column(secure)" json:"secure"`
LastExecTime time.Time `orm:"type(datetime);column(last_exec_time);null" json:"last_exec_time"`
CreateTime time.Time `orm:"type(datetime);column(create_time);auto_now_add" json:"create_time"`
HookType string `orm:"column(hook_type);size(50)" json:"hook_type"`
CreateAt int `orm:"type(int);column(create_at)"`
}
// 获取对应数据库表名
func (m *WebHook) TableName() string {
return "webhooks"
}
// 获取数据使用的引擎
func (m *WebHook) TableEngine() string {
return "INNODB"
}
// 新建 WebHook 对象
func NewWebHook() *WebHook {
return &WebHook{}
}
// 查找
func (m *WebHook) Find() error {
if m.WebHookId <= 0 {
return ErrInvalidParameter
}
o := orm.NewOrm()
if err := o.Read(m) ;err != nil {
return err
}
return nil;
}
// 批量删除
func (m *WebHook) DeleteMulti (id ...int) error {
if len(id) > 0 {
o := orm.NewOrm()
ids := make([]int,len(id))
params := ""
for i := 0;i<len(id);i++ {
ids[i] = id[i]
params = params + ",?"
}
_,err := o.Raw("DELETE webhooks WHERE web_hook_id IN ("+ params[1:] +")",ids).Exec()
if err != nil {
return err
}
return nil
}
return errors.New("Invalid parameter")
}
//删除一条
func (m *WebHook) Delete() error {
o := orm.NewOrm()
_,err := o.Delete(m)
return err
}
// 根据Key查找
func (m *WebHook) FindByKey(key string) error {
o := orm.NewOrm()
if err := o.QueryTable(m.TableName()).Filter("key",key).One(m);err != nil {
return err
}
return nil
}
// 添加或更新
func (m *WebHook) Save() error {
o := orm.NewOrm()
var err error;
if m.WebHookId > 0 {
_,err = o.Update(m)
}else{
key := (time.Now().String() + m.RepositoryName + m.BranchName )
m.Key = hash.Md5(key)
m.Secure = hash.Md5(key + key + time.Now().String())
_,err = o.Insert(m)
}
return err
}