Skip to content

Commit

Permalink
Add helper function for X-Gitlab-Token
Browse files Browse the repository at this point in the history
  • Loading branch information
pup-mikegraf committed Jul 25, 2024
1 parent 32b7839 commit 41bada5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
7 changes: 7 additions & 0 deletions event_parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ type serviceEvent struct {
ObjectKind string `json:"object_kind"`
}

const eventTokenHeader = "X-Gitlab-Token"

// HookEventToken returns the token for the given request.
func HookEventToken(r *http.Request) string {
return r.Header.Get(eventTokenHeader)
}

const eventTypeHeader = "X-Gitlab-Event"

// HookEventType returns the event type for the given request.
Expand Down
13 changes: 13 additions & 0 deletions event_parsing_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ func TestWebhookEventType(t *testing.T) {
}
}

func TestWebhookEventToken(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "https://gitlab.com", nil)
if err != nil {
t.Errorf("Error creating HTTP request: %s", err)
}
req.Header.Set("X-Gitlab-Token", "798d3dd3-67f5-41df-ad19-7882cc6263bf")

actualToken := HookEventToken(req)
if actualToken != "798d3dd3-67f5-41df-ad19-7882cc6263bf" {
t.Errorf("WebhookEventToken is %q, want %q", actualToken, "798d3dd3-67f5-41df-ad19-7882cc6263bf")
}
}

func TestParseBuildHook(t *testing.T) {
raw := loadFixture("testdata/webhooks/build.json")

Expand Down

0 comments on commit 41bada5

Please sign in to comment.