You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

webhook.go 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. "errors"
  8. "github.com/gogits/gogs/modules/log"
  9. )
  10. var (
  11. ErrWebhookNotExist = errors.New("Webhook does not exist")
  12. )
  13. // Content types.
  14. const (
  15. CT_JSON = iota + 1
  16. CT_FORM
  17. )
  18. type HookEvent struct {
  19. PushOnly bool `json:"push_only"`
  20. }
  21. type Webhook struct {
  22. Id int64
  23. RepoId int64
  24. Payload string `xorm:"TEXT"`
  25. ContentType int
  26. Secret string `xorm:"TEXT"`
  27. Events string `xorm:"TEXT"`
  28. *HookEvent `xorm:"-"`
  29. IsSsl bool
  30. IsActive bool
  31. }
  32. func (w *Webhook) GetEvent() {
  33. w.HookEvent = &HookEvent{}
  34. if err := json.Unmarshal([]byte(w.Events), w.HookEvent); err != nil {
  35. log.Error("webhook.GetEvent(%d): %v", w.Id, err)
  36. }
  37. }
  38. func (w *Webhook) SaveEvent() error {
  39. data, err := json.Marshal(w.HookEvent)
  40. w.Events = string(data)
  41. return err
  42. }
  43. // CreateWebhook creates new webhook.
  44. func CreateWebhook(w *Webhook) error {
  45. _, err := orm.Insert(w)
  46. return err
  47. }
  48. // UpdateWebhook updates information of webhook.
  49. func UpdateWebhook(w *Webhook) error {
  50. _, err := orm.AllCols().Update(w)
  51. return err
  52. }
  53. // GetWebhookById returns webhook by given ID.
  54. func GetWebhookById(hookId int64) (*Webhook, error) {
  55. w := &Webhook{Id: hookId}
  56. has, err := orm.Get(w)
  57. if err != nil {
  58. return nil, err
  59. } else if !has {
  60. return nil, ErrWebhookNotExist
  61. }
  62. return w, nil
  63. }
  64. // GetWebhooksByRepoId returns all webhooks of repository.
  65. func GetWebhooksByRepoId(repoId int64) (ws []*Webhook, err error) {
  66. err = orm.Find(&ws, &Webhook{RepoId: repoId})
  67. return ws, err
  68. }
  69. // DeleteWebhook deletes webhook of repository.
  70. func DeleteWebhook(hookId int64) error {
  71. _, err := orm.Delete(&Webhook{Id: hookId})
  72. return err
  73. }