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