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.

payloader.go 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package webhook
  4. import (
  5. "bytes"
  6. "fmt"
  7. "net/http"
  8. webhook_model "code.gitea.io/gitea/models/webhook"
  9. "code.gitea.io/gitea/modules/json"
  10. api "code.gitea.io/gitea/modules/structs"
  11. webhook_module "code.gitea.io/gitea/modules/webhook"
  12. )
  13. // payloadConvertor defines the interface to convert system payload to webhook payload
  14. type payloadConvertor[T any] interface {
  15. Create(*api.CreatePayload) (T, error)
  16. Delete(*api.DeletePayload) (T, error)
  17. Fork(*api.ForkPayload) (T, error)
  18. Issue(*api.IssuePayload) (T, error)
  19. IssueComment(*api.IssueCommentPayload) (T, error)
  20. Push(*api.PushPayload) (T, error)
  21. PullRequest(*api.PullRequestPayload) (T, error)
  22. Review(*api.PullRequestPayload, webhook_module.HookEventType) (T, error)
  23. Repository(*api.RepositoryPayload) (T, error)
  24. Release(*api.ReleasePayload) (T, error)
  25. Wiki(*api.WikiPayload) (T, error)
  26. Package(*api.PackagePayload) (T, error)
  27. }
  28. func convertUnmarshalledJSON[T, P any](convert func(P) (T, error), data []byte) (T, error) {
  29. var p P
  30. if err := json.Unmarshal(data, &p); err != nil {
  31. var t T
  32. return t, fmt.Errorf("could not unmarshal payload: %w", err)
  33. }
  34. return convert(p)
  35. }
  36. func newPayload[T any](rc payloadConvertor[T], data []byte, event webhook_module.HookEventType) (T, error) {
  37. switch event {
  38. case webhook_module.HookEventCreate:
  39. return convertUnmarshalledJSON(rc.Create, data)
  40. case webhook_module.HookEventDelete:
  41. return convertUnmarshalledJSON(rc.Delete, data)
  42. case webhook_module.HookEventFork:
  43. return convertUnmarshalledJSON(rc.Fork, data)
  44. case webhook_module.HookEventIssues, webhook_module.HookEventIssueAssign, webhook_module.HookEventIssueLabel, webhook_module.HookEventIssueMilestone:
  45. return convertUnmarshalledJSON(rc.Issue, data)
  46. case webhook_module.HookEventIssueComment, webhook_module.HookEventPullRequestComment:
  47. // previous code sometimes sent s.PullRequest(p.(*api.PullRequestPayload))
  48. // however I couldn't find in notifier.go such a payload with an HookEvent***Comment event
  49. // History (most recent first):
  50. // - refactored in https://github.com/go-gitea/gitea/pull/12310
  51. // - assertion added in https://github.com/go-gitea/gitea/pull/12046
  52. // - issue raised in https://github.com/go-gitea/gitea/issues/11940#issuecomment-645713996
  53. // > That's because for HookEventPullRequestComment event, some places use IssueCommentPayload and others use PullRequestPayload
  54. // In modules/actions/workflows.go:183 the type assertion is always payload.(*api.IssueCommentPayload)
  55. return convertUnmarshalledJSON(rc.IssueComment, data)
  56. case webhook_module.HookEventPush:
  57. return convertUnmarshalledJSON(rc.Push, data)
  58. case webhook_module.HookEventPullRequest, webhook_module.HookEventPullRequestAssign, webhook_module.HookEventPullRequestLabel,
  59. webhook_module.HookEventPullRequestMilestone, webhook_module.HookEventPullRequestSync, webhook_module.HookEventPullRequestReviewRequest:
  60. return convertUnmarshalledJSON(rc.PullRequest, data)
  61. case webhook_module.HookEventPullRequestReviewApproved, webhook_module.HookEventPullRequestReviewRejected, webhook_module.HookEventPullRequestReviewComment:
  62. return convertUnmarshalledJSON(func(p *api.PullRequestPayload) (T, error) {
  63. return rc.Review(p, event)
  64. }, data)
  65. case webhook_module.HookEventRepository:
  66. return convertUnmarshalledJSON(rc.Repository, data)
  67. case webhook_module.HookEventRelease:
  68. return convertUnmarshalledJSON(rc.Release, data)
  69. case webhook_module.HookEventWiki:
  70. return convertUnmarshalledJSON(rc.Wiki, data)
  71. case webhook_module.HookEventPackage:
  72. return convertUnmarshalledJSON(rc.Package, data)
  73. }
  74. var t T
  75. return t, fmt.Errorf("newPayload unsupported event: %s", event)
  76. }
  77. func newJSONRequest[T any](pc payloadConvertor[T], w *webhook_model.Webhook, t *webhook_model.HookTask, withDefaultHeaders bool) (*http.Request, []byte, error) {
  78. payload, err := newPayload(pc, []byte(t.PayloadContent), t.EventType)
  79. if err != nil {
  80. return nil, nil, err
  81. }
  82. body, err := json.MarshalIndent(payload, "", " ")
  83. if err != nil {
  84. return nil, nil, err
  85. }
  86. method := w.HTTPMethod
  87. if method == "" {
  88. method = http.MethodPost
  89. }
  90. req, err := http.NewRequest(method, w.URL, bytes.NewReader(body))
  91. if err != nil {
  92. return nil, nil, err
  93. }
  94. req.Header.Set("Content-Type", "application/json")
  95. if withDefaultHeaders {
  96. return req, body, addDefaultHeaders(req, []byte(w.Secret), t, body)
  97. }
  98. return req, body, nil
  99. }