Skip to content

Conversation

acouvreur
Copy link
Contributor

This function can be used to execute the registered events directly by specifying the deliveryID, the event name and the event itself.

Closes #115

This function can be used to execute the registered events directly by specifying the deliveryID, the event name and the event itself.

Closes cbrgm#115
@acouvreur acouvreur requested a review from cbrgm as a code owner February 12, 2025 22:38
@cbrgm
Copy link
Owner

cbrgm commented Feb 13, 2025

Hi, this is a great addition!
The file you edited is auto-generated. Changes must be done in gen/template_webhook_event.go starting at line 240. This is how it should look like

// HandleEventRequest parses a Github event from http.Request and executes registered handlers.
func (g *EventHandler) HandleEventRequest(req *http.Request) error {
	payload, err := github.ValidatePayload(req, []byte(g.WebhookSecret))
	if err != nil {
		return fmt.Errorf("could not validate webhook payload: err=%s\n", err)
	}
	event, err := github.ParseWebHook(github.WebHookType(req), payload)
	if err != nil {
		return fmt.Errorf("could not parse webhook: err=%s\n", err)
	}

	deliveryID := github.DeliveryID(req)
	eventName := github.WebHookType(req)

	return g.HandleEvent(deliveryID, eventName, event)
}

// HandleEvent executes registered handlers.
func (g *EventHandler) HandleEvent(deliveryID string, eventName string, event interface{}) error {
	switch event := event.(type) {
	{{ range $_, $webhook := .Webhooks }}
	case *github.{{ $webhook.Event }}:
		return g.{{ $webhook.Event }}(deliveryID, eventName, event)
	{{ end }}
	}
	return nil
}

Can you update the PR and run make generate + make test afterwards?

@acouvreur
Copy link
Contributor Author

acouvreur commented Feb 13, 2025

Done @cbrgm !

githubevents git:(add-handle-event) ✗ make generate
mkdir -p bin
CGO_ENABLED=0 go build -v -ldflags '-w ' -o ./bin/githubhook-gen ./gen/*
command-line-arguments
./bin/githubhook-gen --output=githubevents
➜  githubevents git:(add-handle-event) ✗ make test
	github.com/cbrgm/githubevents/examples/simple-http-server		coverage: 0.0% of statements
	github.com/cbrgm/githubevents/examples/simple-http-server-funcs		coverage: 0.0% of statements
	github.com/cbrgm/githubevents/examples/simple-http-server-packages		coverage: 0.0% of statements
	github.com/cbrgm/githubevents/examples/simple-http-server-packages/plugins		coverage: 0.0% of statements
	github.com/cbrgm/githubevents/gen		coverage: 0.0% of statements
ok  	github.com/cbrgm/githubevents/githubevents	1.362s	coverage: 88.4% of statements

@cbrgm
Copy link
Owner

cbrgm commented Feb 13, 2025

LGTM, let's get this merged and released 🚀

@cbrgm cbrgm merged commit c4fa965 into cbrgm:main Feb 13, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: add HandleEvent function
2 participants