2022-01-23 13:46:30 +00:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2022-01-23 13:46:30 +00:00
|
|
|
|
|
|
|
package webhook
|
|
|
|
|
|
|
|
import (
|
2024-03-07 22:18:38 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2022-01-23 13:46:30 +00:00
|
|
|
|
|
|
|
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 15:23:15 +00:00
|
|
|
webhook_module "code.gitea.io/gitea/modules/webhook"
|
2022-01-23 13:46:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// PackagistPayload represents
|
|
|
|
PackagistPayload struct {
|
|
|
|
PackagistRepository struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
} `json:"repository"`
|
|
|
|
}
|
|
|
|
|
2023-01-01 15:23:15 +00:00
|
|
|
// PackagistMeta contains the metadata for the webhook
|
2022-01-23 13:46:30 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create implements PayloadConvertor Create method
|
2024-03-07 22:18:38 +00:00
|
|
|
func (pc packagistConvertor) Create(_ *api.CreatePayload) (PackagistPayload, error) {
|
|
|
|
return PackagistPayload{}, nil
|
2022-01-23 13:46:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete implements PayloadConvertor Delete method
|
2024-03-07 22:18:38 +00:00
|
|
|
func (pc packagistConvertor) Delete(_ *api.DeletePayload) (PackagistPayload, error) {
|
|
|
|
return PackagistPayload{}, nil
|
2022-01-23 13:46:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fork implements PayloadConvertor Fork method
|
2024-03-07 22:18:38 +00:00
|
|
|
func (pc packagistConvertor) Fork(_ *api.ForkPayload) (PackagistPayload, error) {
|
|
|
|
return PackagistPayload{}, nil
|
2022-01-23 13:46:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Push implements PayloadConvertor Push method
|
2024-03-07 22:18:38 +00:00
|
|
|
// https://packagist.org/about
|
|
|
|
func (pc packagistConvertor) Push(_ *api.PushPayload) (PackagistPayload, error) {
|
|
|
|
return PackagistPayload{
|
|
|
|
PackagistRepository: struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
}{
|
|
|
|
URL: pc.PackageURL,
|
|
|
|
},
|
|
|
|
}, nil
|
2022-01-23 13:46:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Issue implements PayloadConvertor Issue method
|
2024-03-07 22:18:38 +00:00
|
|
|
func (pc packagistConvertor) Issue(_ *api.IssuePayload) (PackagistPayload, error) {
|
|
|
|
return PackagistPayload{}, nil
|
2022-01-23 13:46:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IssueComment implements PayloadConvertor IssueComment method
|
2024-03-07 22:18:38 +00:00
|
|
|
func (pc packagistConvertor) IssueComment(_ *api.IssueCommentPayload) (PackagistPayload, error) {
|
|
|
|
return PackagistPayload{}, nil
|
2022-01-23 13:46:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// PullRequest implements PayloadConvertor PullRequest method
|
2024-03-07 22:18:38 +00:00
|
|
|
func (pc packagistConvertor) PullRequest(_ *api.PullRequestPayload) (PackagistPayload, error) {
|
|
|
|
return PackagistPayload{}, nil
|
2022-01-23 13:46:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Review implements PayloadConvertor Review method
|
2024-03-07 22:18:38 +00:00
|
|
|
func (pc packagistConvertor) Review(_ *api.PullRequestPayload, _ webhook_module.HookEventType) (PackagistPayload, error) {
|
|
|
|
return PackagistPayload{}, nil
|
2022-01-23 13:46:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Repository implements PayloadConvertor Repository method
|
2024-03-07 22:18:38 +00:00
|
|
|
func (pc packagistConvertor) Repository(_ *api.RepositoryPayload) (PackagistPayload, error) {
|
|
|
|
return PackagistPayload{}, nil
|
2022-01-23 13:46:30 +00:00
|
|
|
}
|
|
|
|
|
2022-09-04 19:54:23 +00:00
|
|
|
// Wiki implements PayloadConvertor Wiki method
|
2024-03-07 22:18:38 +00:00
|
|
|
func (pc packagistConvertor) Wiki(_ *api.WikiPayload) (PackagistPayload, error) {
|
|
|
|
return PackagistPayload{}, nil
|
2022-09-04 19:54:23 +00:00
|
|
|
}
|
|
|
|
|
2022-01-23 13:46:30 +00:00
|
|
|
// Release implements PayloadConvertor Release method
|
2024-03-07 22:18:38 +00:00
|
|
|
func (pc packagistConvertor) Release(_ *api.ReleasePayload) (PackagistPayload, error) {
|
|
|
|
return PackagistPayload{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pc packagistConvertor) Package(_ *api.PackagePayload) (PackagistPayload, error) {
|
|
|
|
return PackagistPayload{}, nil
|
2022-01-23 13:46:30 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 22:18:38 +00:00
|
|
|
type packagistConvertor struct {
|
|
|
|
PackageURL string
|
2023-10-31 04:43:38 +00:00
|
|
|
}
|
|
|
|
|
2024-03-07 22:18:38 +00:00
|
|
|
var _ payloadConvertor[PackagistPayload] = packagistConvertor{}
|
2022-01-23 13:46:30 +00:00
|
|
|
|
2024-03-07 22:18:38 +00:00
|
|
|
func newPackagistRequest(ctx context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
|
|
|
|
meta := &PackagistMeta{}
|
|
|
|
if err := json.Unmarshal([]byte(w.Meta), meta); err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("newpackagistRequest meta json: %w", err)
|
|
|
|
}
|
|
|
|
pc := packagistConvertor{
|
|
|
|
PackageURL: meta.PackageURL,
|
2022-01-23 13:46:30 +00:00
|
|
|
}
|
2024-03-07 22:18:38 +00:00
|
|
|
return newJSONRequest(pc, w, t, true)
|
2022-01-23 13:46:30 +00:00
|
|
|
}
|