// Copyright 2013 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 (
	
	
	
	
	
)

// WebHookPayload represents the data that is received from GitHub when a push
// event hook is triggered. The format of these payloads pre-date most of the
// GitHub v3 API, so there are lots of minor incompatibilities with the types
// defined in the rest of the API. Therefore, several types are duplicated
// here to account for these differences.
//
// GitHub API docs: https://help.github.com/articles/post-receive-hooks
//
// Deprecated: Please use PushEvent instead.
type WebHookPayload = PushEvent

// WebHookCommit represents the commit variant we receive from GitHub in a
// WebHookPayload.
//
// Deprecated: Please use HeadCommit instead.
type WebHookCommit = HeadCommit

// WebHookAuthor represents the author or committer of a commit, as specified
// in a WebHookCommit. The commit author may not correspond to a GitHub User.
//
// Deprecated: Please use CommitAuthor instead.
// NOTE Breaking API change: the `Username` field is now called `Login`.
type WebHookAuthor = CommitAuthor

// Hook represents a GitHub (web and service) hook for a repository.
type Hook struct {
	CreatedAt    *Timestamp             `json:"created_at,omitempty"`
	UpdatedAt    *Timestamp             `json:"updated_at,omitempty"`
	URL          *string                `json:"url,omitempty"`
	ID           *int64                 `json:"id,omitempty"`
	Type         *string                `json:"type,omitempty"`
	Name         *string                `json:"name,omitempty"`
	TestURL      *string                `json:"test_url,omitempty"`
	PingURL      *string                `json:"ping_url,omitempty"`
	LastResponse map[string]interface{} `json:"last_response,omitempty"`

	// Only the following fields are used when creating a hook.
	// Config is required.
	Config *HookConfig `json:"config,omitempty"`
	Events []string    `json:"events,omitempty"`
	Active *bool       `json:"active,omitempty"`
}

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

// createHookRequest is a subset of Hook and is used internally
// by CreateHook to pass only the known fields for the endpoint.
//
// See https://github.com/google/go-github/issues/1015 for more
// information.
type createHookRequest struct {
	// Config is required.
	Name   string      `json:"name"`
	Config *HookConfig `json:"config,omitempty"`
	Events []string    `json:"events,omitempty"`
	Active *bool       `json:"active,omitempty"`
}

// CreateHook creates a Hook for the specified repository.
// Config is a required field.
//
// Note that only a subset of the hook fields are used and hook must
// not be nil.
//
// GitHub API docs: https://docs.github.com/rest/repos/webhooks#create-a-repository-webhook
//
//meta:operation POST /repos/{owner}/{repo}/hooks
func ( *RepositoriesService) ( context.Context, ,  string,  *Hook) (*Hook, *Response, error) {
	 := fmt.Sprintf("repos/%v/%v/hooks", , )

	 := &createHookRequest{
		Name:   "web",
		Events: .Events,
		Active: .Active,
		Config: .Config,
	}

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

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

	return , , nil
}

// ListHooks lists all Hooks for the specified repository.
//
// GitHub API docs: https://docs.github.com/rest/repos/webhooks#list-repository-webhooks
//
//meta:operation GET /repos/{owner}/{repo}/hooks
func ( *RepositoriesService) ( context.Context, ,  string,  *ListOptions) ([]*Hook, *Response, error) {
	 := fmt.Sprintf("repos/%v/%v/hooks", , )
	,  := addOptions(, )
	if  != nil {
		return nil, nil, 
	}

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

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

	return , , nil
}

// GetHook returns a single specified Hook.
//
// GitHub API docs: https://docs.github.com/rest/repos/webhooks#get-a-repository-webhook
//
//meta:operation GET /repos/{owner}/{repo}/hooks/{hook_id}
func ( *RepositoriesService) ( context.Context, ,  string,  int64) (*Hook, *Response, error) {
	 := fmt.Sprintf("repos/%v/%v/hooks/%d", , , )
	,  := .client.NewRequest("GET", , nil)
	if  != nil {
		return nil, nil, 
	}
	 := new(Hook)
	,  := .client.Do(, , )
	if  != nil {
		return nil, , 
	}

	return , , nil
}

// EditHook updates a specified Hook.
//
// GitHub API docs: https://docs.github.com/rest/repos/webhooks#update-a-repository-webhook
//
//meta:operation PATCH /repos/{owner}/{repo}/hooks/{hook_id}
func ( *RepositoriesService) ( context.Context, ,  string,  int64,  *Hook) (*Hook, *Response, error) {
	 := fmt.Sprintf("repos/%v/%v/hooks/%d", , , )
	,  := .client.NewRequest("PATCH", , )
	if  != nil {
		return nil, nil, 
	}
	 := new(Hook)
	,  := .client.Do(, , )
	if  != nil {
		return nil, , 
	}

	return , , nil
}

// DeleteHook deletes a specified Hook.
//
// GitHub API docs: https://docs.github.com/rest/repos/webhooks#delete-a-repository-webhook
//
//meta:operation DELETE /repos/{owner}/{repo}/hooks/{hook_id}
func ( *RepositoriesService) ( context.Context, ,  string,  int64) (*Response, error) {
	 := fmt.Sprintf("repos/%v/%v/hooks/%d", , , )
	,  := .client.NewRequest("DELETE", , nil)
	if  != nil {
		return nil, 
	}
	return .client.Do(, , nil)
}

// PingHook triggers a 'ping' event to be sent to the Hook.
//
// GitHub API docs: https://docs.github.com/rest/repos/webhooks#ping-a-repository-webhook
//
//meta:operation POST /repos/{owner}/{repo}/hooks/{hook_id}/pings
func ( *RepositoriesService) ( context.Context, ,  string,  int64) (*Response, error) {
	 := fmt.Sprintf("repos/%v/%v/hooks/%d/pings", , , )
	,  := .client.NewRequest("POST", , nil)
	if  != nil {
		return nil, 
	}
	return .client.Do(, , nil)
}

// TestHook triggers a test Hook by github.
//
// GitHub API docs: https://docs.github.com/rest/repos/webhooks#test-the-push-repository-webhook
//
//meta:operation POST /repos/{owner}/{repo}/hooks/{hook_id}/tests
func ( *RepositoriesService) ( context.Context, ,  string,  int64) (*Response, error) {
	 := fmt.Sprintf("repos/%v/%v/hooks/%d/tests", , , )
	,  := .client.NewRequest("POST", , nil)
	if  != nil {
		return nil, 
	}
	return .client.Do(, , nil)
}

// Subscribe lets servers register to receive updates when a topic is updated.
//
// GitHub API docs: https://docs.github.com/webhooks/about-webhooks-for-repositories#pubsubhubbub
//
//meta:operation POST /hub
func ( *RepositoriesService) ( context.Context, , , ,  string,  []byte) (*Response, error) {
	,  := .createWebSubRequest("subscribe", , , , , )
	if  != nil {
		return nil, 
	}

	return .client.Do(, , nil)
}

// Unsubscribe lets servers unregister to no longer receive updates when a topic is updated.
//
// GitHub API docs: https://docs.github.com/webhooks/about-webhooks-for-repositories#pubsubhubbub
//
//meta:operation POST /hub
func ( *RepositoriesService) ( context.Context, , , ,  string,  []byte) (*Response, error) {
	,  := .createWebSubRequest("unsubscribe", , , , , )
	if  != nil {
		return nil, 
	}

	return .client.Do(, , nil)
}

// createWebSubRequest returns a subscribe/unsubscribe request that implements
// the WebSub (formerly PubSubHubbub) protocol.
//
// See: https://www.w3.org/TR/websub/#subscriber-sends-subscription-request
func ( *RepositoriesService) (, , , ,  string,  []byte) (*http.Request, error) {
	 := fmt.Sprintf(
		"https://github.com/%s/%s/events/%s",
		,
		,
		,
	)
	 := url.Values{}
	.Add("hub.mode", )
	.Add("hub.topic", )
	.Add("hub.callback", )
	if  != nil {
		.Add("hub.secret", string())
	}
	 := strings.NewReader(.Encode())

	,  := .client.NewFormRequest("hub", )
	if  != nil {
		return nil, 
	}

	return , nil
}