webhook.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package models
  5. import (
  6. "encoding/json"
  7. "github.com/gogits/gogs/modules/log"
  8. )
  9. // Content types.
  10. const (
  11. CT_JSON = iota + 1
  12. CT_FORM
  13. )
  14. type HookEvent struct {
  15. PushOnly bool `json:"push_only"`
  16. }
  17. type Webhook struct {
  18. Id int64
  19. RepoId int64
  20. Payload string `xorm:"TEXT"`
  21. ContentType int
  22. Secret string `xorm:"TEXT"`
  23. Events string `xorm:"TEXT"`
  24. IsSsl bool
  25. IsActive bool
  26. }
  27. func (w *Webhook) GetEvent() *HookEvent {
  28. h := &HookEvent{}
  29. if err := json.Unmarshal([]byte(w.Events), h); err != nil {
  30. log.Error("webhook.GetEvent(%d): %v", w.Id, err)
  31. }
  32. return h
  33. }
  34. func (w *Webhook) SaveEvent(h *HookEvent) error {
  35. data, err := json.Marshal(h)
  36. w.Events = string(data)
  37. return err
  38. }
  39. // CreateWebhook creates new webhook.
  40. func CreateWebhook(w *Webhook) error {
  41. _, err := orm.Insert(w)
  42. return err
  43. }
  44. // GetWebhooksByRepoId returns all webhooks of repository.
  45. func GetWebhooksByRepoId(repoId int64) (ws []*Webhook, err error) {
  46. err = orm.Find(&ws, &Webhook{RepoId: repoId})
  47. return ws, err
  48. }