Skip to content

Commit

Permalink
github-events: Add installation, hook ID, and headers to event messag…
Browse files Browse the repository at this point in the history
…es. (#655)

- Adds installation message for identifying GitHub App installations.
These won't be present in the legacy repo based webhooks.
- Adds X-GitHub-Hook-ID as an extension to make it filterable by
downstream clients.
- Records the incoming event headers in the body payload, which includes
useful info for debugging (e.g. delivery IDs).

Part of chainguard-dev/internal-dev#1513
  • Loading branch information
wlynch authored Dec 12, 2024
1 parent b9cd767 commit 5d896a8
Show file tree
Hide file tree
Showing 10 changed files with 414 additions and 6 deletions.
36 changes: 32 additions & 4 deletions modules/github-events/cmd/trampoline/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,21 @@ func main() {
event.SetSource(r.Host)
event.SetSubject(msg.Repository.FullName)
event.SetExtension("action", msg.Action)
if err := event.SetData(cloudevents.ApplicationJSON, struct {
When time.Time `json:"when"`
Body json.RawMessage `json:"body"`
}{
// Needs to be an extension to be a filterable attribute.
// See https://github.com/chainguard-dev/terraform-infra-common/blob/main/pkg/pubsub/cloudevent.go
if id := r.Header.Get("X-GitHub-Hook-ID"); id != "" {
event.SetExtension("github-hook-id", id)
}
if err := event.SetData(cloudevents.ApplicationJSON, eventData{
When: time.Now(),
Headers: &eventHeaders{
HookID: r.Header.Get("X-GitHub-Hook-ID"),
DeliveryID: r.Header.Get("X-GitHub-Delivery"),
UserAgent: r.Header.Get("User-Agent"),
Event: r.Header.Get("X-GitHub-Event"),
InstallationTargetType: r.Header.Get("X-GitHub-Installation-Target-Type"),
InstallationTargetID: r.Header.Get("X-GitHub-Installation-Target-ID"),
},
Body: payload,
}); err != nil {
log.Errorf("failed to set data: %v", err)
Expand All @@ -114,3 +124,21 @@ func main() {
}
clog.FatalContextf(ctx, "ListenAndServe: %v", srv.ListenAndServe())
}

type eventData struct {
When time.Time `json:"when,omitempty"`
// See https://docs.github.com/en/webhooks/webhook-events-and-payloads#delivery-headers
Headers *eventHeaders `json:"headers,omitempty"`
Body json.RawMessage `json:"body,omitempty"`
}

// Relevant headers for GitHub webhook events that we want to record.
// See https://docs.github.com/en/webhooks/webhook-events-and-payloads#delivery-headers
type eventHeaders struct {
HookID string `json:"hook_id,omitempty"`
DeliveryID string `json:"delivery_id,omitempty"`
UserAgent string `json:"user_agent,omitempty"`
Event string `json:"event,omitempty"`
InstallationTargetType string `json:"installation_target_type,omitempty"`
InstallationTargetID string `json:"installation_target_id,omitempty"`
}
44 changes: 44 additions & 0 deletions modules/github-events/schemas/check_run.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,36 @@
"name": "when",
"type": "TIMESTAMP"
},
{
"fields": [
{
"name": "hook_id",
"type": "STRING"
},
{
"name": "delivery_id",
"type": "STRING"
},
{
"name": "user_agent",
"type": "STRING"
},
{
"name": "event",
"type": "STRING"
},
{
"name": "installation_target_type",
"type": "STRING"
},
{
"name": "installation_target_id",
"type": "STRING"
}
],
"name": "headers",
"type": "RECORD"
},
{
"fields": [
{
Expand Down Expand Up @@ -568,6 +598,20 @@
],
"name": "sender",
"type": "RECORD"
},
{
"fields": [
{
"name": "id",
"type": "INTEGER"
},
{
"name": "app_id",
"type": "INTEGER"
}
],
"name": "installation",
"type": "RECORD"
}
],
"name": "body",
Expand Down
44 changes: 44 additions & 0 deletions modules/github-events/schemas/check_suite.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,36 @@
"name": "when",
"type": "TIMESTAMP"
},
{
"fields": [
{
"name": "hook_id",
"type": "STRING"
},
{
"name": "delivery_id",
"type": "STRING"
},
{
"name": "user_agent",
"type": "STRING"
},
{
"name": "event",
"type": "STRING"
},
{
"name": "installation_target_type",
"type": "STRING"
},
{
"name": "installation_target_id",
"type": "STRING"
}
],
"name": "headers",
"type": "RECORD"
},
{
"fields": [
{
Expand Down Expand Up @@ -330,6 +360,20 @@
],
"name": "sender",
"type": "RECORD"
},
{
"fields": [
{
"name": "id",
"type": "INTEGER"
},
{
"name": "app_id",
"type": "INTEGER"
}
],
"name": "installation",
"type": "RECORD"
}
],
"name": "body",
Expand Down
32 changes: 30 additions & 2 deletions modules/github-events/schemas/event_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@ import (
)

type Wrapper[T any] struct {
When time.Time
Body T
When time.Time
Headers *GitHubHeaders
Body T
}

type GitHubHeaders struct {
HookID bigquery.NullString `json:"hook_id,omitempty" bigquery:"hook_id"`
DeliveryID bigquery.NullString `json:"delivery_id,omitempty" bigquery:"delivery_id"`
UserAgent bigquery.NullString `json:"user_agent,omitempty" bigquery:"user_agent"`
Event bigquery.NullString `json:"event,omitempty" bigquery:"event"`
InstallationTargetType bigquery.NullString `json:"installation_target_type,omitempty" bigquery:"installation_target_type"`
InstallationTargetID bigquery.NullString `json:"installation_target_id,omitempty" bigquery:"installation_target_id"`
}

// https://pkg.go.dev/github.com/google/go-github/v60/github#User
Expand All @@ -30,6 +40,14 @@ type Repository struct {
FullName bigquery.NullString `json:"full_name,omitempty" bigquery:"full_name"`
}

// https://pkg.go.dev/github.com/google/go-github/v60/github#Installation
type Installation struct {
// Installation ID
ID bigquery.NullInt64 `json:"id,omitempty" bigquery:"id"`
// App ID
AppID bigquery.NullInt64 `json:"app_id,omitempty" bigquery:"app_id"`
}

// https://pkg.go.dev/github.com/google/go-github/v60/github#PullRequestBranch
type PullRequestBranch struct {
Ref bigquery.NullString `json:"ref,omitempty" bigquery:"ref"`
Expand Down Expand Up @@ -91,6 +109,8 @@ type PullRequestEvent struct {
// Populated when action is synchronize
Before bigquery.NullString `json:"before,omitempty" bigquery:"before"`
After bigquery.NullString `json:"after,omitempty" bigquery:"after"`

Installation *Installation `json:"installation,omitempty" bigquery:"installation"`
}

// https://pkg.go.dev/github.com/google/go-github/v61/github#PushEventRepository
Expand Down Expand Up @@ -119,6 +139,8 @@ type PushEvent struct {
Sender User `json:"sender,omitempty" bigquery:"sender"`

Organization Organization `json:"organization,omitempty" bigquery:"organization"`

Installation *Installation `json:"installation,omitempty" bigquery:"installation"`
}

// https://pkg.go.dev/github.com/google/go-github/v60/github#Workflow
Expand Down Expand Up @@ -158,6 +180,7 @@ type WorkflowRunEvent struct {
Organization Organization `json:"organization,omitempty" bigquery:"organization"`
Repository Repository `json:"repository,omitempty" bigquery:"repository"`
Sender User `json:"sender,omitempty" bigquery:"sender"`
Installation *Installation `json:"installation,omitempty" bigquery:"installation"`
}

// https://pkg.go.dev/github.com/google/go-github/v60/github#IssueCommentEvent
Expand All @@ -168,6 +191,7 @@ type IssueCommentEvent struct {
Repo Repository `json:"repository,omitempty" bigquery:"repository"`
Sender User `json:"sender,omitempty" bigquery:"sender"`
Organization Organization `json:"organization,omitempty" bigquery:"organization"`
Installation *Installation `json:"installation,omitempty" bigquery:"installation"`
}

// https://pkg.go.dev/github.com/google/go-github/v60/github#IssueEvent
Expand All @@ -187,6 +211,7 @@ type IssueEvent struct {
LockReason bigquery.NullString `json:"lock_reason,omitempty" bigquery:"lock_reason"`
RequestedReviewer User `json:"requested_reviewer,omitempty" bigquery:"requested_reviewer"`
ReviewRequester User `json:"review_requester,omitempty" bigquery:"review_requester"`
Installation *Installation `json:"installation,omitempty" bigquery:"installation"`
}

// https://pkg.go.dev/github.com/google/go-github/v60/github#Issue
Expand Down Expand Up @@ -236,6 +261,7 @@ type CheckRunEvent struct {
Repository Repository `json:"repository,omitempty" bigquery:"repository"`
Organization Organization `json:"organization,omitempty" bigquery:"organization"`
Sender User `json:"sender,omitempty" bigquery:"sender"`
Installation *Installation `json:"installation,omitempty" bigquery:"installation"`
}

// https://pkg.go.dev/github.com/google/go-github/v60/github#CheckRun
Expand Down Expand Up @@ -270,6 +296,7 @@ type CheckSuiteEvent struct {
Repository Repository `json:"repository,omitempty" bigquery:"repository"`
Organization Organization `json:"organization,omitempty" bigquery:"organization"`
Sender User `json:"sender,omitempty" bigquery:"sender"`
Installation *Installation `json:"installation,omitempty" bigquery:"installation"`
}

// https://github.com/google/go-github/blob/v60.0.0/github/event_types.go#L1085
Expand All @@ -292,4 +319,5 @@ type ProjectsV2ItemEvent struct {
ProjectV2Item *ProjectV2Item `json:"projects_v2_item,omitempty" bigquery:"projects_v2_item"`
Organization *Organization `json:"organization,omitempty" bigquery:"organization"`
Sender *User `json:"sender,omitempty" bigquery:"sender"`
Installation *Installation `json:"installation,omitempty" bigquery:"installation"`
}
44 changes: 44 additions & 0 deletions modules/github-events/schemas/issue_comment.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,36 @@
"name": "when",
"type": "TIMESTAMP"
},
{
"fields": [
{
"name": "hook_id",
"type": "STRING"
},
{
"name": "delivery_id",
"type": "STRING"
},
{
"name": "user_agent",
"type": "STRING"
},
{
"name": "event",
"type": "STRING"
},
{
"name": "installation_target_type",
"type": "STRING"
},
{
"name": "installation_target_id",
"type": "STRING"
}
],
"name": "headers",
"type": "RECORD"
},
{
"fields": [
{
Expand Down Expand Up @@ -302,6 +332,20 @@
],
"name": "organization",
"type": "RECORD"
},
{
"fields": [
{
"name": "id",
"type": "INTEGER"
},
{
"name": "app_id",
"type": "INTEGER"
}
],
"name": "installation",
"type": "RECORD"
}
],
"name": "body",
Expand Down
44 changes: 44 additions & 0 deletions modules/github-events/schemas/issues.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,36 @@
"name": "when",
"type": "TIMESTAMP"
},
{
"fields": [
{
"name": "hook_id",
"type": "STRING"
},
{
"name": "delivery_id",
"type": "STRING"
},
{
"name": "user_agent",
"type": "STRING"
},
{
"name": "event",
"type": "STRING"
},
{
"name": "installation_target_type",
"type": "STRING"
},
{
"name": "installation_target_id",
"type": "STRING"
}
],
"name": "headers",
"type": "RECORD"
},
{
"fields": [
{
Expand Down Expand Up @@ -356,6 +386,20 @@
],
"name": "review_requester",
"type": "RECORD"
},
{
"fields": [
{
"name": "id",
"type": "INTEGER"
},
{
"name": "app_id",
"type": "INTEGER"
}
],
"name": "installation",
"type": "RECORD"
}
],
"name": "body",
Expand Down
Loading

0 comments on commit 5d896a8

Please sign in to comment.