2022-01-23 16:46:30 +03:00
// Copyright 2022 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2022-01-23 16:46:30 +03:00
package webhook
import (
2024-03-08 01:18:38 +03:00
"context"
"fmt"
"net/http"
2022-01-23 16:46:30 +03:00
webhook_model "code.gitea.io/gitea/models/webhook"
"code.gitea.io/gitea/modules/json"
"code.gitea.io/gitea/modules/log"
)
type (
2024-03-13 11:26:56 +03:00
// PackagistPayload represents a packagist payload
// as expected by https://packagist.org/about
2022-01-23 16:46:30 +03:00
PackagistPayload struct {
PackagistRepository struct {
URL string ` json:"url" `
} ` json:"repository" `
}
2023-01-01 18:23:15 +03:00
// PackagistMeta contains the metadata for the webhook
2022-01-23 16:46:30 +03: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-03-13 11:26:56 +03:00
// newPackagistRequest creates a request with the [PackagistPayload] for packagist (same payload for all events).
2024-03-08 01:18:38 +03: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 )
}
2024-03-13 11:26:56 +03:00
payload := PackagistPayload {
PackagistRepository : struct {
URL string ` json:"url" `
} {
URL : meta . PackageURL ,
} ,
2022-01-23 16:46:30 +03:00
}
2024-03-13 11:26:56 +03:00
return newJSONRequestWithPayload ( payload , w , t , false )
2022-01-23 16:46:30 +03:00
}