xanzy / go-gitlab

GitLab Go SDK
Apache License 2.0
2.37k stars 940 forks source link

feat(dispatcher): elegant and fast Webhook request processing component #1990

Closed flc1125 closed 3 weeks ago

flc1125 commented 3 weeks ago

❗️This feature migrates to https://github.com/flc1125/go-gitlab-webhook


Introduction

An extremely elegant and fast component for receiving and processing Webhook requests has been implemented.

Features

Example

source: examples/dispatcher.go

package main

import (
    "context"
    "net/http"

    "github.com/xanzy/go-gitlab"
)

var (
    _ gitlab.BuildListener         = (*testBuildListener)(nil)
    _ gitlab.CommitCommentListener = (*testCommitCommentListener)(nil)
    _ gitlab.BuildListener         = (*testBuildAndCommitCommentListener)(nil)
    _ gitlab.CommitCommentListener = (*testBuildAndCommitCommentListener)(nil)
)

type testBuildListener struct{}

func (l *testBuildListener) OnBuild(ctx context.Context, event *gitlab.BuildEvent) error {
    // do something
    return nil
}

type testCommitCommentListener struct{}

func (l *testCommitCommentListener) OnCommitComment(ctx context.Context, event *gitlab.CommitCommentEvent) error {
    // do something
    return nil
}

type testBuildAndCommitCommentListener struct {
}

func (l *testBuildAndCommitCommentListener) OnBuild(ctx context.Context, event *gitlab.BuildEvent) error {
    // do something
    return nil
}

func (l *testBuildAndCommitCommentListener) OnCommitComment(ctx context.Context, event *gitlab.CommitCommentEvent) error {
    // do something
    return nil
}

func loadDispatcher() {
    dispatcher := gitlab.NewDispatcher()

    dispatcher.RegisterListeners(
        &testBuildListener{},
        &testCommitCommentListener{},
        &testBuildAndCommitCommentListener{},
    )

    http.Handle("/webhook", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        if err := dispatcher.DispatchRequest(r,
            gitlab.DispatchRequestWithContext(context.Background()),
        ); err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
            return
        }

        w.WriteHeader(http.StatusNoContent)
    }))

    if err := http.ListenAndServe(":8080", nil); err != nil {
        panic(err)
    }
}
svanharmelen commented 3 weeks ago

Thanks for the PR, but unfortunately this is not something I am willing to merge as this project only supports what is in the public API docs in order for me to be able to keep maintaining the project.

Maybe its an idea to create a dedicated package instead of trying to add it to this package? In any case I am going to close the PR, sorry!

flc1125 commented 3 weeks ago

This feature migrates to https://github.com/flc1125/go-gitlab-webhook

svanharmelen commented 3 weeks ago

Very cool you took the effort to create a dedicated package for this 👍🏻👍🏻