2022-01-23 14:46:30 +01:00
|
|
|
// Copyright 2022 The Gitea Authors. All rights reserved.
|
2022-11-27 19:20:29 +01:00
|
|
|
// 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"
|
2024-03-22 16:02:48 +01:00
|
|
|
"html/template"
|
2024-03-07 23:18:38 +01:00
|
|
|
"net/http"
|
2024-03-21 14:04:37 +01:00
|
|
|
"net/url"
|
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"
|
2024-03-20 15:44:01 +01:00
|
|
|
webhook_module "code.gitea.io/gitea/modules/webhook"
|
2024-03-21 14:04:37 +01:00
|
|
|
"code.gitea.io/gitea/services/forms"
|
2024-04-03 14:22:36 +02:00
|
|
|
"code.gitea.io/gitea/services/webhook/shared"
|
2022-01-23 14:46:30 +01:00
|
|
|
)
|
|
|
|
|
2024-03-20 15:44:01 +01:00
|
|
|
type packagistHandler struct{}
|
|
|
|
|
|
|
|
func (packagistHandler) Type() webhook_module.HookType { return webhook_module.PACKAGIST }
|
2024-04-03 14:22:36 +02:00
|
|
|
func (packagistHandler) Icon(size int) template.HTML { return shared.ImgIcon("packagist.png", size) }
|
2024-03-20 15:44:01 +01:00
|
|
|
|
2024-04-03 14:22:36 +02:00
|
|
|
func (packagistHandler) UnmarshalForm(bind func(any)) forms.WebhookForm {
|
2024-03-21 14:04:37 +01:00
|
|
|
var form struct {
|
2024-04-03 14:22:36 +02:00
|
|
|
forms.WebhookCoreForm
|
2024-03-21 14:04:37 +01:00
|
|
|
Username string `binding:"Required"`
|
|
|
|
APIToken string `binding:"Required"`
|
|
|
|
PackageURL string `binding:"Required;ValidUrl"`
|
|
|
|
}
|
|
|
|
bind(&form)
|
|
|
|
|
2024-04-03 14:22:36 +02:00
|
|
|
return forms.WebhookForm{
|
|
|
|
WebhookCoreForm: form.WebhookCoreForm,
|
|
|
|
URL: fmt.Sprintf("https://packagist.org/api/update-package?username=%s&apiToken=%s", url.QueryEscape(form.Username), url.QueryEscape(form.APIToken)),
|
|
|
|
ContentType: webhook_model.ContentTypeJSON,
|
|
|
|
Secret: "",
|
|
|
|
HTTPMethod: http.MethodPost,
|
2024-03-21 14:04:37 +01:00
|
|
|
Metadata: &PackagistMeta{
|
|
|
|
Username: form.Username,
|
|
|
|
APIToken: form.APIToken,
|
|
|
|
PackageURL: form.PackageURL,
|
|
|
|
},
|
|
|
|
}
|
2024-03-21 13:23:27 +01:00
|
|
|
}
|
|
|
|
|
2022-01-23 14:46:30 +01:00
|
|
|
type (
|
2024-03-13 09:26:56 +01:00
|
|
|
// PackagistPayload represents a packagist payload
|
|
|
|
// as expected by https://packagist.org/about
|
2022-01-23 14:46:30 +01:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2024-03-20 15:44:01 +01:00
|
|
|
// Metadata returns packagist metadata
|
|
|
|
func (packagistHandler) Metadata(w *webhook_model.Webhook) any {
|
2022-01-23 14:46:30 +01:00
|
|
|
s := &PackagistMeta{}
|
|
|
|
if err := json.Unmarshal([]byte(w.Meta), s); err != nil {
|
2024-03-20 15:44:01 +01:00
|
|
|
log.Error("packagistHandler.Metadata(%d): %v", w.ID, err)
|
2022-01-23 14:46:30 +01:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2024-03-13 09:26:56 +01:00
|
|
|
// newPackagistRequest creates a request with the [PackagistPayload] for packagist (same payload for all events).
|
2024-03-20 15:44:01 +01:00
|
|
|
func (packagistHandler) NewRequest(ctx 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 {
|
2024-03-20 15:44:01 +01:00
|
|
|
return nil, nil, fmt.Errorf("packagistHandler.NewRequest meta json: %w", err)
|
2024-03-07 23:18:38 +01:00
|
|
|
}
|
2024-03-13 09:26:56 +01:00
|
|
|
|
|
|
|
payload := PackagistPayload{
|
|
|
|
PackagistRepository: struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
}{
|
|
|
|
URL: meta.PackageURL,
|
|
|
|
},
|
2022-01-23 14:46:30 +01:00
|
|
|
}
|
2024-04-03 14:22:36 +02:00
|
|
|
return shared.NewJSONRequestWithPayload(payload, w, t, false)
|
2022-01-23 14:46:30 +01:00
|
|
|
}
|