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