2020-09-04 21:57:13 -05: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 00:13:16 -05:00
|
|
|
webhook_model "code.gitea.io/gitea/models/webhook"
|
2020-09-04 21:57:13 -05: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 00:13:16 -05:00
|
|
|
Review(*api.PullRequestPayload, webhook_model.HookEventType) (api.Payloader, error)
|
2020-09-04 21:57:13 -05:00
|
|
|
Repository(*api.RepositoryPayload) (api.Payloader, error)
|
|
|
|
Release(*api.ReleasePayload) (api.Payloader, error)
|
2022-09-04 14:54:23 -05:00
|
|
|
Wiki(*api.WikiPayload) (api.Payloader, error)
|
2020-09-04 21:57:13 -05:00
|
|
|
}
|
|
|
|
|
2021-11-10 00:13:16 -05:00
|
|
|
func convertPayloader(s PayloadConvertor, p api.Payloader, event webhook_model.HookEventType) (api.Payloader, error) {
|
2020-09-04 21:57:13 -05:00
|
|
|
switch event {
|
2021-11-10 00:13:16 -05:00
|
|
|
case webhook_model.HookEventCreate:
|
2020-09-04 21:57:13 -05:00
|
|
|
return s.Create(p.(*api.CreatePayload))
|
2021-11-10 00:13:16 -05:00
|
|
|
case webhook_model.HookEventDelete:
|
2020-09-04 21:57:13 -05:00
|
|
|
return s.Delete(p.(*api.DeletePayload))
|
2021-11-10 00:13:16 -05:00
|
|
|
case webhook_model.HookEventFork:
|
2020-09-04 21:57:13 -05:00
|
|
|
return s.Fork(p.(*api.ForkPayload))
|
2021-11-10 00:13:16 -05:00
|
|
|
case webhook_model.HookEventIssues, webhook_model.HookEventIssueAssign, webhook_model.HookEventIssueLabel, webhook_model.HookEventIssueMilestone:
|
2020-09-04 21:57:13 -05:00
|
|
|
return s.Issue(p.(*api.IssuePayload))
|
2021-11-10 00:13:16 -05:00
|
|
|
case webhook_model.HookEventIssueComment, webhook_model.HookEventPullRequestComment:
|
2020-09-04 21:57:13 -05:00
|
|
|
pl, ok := p.(*api.IssueCommentPayload)
|
|
|
|
if ok {
|
|
|
|
return s.IssueComment(pl)
|
|
|
|
}
|
|
|
|
return s.PullRequest(p.(*api.PullRequestPayload))
|
2021-11-10 00:13:16 -05:00
|
|
|
case webhook_model.HookEventPush:
|
2020-09-04 21:57:13 -05:00
|
|
|
return s.Push(p.(*api.PushPayload))
|
2021-11-10 00:13:16 -05:00
|
|
|
case webhook_model.HookEventPullRequest, webhook_model.HookEventPullRequestAssign, webhook_model.HookEventPullRequestLabel,
|
|
|
|
webhook_model.HookEventPullRequestMilestone, webhook_model.HookEventPullRequestSync:
|
2020-09-04 21:57:13 -05:00
|
|
|
return s.PullRequest(p.(*api.PullRequestPayload))
|
2021-11-10 00:13:16 -05:00
|
|
|
case webhook_model.HookEventPullRequestReviewApproved, webhook_model.HookEventPullRequestReviewRejected, webhook_model.HookEventPullRequestReviewComment:
|
2020-09-04 21:57:13 -05:00
|
|
|
return s.Review(p.(*api.PullRequestPayload), event)
|
2021-11-10 00:13:16 -05:00
|
|
|
case webhook_model.HookEventRepository:
|
2020-09-04 21:57:13 -05:00
|
|
|
return s.Repository(p.(*api.RepositoryPayload))
|
2021-11-10 00:13:16 -05:00
|
|
|
case webhook_model.HookEventRelease:
|
2020-09-04 21:57:13 -05:00
|
|
|
return s.Release(p.(*api.ReleasePayload))
|
2022-09-04 14:54:23 -05:00
|
|
|
case webhook_model.HookEventWiki:
|
|
|
|
return s.Wiki(p.(*api.WikiPayload))
|
2020-09-04 21:57:13 -05:00
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|