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 (
"errors"
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
}
// JSONPayload Marshals the PackagistPayload to json
func ( f * PackagistPayload ) JSONPayload ( ) ( [ ] byte , error ) {
data , err := json . MarshalIndent ( f , "" , " " )
if err != nil {
return [ ] byte { } , err
}
return data , nil
}
var _ PayloadConvertor = & PackagistPayload { }
// Create implements PayloadConvertor Create method
2023-01-01 18:23:15 +03:00
func ( f * PackagistPayload ) Create ( _ * api . CreatePayload ) ( api . Payloader , error ) {
2022-01-23 16:46:30 +03:00
return nil , nil
}
// Delete implements PayloadConvertor Delete method
2023-01-01 18:23:15 +03:00
func ( f * PackagistPayload ) Delete ( _ * api . DeletePayload ) ( api . Payloader , error ) {
2022-01-23 16:46:30 +03:00
return nil , nil
}
// Fork implements PayloadConvertor Fork method
2023-01-01 18:23:15 +03:00
func ( f * PackagistPayload ) Fork ( _ * api . ForkPayload ) ( api . Payloader , error ) {
2022-01-23 16:46:30 +03:00
return nil , nil
}
// Push implements PayloadConvertor Push method
2023-01-01 18:23:15 +03:00
func ( f * PackagistPayload ) Push ( _ * api . PushPayload ) ( api . Payloader , error ) {
2022-01-23 16:46:30 +03:00
return f , nil
}
// Issue implements PayloadConvertor Issue method
2023-01-01 18:23:15 +03:00
func ( f * PackagistPayload ) Issue ( _ * api . IssuePayload ) ( api . Payloader , error ) {
2022-01-23 16:46:30 +03:00
return nil , nil
}
// IssueComment implements PayloadConvertor IssueComment method
2023-01-01 18:23:15 +03:00
func ( f * PackagistPayload ) IssueComment ( _ * api . IssueCommentPayload ) ( api . Payloader , error ) {
2022-01-23 16:46:30 +03:00
return nil , nil
}
// PullRequest implements PayloadConvertor PullRequest method
2023-01-01 18:23:15 +03:00
func ( f * PackagistPayload ) PullRequest ( _ * api . PullRequestPayload ) ( api . Payloader , error ) {
2022-01-23 16:46:30 +03:00
return nil , nil
}
// Review implements PayloadConvertor Review method
2023-01-01 18:23:15 +03:00
func ( f * PackagistPayload ) Review ( _ * api . PullRequestPayload , _ webhook_module . HookEventType ) ( api . Payloader , error ) {
2022-01-23 16:46:30 +03:00
return nil , nil
}
// Repository implements PayloadConvertor Repository method
2023-01-01 18:23:15 +03:00
func ( f * PackagistPayload ) Repository ( _ * api . RepositoryPayload ) ( api . Payloader , error ) {
2022-01-23 16:46:30 +03:00
return nil , nil
}
2022-09-04 22:54:23 +03:00
// Wiki implements PayloadConvertor Wiki method
2023-01-01 18:23:15 +03:00
func ( f * PackagistPayload ) Wiki ( _ * api . WikiPayload ) ( api . Payloader , error ) {
2022-09-04 22:54:23 +03:00
return nil , nil
}
2022-01-23 16:46:30 +03:00
// Release implements PayloadConvertor Release method
2023-01-01 18:23:15 +03:00
func ( f * PackagistPayload ) Release ( _ * api . ReleasePayload ) ( api . Payloader , error ) {
2022-01-23 16:46:30 +03:00
return nil , nil
}
2023-10-31 07:43:38 +03:00
func ( f * PackagistPayload ) Package ( _ * api . PackagePayload ) ( api . Payloader , error ) {
return nil , nil
}
2022-01-23 16:46:30 +03:00
// GetPackagistPayload converts a packagist webhook into a PackagistPayload
2023-01-01 18:23:15 +03:00
func GetPackagistPayload ( p api . Payloader , event webhook_module . HookEventType , meta string ) ( api . Payloader , error ) {
2022-01-23 16:46:30 +03:00
s := new ( PackagistPayload )
packagist := & PackagistMeta { }
if err := json . Unmarshal ( [ ] byte ( meta ) , & packagist ) ; err != nil {
return s , errors . New ( "GetPackagistPayload meta json:" + err . Error ( ) )
}
s . PackagistRepository . URL = packagist . PackageURL
return convertPayloader ( s , p , event )
}