Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions example/commitpr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func getRef() (ref *github.Reference, err error) {
if baseRef, _, err = client.Git.GetRef(ctx, *sourceOwner, *sourceRepo, branchRef(*baseBranch)); err != nil {
return nil, err
}
newRef := &github.Reference{Ref: github.Ptr(branchRef(*commitBranch)), Object: &github.GitObject{SHA: baseRef.Object.SHA}}
newRef := github.CreateRef{Ref: branchRef(*commitBranch), SHA: *baseRef.Object.SHA}
ref, _, err = client.Git.CreateRef(ctx, *sourceOwner, *sourceRepo, newRef)
return ref, err
}
Expand Down Expand Up @@ -143,7 +143,7 @@ func pushCommit(ref *github.Reference, tree *github.Tree) (err error) {
// Create the commit using the tree.
date := time.Now()
author := &github.CommitAuthor{Date: &github.Timestamp{Time: date}, Name: authorName, Email: authorEmail}
commit := &github.Commit{Author: author, Message: commitMessage, Tree: tree, Parents: []*github.Commit{parent.Commit}}
commit := github.Commit{Author: author, Message: commitMessage, Tree: tree, Parents: []*github.Commit{parent.Commit}}
opts := github.CreateCommitOptions{}
if *privateKey != "" {
armoredBlock, e := os.ReadFile(*privateKey)
Expand All @@ -169,7 +169,10 @@ func pushCommit(ref *github.Reference, tree *github.Tree) (err error) {

// Attach the commit to the master branch.
ref.Object.SHA = newCommit.SHA
_, _, err = client.Git.UpdateRef(ctx, *sourceOwner, *sourceRepo, ref, false)
_, _, err = client.Git.UpdateRef(ctx, *sourceOwner, *sourceRepo, *ref.Ref, github.UpdateRef{
SHA: *newCommit.SHA,
Force: github.Ptr(false),
})
return err
}

Expand Down
2 changes: 1 addition & 1 deletion github/git_blobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (s *GitService) GetBlobRaw(ctx context.Context, owner, repo, sha string) ([
// GitHub API docs: https://docs.github.com/rest/git/blobs#create-a-blob
//
//meta:operation POST /repos/{owner}/{repo}/git/blobs
func (s *GitService) CreateBlob(ctx context.Context, owner, repo string, blob *Blob) (*Blob, *Response, error) {
func (s *GitService) CreateBlob(ctx context.Context, owner, repo string, blob Blob) (*Blob, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/git/blobs", owner, repo)
req, err := s.client.NewRequest("POST", u, blob)
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions github/git_blobs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func TestGitService_CreateBlob(t *testing.T) {
t.Parallel()
client, mux, _ := setup(t)

input := &Blob{
input := Blob{
SHA: Ptr("s"),
Content: Ptr("blob content"),
Encoding: Ptr("utf-8"),
Expand All @@ -123,8 +123,8 @@ func TestGitService_CreateBlob(t *testing.T) {
testMethod(t, r, "POST")

want := input
if !cmp.Equal(v, want) {
t.Errorf("Git.CreateBlob request body: %+v, want %+v", v, want)
if !cmp.Equal(*v, want) {
t.Errorf("Git.CreateBlob request body: %+v, want %+v", *v, want)
}

fmt.Fprint(w, `{
Expand All @@ -143,8 +143,8 @@ func TestGitService_CreateBlob(t *testing.T) {

want := input

if !cmp.Equal(*blob, *want) {
t.Errorf("Git.CreateBlob returned %+v, want %+v", *blob, *want)
if !cmp.Equal(*blob, want) {
t.Errorf("Git.CreateBlob returned %+v, want %+v", *blob, want)
}

const methodName = "CreateBlob"
Expand All @@ -167,7 +167,7 @@ func TestGitService_CreateBlob_invalidOwner(t *testing.T) {
client, _, _ := setup(t)

ctx := context.Background()
_, _, err := client.Git.CreateBlob(ctx, "%", "%", &Blob{})
_, _, err := client.Git.CreateBlob(ctx, "%", "%", Blob{})
testURLParseError(t, err)
}

Expand Down
5 changes: 1 addition & 4 deletions github/git_commits.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,7 @@ type CreateCommitOptions struct {
// GitHub API docs: https://docs.github.com/rest/git/commits#create-a-commit
//
//meta:operation POST /repos/{owner}/{repo}/git/commits
func (s *GitService) CreateCommit(ctx context.Context, owner, repo string, commit *Commit, opts *CreateCommitOptions) (*Commit, *Response, error) {
if commit == nil {
return nil, nil, errors.New("commit must be provided")
}
func (s *GitService) CreateCommit(ctx context.Context, owner, repo string, commit Commit, opts *CreateCommitOptions) (*Commit, *Response, error) {
if opts == nil {
opts = &CreateCommitOptions{}
}
Expand Down
21 changes: 5 additions & 16 deletions github/git_commits_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func TestGitService_CreateCommit(t *testing.T) {
t.Parallel()
client, mux, _ := setup(t)

input := &Commit{
input := Commit{
Message: Ptr("Commit Message."),
Tree: &Tree{SHA: Ptr("t")},
Parents: []*Commit{{SHA: Ptr("p")}},
Expand Down Expand Up @@ -231,7 +231,7 @@ func TestGitService_CreateSignedCommit(t *testing.T) {

signature := "----- BEGIN PGP SIGNATURE -----\n\naaaa\naaaa\n----- END PGP SIGNATURE -----"

input := &Commit{
input := Commit{
Message: Ptr("Commit Message."),
Tree: &Tree{SHA: Ptr("t")},
Parents: []*Commit{{SHA: Ptr("p")}},
Expand Down Expand Up @@ -288,7 +288,7 @@ func TestGitService_CreateSignedCommitWithInvalidParams(t *testing.T) {
t.Parallel()
client, _, _ := setup(t)

input := &Commit{}
input := Commit{}

ctx := context.Background()
opts := CreateCommitOptions{Signer: uncalledSigner(t)}
Expand All @@ -298,17 +298,6 @@ func TestGitService_CreateSignedCommitWithInvalidParams(t *testing.T) {
}
}

func TestGitService_CreateCommitWithNilCommit(t *testing.T) {
t.Parallel()
client, _, _ := setup(t)

ctx := context.Background()
_, _, err := client.Git.CreateCommit(ctx, "o", "r", nil, nil)
if err == nil {
t.Error("Expected error to be returned because commit=nil")
}
}

func TestGitService_CreateCommit_WithSigner(t *testing.T) {
t.Parallel()
client, mux, _ := setup(t)
Expand All @@ -327,7 +316,7 @@ committer go-github <[email protected]> 1493849023 +0200

Commit Message.`
sha := "commitSha"
input := &Commit{
input := Commit{
SHA: &sha,
Message: Ptr("Commit Message."),
Tree: &Tree{SHA: Ptr("t")},
Expand Down Expand Up @@ -513,7 +502,7 @@ func TestGitService_CreateCommit_invalidOwner(t *testing.T) {
client, _, _ := setup(t)

ctx := context.Background()
_, _, err := client.Git.CreateCommit(ctx, "%", "%", &Commit{}, nil)
_, _, err := client.Git.CreateCommit(ctx, "%", "%", Commit{}, nil)
testURLParseError(t, err)
}

Expand Down
54 changes: 26 additions & 28 deletions github/git_refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ func (o GitObject) String() string {
return Stringify(o)
}

// createRefRequest represents the payload for creating a reference.
type createRefRequest struct {
Ref *string `json:"ref"`
SHA *string `json:"sha"`
// CreateRef represents the payload for creating a reference.
type CreateRef struct {
Ref string `json:"ref"`
SHA string `json:"sha"`
}

// updateRefRequest represents the payload for updating a reference.
type updateRefRequest struct {
SHA *string `json:"sha"`
Force *bool `json:"force"`
// UpdateRef represents the payload for updating a reference.
type UpdateRef struct {
SHA string `json:"sha"`
Force *bool `json:"force,omitempty"`
}

// GetRef fetches a single reference in a repository.
Expand Down Expand Up @@ -127,20 +127,20 @@ func (s *GitService) ListMatchingRefs(ctx context.Context, owner, repo string, o
// GitHub API docs: https://docs.github.com/rest/git/refs#create-a-reference
//
//meta:operation POST /repos/{owner}/{repo}/git/refs
func (s *GitService) CreateRef(ctx context.Context, owner, repo string, ref *Reference) (*Reference, *Response, error) {
if ref == nil {
return nil, nil, errors.New("reference must be provided")
}
if ref.Ref == nil {
func (s *GitService) CreateRef(ctx context.Context, owner, repo string, ref CreateRef) (*Reference, *Response, error) {
if ref.Ref == "" {
return nil, nil, errors.New("ref must be provided")
}

if ref.SHA == "" {
return nil, nil, errors.New("sha must be provided")
}

// ensure the 'refs/' prefix is present
ref.Ref = "refs/" + strings.TrimPrefix(ref.Ref, "refs/")

u := fmt.Sprintf("repos/%v/%v/git/refs", owner, repo)
req, err := s.client.NewRequest("POST", u, &createRefRequest{
// back-compat with previous behavior that didn't require 'refs/' prefix
Ref: Ptr("refs/" + strings.TrimPrefix(*ref.Ref, "refs/")),
SHA: ref.Object.SHA,
})
req, err := s.client.NewRequest("POST", u, ref)
if err != nil {
return nil, nil, err
}
Expand All @@ -159,20 +159,18 @@ func (s *GitService) CreateRef(ctx context.Context, owner, repo string, ref *Ref
// GitHub API docs: https://docs.github.com/rest/git/refs#update-a-reference
//
//meta:operation PATCH /repos/{owner}/{repo}/git/refs/{ref}
func (s *GitService) UpdateRef(ctx context.Context, owner, repo string, ref *Reference, force bool) (*Reference, *Response, error) {
if ref == nil {
return nil, nil, errors.New("reference must be provided")
}
if ref.Ref == nil {
func (s *GitService) UpdateRef(ctx context.Context, owner, repo, ref string, updateRef UpdateRef) (*Reference, *Response, error) {
if ref == "" {
return nil, nil, errors.New("ref must be provided")
}

refPath := strings.TrimPrefix(*ref.Ref, "refs/")
if updateRef.SHA == "" {
return nil, nil, errors.New("sha must be provided")
}

refPath := strings.TrimPrefix(ref, "refs/")
u := fmt.Sprintf("repos/%v/%v/git/refs/%v", owner, repo, refURLEscape(refPath))
req, err := s.client.NewRequest("PATCH", u, &updateRefRequest{
SHA: ref.Object.SHA,
Force: &force,
})
req, err := s.client.NewRequest("PATCH", u, updateRef)
if err != nil {
return nil, nil, err
}
Expand Down
Loading
Loading