github.com/google/go-github/v66@v66.0.0/github/event.go (about) 1 // Copyright 2018 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 package github 7 8 import ( 9 "encoding/json" 10 ) 11 12 // Event represents a GitHub event. 13 type Event struct { 14 Type *string `json:"type,omitempty"` 15 Public *bool `json:"public,omitempty"` 16 RawPayload *json.RawMessage `json:"payload,omitempty"` 17 Repo *Repository `json:"repo,omitempty"` 18 Actor *User `json:"actor,omitempty"` 19 Org *Organization `json:"org,omitempty"` 20 CreatedAt *Timestamp `json:"created_at,omitempty"` 21 ID *string `json:"id,omitempty"` 22 } 23 24 func (e Event) String() string { 25 return Stringify(e) 26 } 27 28 // ParsePayload parses the event payload. For recognized event types, 29 // a value of the corresponding struct type will be returned. 30 func (e *Event) ParsePayload() (interface{}, error) { 31 // It would be nice if e.Type were the snake_case name of the event, 32 // but the existing interface uses the struct name instead. 33 payload := EventForType(typeToMessageMapping[e.GetType()]) 34 35 if err := json.Unmarshal(e.GetRawPayload(), &payload); err != nil { 36 return nil, err 37 } 38 39 return payload, nil 40 } 41 42 // Payload returns the parsed event payload. For recognized event types, 43 // a value of the corresponding struct type will be returned. 44 // 45 // Deprecated: Use ParsePayload instead, which returns an error 46 // rather than panics if JSON unmarshaling raw payload fails. 47 func (e *Event) Payload() (payload interface{}) { 48 var err error 49 payload, err = e.ParsePayload() 50 if err != nil { 51 panic(err) 52 } 53 return payload 54 }