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"
api "code.gitea.io/gitea/modules/structs"
2023-01-01 18:23:15 +03:00
webhook_module "code.gitea.io/gitea/modules/webhook"
2022-01-23 16:46:30 +03:00
)
type (
// PackagistPayload represents
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-07-10 14:37:16 +03:00
type packagistConvertor struct {
PackageURL string
}
2022-01-23 16:46:30 +03:00
// Create implements PayloadConvertor Create method
2024-03-08 01:18:38 +03:00
func ( pc packagistConvertor ) Create ( _ * api . CreatePayload ) ( PackagistPayload , error ) {
return PackagistPayload { } , nil
2022-01-23 16:46:30 +03:00
}
// Delete implements PayloadConvertor Delete method
2024-03-08 01:18:38 +03:00
func ( pc packagistConvertor ) Delete ( _ * api . DeletePayload ) ( PackagistPayload , error ) {
return PackagistPayload { } , nil
2022-01-23 16:46:30 +03:00
}
// Fork implements PayloadConvertor Fork method
2024-03-08 01:18:38 +03:00
func ( pc packagistConvertor ) Fork ( _ * api . ForkPayload ) ( PackagistPayload , error ) {
return PackagistPayload { } , nil
2022-01-23 16:46:30 +03:00
}
// Push implements PayloadConvertor Push method
2024-03-08 01:18:38 +03: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 16:46:30 +03:00
}
// Issue implements PayloadConvertor Issue method
2024-03-08 01:18:38 +03:00
func ( pc packagistConvertor ) Issue ( _ * api . IssuePayload ) ( PackagistPayload , error ) {
return PackagistPayload { } , nil
2022-01-23 16:46:30 +03:00
}
// IssueComment implements PayloadConvertor IssueComment method
2024-03-08 01:18:38 +03:00
func ( pc packagistConvertor ) IssueComment ( _ * api . IssueCommentPayload ) ( PackagistPayload , error ) {
return PackagistPayload { } , nil
2022-01-23 16:46:30 +03:00
}
// PullRequest implements PayloadConvertor PullRequest method
2024-03-08 01:18:38 +03:00
func ( pc packagistConvertor ) PullRequest ( _ * api . PullRequestPayload ) ( PackagistPayload , error ) {
return PackagistPayload { } , nil
2022-01-23 16:46:30 +03:00
}
// Review implements PayloadConvertor Review method
2024-03-08 01:18:38 +03:00
func ( pc packagistConvertor ) Review ( _ * api . PullRequestPayload , _ webhook_module . HookEventType ) ( PackagistPayload , error ) {
return PackagistPayload { } , nil
2022-01-23 16:46:30 +03:00
}
// Repository implements PayloadConvertor Repository method
2024-03-08 01:18:38 +03:00
func ( pc packagistConvertor ) Repository ( _ * api . RepositoryPayload ) ( PackagistPayload , error ) {
return PackagistPayload { } , nil
2022-01-23 16:46:30 +03:00
}
2022-09-04 22:54:23 +03:00
// Wiki implements PayloadConvertor Wiki method
2024-03-08 01:18:38 +03:00
func ( pc packagistConvertor ) Wiki ( _ * api . WikiPayload ) ( PackagistPayload , error ) {
return PackagistPayload { } , nil
2022-09-04 22:54:23 +03:00
}
2022-01-23 16:46:30 +03:00
// Release implements PayloadConvertor Release method
2024-03-08 01:18:38 +03: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 16:46:30 +03:00
}
2024-06-11 21:47:45 +03:00
func newPackagistRequest ( _ context . Context , w * webhook_model . Webhook , t * webhook_model . HookTask ) ( * http . Request , [ ] byte , error ) {
2024-03-08 01:18:38 +03: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 14:37:16 +03:00
var pc payloadConvertor [ PackagistPayload ] = packagistConvertor {
2024-03-08 01:18:38 +03:00
PackageURL : meta . PackageURL ,
2022-01-23 16:46:30 +03:00
}
2024-03-08 01:18:38 +03:00
return newJSONRequest ( pc , w , t , true )
2022-01-23 16:46:30 +03:00
}