Skip to content

πŸš€ A lightweight Gitlab webhook dispatcher for efficient event handling

License

Notifications You must be signed in to change notification settings

flc1125/go-gitlab-webhook

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

8 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Gitlab Webhook Dispatcher πŸš€

Supported Go Versions Package Version GoDoc codecov Go Report Card lint tests MIT license

This is a simple webhook dispatcher for Gitlab. It listens for incoming webhooks and dispatches them to the appropriate handler.

✨ Features

  • πŸ“‹ Very convenient registration of listeners
  • πŸ”„ A single listener can implement multiple different webhook functions
  • ⚑ Support asynchronous and efficient processing
  • πŸš€ Multiple dispatch methods

πŸ“¦ Installation

go get github.com/flc1125/go-gitlab-webhook

πŸ’» Usage

package main

import (
	"context"
	"net/http"

	"github.com/xanzy/go-gitlab"

	"github.com/flc1125/go-gitlab-webhook"
)

var (
	_ gitlabwebhook.BuildListener         = (*testBuildListener)(nil)
	_ gitlabwebhook.CommitCommentListener = (*testCommitCommentListener)(nil)
	_ gitlabwebhook.BuildListener         = (*testBuildAndCommitCommentListener)(nil)
	_ gitlabwebhook.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 main() {
	dispatcher := gitlabwebhook.NewDispatcher(
		gitlabwebhook.RegisterListeners(
			&testBuildListener{},
			&testCommitCommentListener{},
			&testBuildAndCommitCommentListener{},
		),
	)

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

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

		w.WriteHeader(http.StatusNoContent)
	}))

	if err := http.ListenAndServe(":8080", nil); err != nil {
		panic(err)
	}
}

πŸ“œ License

MIT License. See LICENSE for the full license text.

πŸ’– Thanks

  • xanzy/go-gitlab: Go library for accessing the GitLab API
  • stretchr/testify: A toolkit with common assertions and mocks that plays nicely with the standard library