gitea/models/webhook.go

101 lines
2.1 KiB
Go
Raw Normal View History

2014-05-06 00:52:25 +00:00
// Copyright 2014 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package models
import (
"encoding/json"
2014-05-06 01:36:08 +00:00
"errors"
2014-05-06 00:52:25 +00:00
"github.com/gogits/gogs/modules/log"
)
2014-05-06 01:36:08 +00:00
var (
ErrWebhookNotExist = errors.New("Webhook does not exist")
)
2014-05-06 00:52:25 +00:00
// Content types.
const (
CT_JSON = iota + 1
CT_FORM
)
type HookEvent struct {
PushOnly bool `json:"push_only"`
}
type Webhook struct {
Id int64
RepoId int64
2014-05-06 15:50:31 +00:00
Url string `xorm:"TEXT"`
2014-05-06 00:52:25 +00:00
ContentType int
Secret string `xorm:"TEXT"`
Events string `xorm:"TEXT"`
2014-05-06 01:36:08 +00:00
*HookEvent `xorm:"-"`
2014-05-06 00:52:25 +00:00
IsSsl bool
IsActive bool
}
2014-05-06 01:36:08 +00:00
func (w *Webhook) GetEvent() {
w.HookEvent = &HookEvent{}
if err := json.Unmarshal([]byte(w.Events), w.HookEvent); err != nil {
2014-05-06 00:52:25 +00:00
log.Error("webhook.GetEvent(%d): %v", w.Id, err)
}
}
2014-05-06 01:36:08 +00:00
func (w *Webhook) SaveEvent() error {
data, err := json.Marshal(w.HookEvent)
2014-05-06 00:52:25 +00:00
w.Events = string(data)
return err
}
2014-05-06 15:50:31 +00:00
func (w *Webhook) HasPushEvent() bool {
if w.PushOnly {
return true
}
return false
}
2014-05-06 00:52:25 +00:00
// CreateWebhook creates new webhook.
func CreateWebhook(w *Webhook) error {
_, err := orm.Insert(w)
return err
}
2014-05-06 01:36:08 +00:00
// UpdateWebhook updates information of webhook.
func UpdateWebhook(w *Webhook) error {
_, err := orm.AllCols().Update(w)
return err
}
// GetWebhookById returns webhook by given ID.
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
}
2014-05-06 15:50:31 +00:00
// GetActiveWebhooksByRepoId returns all active webhooks of repository.
func GetActiveWebhooksByRepoId(repoId int64) (ws []*Webhook, err error) {
err = orm.Find(&ws, &Webhook{RepoId: repoId, IsActive: true})
return ws, err
}
2014-05-06 00:52:25 +00:00
// GetWebhooksByRepoId returns all webhooks of repository.
func GetWebhooksByRepoId(repoId int64) (ws []*Webhook, err error) {
err = orm.Find(&ws, &Webhook{RepoId: repoId})
return ws, err
}
2014-05-06 01:36:08 +00:00
// DeleteWebhook deletes webhook of repository.
func DeleteWebhook(hookId int64) error {
_, err := orm.Delete(&Webhook{Id: hookId})
return err
}