2020-09-05 05:57:13 +03:00
// Copyright 2020 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2020-09-05 05:57:13 +03:00
package webhook
import (
api "code.gitea.io/gitea/modules/structs"
2023-01-01 18:23:15 +03:00
webhook_module "code.gitea.io/gitea/modules/webhook"
2020-09-05 05:57:13 +03:00
)
// PayloadConvertor defines the interface to convert system webhook payload to external payload
type PayloadConvertor interface {
api . Payloader
Create ( * api . CreatePayload ) ( api . Payloader , error )
Delete ( * api . DeletePayload ) ( api . Payloader , error )
Fork ( * api . ForkPayload ) ( api . Payloader , error )
Issue ( * api . IssuePayload ) ( api . Payloader , error )
IssueComment ( * api . IssueCommentPayload ) ( api . Payloader , error )
Push ( * api . PushPayload ) ( api . Payloader , error )
PullRequest ( * api . PullRequestPayload ) ( api . Payloader , error )
2023-01-01 18:23:15 +03:00
Review ( * api . PullRequestPayload , webhook_module . HookEventType ) ( api . Payloader , error )
2020-09-05 05:57:13 +03:00
Repository ( * api . RepositoryPayload ) ( api . Payloader , error )
Release ( * api . ReleasePayload ) ( api . Payloader , error )
2022-09-04 22:54:23 +03:00
Wiki ( * api . WikiPayload ) ( api . Payloader , error )
2020-09-05 05:57:13 +03:00
}
2023-01-01 18:23:15 +03:00
func convertPayloader ( s PayloadConvertor , p api . Payloader , event webhook_module . HookEventType ) ( api . Payloader , error ) {
2020-09-05 05:57:13 +03:00
switch event {
2023-01-01 18:23:15 +03:00
case webhook_module . HookEventCreate :
2020-09-05 05:57:13 +03:00
return s . Create ( p . ( * api . CreatePayload ) )
2023-01-01 18:23:15 +03:00
case webhook_module . HookEventDelete :
2020-09-05 05:57:13 +03:00
return s . Delete ( p . ( * api . DeletePayload ) )
2023-01-01 18:23:15 +03:00
case webhook_module . HookEventFork :
2020-09-05 05:57:13 +03:00
return s . Fork ( p . ( * api . ForkPayload ) )
2023-01-01 18:23:15 +03:00
case webhook_module . HookEventIssues , webhook_module . HookEventIssueAssign , webhook_module . HookEventIssueLabel , webhook_module . HookEventIssueMilestone :
2020-09-05 05:57:13 +03:00
return s . Issue ( p . ( * api . IssuePayload ) )
2023-01-01 18:23:15 +03:00
case webhook_module . HookEventIssueComment , webhook_module . HookEventPullRequestComment :
2020-09-05 05:57:13 +03:00
pl , ok := p . ( * api . IssueCommentPayload )
if ok {
return s . IssueComment ( pl )
}
return s . PullRequest ( p . ( * api . PullRequestPayload ) )
2023-01-01 18:23:15 +03:00
case webhook_module . HookEventPush :
2020-09-05 05:57:13 +03:00
return s . Push ( p . ( * api . PushPayload ) )
2023-01-01 18:23:15 +03:00
case webhook_module . HookEventPullRequest , webhook_module . HookEventPullRequestAssign , webhook_module . HookEventPullRequestLabel ,
webhook_module . HookEventPullRequestMilestone , webhook_module . HookEventPullRequestSync :
2020-09-05 05:57:13 +03:00
return s . PullRequest ( p . ( * api . PullRequestPayload ) )
2023-01-01 18:23:15 +03:00
case webhook_module . HookEventPullRequestReviewApproved , webhook_module . HookEventPullRequestReviewRejected , webhook_module . HookEventPullRequestReviewComment :
2020-09-05 05:57:13 +03:00
return s . Review ( p . ( * api . PullRequestPayload ) , event )
2023-01-01 18:23:15 +03:00
case webhook_module . HookEventRepository :
2020-09-05 05:57:13 +03:00
return s . Repository ( p . ( * api . RepositoryPayload ) )
2023-01-01 18:23:15 +03:00
case webhook_module . HookEventRelease :
2020-09-05 05:57:13 +03:00
return s . Release ( p . ( * api . ReleasePayload ) )
2023-01-01 18:23:15 +03:00
case webhook_module . HookEventWiki :
2022-09-04 22:54:23 +03:00
return s . Wiki ( p . ( * api . WikiPayload ) )
2020-09-05 05:57:13 +03:00
}
return s , nil
}