-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
99 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,5 @@ | |
# Dependency directories (remove the comment below to include it) | ||
vendor/ | ||
.idea | ||
_backup | ||
_example | ||
.todo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |