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.

626 lines
17 KiB

10 years ago
10 years ago
8 years ago
8 years ago
8 years ago
10 years ago
10 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
5 years ago
10 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 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
5 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
5 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
5 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  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. "time"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/setting"
  12. api "code.gitea.io/gitea/modules/structs"
  13. "code.gitea.io/gitea/modules/timeutil"
  14. gouuid "github.com/satori/go.uuid"
  15. )
  16. // HookContentType is the content type of a web hook
  17. type HookContentType int
  18. const (
  19. // ContentTypeJSON is a JSON payload for web hooks
  20. ContentTypeJSON HookContentType = iota + 1
  21. // ContentTypeForm is an url-encoded form payload for web hook
  22. ContentTypeForm
  23. )
  24. var hookContentTypes = map[string]HookContentType{
  25. "json": ContentTypeJSON,
  26. "form": ContentTypeForm,
  27. }
  28. // ToHookContentType returns HookContentType by given name.
  29. func ToHookContentType(name string) HookContentType {
  30. return hookContentTypes[name]
  31. }
  32. // Name returns the name of a given web hook's content type
  33. func (t HookContentType) Name() string {
  34. switch t {
  35. case ContentTypeJSON:
  36. return "json"
  37. case ContentTypeForm:
  38. return "form"
  39. }
  40. return ""
  41. }
  42. // IsValidHookContentType returns true if given name is a valid hook content type.
  43. func IsValidHookContentType(name string) bool {
  44. _, ok := hookContentTypes[name]
  45. return ok
  46. }
  47. // HookEvents is a set of web hook events
  48. type HookEvents struct {
  49. Create bool `json:"create"`
  50. Delete bool `json:"delete"`
  51. Fork bool `json:"fork"`
  52. Issues bool `json:"issues"`
  53. IssueComment bool `json:"issue_comment"`
  54. Push bool `json:"push"`
  55. PullRequest bool `json:"pull_request"`
  56. Repository bool `json:"repository"`
  57. Release bool `json:"release"`
  58. }
  59. // HookEvent represents events that will delivery hook.
  60. type HookEvent struct {
  61. PushOnly bool `json:"push_only"`
  62. SendEverything bool `json:"send_everything"`
  63. ChooseEvents bool `json:"choose_events"`
  64. BranchFilter string `json:"branch_filter"`
  65. HookEvents `json:"events"`
  66. }
  67. // HookStatus is the status of a web hook
  68. type HookStatus int
  69. // Possible statuses of a web hook
  70. const (
  71. HookStatusNone = iota
  72. HookStatusSucceed
  73. HookStatusFail
  74. )
  75. // Webhook represents a web hook object.
  76. type Webhook struct {
  77. ID int64 `xorm:"pk autoincr"`
  78. RepoID int64 `xorm:"INDEX"`
  79. OrgID int64 `xorm:"INDEX"`
  80. URL string `xorm:"url TEXT"`
  81. Signature string `xorm:"TEXT"`
  82. HTTPMethod string `xorm:"http_method"`
  83. ContentType HookContentType
  84. Secret string `xorm:"TEXT"`
  85. Events string `xorm:"TEXT"`
  86. *HookEvent `xorm:"-"`
  87. IsSSL bool `xorm:"is_ssl"`
  88. IsActive bool `xorm:"INDEX"`
  89. HookTaskType HookTaskType
  90. Meta string `xorm:"TEXT"` // store hook-specific attributes
  91. LastStatus HookStatus // Last delivery status
  92. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  93. UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
  94. }
  95. // AfterLoad updates the webhook object upon setting a column
  96. func (w *Webhook) AfterLoad() {
  97. w.HookEvent = &HookEvent{}
  98. if err := json.Unmarshal([]byte(w.Events), w.HookEvent); err != nil {
  99. log.Error("Unmarshal[%d]: %v", w.ID, err)
  100. }
  101. }
  102. // History returns history of webhook by given conditions.
  103. func (w *Webhook) History(page int) ([]*HookTask, error) {
  104. return HookTasks(w.ID, page)
  105. }
  106. // UpdateEvent handles conversion from HookEvent to Events.
  107. func (w *Webhook) UpdateEvent() error {
  108. data, err := json.Marshal(w.HookEvent)
  109. w.Events = string(data)
  110. return err
  111. }
  112. // HasCreateEvent returns true if hook enabled create event.
  113. func (w *Webhook) HasCreateEvent() bool {
  114. return w.SendEverything ||
  115. (w.ChooseEvents && w.HookEvents.Create)
  116. }
  117. // HasDeleteEvent returns true if hook enabled delete event.
  118. func (w *Webhook) HasDeleteEvent() bool {
  119. return w.SendEverything ||
  120. (w.ChooseEvents && w.HookEvents.Delete)
  121. }
  122. // HasForkEvent returns true if hook enabled fork event.
  123. func (w *Webhook) HasForkEvent() bool {
  124. return w.SendEverything ||
  125. (w.ChooseEvents && w.HookEvents.Fork)
  126. }
  127. // HasIssuesEvent returns true if hook enabled issues event.
  128. func (w *Webhook) HasIssuesEvent() bool {
  129. return w.SendEverything ||
  130. (w.ChooseEvents && w.HookEvents.Issues)
  131. }
  132. // HasIssueCommentEvent returns true if hook enabled issue_comment event.
  133. func (w *Webhook) HasIssueCommentEvent() bool {
  134. return w.SendEverything ||
  135. (w.ChooseEvents && w.HookEvents.IssueComment)
  136. }
  137. // HasPushEvent returns true if hook enabled push event.
  138. func (w *Webhook) HasPushEvent() bool {
  139. return w.PushOnly || w.SendEverything ||
  140. (w.ChooseEvents && w.HookEvents.Push)
  141. }
  142. // HasPullRequestEvent returns true if hook enabled pull request event.
  143. func (w *Webhook) HasPullRequestEvent() bool {
  144. return w.SendEverything ||
  145. (w.ChooseEvents && w.HookEvents.PullRequest)
  146. }
  147. // HasReleaseEvent returns if hook enabled release event.
  148. func (w *Webhook) HasReleaseEvent() bool {
  149. return w.SendEverything ||
  150. (w.ChooseEvents && w.HookEvents.Release)
  151. }
  152. // HasRepositoryEvent returns if hook enabled repository event.
  153. func (w *Webhook) HasRepositoryEvent() bool {
  154. return w.SendEverything ||
  155. (w.ChooseEvents && w.HookEvents.Repository)
  156. }
  157. // EventCheckers returns event checkers
  158. func (w *Webhook) EventCheckers() []struct {
  159. Has func() bool
  160. Type HookEventType
  161. } {
  162. return []struct {
  163. Has func() bool
  164. Type HookEventType
  165. }{
  166. {w.HasCreateEvent, HookEventCreate},
  167. {w.HasDeleteEvent, HookEventDelete},
  168. {w.HasForkEvent, HookEventFork},
  169. {w.HasPushEvent, HookEventPush},
  170. {w.HasIssuesEvent, HookEventIssues},
  171. {w.HasIssueCommentEvent, HookEventIssueComment},
  172. {w.HasPullRequestEvent, HookEventPullRequest},
  173. {w.HasRepositoryEvent, HookEventRepository},
  174. {w.HasReleaseEvent, HookEventRelease},
  175. }
  176. }
  177. // EventsArray returns an array of hook events
  178. func (w *Webhook) EventsArray() []string {
  179. events := make([]string, 0, 7)
  180. for _, c := range w.EventCheckers() {
  181. if c.Has() {
  182. events = append(events, string(c.Type))
  183. }
  184. }
  185. return events
  186. }
  187. // CreateWebhook creates a new web hook.
  188. func CreateWebhook(w *Webhook) error {
  189. return createWebhook(x, w)
  190. }
  191. func createWebhook(e Engine, w *Webhook) error {
  192. _, err := e.Insert(w)
  193. return err
  194. }
  195. // getWebhook uses argument bean as query condition,
  196. // ID must be specified and do not assign unnecessary fields.
  197. func getWebhook(bean *Webhook) (*Webhook, error) {
  198. has, err := x.Get(bean)
  199. if err != nil {
  200. return nil, err
  201. } else if !has {
  202. return nil, ErrWebhookNotExist{bean.ID}
  203. }
  204. return bean, nil
  205. }
  206. // GetWebhookByID returns webhook of repository by given ID.
  207. func GetWebhookByID(id int64) (*Webhook, error) {
  208. return getWebhook(&Webhook{
  209. ID: id,
  210. })
  211. }
  212. // GetWebhookByRepoID returns webhook of repository by given ID.
  213. func GetWebhookByRepoID(repoID, id int64) (*Webhook, error) {
  214. return getWebhook(&Webhook{
  215. ID: id,
  216. RepoID: repoID,
  217. })
  218. }
  219. // GetWebhookByOrgID returns webhook of organization by given ID.
  220. func GetWebhookByOrgID(orgID, id int64) (*Webhook, error) {
  221. return getWebhook(&Webhook{
  222. ID: id,
  223. OrgID: orgID,
  224. })
  225. }
  226. // GetActiveWebhooksByRepoID returns all active webhooks of repository.
  227. func GetActiveWebhooksByRepoID(repoID int64) ([]*Webhook, error) {
  228. return getActiveWebhooksByRepoID(x, repoID)
  229. }
  230. func getActiveWebhooksByRepoID(e Engine, repoID int64) ([]*Webhook, error) {
  231. webhooks := make([]*Webhook, 0, 5)
  232. return webhooks, e.Where("is_active=?", true).
  233. Find(&webhooks, &Webhook{RepoID: repoID})
  234. }
  235. // GetWebhooksByRepoID returns all webhooks of a repository.
  236. func GetWebhooksByRepoID(repoID int64) ([]*Webhook, error) {
  237. webhooks := make([]*Webhook, 0, 5)
  238. return webhooks, x.Find(&webhooks, &Webhook{RepoID: repoID})
  239. }
  240. // GetActiveWebhooksByOrgID returns all active webhooks for an organization.
  241. func GetActiveWebhooksByOrgID(orgID int64) (ws []*Webhook, err error) {
  242. return getActiveWebhooksByOrgID(x, orgID)
  243. }
  244. func getActiveWebhooksByOrgID(e Engine, orgID int64) (ws []*Webhook, err error) {
  245. err = e.
  246. Where("org_id=?", orgID).
  247. And("is_active=?", true).
  248. Find(&ws)
  249. return ws, err
  250. }
  251. // GetWebhooksByOrgID returns all webhooks for an organization.
  252. func GetWebhooksByOrgID(orgID int64) (ws []*Webhook, err error) {
  253. err = x.Find(&ws, &Webhook{OrgID: orgID})
  254. return ws, err
  255. }
  256. // GetDefaultWebhook returns admin-default webhook by given ID.
  257. func GetDefaultWebhook(id int64) (*Webhook, error) {
  258. webhook := &Webhook{ID: id}
  259. has, err := x.
  260. Where("repo_id=? AND org_id=?", 0, 0).
  261. Get(webhook)
  262. if err != nil {
  263. return nil, err
  264. } else if !has {
  265. return nil, ErrWebhookNotExist{id}
  266. }
  267. return webhook, nil
  268. }
  269. // GetDefaultWebhooks returns all admin-default webhooks.
  270. func GetDefaultWebhooks() ([]*Webhook, error) {
  271. return getDefaultWebhooks(x)
  272. }
  273. func getDefaultWebhooks(e Engine) ([]*Webhook, error) {
  274. webhooks := make([]*Webhook, 0, 5)
  275. return webhooks, e.
  276. Where("repo_id=? AND org_id=?", 0, 0).
  277. Find(&webhooks)
  278. }
  279. // UpdateWebhook updates information of webhook.
  280. func UpdateWebhook(w *Webhook) error {
  281. _, err := x.ID(w.ID).AllCols().Update(w)
  282. return err
  283. }
  284. // UpdateWebhookLastStatus updates last status of webhook.
  285. func UpdateWebhookLastStatus(w *Webhook) error {
  286. _, err := x.ID(w.ID).Cols("last_status").Update(w)
  287. return err
  288. }
  289. // deleteWebhook uses argument bean as query condition,
  290. // ID must be specified and do not assign unnecessary fields.
  291. func deleteWebhook(bean *Webhook) (err error) {
  292. sess := x.NewSession()
  293. defer sess.Close()
  294. if err = sess.Begin(); err != nil {
  295. return err
  296. }
  297. if count, err := sess.Delete(bean); err != nil {
  298. return err
  299. } else if count == 0 {
  300. return ErrWebhookNotExist{ID: bean.ID}
  301. } else if _, err = sess.Delete(&HookTask{HookID: bean.ID}); err != nil {
  302. return err
  303. }
  304. return sess.Commit()
  305. }
  306. // DeleteWebhookByRepoID deletes webhook of repository by given ID.
  307. func DeleteWebhookByRepoID(repoID, id int64) error {
  308. return deleteWebhook(&Webhook{
  309. ID: id,
  310. RepoID: repoID,
  311. })
  312. }
  313. // DeleteWebhookByOrgID deletes webhook of organization by given ID.
  314. func DeleteWebhookByOrgID(orgID, id int64) error {
  315. return deleteWebhook(&Webhook{
  316. ID: id,
  317. OrgID: orgID,
  318. })
  319. }
  320. // DeleteDefaultWebhook deletes an admin-default webhook by given ID.
  321. func DeleteDefaultWebhook(id int64) error {
  322. sess := x.NewSession()
  323. defer sess.Close()
  324. if err := sess.Begin(); err != nil {
  325. return err
  326. }
  327. count, err := sess.
  328. Where("repo_id=? AND org_id=?", 0, 0).
  329. Delete(&Webhook{ID: id})
  330. if err != nil {
  331. return err
  332. } else if count == 0 {
  333. return ErrWebhookNotExist{ID: id}
  334. }
  335. if _, err := sess.Delete(&HookTask{HookID: id}); err != nil {
  336. return err
  337. }
  338. return sess.Commit()
  339. }
  340. // copyDefaultWebhooksToRepo creates copies of the default webhooks in a new repo
  341. func copyDefaultWebhooksToRepo(e Engine, repoID int64) error {
  342. ws, err := getDefaultWebhooks(e)
  343. if err != nil {
  344. return fmt.Errorf("GetDefaultWebhooks: %v", err)
  345. }
  346. for _, w := range ws {
  347. w.ID = 0
  348. w.RepoID = repoID
  349. if err := createWebhook(e, w); err != nil {
  350. return fmt.Errorf("CreateWebhook: %v", err)
  351. }
  352. }
  353. return nil
  354. }
  355. // ___ ___ __ ___________ __
  356. // / | \ ____ ____ | | _\__ ___/____ _____| | __
  357. // / ~ \/ _ \ / _ \| |/ / | | \__ \ / ___/ |/ /
  358. // \ Y ( <_> | <_> ) < | | / __ \_\___ \| <
  359. // \___|_ / \____/ \____/|__|_ \ |____| (____ /____ >__|_ \
  360. // \/ \/ \/ \/ \/
  361. // HookTaskType is the type of an hook task
  362. type HookTaskType int
  363. // Types of hook tasks
  364. const (
  365. GOGS HookTaskType = iota + 1
  366. SLACK
  367. GITEA
  368. DISCORD
  369. DINGTALK
  370. TELEGRAM
  371. MSTEAMS
  372. )
  373. var hookTaskTypes = map[string]HookTaskType{
  374. "gitea": GITEA,
  375. "gogs": GOGS,
  376. "slack": SLACK,
  377. "discord": DISCORD,
  378. "dingtalk": DINGTALK,
  379. "telegram": TELEGRAM,
  380. "msteams": MSTEAMS,
  381. }
  382. // ToHookTaskType returns HookTaskType by given name.
  383. func ToHookTaskType(name string) HookTaskType {
  384. return hookTaskTypes[name]
  385. }
  386. // Name returns the name of an hook task type
  387. func (t HookTaskType) Name() string {
  388. switch t {
  389. case GITEA:
  390. return "gitea"
  391. case GOGS:
  392. return "gogs"
  393. case SLACK:
  394. return "slack"
  395. case DISCORD:
  396. return "discord"
  397. case DINGTALK:
  398. return "dingtalk"
  399. case TELEGRAM:
  400. return "telegram"
  401. case MSTEAMS:
  402. return "msteams"
  403. }
  404. return ""
  405. }
  406. // IsValidHookTaskType returns true if given name is a valid hook task type.
  407. func IsValidHookTaskType(name string) bool {
  408. _, ok := hookTaskTypes[name]
  409. return ok
  410. }
  411. // HookEventType is the type of an hook event
  412. type HookEventType string
  413. // Types of hook events
  414. const (
  415. HookEventCreate HookEventType = "create"
  416. HookEventDelete HookEventType = "delete"
  417. HookEventFork HookEventType = "fork"
  418. HookEventPush HookEventType = "push"
  419. HookEventIssues HookEventType = "issues"
  420. HookEventIssueComment HookEventType = "issue_comment"
  421. HookEventPullRequest HookEventType = "pull_request"
  422. HookEventRepository HookEventType = "repository"
  423. HookEventRelease HookEventType = "release"
  424. HookEventPullRequestApproved HookEventType = "pull_request_approved"
  425. HookEventPullRequestRejected HookEventType = "pull_request_rejected"
  426. HookEventPullRequestComment HookEventType = "pull_request_comment"
  427. )
  428. // HookRequest represents hook task request information.
  429. type HookRequest struct {
  430. Headers map[string]string `json:"headers"`
  431. }
  432. // HookResponse represents hook task response information.
  433. type HookResponse struct {
  434. Status int `json:"status"`
  435. Headers map[string]string `json:"headers"`
  436. Body string `json:"body"`
  437. }
  438. // HookTask represents a hook task.
  439. type HookTask struct {
  440. ID int64 `xorm:"pk autoincr"`
  441. RepoID int64 `xorm:"INDEX"`
  442. HookID int64
  443. UUID string
  444. Type HookTaskType
  445. URL string `xorm:"TEXT"`
  446. Signature string `xorm:"TEXT"`
  447. api.Payloader `xorm:"-"`
  448. PayloadContent string `xorm:"TEXT"`
  449. HTTPMethod string `xorm:"http_method"`
  450. ContentType HookContentType
  451. EventType HookEventType
  452. IsSSL bool
  453. IsDelivered bool
  454. Delivered int64
  455. DeliveredString string `xorm:"-"`
  456. // History info.
  457. IsSucceed bool
  458. RequestContent string `xorm:"TEXT"`
  459. RequestInfo *HookRequest `xorm:"-"`
  460. ResponseContent string `xorm:"TEXT"`
  461. ResponseInfo *HookResponse `xorm:"-"`
  462. }
  463. // BeforeUpdate will be invoked by XORM before updating a record
  464. // representing this object
  465. func (t *HookTask) BeforeUpdate() {
  466. if t.RequestInfo != nil {
  467. t.RequestContent = t.simpleMarshalJSON(t.RequestInfo)
  468. }
  469. if t.ResponseInfo != nil {
  470. t.ResponseContent = t.simpleMarshalJSON(t.ResponseInfo)
  471. }
  472. }
  473. // AfterLoad updates the webhook object upon setting a column
  474. func (t *HookTask) AfterLoad() {
  475. t.DeliveredString = time.Unix(0, t.Delivered).Format("2006-01-02 15:04:05 MST")
  476. if len(t.RequestContent) == 0 {
  477. return
  478. }
  479. t.RequestInfo = &HookRequest{}
  480. if err := json.Unmarshal([]byte(t.RequestContent), t.RequestInfo); err != nil {
  481. log.Error("Unmarshal RequestContent[%d]: %v", t.ID, err)
  482. }
  483. if len(t.ResponseContent) > 0 {
  484. t.ResponseInfo = &HookResponse{}
  485. if err := json.Unmarshal([]byte(t.ResponseContent), t.ResponseInfo); err != nil {
  486. log.Error("Unmarshal ResponseContent[%d]: %v", t.ID, err)
  487. }
  488. }
  489. }
  490. func (t *HookTask) simpleMarshalJSON(v interface{}) string {
  491. p, err := json.Marshal(v)
  492. if err != nil {
  493. log.Error("Marshal [%d]: %v", t.ID, err)
  494. }
  495. return string(p)
  496. }
  497. // HookTasks returns a list of hook tasks by given conditions.
  498. func HookTasks(hookID int64, page int) ([]*HookTask, error) {
  499. tasks := make([]*HookTask, 0, setting.Webhook.PagingNum)
  500. return tasks, x.
  501. Limit(setting.Webhook.PagingNum, (page-1)*setting.Webhook.PagingNum).
  502. Where("hook_id=?", hookID).
  503. Desc("id").
  504. Find(&tasks)
  505. }
  506. // CreateHookTask creates a new hook task,
  507. // it handles conversion from Payload to PayloadContent.
  508. func CreateHookTask(t *HookTask) error {
  509. return createHookTask(x, t)
  510. }
  511. func createHookTask(e Engine, t *HookTask) error {
  512. data, err := t.Payloader.JSONPayload()
  513. if err != nil {
  514. return err
  515. }
  516. t.UUID = gouuid.NewV4().String()
  517. t.PayloadContent = string(data)
  518. _, err = e.Insert(t)
  519. return err
  520. }
  521. // UpdateHookTask updates information of hook task.
  522. func UpdateHookTask(t *HookTask) error {
  523. _, err := x.ID(t.ID).AllCols().Update(t)
  524. return err
  525. }
  526. // FindUndeliveredHookTasks represents find the undelivered hook tasks
  527. func FindUndeliveredHookTasks() ([]*HookTask, error) {
  528. tasks := make([]*HookTask, 0, 10)
  529. if err := x.Where("is_delivered=?", false).Find(&tasks); err != nil {
  530. return nil, err
  531. }
  532. return tasks, nil
  533. }
  534. // FindRepoUndeliveredHookTasks represents find the undelivered hook tasks of one repository
  535. func FindRepoUndeliveredHookTasks(repoID int64) ([]*HookTask, error) {
  536. tasks := make([]*HookTask, 0, 5)
  537. if err := x.Where("repo_id=? AND is_delivered=?", repoID, false).Find(&tasks); err != nil {
  538. return nil, err
  539. }
  540. return tasks, nil
  541. }