2022-01-23 19:16:30 +05:30
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2022-01-23 19:16:30 +05:30
|
|
|
|
|
|
|
package webhook
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
webhook_model "code.gitea.io/gitea/models/webhook"
|
|
|
|
"code.gitea.io/gitea/modules/json"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
2023-01-01 20:53:15 +05:30
|
|
|
webhook_module "code.gitea.io/gitea/modules/webhook"
|
2022-01-23 19:16:30 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// PackagistPayload represents
|
|
|
|
PackagistPayload struct {
|
|
|
|
PackagistRepository struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
} `json:"repository"`
|
|
|
|
}
|
|
|
|
|
2023-01-01 20:53:15 +05:30
|
|
|
// PackagistMeta contains the metadata for the webhook
|
2022-01-23 19:16:30 +05:30
|
|
|
PackagistMeta struct {
|
|
|
|
Username string `json:"username"`
|
|
|
|
APIToken string `json:"api_token"`
|
|
|
|
PackageURL string `json:"package_url"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetPackagistHook returns packagist metadata
|
|
|
|
func GetPackagistHook(w *webhook_model.Webhook) *PackagistMeta {
|
|
|
|
s := &PackagistMeta{}
|
|
|
|
if err := json.Unmarshal([]byte(w.Meta), s); err != nil {
|
|
|
|
log.Error("webhook.GetPackagistHook(%d): %v", w.ID, err)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
// JSONPayload Marshals the PackagistPayload to json
|
|
|
|
func (f *PackagistPayload) JSONPayload() ([]byte, error) {
|
|
|
|
data, err := json.MarshalIndent(f, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ PayloadConvertor = &PackagistPayload{}
|
|
|
|
|
|
|
|
// Create implements PayloadConvertor Create method
|
2023-01-01 20:53:15 +05:30
|
|
|
func (f *PackagistPayload) Create(_ *api.CreatePayload) (api.Payloader, error) {
|
2022-01-23 19:16:30 +05:30
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete implements PayloadConvertor Delete method
|
2023-01-01 20:53:15 +05:30
|
|
|
func (f *PackagistPayload) Delete(_ *api.DeletePayload) (api.Payloader, error) {
|
2022-01-23 19:16:30 +05:30
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fork implements PayloadConvertor Fork method
|
2023-01-01 20:53:15 +05:30
|
|
|
func (f *PackagistPayload) Fork(_ *api.ForkPayload) (api.Payloader, error) {
|
2022-01-23 19:16:30 +05:30
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Push implements PayloadConvertor Push method
|
2023-01-01 20:53:15 +05:30
|
|
|
func (f *PackagistPayload) Push(_ *api.PushPayload) (api.Payloader, error) {
|
2022-01-23 19:16:30 +05:30
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Issue implements PayloadConvertor Issue method
|
2023-01-01 20:53:15 +05:30
|
|
|
func (f *PackagistPayload) Issue(_ *api.IssuePayload) (api.Payloader, error) {
|
2022-01-23 19:16:30 +05:30
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// IssueComment implements PayloadConvertor IssueComment method
|
2023-01-01 20:53:15 +05:30
|
|
|
func (f *PackagistPayload) IssueComment(_ *api.IssueCommentPayload) (api.Payloader, error) {
|
2022-01-23 19:16:30 +05:30
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PullRequest implements PayloadConvertor PullRequest method
|
2023-01-01 20:53:15 +05:30
|
|
|
func (f *PackagistPayload) PullRequest(_ *api.PullRequestPayload) (api.Payloader, error) {
|
2022-01-23 19:16:30 +05:30
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Review implements PayloadConvertor Review method
|
2023-01-01 20:53:15 +05:30
|
|
|
func (f *PackagistPayload) Review(_ *api.PullRequestPayload, _ webhook_module.HookEventType) (api.Payloader, error) {
|
2022-01-23 19:16:30 +05:30
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Repository implements PayloadConvertor Repository method
|
2023-01-01 20:53:15 +05:30
|
|
|
func (f *PackagistPayload) Repository(_ *api.RepositoryPayload) (api.Payloader, error) {
|
2022-01-23 19:16:30 +05:30
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-09-05 01:24:23 +05:30
|
|
|
// Wiki implements PayloadConvertor Wiki method
|
2023-01-01 20:53:15 +05:30
|
|
|
func (f *PackagistPayload) Wiki(_ *api.WikiPayload) (api.Payloader, error) {
|
2022-09-05 01:24:23 +05:30
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-01-23 19:16:30 +05:30
|
|
|
// Release implements PayloadConvertor Release method
|
2023-01-01 20:53:15 +05:30
|
|
|
func (f *PackagistPayload) Release(_ *api.ReleasePayload) (api.Payloader, error) {
|
2022-01-23 19:16:30 +05:30
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPackagistPayload converts a packagist webhook into a PackagistPayload
|
2023-01-01 20:53:15 +05:30
|
|
|
func GetPackagistPayload(p api.Payloader, event webhook_module.HookEventType, meta string) (api.Payloader, error) {
|
2022-01-23 19:16:30 +05:30
|
|
|
s := new(PackagistPayload)
|
|
|
|
|
|
|
|
packagist := &PackagistMeta{}
|
|
|
|
if err := json.Unmarshal([]byte(meta), &packagist); err != nil {
|
|
|
|
return s, errors.New("GetPackagistPayload meta json:" + err.Error())
|
|
|
|
}
|
|
|
|
s.PackagistRepository.URL = packagist.PackageURL
|
|
|
|
return convertPayloader(s, p, event)
|
|
|
|
}
|