Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Adds HookEventToken helper function for X-Gitlab-Token #1976

Merged
merged 1 commit into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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