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

// SignatureVerification represents GPG signature verification.
type SignatureVerification struct {
	Verified  *bool   `json:"verified,omitempty"`
	Reason    *string `json:"reason,omitempty"`
	Signature *string `json:"signature,omitempty"`
	Payload   *string `json:"payload,omitempty"`
}

// MessageSigner is used by GitService.CreateCommit to sign a commit.
//
// To create a MessageSigner that signs a commit with a [golang.org/x/crypto/openpgp.Entity],
// or [github.com/ProtonMail/go-crypto/openpgp.Entity], use:
//
//	commit.Signer = github.MessageSignerFunc(func(w io.Writer, r io.Reader) error {
//		return openpgp.ArmoredDetachSign(w, openpgpEntity, r, nil)
//	})
type MessageSigner interface {
	Sign(w io.Writer, r io.Reader) error
}

// MessageSignerFunc is a single function implementation of MessageSigner.
type MessageSignerFunc func(w io.Writer, r io.Reader) error

func ( MessageSignerFunc) ( io.Writer,  io.Reader) error {
	return (, )
}

// Commit represents a GitHub commit.
type Commit struct {
	SHA          *string                `json:"sha,omitempty"`
	Author       *CommitAuthor          `json:"author,omitempty"`
	Committer    *CommitAuthor          `json:"committer,omitempty"`
	Message      *string                `json:"message,omitempty"`
	Tree         *Tree                  `json:"tree,omitempty"`
	Parents      []*Commit              `json:"parents,omitempty"`
	Stats        *CommitStats           `json:"stats,omitempty"`
	HTMLURL      *string                `json:"html_url,omitempty"`
	URL          *string                `json:"url,omitempty"`
	Verification *SignatureVerification `json:"verification,omitempty"`
	NodeID       *string                `json:"node_id,omitempty"`

	// CommentCount is the number of GitHub comments on the commit. This
	// is only populated for requests that fetch GitHub data like
	// Pulls.ListCommits, Repositories.ListCommits, etc.
	CommentCount *int `json:"comment_count,omitempty"`
}

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

// CommitAuthor represents the author or committer of a commit. The commit
// author may not correspond to a GitHub User.
type CommitAuthor struct {
	Date  *Timestamp `json:"date,omitempty"`
	Name  *string    `json:"name,omitempty"`
	Email *string    `json:"email,omitempty"`

	// The following fields are only populated by Webhook events.
	Login *string `json:"username,omitempty"` // Renamed for go-github consistency.
}

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

// GetCommit fetches the Commit object for a given SHA.
//
// GitHub API docs: https://docs.github.com/rest/git/commits#get-a-commit-object
//
//meta:operation GET /repos/{owner}/{repo}/git/commits/{commit_sha}
func ( *GitService) ( context.Context,  string,  string,  string) (*Commit, *Response, error) {
	 := fmt.Sprintf("repos/%v/%v/git/commits/%v", , , )
	,  := .client.NewRequest("GET", , nil)
	if  != nil {
		return nil, nil, 
	}

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

	return , , nil
}

// createCommit represents the body of a CreateCommit request.
type createCommit struct {
	Author    *CommitAuthor `json:"author,omitempty"`
	Committer *CommitAuthor `json:"committer,omitempty"`
	Message   *string       `json:"message,omitempty"`
	Tree      *string       `json:"tree,omitempty"`
	Parents   []string      `json:"parents,omitempty"`
	Signature *string       `json:"signature,omitempty"`
}

type CreateCommitOptions struct {
	// CreateCommit will sign the commit with this signer. See MessageSigner doc for more details.
	// Ignored on commits where Verification.Signature is defined.
	Signer MessageSigner
}

// CreateCommit creates a new commit in a repository.
// commit must not be nil.
//
// The commit.Committer is optional and will be filled with the commit.Author
// data if omitted. If the commit.Author is omitted, it will be filled in with
// the authenticated user’s information and the current date.
//
// GitHub API docs: https://docs.github.com/rest/git/commits#create-a-commit
//
//meta:operation POST /repos/{owner}/{repo}/git/commits
func ( *GitService) ( context.Context,  string,  string,  *Commit,  *CreateCommitOptions) (*Commit, *Response, error) {
	if  == nil {
		return nil, nil, fmt.Errorf("commit must be provided")
	}
	if  == nil {
		 = &CreateCommitOptions{}
	}

	 := fmt.Sprintf("repos/%v/%v/git/commits", , )

	 := make([]string, len(.Parents))
	for ,  := range .Parents {
		[] = *.SHA
	}

	 := &createCommit{
		Author:    .Author,
		Committer: .Committer,
		Message:   .Message,
		Parents:   ,
	}
	if .Tree != nil {
		.Tree = .Tree.SHA
	}
	switch {
	case .Verification != nil:
		.Signature = .Verification.Signature
	case .Signer != nil:
		,  := createSignature(.Signer, )
		if  != nil {
			return nil, nil, 
		}
		.Signature = &
	}

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

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

	return , , nil
}

func createSignature( MessageSigner,  *createCommit) (string, error) {
	if  == nil {
		return "", errors.New("createSignature: invalid parameters")
	}

	,  := createSignatureMessage()
	if  != nil {
		return "", 
	}

	var  bytes.Buffer
	 = .Sign(&, strings.NewReader())
	if  != nil {
		return "", 
	}

	return .String(), nil
}

func createSignatureMessage( *createCommit) (string, error) {
	if  == nil || .Message == nil || *.Message == "" || .Author == nil {
		return "", errors.New("createSignatureMessage: invalid parameters")
	}

	var  []string

	if .Tree != nil {
		 = append(, fmt.Sprintf("tree %s", *.Tree))
	}

	for ,  := range .Parents {
		 = append(, fmt.Sprintf("parent %s", ))
	}

	 = append(, fmt.Sprintf("author %s <%s> %d %s", .Author.GetName(), .Author.GetEmail(), .Author.GetDate().Unix(), .Author.GetDate().Format("-0700")))

	 := .Committer
	if  == nil {
		 = .Author
	}

	// There needs to be a double newline after committer
	 = append(, fmt.Sprintf("committer %s <%s> %d %s\n", .GetName(), .GetEmail(), .GetDate().Unix(), .GetDate().Format("-0700")))
	 = append(, *.Message)

	return strings.Join(, "\n"), nil
}