github.com/google/go-github/v49@v49.1.0/github/repos_hooks_deliveries.go (about) 1 // Copyright 2021 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 "context" 10 "encoding/json" 11 "fmt" 12 ) 13 14 // HookDelivery represents the data that is received from GitHub's Webhook Delivery API 15 // 16 // GitHub API docs: 17 // - https://docs.github.com/en/rest/webhooks/repo-deliveries#list-deliveries-for-a-repository-webhook 18 // - https://docs.github.com/en/rest/webhooks/repo-deliveries#get-a-delivery-for-a-repository-webhook 19 type HookDelivery struct { 20 ID *int64 `json:"id,omitempty"` 21 GUID *string `json:"guid,omitempty"` 22 DeliveredAt *Timestamp `json:"delivered_at,omitempty"` 23 Redelivery *bool `json:"redelivery,omitempty"` 24 Duration *float64 `json:"duration,omitempty"` 25 Status *string `json:"status,omitempty"` 26 StatusCode *int `json:"status_code,omitempty"` 27 Event *string `json:"event,omitempty"` 28 Action *string `json:"action,omitempty"` 29 InstallationID *int64 `json:"installation_id,omitempty"` 30 RepositoryID *int64 `json:"repository_id,omitempty"` 31 32 // Request is populated by GetHookDelivery. 33 Request *HookRequest `json:"request,omitempty"` 34 // Response is populated by GetHookDelivery. 35 Response *HookResponse `json:"response,omitempty"` 36 } 37 38 func (d HookDelivery) String() string { 39 return Stringify(d) 40 } 41 42 // HookRequest is a part of HookDelivery that contains 43 // the HTTP headers and the JSON payload of the webhook request. 44 type HookRequest struct { 45 Headers map[string]string `json:"headers,omitempty"` 46 RawPayload *json.RawMessage `json:"payload,omitempty"` 47 } 48 49 func (r HookRequest) String() string { 50 return Stringify(r) 51 } 52 53 // HookResponse is a part of HookDelivery that contains 54 // the HTTP headers and the response body served by the webhook endpoint. 55 type HookResponse struct { 56 Headers map[string]string `json:"headers,omitempty"` 57 RawPayload *json.RawMessage `json:"payload,omitempty"` 58 } 59 60 func (r HookResponse) String() string { 61 return Stringify(r) 62 } 63 64 // ListHookDeliveries lists webhook deliveries for a webhook configured in a repository. 65 // 66 // GitHub API docs: https://docs.github.com/en/rest/webhooks/repo-deliveries#list-deliveries-for-a-repository-webhook 67 func (s *RepositoriesService) ListHookDeliveries(ctx context.Context, owner, repo string, id int64, opts *ListCursorOptions) ([]*HookDelivery, *Response, error) { 68 u := fmt.Sprintf("repos/%v/%v/hooks/%v/deliveries", owner, repo, id) 69 u, err := addOptions(u, opts) 70 if err != nil { 71 return nil, nil, err 72 } 73 74 req, err := s.client.NewRequest("GET", u, nil) 75 if err != nil { 76 return nil, nil, err 77 } 78 79 deliveries := []*HookDelivery{} 80 resp, err := s.client.Do(ctx, req, &deliveries) 81 if err != nil { 82 return nil, resp, err 83 } 84 85 return deliveries, resp, nil 86 } 87 88 // GetHookDelivery returns a delivery for a webhook configured in a repository. 89 // 90 // GitHub API docs: https://docs.github.com/en/rest/webhooks/repo-deliveries#get-a-delivery-for-a-repository-webhook 91 func (s *RepositoriesService) GetHookDelivery(ctx context.Context, owner, repo string, hookID, deliveryID int64) (*HookDelivery, *Response, error) { 92 u := fmt.Sprintf("repos/%v/%v/hooks/%v/deliveries/%v", owner, repo, hookID, deliveryID) 93 req, err := s.client.NewRequest("GET", u, nil) 94 if err != nil { 95 return nil, nil, err 96 } 97 98 h := new(HookDelivery) 99 resp, err := s.client.Do(ctx, req, h) 100 if err != nil { 101 return nil, resp, err 102 } 103 104 return h, resp, nil 105 } 106 107 // RedeliverHookDelivery redelivers a delivery for a webhook configured in a repository. 108 // 109 // GitHub API docs: https://docs.github.com/en/rest/webhooks/repo-deliveries#redeliver-a-delivery-for-a-repository-webhook 110 func (s *RepositoriesService) RedeliverHookDelivery(ctx context.Context, owner, repo string, hookID, deliveryID int64) (*HookDelivery, *Response, error) { 111 u := fmt.Sprintf("repos/%v/%v/hooks/%v/deliveries/%v/attempts", owner, repo, hookID, deliveryID) 112 req, err := s.client.NewRequest("POST", u, nil) 113 if err != nil { 114 return nil, nil, err 115 } 116 117 h := new(HookDelivery) 118 resp, err := s.client.Do(ctx, req, h) 119 if err != nil { 120 return nil, resp, err 121 } 122 123 return h, resp, nil 124 } 125 126 // ParseRequestPayload parses the request payload. For recognized event types, 127 // a value of the corresponding struct type will be returned. 128 func (d *HookDelivery) ParseRequestPayload() (interface{}, error) { 129 eType, ok := eventTypeMapping[*d.Event] 130 if !ok { 131 return nil, fmt.Errorf("unsupported event type %q", *d.Event) 132 } 133 134 e := &Event{Type: &eType, RawPayload: d.Request.RawPayload} 135 return e.ParsePayload() 136 }