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.

packagist.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package webhook
  4. import (
  5. "errors"
  6. webhook_model "code.gitea.io/gitea/models/webhook"
  7. "code.gitea.io/gitea/modules/json"
  8. "code.gitea.io/gitea/modules/log"
  9. api "code.gitea.io/gitea/modules/structs"
  10. webhook_module "code.gitea.io/gitea/modules/webhook"
  11. )
  12. type (
  13. // PackagistPayload represents
  14. PackagistPayload struct {
  15. PackagistRepository struct {
  16. URL string `json:"url"`
  17. } `json:"repository"`
  18. }
  19. // PackagistMeta contains the metadata for the webhook
  20. PackagistMeta struct {
  21. Username string `json:"username"`
  22. APIToken string `json:"api_token"`
  23. PackageURL string `json:"package_url"`
  24. }
  25. )
  26. // GetPackagistHook returns packagist metadata
  27. func GetPackagistHook(w *webhook_model.Webhook) *PackagistMeta {
  28. s := &PackagistMeta{}
  29. if err := json.Unmarshal([]byte(w.Meta), s); err != nil {
  30. log.Error("webhook.GetPackagistHook(%d): %v", w.ID, err)
  31. }
  32. return s
  33. }
  34. // JSONPayload Marshals the PackagistPayload to json
  35. func (f *PackagistPayload) JSONPayload() ([]byte, error) {
  36. data, err := json.MarshalIndent(f, "", " ")
  37. if err != nil {
  38. return []byte{}, err
  39. }
  40. return data, nil
  41. }
  42. var _ PayloadConvertor = &PackagistPayload{}
  43. // Create implements PayloadConvertor Create method
  44. func (f *PackagistPayload) Create(_ *api.CreatePayload) (api.Payloader, error) {
  45. return nil, nil
  46. }
  47. // Delete implements PayloadConvertor Delete method
  48. func (f *PackagistPayload) Delete(_ *api.DeletePayload) (api.Payloader, error) {
  49. return nil, nil
  50. }
  51. // Fork implements PayloadConvertor Fork method
  52. func (f *PackagistPayload) Fork(_ *api.ForkPayload) (api.Payloader, error) {
  53. return nil, nil
  54. }
  55. // Push implements PayloadConvertor Push method
  56. func (f *PackagistPayload) Push(_ *api.PushPayload) (api.Payloader, error) {
  57. return f, nil
  58. }
  59. // Issue implements PayloadConvertor Issue method
  60. func (f *PackagistPayload) Issue(_ *api.IssuePayload) (api.Payloader, error) {
  61. return nil, nil
  62. }
  63. // IssueComment implements PayloadConvertor IssueComment method
  64. func (f *PackagistPayload) IssueComment(_ *api.IssueCommentPayload) (api.Payloader, error) {
  65. return nil, nil
  66. }
  67. // PullRequest implements PayloadConvertor PullRequest method
  68. func (f *PackagistPayload) PullRequest(_ *api.PullRequestPayload) (api.Payloader, error) {
  69. return nil, nil
  70. }
  71. // Review implements PayloadConvertor Review method
  72. func (f *PackagistPayload) Review(_ *api.PullRequestPayload, _ webhook_module.HookEventType) (api.Payloader, error) {
  73. return nil, nil
  74. }
  75. // Repository implements PayloadConvertor Repository method
  76. func (f *PackagistPayload) Repository(_ *api.RepositoryPayload) (api.Payloader, error) {
  77. return nil, nil
  78. }
  79. // Wiki implements PayloadConvertor Wiki method
  80. func (f *PackagistPayload) Wiki(_ *api.WikiPayload) (api.Payloader, error) {
  81. return nil, nil
  82. }
  83. // Release implements PayloadConvertor Release method
  84. func (f *PackagistPayload) Release(_ *api.ReleasePayload) (api.Payloader, error) {
  85. return nil, nil
  86. }
  87. func (f *PackagistPayload) Package(_ *api.PackagePayload) (api.Payloader, error) {
  88. return nil, nil
  89. }
  90. // GetPackagistPayload converts a packagist webhook into a PackagistPayload
  91. func GetPackagistPayload(p api.Payloader, event webhook_module.HookEventType, meta string) (api.Payloader, error) {
  92. s := new(PackagistPayload)
  93. packagist := &PackagistMeta{}
  94. if err := json.Unmarshal([]byte(meta), &packagist); err != nil {
  95. return s, errors.New("GetPackagistPayload meta json:" + err.Error())
  96. }
  97. s.PackagistRepository.URL = packagist.PackageURL
  98. return convertPayloader(s, p, event)
  99. }