github.com/google/go-github/v66@v66.0.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/rest/webhooks/repo-deliveries#list-deliveries-for-a-repository-webhook 18 // - https://docs.github.com/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/rest/repos/webhooks#list-deliveries-for-a-repository-webhook 67 // 68 //meta:operation GET /repos/{owner}/{repo}/hooks/{hook_id}/deliveries 69 func (s *RepositoriesService) ListHookDeliveries(ctx context.Context, owner, repo string, id int64, opts *ListCursorOptions) ([]*HookDelivery, *Response, error) { 70 u := fmt.Sprintf("repos/%v/%v/hooks/%v/deliveries", owner, repo, id) 71 u, err := addOptions(u, opts) 72 if err != nil { 73 return nil, nil, err 74 } 75 76 req, err := s.client.NewRequest("GET", u, nil) 77 if err != nil { 78 return nil, nil, err 79 } 80 81 deliveries := []*HookDelivery{} 82 resp, err := s.client.Do(ctx, req, &deliveries) 83 if err != nil { 84 return nil, resp, err 85 } 86 87 return deliveries, resp, nil 88 } 89 90 // GetHookDelivery returns a delivery for a webhook configured in a repository. 91 // 92 // GitHub API docs: https://docs.github.com/rest/repos/webhooks#get-a-delivery-for-a-repository-webhook 93 // 94 //meta:operation GET /repos/{owner}/{repo}/hooks/{hook_id}/deliveries/{delivery_id} 95 func (s *RepositoriesService) GetHookDelivery(ctx context.Context, owner, repo string, hookID, deliveryID int64) (*HookDelivery, *Response, error) { 96 u := fmt.Sprintf("repos/%v/%v/hooks/%v/deliveries/%v", owner, repo, hookID, deliveryID) 97 req, err := s.client.NewRequest("GET", u, nil) 98 if err != nil { 99 return nil, nil, err 100 } 101 102 h := new(HookDelivery) 103 resp, err := s.client.Do(ctx, req, h) 104 if err != nil { 105 return nil, resp, err 106 } 107 108 return h, resp, nil 109 } 110 111 // RedeliverHookDelivery redelivers a delivery for a webhook configured in a repository. 112 // 113 // GitHub API docs: https://docs.github.com/rest/repos/webhooks#redeliver-a-delivery-for-a-repository-webhook 114 // 115 //meta:operation POST /repos/{owner}/{repo}/hooks/{hook_id}/deliveries/{delivery_id}/attempts 116 func (s *RepositoriesService) RedeliverHookDelivery(ctx context.Context, owner, repo string, hookID, deliveryID int64) (*HookDelivery, *Response, error) { 117 u := fmt.Sprintf("repos/%v/%v/hooks/%v/deliveries/%v/attempts", owner, repo, hookID, deliveryID) 118 req, err := s.client.NewRequest("POST", u, nil) 119 if err != nil { 120 return nil, nil, err 121 } 122 123 h := new(HookDelivery) 124 resp, err := s.client.Do(ctx, req, h) 125 if err != nil { 126 return nil, resp, err 127 } 128 129 return h, resp, nil 130 } 131 132 // ParseRequestPayload parses the request payload. For recognized event types, 133 // a value of the corresponding struct type will be returned. 134 func (d *HookDelivery) ParseRequestPayload() (interface{}, error) { 135 eType, ok := messageToTypeName[d.GetEvent()] 136 if !ok { 137 return nil, fmt.Errorf("unsupported event type %q", d.GetEvent()) 138 } 139 140 e := &Event{Type: &eType, RawPayload: d.Request.RawPayload} 141 return e.ParsePayload() 142 }