// Copyright 2021 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package github

import (
	
	
	
)

// HookDelivery represents the data that is received from GitHub's Webhook Delivery API
//
// GitHub API docs:
// - https://docs.github.com/rest/webhooks/repo-deliveries#list-deliveries-for-a-repository-webhook
// - https://docs.github.com/rest/webhooks/repo-deliveries#get-a-delivery-for-a-repository-webhook
type HookDelivery struct {
	ID             *int64     `json:"id,omitempty"`
	GUID           *string    `json:"guid,omitempty"`
	DeliveredAt    *Timestamp `json:"delivered_at,omitempty"`
	Redelivery     *bool      `json:"redelivery,omitempty"`
	Duration       *float64   `json:"duration,omitempty"`
	Status         *string    `json:"status,omitempty"`
	StatusCode     *int       `json:"status_code,omitempty"`
	Event          *string    `json:"event,omitempty"`
	Action         *string    `json:"action,omitempty"`
	InstallationID *int64     `json:"installation_id,omitempty"`
	RepositoryID   *int64     `json:"repository_id,omitempty"`

	// Request is populated by GetHookDelivery.
	Request *HookRequest `json:"request,omitempty"`
	// Response is populated by GetHookDelivery.
	Response *HookResponse `json:"response,omitempty"`
}

func ( HookDelivery) () string {
	return Stringify()
}

// HookRequest is a part of HookDelivery that contains
// the HTTP headers and the JSON payload of the webhook request.
type HookRequest struct {
	Headers    map[string]string `json:"headers,omitempty"`
	RawPayload *json.RawMessage  `json:"payload,omitempty"`
}

func ( HookRequest) () string {
	return Stringify()
}

// HookResponse is a part of HookDelivery that contains
// the HTTP headers and the response body served by the webhook endpoint.
type HookResponse struct {
	Headers    map[string]string `json:"headers,omitempty"`
	RawPayload *json.RawMessage  `json:"payload,omitempty"`
}

func ( HookResponse) () string {
	return Stringify()
}

// ListHookDeliveries lists webhook deliveries for a webhook configured in a repository.
//
// GitHub API docs: https://docs.github.com/rest/repos/webhooks#list-deliveries-for-a-repository-webhook
//
//meta:operation GET /repos/{owner}/{repo}/hooks/{hook_id}/deliveries
func ( *RepositoriesService) ( context.Context, ,  string,  int64,  *ListCursorOptions) ([]*HookDelivery, *Response, error) {
	 := fmt.Sprintf("repos/%v/%v/hooks/%v/deliveries", , , )
	,  := addOptions(, )
	if  != nil {
		return nil, nil, 
	}

	,  := .client.NewRequest("GET", , nil)
	if  != nil {
		return nil, nil, 
	}

	 := []*HookDelivery{}
	,  := .client.Do(, , &)
	if  != nil {
		return nil, , 
	}

	return , , nil
}

// GetHookDelivery returns a delivery for a webhook configured in a repository.
//
// GitHub API docs: https://docs.github.com/rest/repos/webhooks#get-a-delivery-for-a-repository-webhook
//
//meta:operation GET /repos/{owner}/{repo}/hooks/{hook_id}/deliveries/{delivery_id}
func ( *RepositoriesService) ( context.Context, ,  string, ,  int64) (*HookDelivery, *Response, error) {
	 := fmt.Sprintf("repos/%v/%v/hooks/%v/deliveries/%v", , , , )
	,  := .client.NewRequest("GET", , nil)
	if  != nil {
		return nil, nil, 
	}

	 := new(HookDelivery)
	,  := .client.Do(, , )
	if  != nil {
		return nil, , 
	}

	return , , nil
}

// RedeliverHookDelivery redelivers a delivery for a webhook configured in a repository.
//
// GitHub API docs: https://docs.github.com/rest/repos/webhooks#redeliver-a-delivery-for-a-repository-webhook
//
//meta:operation POST /repos/{owner}/{repo}/hooks/{hook_id}/deliveries/{delivery_id}/attempts
func ( *RepositoriesService) ( context.Context, ,  string, ,  int64) (*HookDelivery, *Response, error) {
	 := fmt.Sprintf("repos/%v/%v/hooks/%v/deliveries/%v/attempts", , , , )
	,  := .client.NewRequest("POST", , nil)
	if  != nil {
		return nil, nil, 
	}

	 := new(HookDelivery)
	,  := .client.Do(, , )
	if  != nil {
		return nil, , 
	}

	return , , nil
}

// ParseRequestPayload parses the request payload. For recognized event types,
// a value of the corresponding struct type will be returned.
func ( *HookDelivery) () (interface{}, error) {
	,  := messageToTypeName[.GetEvent()]
	if ! {
		return nil, fmt.Errorf("unsupported event type %q", .GetEvent())
	}

	 := &Event{Type: &, RawPayload: .Request.RawPayload}
	return .ParsePayload()
}