Files

139 lines
4.1 KiB
Go
Raw Permalink Normal View History

2022-01-23 14:46:30 +01:00
// Copyright 2022 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
2022-01-23 14:46:30 +01:00
package webhook
import (
2024-03-07 23:18:38 +01:00
"context"
"fmt"
"net/http"
2022-01-23 14:46:30 +01: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 16:23:15 +01:00
webhook_module "code.gitea.io/gitea/modules/webhook"
2022-01-23 14:46:30 +01:00
)
type (
// PackagistPayload represents
PackagistPayload struct {
PackagistRepository struct {
URL string `json:"url"`
} `json:"repository"`
}
2023-01-01 16:23:15 +01:00
// PackagistMeta contains the metadata for the webhook
2022-01-23 14:46:30 +01: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
}
2024-07-10 19:37:16 +08:00
type packagistConvertor struct {
PackageURL string
}
2022-01-23 14:46:30 +01:00
// Create implements PayloadConvertor Create method
2024-03-07 23:18:38 +01:00
func (pc packagistConvertor) Create(_ *api.CreatePayload) (PackagistPayload, error) {
return PackagistPayload{}, nil
2022-01-23 14:46:30 +01:00
}
// Delete implements PayloadConvertor Delete method
2024-03-07 23:18:38 +01:00
func (pc packagistConvertor) Delete(_ *api.DeletePayload) (PackagistPayload, error) {
return PackagistPayload{}, nil
2022-01-23 14:46:30 +01:00
}
// Fork implements PayloadConvertor Fork method
2024-03-07 23:18:38 +01:00
func (pc packagistConvertor) Fork(_ *api.ForkPayload) (PackagistPayload, error) {
return PackagistPayload{}, nil
2022-01-23 14:46:30 +01:00
}
// Push implements PayloadConvertor Push method
2024-03-07 23:18:38 +01: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 14:46:30 +01:00
}
// Issue implements PayloadConvertor Issue method
2024-03-07 23:18:38 +01:00
func (pc packagistConvertor) Issue(_ *api.IssuePayload) (PackagistPayload, error) {
return PackagistPayload{}, nil
2022-01-23 14:46:30 +01:00
}
// IssueComment implements PayloadConvertor IssueComment method
2024-03-07 23:18:38 +01:00
func (pc packagistConvertor) IssueComment(_ *api.IssueCommentPayload) (PackagistPayload, error) {
return PackagistPayload{}, nil
2022-01-23 14:46:30 +01:00
}
// PullRequest implements PayloadConvertor PullRequest method
2024-03-07 23:18:38 +01:00
func (pc packagistConvertor) PullRequest(_ *api.PullRequestPayload) (PackagistPayload, error) {
return PackagistPayload{}, nil
2022-01-23 14:46:30 +01:00
}
// Review implements PayloadConvertor Review method
2024-03-07 23:18:38 +01:00
func (pc packagistConvertor) Review(_ *api.PullRequestPayload, _ webhook_module.HookEventType) (PackagistPayload, error) {
return PackagistPayload{}, nil
2022-01-23 14:46:30 +01:00
}
// Repository implements PayloadConvertor Repository method
2024-03-07 23:18:38 +01:00
func (pc packagistConvertor) Repository(_ *api.RepositoryPayload) (PackagistPayload, error) {
return PackagistPayload{}, nil
2022-01-23 14:46:30 +01:00
}
2022-09-04 21:54:23 +02:00
// Wiki implements PayloadConvertor Wiki method
2024-03-07 23:18:38 +01:00
func (pc packagistConvertor) Wiki(_ *api.WikiPayload) (PackagistPayload, error) {
return PackagistPayload{}, nil
2022-09-04 21:54:23 +02:00
}
2022-01-23 14:46:30 +01:00
// Release implements PayloadConvertor Release method
2024-03-07 23:18:38 +01: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 14:46:30 +01:00
}
2025-02-03 19:25:59 -08:00
func (pc packagistConvertor) Status(_ *api.CommitStatusPayload) (PackagistPayload, error) {
return PackagistPayload{}, nil
}
2025-06-20 14:14:00 +02:00
func (pc packagistConvertor) WorkflowRun(_ *api.WorkflowRunPayload) (PackagistPayload, error) {
return PackagistPayload{}, nil
}
2025-03-11 18:40:38 +01:00
func (pc packagistConvertor) WorkflowJob(_ *api.WorkflowJobPayload) (PackagistPayload, error) {
return PackagistPayload{}, nil
}
2024-06-11 20:47:45 +02:00
func newPackagistRequest(_ context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
2024-03-07 23:18:38 +01:00
meta := &PackagistMeta{}
if err := json.Unmarshal([]byte(w.Meta), meta); err != nil {
return nil, nil, fmt.Errorf("newpackagistRequest meta json: %w", err)
}
2024-07-10 19:37:16 +08:00
var pc payloadConvertor[PackagistPayload] = packagistConvertor{
2024-03-07 23:18:38 +01:00
PackageURL: meta.PackageURL,
2022-01-23 14:46:30 +01:00
}
2024-03-07 23:18:38 +01:00
return newJSONRequest(pc, w, t, true)
2022-01-23 14:46:30 +01:00
}
func init() {
RegisterWebhookRequester(webhook_module.PACKAGIST, newPackagistRequest)
}