123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- package models
- import (
- "encoding/json"
- "errors"
- "github.com/gogits/gogs/modules/log"
- )
- var (
- ErrWebhookNotExist = errors.New("Webhook does not exist")
- )
- const (
- CT_JSON = iota + 1
- CT_FORM
- )
- type HookEvent struct {
- PushOnly bool `json:"push_only"`
- }
- type Webhook struct {
- Id int64
- RepoId int64
- Url string `xorm:"TEXT"`
- ContentType int
- Secret string `xorm:"TEXT"`
- Events string `xorm:"TEXT"`
- *HookEvent `xorm:"-"`
- IsSsl bool
- IsActive bool
- }
- func (w *Webhook) GetEvent() {
- w.HookEvent = &HookEvent{}
- if err := json.Unmarshal([]byte(w.Events), w.HookEvent); err != nil {
- log.Error("webhook.GetEvent(%d): %v", w.Id, err)
- }
- }
- func (w *Webhook) SaveEvent() error {
- data, err := json.Marshal(w.HookEvent)
- w.Events = string(data)
- return err
- }
- func (w *Webhook) HasPushEvent() bool {
- if w.PushOnly {
- return true
- }
- return false
- }
- func CreateWebhook(w *Webhook) error {
- _, err := orm.Insert(w)
- return err
- }
- func UpdateWebhook(w *Webhook) error {
- _, err := orm.AllCols().Update(w)
- return err
- }
- func GetWebhookById(hookId int64) (*Webhook, error) {
- w := &Webhook{Id: hookId}
- has, err := orm.Get(w)
- if err != nil {
- return nil, err
- } else if !has {
- return nil, ErrWebhookNotExist
- }
- return w, nil
- }
- func GetActiveWebhooksByRepoId(repoId int64) (ws []*Webhook, err error) {
- err = orm.Find(&ws, &Webhook{RepoId: repoId, IsActive: true})
- return ws, err
- }
- func GetWebhooksByRepoId(repoId int64) (ws []*Webhook, err error) {
- err = orm.Find(&ws, &Webhook{RepoId: repoId})
- return ws, err
- }
- func DeleteWebhook(hookId int64) error {
- _, err := orm.Delete(&Webhook{Id: hookId})
- return err
- }
|