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

webhook.go 23 kB

11 years ago
11 years ago
8 years ago
8 years ago
8 years ago
11 years ago
11 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
6 years ago
11 years ago
9 years ago
9 years ago
11 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
5 years ago
API add/generalize pagination (#9452) * paginate results * fixed deadlock * prevented breaking change * updated swagger * go fmt * fixed find topic * go mod tidy * go mod vendor with go1.13.5 * fixed repo find topics * fixed unit test * added Limit method to Engine struct; use engine variable when provided; fixed gitignore * use ItemsPerPage for default pagesize; fix GetWatchers, getOrgUsersByOrgID and GetStargazers; fix GetAllCommits headers; reverted some changed behaviors * set Page value on Home route * improved memory allocations * fixed response headers * removed logfiles * fixed import order * import order * improved swagger * added function to get models.ListOptions from context * removed pagesize diff on unit test * fixed imports * removed unnecessary struct field * fixed go fmt * scoped PR * code improvements * code improvements * go mod tidy * fixed import order * fixed commit statuses session * fixed files headers * fixed headers; added pagination for notifications * go mod tidy * go fmt * removed Private from user search options; added setting.UI.IssuePagingNum as default valeu on repo's issues list * Apply suggestions from code review Co-Authored-By: 6543 <6543@obermui.de> Co-Authored-By: zeripath <art27@cantab.net> * fixed build error * CI.restart() * fixed merge conflicts resolve * fixed conflicts resolve * improved FindTrackedTimesOptions.ToOptions() method * added backwards compatibility on ListReleases request; fixed issue tracked time ToSession * fixed build error; fixed swagger template * fixed swagger template * fixed ListReleases backwards compatibility * added page to user search route Co-authored-by: techknowlogick <matti@mdranta.net> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: zeripath <art27@cantab.net>
5 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
6 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
6 years ago
Better logging (#6038) (#6095) * Panic don't fatal on create new logger Fixes #5854 Signed-off-by: Andrew Thornton <art27@cantab.net> * partial broken * Update the logging infrastrcture Signed-off-by: Andrew Thornton <art27@cantab.net> * Reset the skip levels for Fatal and Error Signed-off-by: Andrew Thornton <art27@cantab.net> * broken ncsa * More log.Error fixes Signed-off-by: Andrew Thornton <art27@cantab.net> * Remove nal * set log-levels to lowercase * Make console_test test all levels * switch to lowercased levels * OK now working * Fix vetting issues * Fix lint * Fix tests * change default logging to match current gitea * Improve log testing Signed-off-by: Andrew Thornton <art27@cantab.net> * reset error skip levels to 0 * Update documentation and access logger configuration * Redirect the router log back to gitea if redirect macaron log but also allow setting the log level - i.e. TRACE * Fix broken level caching * Refactor the router log * Add Router logger * Add colorizing options * Adjust router colors * Only create logger if they will be used * update app.ini.sample * rename Attribute ColorAttribute * Change from white to green for function * Set fatal/error levels * Restore initial trace logger * Fix Trace arguments in modules/auth/auth.go * Properly handle XORMLogger * Improve admin/config page * fix fmt * Add auto-compression of old logs * Update error log levels * Remove the unnecessary skip argument from Error, Fatal and Critical * Add stacktrace support * Fix tests * Remove x/sync from vendors? * Add stderr option to console logger * Use filepath.ToSlash to protect against Windows in tests * Remove prefixed underscores from names in colors.go * Remove not implemented database logger This was removed from Gogs on 4 Mar 2016 but left in the configuration since then. * Ensure that log paths are relative to ROOT_PATH * use path.Join * rename jsonConfig to logConfig * Rename "config" to "jsonConfig" to make it clearer * Requested changes * Requested changes: XormLogger * Try to color the windows terminal If successful default to colorizing the console logs * fixup * Colorize initially too * update vendor * Colorize logs on default and remove if this is not a colorizing logger * Fix documentation * fix test * Use go-isatty to detect if on windows we are on msys or cygwin * Fix spelling mistake * Add missing vendors * More changes * Rationalise the ANSI writer protection * Adjust colors on advice from @0x5c * Make Flags a comma separated list * Move to use the windows constant for ENABLE_VIRTUAL_TERMINAL_PROCESSING * Ensure matching is done on the non-colored message - to simpify EXPRESSION
6 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2017 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package models
  6. import (
  7. "encoding/json"
  8. "fmt"
  9. "strings"
  10. "time"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/setting"
  13. api "code.gitea.io/gitea/modules/structs"
  14. "code.gitea.io/gitea/modules/timeutil"
  15. gouuid "github.com/google/uuid"
  16. )
  17. // HookContentType is the content type of a web hook
  18. type HookContentType int
  19. const (
  20. // ContentTypeJSON is a JSON payload for web hooks
  21. ContentTypeJSON HookContentType = iota + 1
  22. // ContentTypeForm is an url-encoded form payload for web hook
  23. ContentTypeForm
  24. )
  25. var hookContentTypes = map[string]HookContentType{
  26. "json": ContentTypeJSON,
  27. "form": ContentTypeForm,
  28. }
  29. // ToHookContentType returns HookContentType by given name.
  30. func ToHookContentType(name string) HookContentType {
  31. return hookContentTypes[name]
  32. }
  33. // Name returns the name of a given web hook's content type
  34. func (t HookContentType) Name() string {
  35. switch t {
  36. case ContentTypeJSON:
  37. return "json"
  38. case ContentTypeForm:
  39. return "form"
  40. }
  41. return ""
  42. }
  43. // IsValidHookContentType returns true if given name is a valid hook content type.
  44. func IsValidHookContentType(name string) bool {
  45. _, ok := hookContentTypes[name]
  46. return ok
  47. }
  48. // HookEvents is a set of web hook events
  49. type HookEvents struct {
  50. Create bool `json:"create"`
  51. Delete bool `json:"delete"`
  52. Fork bool `json:"fork"`
  53. Issues bool `json:"issues"`
  54. IssueAssign bool `json:"issue_assign"`
  55. IssueLabel bool `json:"issue_label"`
  56. IssueMilestone bool `json:"issue_milestone"`
  57. IssueComment bool `json:"issue_comment"`
  58. Push bool `json:"push"`
  59. PullRequest bool `json:"pull_request"`
  60. PullRequestAssign bool `json:"pull_request_assign"`
  61. PullRequestLabel bool `json:"pull_request_label"`
  62. PullRequestMilestone bool `json:"pull_request_milestone"`
  63. PullRequestComment bool `json:"pull_request_comment"`
  64. PullRequestReview bool `json:"pull_request_review"`
  65. PullRequestSync bool `json:"pull_request_sync"`
  66. Repository bool `json:"repository"`
  67. Release bool `json:"release"`
  68. }
  69. // HookEvent represents events that will delivery hook.
  70. type HookEvent struct {
  71. PushOnly bool `json:"push_only"`
  72. SendEverything bool `json:"send_everything"`
  73. ChooseEvents bool `json:"choose_events"`
  74. BranchFilter string `json:"branch_filter"`
  75. HookEvents `json:"events"`
  76. }
  77. // HookStatus is the status of a web hook
  78. type HookStatus int
  79. // Possible statuses of a web hook
  80. const (
  81. HookStatusNone = iota
  82. HookStatusSucceed
  83. HookStatusFail
  84. )
  85. // Webhook represents a web hook object.
  86. type Webhook struct {
  87. ID int64 `xorm:"pk autoincr"`
  88. RepoID int64 `xorm:"INDEX"` // An ID of 0 indicates either a default or system webhook
  89. OrgID int64 `xorm:"INDEX"`
  90. IsSystemWebhook bool
  91. URL string `xorm:"url TEXT"`
  92. Signature string `xorm:"TEXT"`
  93. HTTPMethod string `xorm:"http_method"`
  94. ContentType HookContentType
  95. Secret string `xorm:"TEXT"`
  96. Events string `xorm:"TEXT"`
  97. *HookEvent `xorm:"-"`
  98. IsSSL bool `xorm:"is_ssl"`
  99. IsActive bool `xorm:"INDEX"`
  100. Type HookTaskType `xorm:"VARCHAR(16) 'type'"`
  101. Meta string `xorm:"TEXT"` // store hook-specific attributes
  102. LastStatus HookStatus // Last delivery status
  103. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  104. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  105. }
  106. // AfterLoad updates the webhook object upon setting a column
  107. func (w *Webhook) AfterLoad() {
  108. w.HookEvent = &HookEvent{}
  109. if err := json.Unmarshal([]byte(w.Events), w.HookEvent); err != nil {
  110. log.Error("Unmarshal[%d]: %v", w.ID, err)
  111. }
  112. }
  113. // History returns history of webhook by given conditions.
  114. func (w *Webhook) History(page int) ([]*HookTask, error) {
  115. return HookTasks(w.ID, page)
  116. }
  117. // UpdateEvent handles conversion from HookEvent to Events.
  118. func (w *Webhook) UpdateEvent() error {
  119. data, err := json.Marshal(w.HookEvent)
  120. w.Events = string(data)
  121. return err
  122. }
  123. // HasCreateEvent returns true if hook enabled create event.
  124. func (w *Webhook) HasCreateEvent() bool {
  125. return w.SendEverything ||
  126. (w.ChooseEvents && w.HookEvents.Create)
  127. }
  128. // HasDeleteEvent returns true if hook enabled delete event.
  129. func (w *Webhook) HasDeleteEvent() bool {
  130. return w.SendEverything ||
  131. (w.ChooseEvents && w.HookEvents.Delete)
  132. }
  133. // HasForkEvent returns true if hook enabled fork event.
  134. func (w *Webhook) HasForkEvent() bool {
  135. return w.SendEverything ||
  136. (w.ChooseEvents && w.HookEvents.Fork)
  137. }
  138. // HasIssuesEvent returns true if hook enabled issues event.
  139. func (w *Webhook) HasIssuesEvent() bool {
  140. return w.SendEverything ||
  141. (w.ChooseEvents && w.HookEvents.Issues)
  142. }
  143. // HasIssuesAssignEvent returns true if hook enabled issues assign event.
  144. func (w *Webhook) HasIssuesAssignEvent() bool {
  145. return w.SendEverything ||
  146. (w.ChooseEvents && w.HookEvents.IssueAssign)
  147. }
  148. // HasIssuesLabelEvent returns true if hook enabled issues label event.
  149. func (w *Webhook) HasIssuesLabelEvent() bool {
  150. return w.SendEverything ||
  151. (w.ChooseEvents && w.HookEvents.IssueLabel)
  152. }
  153. // HasIssuesMilestoneEvent returns true if hook enabled issues milestone event.
  154. func (w *Webhook) HasIssuesMilestoneEvent() bool {
  155. return w.SendEverything ||
  156. (w.ChooseEvents && w.HookEvents.IssueMilestone)
  157. }
  158. // HasIssueCommentEvent returns true if hook enabled issue_comment event.
  159. func (w *Webhook) HasIssueCommentEvent() bool {
  160. return w.SendEverything ||
  161. (w.ChooseEvents && w.HookEvents.IssueComment)
  162. }
  163. // HasPushEvent returns true if hook enabled push event.
  164. func (w *Webhook) HasPushEvent() bool {
  165. return w.PushOnly || w.SendEverything ||
  166. (w.ChooseEvents && w.HookEvents.Push)
  167. }
  168. // HasPullRequestEvent returns true if hook enabled pull request event.
  169. func (w *Webhook) HasPullRequestEvent() bool {
  170. return w.SendEverything ||
  171. (w.ChooseEvents && w.HookEvents.PullRequest)
  172. }
  173. // HasPullRequestAssignEvent returns true if hook enabled pull request assign event.
  174. func (w *Webhook) HasPullRequestAssignEvent() bool {
  175. return w.SendEverything ||
  176. (w.ChooseEvents && w.HookEvents.PullRequestAssign)
  177. }
  178. // HasPullRequestLabelEvent returns true if hook enabled pull request label event.
  179. func (w *Webhook) HasPullRequestLabelEvent() bool {
  180. return w.SendEverything ||
  181. (w.ChooseEvents && w.HookEvents.PullRequestLabel)
  182. }
  183. // HasPullRequestMilestoneEvent returns true if hook enabled pull request milestone event.
  184. func (w *Webhook) HasPullRequestMilestoneEvent() bool {
  185. return w.SendEverything ||
  186. (w.ChooseEvents && w.HookEvents.PullRequestMilestone)
  187. }
  188. // HasPullRequestCommentEvent returns true if hook enabled pull_request_comment event.
  189. func (w *Webhook) HasPullRequestCommentEvent() bool {
  190. return w.SendEverything ||
  191. (w.ChooseEvents && w.HookEvents.PullRequestComment)
  192. }
  193. // HasPullRequestApprovedEvent returns true if hook enabled pull request review event.
  194. func (w *Webhook) HasPullRequestApprovedEvent() bool {
  195. return w.SendEverything ||
  196. (w.ChooseEvents && w.HookEvents.PullRequestReview)
  197. }
  198. // HasPullRequestRejectedEvent returns true if hook enabled pull request review event.
  199. func (w *Webhook) HasPullRequestRejectedEvent() bool {
  200. return w.SendEverything ||
  201. (w.ChooseEvents && w.HookEvents.PullRequestReview)
  202. }
  203. // HasPullRequestReviewCommentEvent returns true if hook enabled pull request review event.
  204. func (w *Webhook) HasPullRequestReviewCommentEvent() bool {
  205. return w.SendEverything ||
  206. (w.ChooseEvents && w.HookEvents.PullRequestReview)
  207. }
  208. // HasPullRequestSyncEvent returns true if hook enabled pull request sync event.
  209. func (w *Webhook) HasPullRequestSyncEvent() bool {
  210. return w.SendEverything ||
  211. (w.ChooseEvents && w.HookEvents.PullRequestSync)
  212. }
  213. // HasReleaseEvent returns if hook enabled release event.
  214. func (w *Webhook) HasReleaseEvent() bool {
  215. return w.SendEverything ||
  216. (w.ChooseEvents && w.HookEvents.Release)
  217. }
  218. // HasRepositoryEvent returns if hook enabled repository event.
  219. func (w *Webhook) HasRepositoryEvent() bool {
  220. return w.SendEverything ||
  221. (w.ChooseEvents && w.HookEvents.Repository)
  222. }
  223. // EventCheckers returns event checkers
  224. func (w *Webhook) EventCheckers() []struct {
  225. Has func() bool
  226. Type HookEventType
  227. } {
  228. return []struct {
  229. Has func() bool
  230. Type HookEventType
  231. }{
  232. {w.HasCreateEvent, HookEventCreate},
  233. {w.HasDeleteEvent, HookEventDelete},
  234. {w.HasForkEvent, HookEventFork},
  235. {w.HasPushEvent, HookEventPush},
  236. {w.HasIssuesEvent, HookEventIssues},
  237. {w.HasIssuesAssignEvent, HookEventIssueAssign},
  238. {w.HasIssuesLabelEvent, HookEventIssueLabel},
  239. {w.HasIssuesMilestoneEvent, HookEventIssueMilestone},
  240. {w.HasIssueCommentEvent, HookEventIssueComment},
  241. {w.HasPullRequestEvent, HookEventPullRequest},
  242. {w.HasPullRequestAssignEvent, HookEventPullRequestAssign},
  243. {w.HasPullRequestLabelEvent, HookEventPullRequestLabel},
  244. {w.HasPullRequestMilestoneEvent, HookEventPullRequestMilestone},
  245. {w.HasPullRequestCommentEvent, HookEventPullRequestComment},
  246. {w.HasPullRequestApprovedEvent, HookEventPullRequestReviewApproved},
  247. {w.HasPullRequestRejectedEvent, HookEventPullRequestReviewRejected},
  248. {w.HasPullRequestCommentEvent, HookEventPullRequestReviewComment},
  249. {w.HasPullRequestSyncEvent, HookEventPullRequestSync},
  250. {w.HasRepositoryEvent, HookEventRepository},
  251. {w.HasReleaseEvent, HookEventRelease},
  252. }
  253. }
  254. // EventsArray returns an array of hook events
  255. func (w *Webhook) EventsArray() []string {
  256. events := make([]string, 0, 7)
  257. for _, c := range w.EventCheckers() {
  258. if c.Has() {
  259. events = append(events, string(c.Type))
  260. }
  261. }
  262. return events
  263. }
  264. // CreateWebhook creates a new web hook.
  265. func CreateWebhook(w *Webhook) error {
  266. return createWebhook(x, w)
  267. }
  268. func createWebhook(e Engine, w *Webhook) error {
  269. w.Type = strings.TrimSpace(w.Type)
  270. _, err := e.Insert(w)
  271. return err
  272. }
  273. // getWebhook uses argument bean as query condition,
  274. // ID must be specified and do not assign unnecessary fields.
  275. func getWebhook(bean *Webhook) (*Webhook, error) {
  276. has, err := x.Get(bean)
  277. if err != nil {
  278. return nil, err
  279. } else if !has {
  280. return nil, ErrWebhookNotExist{bean.ID}
  281. }
  282. return bean, nil
  283. }
  284. // GetWebhookByID returns webhook of repository by given ID.
  285. func GetWebhookByID(id int64) (*Webhook, error) {
  286. return getWebhook(&Webhook{
  287. ID: id,
  288. })
  289. }
  290. // GetWebhookByRepoID returns webhook of repository by given ID.
  291. func GetWebhookByRepoID(repoID, id int64) (*Webhook, error) {
  292. return getWebhook(&Webhook{
  293. ID: id,
  294. RepoID: repoID,
  295. })
  296. }
  297. // GetWebhookByOrgID returns webhook of organization by given ID.
  298. func GetWebhookByOrgID(orgID, id int64) (*Webhook, error) {
  299. return getWebhook(&Webhook{
  300. ID: id,
  301. OrgID: orgID,
  302. })
  303. }
  304. // GetActiveWebhooksByRepoID returns all active webhooks of repository.
  305. func GetActiveWebhooksByRepoID(repoID int64) ([]*Webhook, error) {
  306. return getActiveWebhooksByRepoID(x, repoID)
  307. }
  308. func getActiveWebhooksByRepoID(e Engine, repoID int64) ([]*Webhook, error) {
  309. webhooks := make([]*Webhook, 0, 5)
  310. return webhooks, e.Where("is_active=?", true).
  311. Find(&webhooks, &Webhook{RepoID: repoID})
  312. }
  313. // GetWebhooksByRepoID returns all webhooks of a repository.
  314. func GetWebhooksByRepoID(repoID int64, listOptions ListOptions) ([]*Webhook, error) {
  315. if listOptions.Page == 0 {
  316. webhooks := make([]*Webhook, 0, 5)
  317. return webhooks, x.Find(&webhooks, &Webhook{RepoID: repoID})
  318. }
  319. sess := listOptions.getPaginatedSession()
  320. webhooks := make([]*Webhook, 0, listOptions.PageSize)
  321. return webhooks, sess.Find(&webhooks, &Webhook{RepoID: repoID})
  322. }
  323. // GetActiveWebhooksByOrgID returns all active webhooks for an organization.
  324. func GetActiveWebhooksByOrgID(orgID int64) (ws []*Webhook, err error) {
  325. return getActiveWebhooksByOrgID(x, orgID)
  326. }
  327. func getActiveWebhooksByOrgID(e Engine, orgID int64) (ws []*Webhook, err error) {
  328. err = e.
  329. Where("org_id=?", orgID).
  330. And("is_active=?", true).
  331. Find(&ws)
  332. return ws, err
  333. }
  334. // GetWebhooksByOrgID returns paginated webhooks for an organization.
  335. func GetWebhooksByOrgID(orgID int64, listOptions ListOptions) ([]*Webhook, error) {
  336. if listOptions.Page == 0 {
  337. ws := make([]*Webhook, 0, 5)
  338. return ws, x.Find(&ws, &Webhook{OrgID: orgID})
  339. }
  340. sess := listOptions.getPaginatedSession()
  341. ws := make([]*Webhook, 0, listOptions.PageSize)
  342. return ws, sess.Find(&ws, &Webhook{OrgID: orgID})
  343. }
  344. // GetDefaultWebhooks returns all admin-default webhooks.
  345. func GetDefaultWebhooks() ([]*Webhook, error) {
  346. return getDefaultWebhooks(x)
  347. }
  348. func getDefaultWebhooks(e Engine) ([]*Webhook, error) {
  349. webhooks := make([]*Webhook, 0, 5)
  350. return webhooks, e.
  351. Where("repo_id=? AND org_id=? AND is_system_webhook=?", 0, 0, false).
  352. Find(&webhooks)
  353. }
  354. // GetSystemOrDefaultWebhook returns admin system or default webhook by given ID.
  355. func GetSystemOrDefaultWebhook(id int64) (*Webhook, error) {
  356. webhook := &Webhook{ID: id}
  357. has, err := x.
  358. Where("repo_id=? AND org_id=?", 0, 0).
  359. Get(webhook)
  360. if err != nil {
  361. return nil, err
  362. } else if !has {
  363. return nil, ErrWebhookNotExist{id}
  364. }
  365. return webhook, nil
  366. }
  367. // GetSystemWebhooks returns all admin system webhooks.
  368. func GetSystemWebhooks() ([]*Webhook, error) {
  369. return getSystemWebhooks(x)
  370. }
  371. func getSystemWebhooks(e Engine) ([]*Webhook, error) {
  372. webhooks := make([]*Webhook, 0, 5)
  373. return webhooks, e.
  374. Where("repo_id=? AND org_id=? AND is_system_webhook=?", 0, 0, true).
  375. Find(&webhooks)
  376. }
  377. // UpdateWebhook updates information of webhook.
  378. func UpdateWebhook(w *Webhook) error {
  379. _, err := x.ID(w.ID).AllCols().Update(w)
  380. return err
  381. }
  382. // UpdateWebhookLastStatus updates last status of webhook.
  383. func UpdateWebhookLastStatus(w *Webhook) error {
  384. _, err := x.ID(w.ID).Cols("last_status").Update(w)
  385. return err
  386. }
  387. // deleteWebhook uses argument bean as query condition,
  388. // ID must be specified and do not assign unnecessary fields.
  389. func deleteWebhook(bean *Webhook) (err error) {
  390. sess := x.NewSession()
  391. defer sess.Close()
  392. if err = sess.Begin(); err != nil {
  393. return err
  394. }
  395. if count, err := sess.Delete(bean); err != nil {
  396. return err
  397. } else if count == 0 {
  398. return ErrWebhookNotExist{ID: bean.ID}
  399. } else if _, err = sess.Delete(&HookTask{HookID: bean.ID}); err != nil {
  400. return err
  401. }
  402. return sess.Commit()
  403. }
  404. // DeleteWebhookByRepoID deletes webhook of repository by given ID.
  405. func DeleteWebhookByRepoID(repoID, id int64) error {
  406. return deleteWebhook(&Webhook{
  407. ID: id,
  408. RepoID: repoID,
  409. })
  410. }
  411. // DeleteWebhookByOrgID deletes webhook of organization by given ID.
  412. func DeleteWebhookByOrgID(orgID, id int64) error {
  413. return deleteWebhook(&Webhook{
  414. ID: id,
  415. OrgID: orgID,
  416. })
  417. }
  418. // DeleteDefaultSystemWebhook deletes an admin-configured default or system webhook (where Org and Repo ID both 0)
  419. func DeleteDefaultSystemWebhook(id int64) error {
  420. sess := x.NewSession()
  421. defer sess.Close()
  422. if err := sess.Begin(); err != nil {
  423. return err
  424. }
  425. count, err := sess.
  426. Where("repo_id=? AND org_id=?", 0, 0).
  427. Delete(&Webhook{ID: id})
  428. if err != nil {
  429. return err
  430. } else if count == 0 {
  431. return ErrWebhookNotExist{ID: id}
  432. }
  433. if _, err := sess.Delete(&HookTask{HookID: id}); err != nil {
  434. return err
  435. }
  436. return sess.Commit()
  437. }
  438. // copyDefaultWebhooksToRepo creates copies of the default webhooks in a new repo
  439. func copyDefaultWebhooksToRepo(e Engine, repoID int64) error {
  440. ws, err := getDefaultWebhooks(e)
  441. if err != nil {
  442. return fmt.Errorf("GetDefaultWebhooks: %v", err)
  443. }
  444. for _, w := range ws {
  445. w.ID = 0
  446. w.RepoID = repoID
  447. if err := createWebhook(e, w); err != nil {
  448. return fmt.Errorf("CreateWebhook: %v", err)
  449. }
  450. }
  451. return nil
  452. }
  453. // ___ ___ __ ___________ __
  454. // / | \ ____ ____ | | _\__ ___/____ _____| | __
  455. // / ~ \/ _ \ / _ \| |/ / | | \__ \ / ___/ |/ /
  456. // \ Y ( <_> | <_> ) < | | / __ \_\___ \| <
  457. // \___|_ / \____/ \____/|__|_ \ |____| (____ /____ >__|_ \
  458. // \/ \/ \/ \/ \/
  459. // HookTaskType is the type of an hook task
  460. type HookTaskType = string
  461. // Types of hook tasks
  462. const (
  463. GITEA HookTaskType = "gitea"
  464. GOGS HookTaskType = "gogs"
  465. SLACK HookTaskType = "slack"
  466. DISCORD HookTaskType = "discord"
  467. DINGTALK HookTaskType = "dingtalk"
  468. TELEGRAM HookTaskType = "telegram"
  469. MSTEAMS HookTaskType = "msteams"
  470. FEISHU HookTaskType = "feishu"
  471. MATRIX HookTaskType = "matrix"
  472. )
  473. // HookEventType is the type of an hook event
  474. type HookEventType string
  475. // Types of hook events
  476. const (
  477. HookEventCreate HookEventType = "create"
  478. HookEventDelete HookEventType = "delete"
  479. HookEventFork HookEventType = "fork"
  480. HookEventPush HookEventType = "push"
  481. HookEventIssues HookEventType = "issues"
  482. HookEventIssueAssign HookEventType = "issue_assign"
  483. HookEventIssueLabel HookEventType = "issue_label"
  484. HookEventIssueMilestone HookEventType = "issue_milestone"
  485. HookEventIssueComment HookEventType = "issue_comment"
  486. HookEventPullRequest HookEventType = "pull_request"
  487. HookEventPullRequestAssign HookEventType = "pull_request_assign"
  488. HookEventPullRequestLabel HookEventType = "pull_request_label"
  489. HookEventPullRequestMilestone HookEventType = "pull_request_milestone"
  490. HookEventPullRequestComment HookEventType = "pull_request_comment"
  491. HookEventPullRequestReviewApproved HookEventType = "pull_request_review_approved"
  492. HookEventPullRequestReviewRejected HookEventType = "pull_request_review_rejected"
  493. HookEventPullRequestReviewComment HookEventType = "pull_request_review_comment"
  494. HookEventPullRequestSync HookEventType = "pull_request_sync"
  495. HookEventRepository HookEventType = "repository"
  496. HookEventRelease HookEventType = "release"
  497. )
  498. // Event returns the HookEventType as an event string
  499. func (h HookEventType) Event() string {
  500. switch h {
  501. case HookEventCreate:
  502. return "create"
  503. case HookEventDelete:
  504. return "delete"
  505. case HookEventFork:
  506. return "fork"
  507. case HookEventPush:
  508. return "push"
  509. case HookEventIssues, HookEventIssueAssign, HookEventIssueLabel, HookEventIssueMilestone:
  510. return "issues"
  511. case HookEventPullRequest, HookEventPullRequestAssign, HookEventPullRequestLabel, HookEventPullRequestMilestone,
  512. HookEventPullRequestSync:
  513. return "pull_request"
  514. case HookEventIssueComment, HookEventPullRequestComment:
  515. return "issue_comment"
  516. case HookEventPullRequestReviewApproved:
  517. return "pull_request_approved"
  518. case HookEventPullRequestReviewRejected:
  519. return "pull_request_rejected"
  520. case HookEventPullRequestReviewComment:
  521. return "pull_request_comment"
  522. case HookEventRepository:
  523. return "repository"
  524. case HookEventRelease:
  525. return "release"
  526. }
  527. return ""
  528. }
  529. // HookRequest represents hook task request information.
  530. type HookRequest struct {
  531. Headers map[string]string `json:"headers"`
  532. }
  533. // HookResponse represents hook task response information.
  534. type HookResponse struct {
  535. Status int `json:"status"`
  536. Headers map[string]string `json:"headers"`
  537. Body string `json:"body"`
  538. }
  539. // HookTask represents a hook task.
  540. type HookTask struct {
  541. ID int64 `xorm:"pk autoincr"`
  542. RepoID int64 `xorm:"INDEX"`
  543. HookID int64
  544. UUID string
  545. Typ HookTaskType `xorm:"VARCHAR(16) index"`
  546. URL string `xorm:"TEXT"`
  547. Signature string `xorm:"TEXT"`
  548. api.Payloader `xorm:"-"`
  549. PayloadContent string `xorm:"TEXT"`
  550. HTTPMethod string `xorm:"http_method"`
  551. ContentType HookContentType
  552. EventType HookEventType
  553. IsSSL bool
  554. IsDelivered bool
  555. Delivered int64
  556. DeliveredString string `xorm:"-"`
  557. // History info.
  558. IsSucceed bool
  559. RequestContent string `xorm:"TEXT"`
  560. RequestInfo *HookRequest `xorm:"-"`
  561. ResponseContent string `xorm:"TEXT"`
  562. ResponseInfo *HookResponse `xorm:"-"`
  563. }
  564. // BeforeUpdate will be invoked by XORM before updating a record
  565. // representing this object
  566. func (t *HookTask) BeforeUpdate() {
  567. if t.RequestInfo != nil {
  568. t.RequestContent = t.simpleMarshalJSON(t.RequestInfo)
  569. }
  570. if t.ResponseInfo != nil {
  571. t.ResponseContent = t.simpleMarshalJSON(t.ResponseInfo)
  572. }
  573. }
  574. // AfterLoad updates the webhook object upon setting a column
  575. func (t *HookTask) AfterLoad() {
  576. t.DeliveredString = time.Unix(0, t.Delivered).Format("2006-01-02 15:04:05 MST")
  577. if len(t.RequestContent) == 0 {
  578. return
  579. }
  580. t.RequestInfo = &HookRequest{}
  581. if err := json.Unmarshal([]byte(t.RequestContent), t.RequestInfo); err != nil {
  582. log.Error("Unmarshal RequestContent[%d]: %v", t.ID, err)
  583. }
  584. if len(t.ResponseContent) > 0 {
  585. t.ResponseInfo = &HookResponse{}
  586. if err := json.Unmarshal([]byte(t.ResponseContent), t.ResponseInfo); err != nil {
  587. log.Error("Unmarshal ResponseContent[%d]: %v", t.ID, err)
  588. }
  589. }
  590. }
  591. func (t *HookTask) simpleMarshalJSON(v interface{}) string {
  592. p, err := json.Marshal(v)
  593. if err != nil {
  594. log.Error("Marshal [%d]: %v", t.ID, err)
  595. }
  596. return string(p)
  597. }
  598. // HookTasks returns a list of hook tasks by given conditions.
  599. func HookTasks(hookID int64, page int) ([]*HookTask, error) {
  600. tasks := make([]*HookTask, 0, setting.Webhook.PagingNum)
  601. return tasks, x.
  602. Limit(setting.Webhook.PagingNum, (page-1)*setting.Webhook.PagingNum).
  603. Where("hook_id=?", hookID).
  604. Desc("id").
  605. Find(&tasks)
  606. }
  607. // CreateHookTask creates a new hook task,
  608. // it handles conversion from Payload to PayloadContent.
  609. func CreateHookTask(t *HookTask) error {
  610. return createHookTask(x, t)
  611. }
  612. func createHookTask(e Engine, t *HookTask) error {
  613. data, err := t.Payloader.JSONPayload()
  614. if err != nil {
  615. return err
  616. }
  617. t.UUID = gouuid.New().String()
  618. t.PayloadContent = string(data)
  619. _, err = e.Insert(t)
  620. return err
  621. }
  622. // UpdateHookTask updates information of hook task.
  623. func UpdateHookTask(t *HookTask) error {
  624. _, err := x.ID(t.ID).AllCols().Update(t)
  625. return err
  626. }
  627. // FindUndeliveredHookTasks represents find the undelivered hook tasks
  628. func FindUndeliveredHookTasks() ([]*HookTask, error) {
  629. tasks := make([]*HookTask, 0, 10)
  630. if err := x.Where("is_delivered=?", false).Find(&tasks); err != nil {
  631. return nil, err
  632. }
  633. return tasks, nil
  634. }
  635. // FindRepoUndeliveredHookTasks represents find the undelivered hook tasks of one repository
  636. func FindRepoUndeliveredHookTasks(repoID int64) ([]*HookTask, error) {
  637. tasks := make([]*HookTask, 0, 5)
  638. if err := x.Where("repo_id=? AND is_delivered=?", repoID, false).Find(&tasks); err != nil {
  639. return nil, err
  640. }
  641. return tasks, nil
  642. }