gitea源码

packagist.go 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package webhook
  4. import (
  5. "context"
  6. "fmt"
  7. "net/http"
  8. webhook_model "code.gitea.io/gitea/models/webhook"
  9. "code.gitea.io/gitea/modules/json"
  10. "code.gitea.io/gitea/modules/log"
  11. api "code.gitea.io/gitea/modules/structs"
  12. webhook_module "code.gitea.io/gitea/modules/webhook"
  13. )
  14. type (
  15. // PackagistPayload represents
  16. PackagistPayload struct {
  17. PackagistRepository struct {
  18. URL string `json:"url"`
  19. } `json:"repository"`
  20. }
  21. // PackagistMeta contains the metadata for the webhook
  22. PackagistMeta struct {
  23. Username string `json:"username"`
  24. APIToken string `json:"api_token"`
  25. PackageURL string `json:"package_url"`
  26. }
  27. )
  28. // GetPackagistHook returns packagist metadata
  29. func GetPackagistHook(w *webhook_model.Webhook) *PackagistMeta {
  30. s := &PackagistMeta{}
  31. if err := json.Unmarshal([]byte(w.Meta), s); err != nil {
  32. log.Error("webhook.GetPackagistHook(%d): %v", w.ID, err)
  33. }
  34. return s
  35. }
  36. type packagistConvertor struct {
  37. PackageURL string
  38. }
  39. // Create implements PayloadConvertor Create method
  40. func (pc packagistConvertor) Create(_ *api.CreatePayload) (PackagistPayload, error) {
  41. return PackagistPayload{}, nil
  42. }
  43. // Delete implements PayloadConvertor Delete method
  44. func (pc packagistConvertor) Delete(_ *api.DeletePayload) (PackagistPayload, error) {
  45. return PackagistPayload{}, nil
  46. }
  47. // Fork implements PayloadConvertor Fork method
  48. func (pc packagistConvertor) Fork(_ *api.ForkPayload) (PackagistPayload, error) {
  49. return PackagistPayload{}, nil
  50. }
  51. // Push implements PayloadConvertor Push method
  52. // https://packagist.org/about
  53. func (pc packagistConvertor) Push(_ *api.PushPayload) (PackagistPayload, error) {
  54. return PackagistPayload{
  55. PackagistRepository: struct {
  56. URL string `json:"url"`
  57. }{
  58. URL: pc.PackageURL,
  59. },
  60. }, nil
  61. }
  62. // Issue implements PayloadConvertor Issue method
  63. func (pc packagistConvertor) Issue(_ *api.IssuePayload) (PackagistPayload, error) {
  64. return PackagistPayload{}, nil
  65. }
  66. // IssueComment implements PayloadConvertor IssueComment method
  67. func (pc packagistConvertor) IssueComment(_ *api.IssueCommentPayload) (PackagistPayload, error) {
  68. return PackagistPayload{}, nil
  69. }
  70. // PullRequest implements PayloadConvertor PullRequest method
  71. func (pc packagistConvertor) PullRequest(_ *api.PullRequestPayload) (PackagistPayload, error) {
  72. return PackagistPayload{}, nil
  73. }
  74. // Review implements PayloadConvertor Review method
  75. func (pc packagistConvertor) Review(_ *api.PullRequestPayload, _ webhook_module.HookEventType) (PackagistPayload, error) {
  76. return PackagistPayload{}, nil
  77. }
  78. // Repository implements PayloadConvertor Repository method
  79. func (pc packagistConvertor) Repository(_ *api.RepositoryPayload) (PackagistPayload, error) {
  80. return PackagistPayload{}, nil
  81. }
  82. // Wiki implements PayloadConvertor Wiki method
  83. func (pc packagistConvertor) Wiki(_ *api.WikiPayload) (PackagistPayload, error) {
  84. return PackagistPayload{}, nil
  85. }
  86. // Release implements PayloadConvertor Release method
  87. func (pc packagistConvertor) Release(_ *api.ReleasePayload) (PackagistPayload, error) {
  88. return PackagistPayload{}, nil
  89. }
  90. func (pc packagistConvertor) Package(_ *api.PackagePayload) (PackagistPayload, error) {
  91. return PackagistPayload{}, nil
  92. }
  93. func (pc packagistConvertor) Status(_ *api.CommitStatusPayload) (PackagistPayload, error) {
  94. return PackagistPayload{}, nil
  95. }
  96. func (pc packagistConvertor) WorkflowRun(_ *api.WorkflowRunPayload) (PackagistPayload, error) {
  97. return PackagistPayload{}, nil
  98. }
  99. func (pc packagistConvertor) WorkflowJob(_ *api.WorkflowJobPayload) (PackagistPayload, error) {
  100. return PackagistPayload{}, nil
  101. }
  102. func newPackagistRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
  103. meta := &PackagistMeta{}
  104. if err := json.Unmarshal([]byte(w.Meta), meta); err != nil {
  105. return nil, nil, fmt.Errorf("newpackagistRequest meta json: %w", err)
  106. }
  107. var pc payloadConvertor[PackagistPayload] = packagistConvertor{
  108. PackageURL: meta.PackageURL,
  109. }
  110. return newJSONRequest(pc, w, t, true)
  111. }
  112. func init() {
  113. RegisterWebhookRequester(webhook_module.PACKAGIST, newPackagistRequest)
  114. }