2020-09-05 04:57:13 +02:00
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package webhook
|
|
|
|
|
|
|
|
import (
|
2021-11-10 06:13:16 +01:00
|
|
|
webhook_model "code.gitea.io/gitea/models/webhook"
|
2020-09-05 04:57:13 +02:00
|
|
|
api "code.gitea.io/gitea/modules/structs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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)
|
2021-11-10 06:13:16 +01:00
|
|
|
Review(*api.PullRequestPayload, webhook_model.HookEventType) (api.Payloader, error)
|
2020-09-05 04:57:13 +02:00
|
|
|
Repository(*api.RepositoryPayload) (api.Payloader, error)
|
|
|
|
Release(*api.ReleasePayload) (api.Payloader, error)
|
|
|
|
}
|
|
|
|
|
2021-11-10 06:13:16 +01:00
|
|
|
func convertPayloader(s PayloadConvertor, p api.Payloader, event webhook_model.HookEventType) (api.Payloader, error) {
|
2020-09-05 04:57:13 +02:00
|
|
|
switch event {
|
2021-11-10 06:13:16 +01:00
|
|
|
case webhook_model.HookEventCreate:
|
2020-09-05 04:57:13 +02:00
|
|
|
return s.Create(p.(*api.CreatePayload))
|
2021-11-10 06:13:16 +01:00
|
|
|
case webhook_model.HookEventDelete:
|
2020-09-05 04:57:13 +02:00
|
|
|
return s.Delete(p.(*api.DeletePayload))
|
2021-11-10 06:13:16 +01:00
|
|
|
case webhook_model.HookEventFork:
|
2020-09-05 04:57:13 +02:00
|
|
|
return s.Fork(p.(*api.ForkPayload))
|
2021-11-10 06:13:16 +01:00
|
|
|
case webhook_model.HookEventIssues, webhook_model.HookEventIssueAssign, webhook_model.HookEventIssueLabel, webhook_model.HookEventIssueMilestone:
|
2020-09-05 04:57:13 +02:00
|
|
|
return s.Issue(p.(*api.IssuePayload))
|
2021-11-10 06:13:16 +01:00
|
|
|
case webhook_model.HookEventIssueComment, webhook_model.HookEventPullRequestComment:
|
2020-09-05 04:57:13 +02:00
|
|
|
pl, ok := p.(*api.IssueCommentPayload)
|
|
|
|
if ok {
|
|
|
|
return s.IssueComment(pl)
|
|
|
|
}
|
|
|
|
return s.PullRequest(p.(*api.PullRequestPayload))
|
2021-11-10 06:13:16 +01:00
|
|
|
case webhook_model.HookEventPush:
|
2020-09-05 04:57:13 +02:00
|
|
|
return s.Push(p.(*api.PushPayload))
|
2021-11-10 06:13:16 +01:00
|
|
|
case webhook_model.HookEventPullRequest, webhook_model.HookEventPullRequestAssign, webhook_model.HookEventPullRequestLabel,
|
|
|
|
webhook_model.HookEventPullRequestMilestone, webhook_model.HookEventPullRequestSync:
|
2020-09-05 04:57:13 +02:00
|
|
|
return s.PullRequest(p.(*api.PullRequestPayload))
|
2021-11-10 06:13:16 +01:00
|
|
|
case webhook_model.HookEventPullRequestReviewApproved, webhook_model.HookEventPullRequestReviewRejected, webhook_model.HookEventPullRequestReviewComment:
|
2020-09-05 04:57:13 +02:00
|
|
|
return s.Review(p.(*api.PullRequestPayload), event)
|
2021-11-10 06:13:16 +01:00
|
|
|
case webhook_model.HookEventRepository:
|
2020-09-05 04:57:13 +02:00
|
|
|
return s.Repository(p.(*api.RepositoryPayload))
|
2021-11-10 06:13:16 +01:00
|
|
|
case webhook_model.HookEventRelease:
|
2020-09-05 04:57:13 +02:00
|
|
|
return s.Release(p.(*api.ReleasePayload))
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|