Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
flc1125 committed Aug 20, 2024
1 parent ffa0990 commit 1218bf1
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 2 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@
# Dependency directories (remove the comment below to include it)
vendor/
.idea
_backup
_example
.todo
100 changes: 99 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,99 @@
# go-gitlab-webhook
# Gitlab Webhook Dispatcher 🚀

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

```shell
go get https://github.com/flc1125/go-gitlab-webhook
```

## 💻 Usage

```go
package main

import (
"context"
"net/http"

"github.com/xanzy/go-gitlab"

"github.com/flc1125/gitlabwebhook"
)

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](LICENSE) for the full license text.

0 comments on commit 1218bf1

Please sign in to comment.