Store webhook event in database (#29145)
Refactor the webhook logic, to have the type-dependent processing happen only in one place. --- 1. An event happens 2. It is pre-processed (depending on the webhook type) and its body is added to a task queue 3. When the task is processed, some more logic (depending on the webhook type as well) is applied to make an HTTP request This means that webhook-type dependant logic is needed in step 2 and 3. This is cumbersome and brittle to maintain. Updated webhook flow with this PR: 1. An event happens 2. It is stored as-is and added to a task queue 3. When the task is processed, the event is processed (depending on the webhook type) to make an HTTP request So the only webhook-type dependent logic happens in one place (step 3) which should be much more robust. - the raw event must be stored in the hooktask (until now, the pre-processed body was stored) - to ensure that previous hooktasks are correctly sent, a `payload_version` is added (version 1: the body has already been pre-process / version 2: the body is the raw event) So future webhook additions will only have to deal with creating an http.Request based on the raw event (no need to adjust the code in multiple places, like currently). Moreover since this processing happens when fetching from the task queue, it ensures that the queuing of new events (upon a `git push` for instance) does not get slowed down by a slow webhook. As a concrete example, the PR #19307 for custom webhooks, should be substantially smaller: - no need to change `services/webhook/deliver.go` - minimal change in `services/webhook/webhook.go` (add the new webhook to the map) - no need to change all the individual webhook files (since with this refactor the `*webhook_model.Webhook` is provided as argument) (cherry picked from commit 26653b196bd1d15c532af41f60351596dd4330bd) Conflicts: services/webhook/deliver_test.go trivial context conflict
This commit is contained in:
parent
7ea8993a0e
commit
53f6f62ad4
28 changed files with 1686 additions and 1518 deletions
|
@ -4,7 +4,9 @@
|
|||
package webhook
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
webhook_model "code.gitea.io/gitea/models/webhook"
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
|
@ -38,84 +40,85 @@ func GetPackagistHook(w *webhook_model.Webhook) *PackagistMeta {
|
|||
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
|
||||
func (f *PackagistPayload) Create(_ *api.CreatePayload) (api.Payloader, error) {
|
||||
return nil, nil
|
||||
func (pc packagistConvertor) Create(_ *api.CreatePayload) (PackagistPayload, error) {
|
||||
return PackagistPayload{}, nil
|
||||
}
|
||||
|
||||
// Delete implements PayloadConvertor Delete method
|
||||
func (f *PackagistPayload) Delete(_ *api.DeletePayload) (api.Payloader, error) {
|
||||
return nil, nil
|
||||
func (pc packagistConvertor) Delete(_ *api.DeletePayload) (PackagistPayload, error) {
|
||||
return PackagistPayload{}, nil
|
||||
}
|
||||
|
||||
// Fork implements PayloadConvertor Fork method
|
||||
func (f *PackagistPayload) Fork(_ *api.ForkPayload) (api.Payloader, error) {
|
||||
return nil, nil
|
||||
func (pc packagistConvertor) Fork(_ *api.ForkPayload) (PackagistPayload, error) {
|
||||
return PackagistPayload{}, nil
|
||||
}
|
||||
|
||||
// Push implements PayloadConvertor Push method
|
||||
func (f *PackagistPayload) Push(_ *api.PushPayload) (api.Payloader, error) {
|
||||
return f, nil
|
||||
// https://packagist.org/about
|
||||
func (pc packagistConvertor) Push(_ *api.PushPayload) (PackagistPayload, error) {
|
||||
return PackagistPayload{
|
||||
PackagistRepository: struct {
|
||||
URL string `json:"url"`
|
||||
}{
|
||||
URL: pc.PackageURL,
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Issue implements PayloadConvertor Issue method
|
||||
func (f *PackagistPayload) Issue(_ *api.IssuePayload) (api.Payloader, error) {
|
||||
return nil, nil
|
||||
func (pc packagistConvertor) Issue(_ *api.IssuePayload) (PackagistPayload, error) {
|
||||
return PackagistPayload{}, nil
|
||||
}
|
||||
|
||||
// IssueComment implements PayloadConvertor IssueComment method
|
||||
func (f *PackagistPayload) IssueComment(_ *api.IssueCommentPayload) (api.Payloader, error) {
|
||||
return nil, nil
|
||||
func (pc packagistConvertor) IssueComment(_ *api.IssueCommentPayload) (PackagistPayload, error) {
|
||||
return PackagistPayload{}, nil
|
||||
}
|
||||
|
||||
// PullRequest implements PayloadConvertor PullRequest method
|
||||
func (f *PackagistPayload) PullRequest(_ *api.PullRequestPayload) (api.Payloader, error) {
|
||||
return nil, nil
|
||||
func (pc packagistConvertor) PullRequest(_ *api.PullRequestPayload) (PackagistPayload, error) {
|
||||
return PackagistPayload{}, nil
|
||||
}
|
||||
|
||||
// Review implements PayloadConvertor Review method
|
||||
func (f *PackagistPayload) Review(_ *api.PullRequestPayload, _ webhook_module.HookEventType) (api.Payloader, error) {
|
||||
return nil, nil
|
||||
func (pc packagistConvertor) Review(_ *api.PullRequestPayload, _ webhook_module.HookEventType) (PackagistPayload, error) {
|
||||
return PackagistPayload{}, nil
|
||||
}
|
||||
|
||||
// Repository implements PayloadConvertor Repository method
|
||||
func (f *PackagistPayload) Repository(_ *api.RepositoryPayload) (api.Payloader, error) {
|
||||
return nil, nil
|
||||
func (pc packagistConvertor) Repository(_ *api.RepositoryPayload) (PackagistPayload, error) {
|
||||
return PackagistPayload{}, nil
|
||||
}
|
||||
|
||||
// Wiki implements PayloadConvertor Wiki method
|
||||
func (f *PackagistPayload) Wiki(_ *api.WikiPayload) (api.Payloader, error) {
|
||||
return nil, nil
|
||||
func (pc packagistConvertor) Wiki(_ *api.WikiPayload) (PackagistPayload, error) {
|
||||
return PackagistPayload{}, nil
|
||||
}
|
||||
|
||||
// Release implements PayloadConvertor Release method
|
||||
func (f *PackagistPayload) Release(_ *api.ReleasePayload) (api.Payloader, error) {
|
||||
return nil, nil
|
||||
func (pc packagistConvertor) Release(_ *api.ReleasePayload) (PackagistPayload, error) {
|
||||
return PackagistPayload{}, nil
|
||||
}
|
||||
|
||||
func (f *PackagistPayload) Package(_ *api.PackagePayload) (api.Payloader, error) {
|
||||
return nil, nil
|
||||
func (pc packagistConvertor) Package(_ *api.PackagePayload) (PackagistPayload, error) {
|
||||
return PackagistPayload{}, nil
|
||||
}
|
||||
|
||||
// GetPackagistPayload converts a packagist webhook into a PackagistPayload
|
||||
func GetPackagistPayload(p api.Payloader, event webhook_module.HookEventType, meta string) (api.Payloader, error) {
|
||||
s := new(PackagistPayload)
|
||||
type packagistConvertor struct {
|
||||
PackageURL string
|
||||
}
|
||||
|
||||
packagist := &PackagistMeta{}
|
||||
if err := json.Unmarshal([]byte(meta), &packagist); err != nil {
|
||||
return s, errors.New("GetPackagistPayload meta json:" + err.Error())
|
||||
var _ payloadConvertor[PackagistPayload] = packagistConvertor{}
|
||||
|
||||
func newPackagistRequest(ctx context.Context, w *webhook_model.Webhook, t *webhook_model.HookTask) (*http.Request, []byte, error) {
|
||||
meta := &PackagistMeta{}
|
||||
if err := json.Unmarshal([]byte(w.Meta), meta); err != nil {
|
||||
return nil, nil, fmt.Errorf("newpackagistRequest meta json: %w", err)
|
||||
}
|
||||
s.PackagistRepository.URL = packagist.PackageURL
|
||||
return convertPayloader(s, p, event)
|
||||
pc := packagistConvertor{
|
||||
PackageURL: meta.PackageURL,
|
||||
}
|
||||
return newJSONRequest(pc, w, t, true)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue