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 (
|
2024-03-08 03:48:38 +05:30
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2022-01-23 19:16:30 +05:30
|
|
|
|
|
|
|
webhook_model "code.gitea.io/gitea/models/webhook"
|
|
|
|
"code.gitea.io/gitea/modules/json"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2024-03-20 20:14:01 +05:30
|
|
|
webhook_module "code.gitea.io/gitea/modules/webhook"
|
2022-01-23 19:16:30 +05:30
|
|
|
)
|
|
|
|
|
2024-03-20 20:14:01 +05:30
|
|
|
type packagistHandler struct{}
|
|
|
|
|
|
|
|
func (packagistHandler) Type() webhook_module.HookType { return webhook_module.PACKAGIST }
|
|
|
|
|
2024-03-21 17:53:27 +05:30
|
|
|
func (packagistHandler) FormFields(bind func(any)) FormFields {
|
|
|
|
panic("TODO")
|
|
|
|
}
|
|
|
|
|
2022-01-23 19:16:30 +05:30
|
|
|
type (
|
2024-03-13 13:56:56 +05:30
|
|
|
// PackagistPayload represents a packagist payload
|
|
|
|
// as expected by https://packagist.org/about
|
2022-01-23 19:16:30 +05:30
|
|
|
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"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-03-20 20:14:01 +05:30
|
|
|
// Metadata returns packagist metadata
|
|
|
|
func (packagistHandler) Metadata(w *webhook_model.Webhook) any {
|
2022-01-23 19:16:30 +05:30
|
|
|
s := &PackagistMeta{}
|
|
|
|
if err := json.Unmarshal([]byte(w.Meta), s); err != nil {
|
2024-03-20 20:14:01 +05:30
|
|
|
log.Error("packagistHandler.Metadata(%d): %v", w.ID, err)
|
2022-01-23 19:16:30 +05:30
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2024-03-13 13:56:56 +05:30
|
|
|
// newPackagistRequest creates a request with the [PackagistPayload] for packagist (same payload for all events).
|
2024-03-20 20:14:01 +05:30
|
|
|
func (packagistHandler) NewRequest(ctx context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
|
2024-03-08 03:48:38 +05:30
|
|
|
meta := &PackagistMeta{}
|
|
|
|
if err := json.Unmarshal([]byte(w.Meta), meta); err != nil {
|
2024-03-20 20:14:01 +05:30
|
|
|
return nil, nil, fmt.Errorf("packagistHandler.NewRequest meta json: %w", err)
|
2024-03-08 03:48:38 +05:30
|
|
|
}
|
2024-03-13 13:56:56 +05:30
|
|
|
|
|
|
|
payload := PackagistPayload{
|
|
|
|
PackagistRepository: struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
}{
|
|
|
|
URL: meta.PackageURL,
|
|
|
|
},
|
2022-01-23 19:16:30 +05:30
|
|
|
}
|
2024-03-13 13:56:56 +05:30
|
|
|
return newJSONRequestWithPayload(payload, w, t, false)
|
2022-01-23 19:16:30 +05:30
|
|
|
}
|