From 92a65bbdfa02ed5dbaa8c220690a189e8858bffd Mon Sep 17 00:00:00 2001 From: rupokghosh Date: Sun, 27 Jul 2025 17:25:15 -0400 Subject: [PATCH] refactor: convert required pointer params to values update exmaples change to using values refactor(git)!: improve CreateRef API with value types and exported struct format new updateref struct remove backwards compatibility create tag in git tags slfhg --- example/commitpr/main.go | 9 ++- github/git_blobs.go | 2 +- github/git_blobs_test.go | 12 +-- github/git_commits.go | 5 +- github/git_commits_test.go | 21 ++--- github/git_refs.go | 54 +++++++------ github/git_refs_test.go | 132 +++++++++++++++----------------- github/git_tags.go | 34 ++------ github/git_tags_test.go | 35 ++++----- github/github-accessors.go | 16 ++++ github/github-accessors_test.go | 19 +++++ 11 files changed, 164 insertions(+), 175 deletions(-) diff --git a/example/commitpr/main.go b/example/commitpr/main.go index bc402dac750..c03b7fea30f 100644 --- a/example/commitpr/main.go +++ b/example/commitpr/main.go @@ -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 } @@ -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) @@ -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 } diff --git a/github/git_blobs.go b/github/git_blobs.go index a1a84084e9d..c734d595b30 100644 --- a/github/git_blobs.go +++ b/github/git_blobs.go @@ -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 { diff --git a/github/git_blobs_test.go b/github/git_blobs_test.go index f8f35b2ceba..2cf32022aa0 100644 --- a/github/git_blobs_test.go +++ b/github/git_blobs_test.go @@ -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"), @@ -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, `{ @@ -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" @@ -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) } diff --git a/github/git_commits.go b/github/git_commits.go index aa7620caa67..e4fcc94ff10 100644 --- a/github/git_commits.go +++ b/github/git_commits.go @@ -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{} } diff --git a/github/git_commits_test.go b/github/git_commits_test.go index 9e94624c2a0..15124f939e2 100644 --- a/github/git_commits_test.go +++ b/github/git_commits_test.go @@ -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")}}, @@ -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")}}, @@ -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)} @@ -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) @@ -327,7 +316,7 @@ committer go-github 1493849023 +0200 Commit Message.` sha := "commitSha" - input := &Commit{ + input := Commit{ SHA: &sha, Message: Ptr("Commit Message."), Tree: &Tree{SHA: Ptr("t")}, @@ -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) } diff --git a/github/git_refs.go b/github/git_refs.go index ad45d1f3e4c..735aef006bc 100644 --- a/github/git_refs.go +++ b/github/git_refs.go @@ -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. @@ -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 } @@ -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 } diff --git a/github/git_refs_test.go b/github/git_refs_test.go index b105680e897..f3d1a5f6307 100644 --- a/github/git_refs_test.go +++ b/github/git_refs_test.go @@ -386,18 +386,18 @@ func TestGitService_CreateRef(t *testing.T) { t.Parallel() client, mux, _ := setup(t) - args := &createRefRequest{ - Ref: Ptr("refs/heads/b"), - SHA: Ptr("aa218f56b14c9653891f9e74264a383fa43fefbd"), + args := CreateRef{ + Ref: "refs/heads/b", + SHA: "aa218f56b14c9653891f9e74264a383fa43fefbd", } mux.HandleFunc("/repos/o/r/git/refs", func(w http.ResponseWriter, r *http.Request) { - v := new(createRefRequest) + v := new(CreateRef) assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") - if !cmp.Equal(v, args) { - t.Errorf("Request body = %+v, want %+v", v, args) + if !cmp.Equal(*v, args) { + t.Errorf("Request body = %+v, want %+v", *v, args) } fmt.Fprint(w, ` { @@ -412,11 +412,9 @@ func TestGitService_CreateRef(t *testing.T) { }) ctx := context.Background() - ref, _, err := client.Git.CreateRef(ctx, "o", "r", &Reference{ - Ref: Ptr("refs/heads/b"), - Object: &GitObject{ - SHA: Ptr("aa218f56b14c9653891f9e74264a383fa43fefbd"), - }, + ref, _, err := client.Git.CreateRef(ctx, "o", "r", CreateRef{ + Ref: "refs/heads/b", + SHA: "aa218f56b14c9653891f9e74264a383fa43fefbd", }) if err != nil { t.Errorf("Git.CreateRef returned error: %v", err) @@ -436,11 +434,9 @@ func TestGitService_CreateRef(t *testing.T) { } // without 'refs/' prefix - _, _, err = client.Git.CreateRef(ctx, "o", "r", &Reference{ - Ref: Ptr("heads/b"), - Object: &GitObject{ - SHA: Ptr("aa218f56b14c9653891f9e74264a383fa43fefbd"), - }, + _, _, err = client.Git.CreateRef(ctx, "o", "r", CreateRef{ + Ref: "heads/b", + SHA: "aa218f56b14c9653891f9e74264a383fa43fefbd", }) if err != nil { t.Errorf("Git.CreateRef returned error: %v", err) @@ -448,29 +444,21 @@ func TestGitService_CreateRef(t *testing.T) { const methodName = "CreateRef" testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Git.CreateRef(ctx, "o", "r", nil) + _, _, err = client.Git.CreateRef(ctx, "o", "r", CreateRef{Ref: ""}) return err }) testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Git.CreateRef(ctx, "o", "r", &Reference{Ref: nil}) - return err - }) - testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Git.CreateRef(ctx, "\n", "\n", &Reference{ - Ref: Ptr("refs/heads/b"), - Object: &GitObject{ - SHA: Ptr("aa218f56b14c9653891f9e74264a383fa43fefbd"), - }, + _, _, err = client.Git.CreateRef(ctx, "\n", "\n", CreateRef{ + Ref: "refs/heads/b", + SHA: "aa218f56b14c9653891f9e74264a383fa43fefbd", }) return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Git.CreateRef(ctx, "o", "r", &Reference{ - Ref: Ptr("refs/heads/b"), - Object: &GitObject{ - SHA: Ptr("aa218f56b14c9653891f9e74264a383fa43fefbd"), - }, + got, resp, err := client.Git.CreateRef(ctx, "o", "r", CreateRef{ + Ref: "refs/heads/b", + SHA: "aa218f56b14c9653891f9e74264a383fa43fefbd", }) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) @@ -483,18 +471,18 @@ func TestGitService_UpdateRef(t *testing.T) { t.Parallel() client, mux, _ := setup(t) - args := &updateRefRequest{ - SHA: Ptr("aa218f56b14c9653891f9e74264a383fa43fefbd"), + args := UpdateRef{ + SHA: "aa218f56b14c9653891f9e74264a383fa43fefbd", Force: Ptr(true), } mux.HandleFunc("/repos/o/r/git/refs/heads/b", func(w http.ResponseWriter, r *http.Request) { - v := new(updateRefRequest) + v := new(UpdateRef) assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") - if !cmp.Equal(v, args) { - t.Errorf("Request body = %+v, want %+v", v, args) + if !cmp.Equal(*v, args) { + t.Errorf("Request body = %+v, want %+v", *v, args) } fmt.Fprint(w, ` { @@ -509,10 +497,10 @@ func TestGitService_UpdateRef(t *testing.T) { }) ctx := context.Background() - ref, _, err := client.Git.UpdateRef(ctx, "o", "r", &Reference{ - Ref: Ptr("refs/heads/b"), - Object: &GitObject{SHA: Ptr("aa218f56b14c9653891f9e74264a383fa43fefbd")}, - }, true) + ref, _, err := client.Git.UpdateRef(ctx, "o", "r", "refs/heads/b", UpdateRef{ + SHA: "aa218f56b14c9653891f9e74264a383fa43fefbd", + Force: Ptr(true), + }) if err != nil { t.Errorf("Git.UpdateRef returned error: %v", err) } @@ -531,36 +519,36 @@ func TestGitService_UpdateRef(t *testing.T) { } // without 'refs/' prefix - _, _, err = client.Git.UpdateRef(ctx, "o", "r", &Reference{ - Ref: Ptr("heads/b"), - Object: &GitObject{SHA: Ptr("aa218f56b14c9653891f9e74264a383fa43fefbd")}, - }, true) + _, _, err = client.Git.UpdateRef(ctx, "o", "r", "heads/b", UpdateRef{ + SHA: "aa218f56b14c9653891f9e74264a383fa43fefbd", + Force: Ptr(true), + }) if err != nil { t.Errorf("Git.UpdateRef returned error: %v", err) } const methodName = "UpdateRef" testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Git.UpdateRef(ctx, "o", "r", nil, true) + _, _, err = client.Git.UpdateRef(ctx, "o", "r", "", UpdateRef{SHA: "aa218f56b14c9653891f9e74264a383fa43fefbd"}) return err }) testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Git.UpdateRef(ctx, "o", "r", &Reference{Ref: nil}, true) + _, _, err = client.Git.UpdateRef(ctx, "o", "r", "refs/heads/b", UpdateRef{SHA: ""}) return err }) testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Git.UpdateRef(ctx, "\n", "\n", &Reference{ - Ref: Ptr("refs/heads/b"), - Object: &GitObject{SHA: Ptr("aa218f56b14c9653891f9e74264a383fa43fefbd")}, - }, true) + _, _, err = client.Git.UpdateRef(ctx, "\n", "\n", "refs/heads/b", UpdateRef{ + SHA: "aa218f56b14c9653891f9e74264a383fa43fefbd", + Force: Ptr(true), + }) return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Git.UpdateRef(ctx, "o", "r", &Reference{ - Ref: Ptr("refs/heads/b"), - Object: &GitObject{SHA: Ptr("aa218f56b14c9653891f9e74264a383fa43fefbd")}, - }, true) + got, resp, err := client.Git.UpdateRef(ctx, "o", "r", "refs/heads/b", UpdateRef{ + SHA: "aa218f56b14c9653891f9e74264a383fa43fefbd", + Force: Ptr(true), + }) if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } @@ -644,18 +632,18 @@ func TestGitService_UpdateRef_pathEscape(t *testing.T) { t.Parallel() client, mux, _ := setup(t) - args := &updateRefRequest{ - SHA: Ptr("aa218f56b14c9653891f9e74264a383fa43fefbd"), + args := UpdateRef{ + SHA: "aa218f56b14c9653891f9e74264a383fa43fefbd", Force: Ptr(true), } mux.HandleFunc("/repos/o/r/git/refs/heads/b#1", func(w http.ResponseWriter, r *http.Request) { - v := new(updateRefRequest) + v := new(UpdateRef) assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "PATCH") - if !cmp.Equal(v, args) { - t.Errorf("Request body = %+v, want %+v", v, args) + if !cmp.Equal(*v, args) { + t.Errorf("Request body = %+v, want %+v", *v, args) } fmt.Fprint(w, ` { @@ -670,10 +658,10 @@ func TestGitService_UpdateRef_pathEscape(t *testing.T) { }) ctx := context.Background() - ref, _, err := client.Git.UpdateRef(ctx, "o", "r", &Reference{ - Ref: Ptr("refs/heads/b#1"), - Object: &GitObject{SHA: Ptr("aa218f56b14c9653891f9e74264a383fa43fefbd")}, - }, true) + ref, _, err := client.Git.UpdateRef(ctx, "o", "r", "refs/heads/b#1", UpdateRef{ + SHA: "aa218f56b14c9653891f9e74264a383fa43fefbd", + Force: Ptr(true), + }) if err != nil { t.Errorf("Git.UpdateRef returned error: %v", err) } @@ -740,13 +728,13 @@ func TestGitObject_Marshal(t *testing.T) { testJSONMarshal(t, u, want) } -func TestCreateRefRequest_Marshal(t *testing.T) { +func TestCreateRef_Marshal(t *testing.T) { t.Parallel() - testJSONMarshal(t, &createRefRequest{}, "{}") + testJSONMarshal(t, CreateRef{}, `{"ref":"","sha":""}`) - u := &createRefRequest{ - Ref: Ptr("ref"), - SHA: Ptr("sha"), + u := CreateRef{ + Ref: "ref", + SHA: "sha", } want := `{ @@ -757,12 +745,12 @@ func TestCreateRefRequest_Marshal(t *testing.T) { testJSONMarshal(t, u, want) } -func TestUpdateRefRequest_Marshal(t *testing.T) { +func TestUpdateRef_Marshal(t *testing.T) { t.Parallel() - testJSONMarshal(t, &updateRefRequest{}, "{}") + testJSONMarshal(t, UpdateRef{}, `{"sha":""}`) - u := &updateRefRequest{ - SHA: Ptr("sha"), + u := UpdateRef{ + SHA: "sha", Force: Ptr(true), } diff --git a/github/git_tags.go b/github/git_tags.go index 3aea2afdcec..750b03760c0 100644 --- a/github/git_tags.go +++ b/github/git_tags.go @@ -7,7 +7,6 @@ package github import ( "context" - "errors" "fmt" ) @@ -23,14 +22,12 @@ type Tag struct { NodeID *string `json:"node_id,omitempty"` } -// createTagRequest represents the body of a CreateTag request. This is mostly -// identical to Tag with the exception that the object SHA and Type are -// top-level fields, rather than being nested inside a JSON object. -type createTagRequest struct { - Tag *string `json:"tag,omitempty"` - Message *string `json:"message,omitempty"` - Object *string `json:"object,omitempty"` - Type *string `json:"type,omitempty"` +// CreateTag represents the payload for creating a tag. +type CreateTag struct { + Tag string `json:"tag,omitempty"` + Message string `json:"message,omitempty"` + Object string `json:"object,omitempty"` + Type string `json:"type,omitempty"` Tagger *CommitAuthor `json:"tagger,omitempty"` } @@ -60,25 +57,10 @@ func (s *GitService) GetTag(ctx context.Context, owner, repo, sha string) (*Tag, // GitHub API docs: https://docs.github.com/rest/git/tags#create-a-tag-object // //meta:operation POST /repos/{owner}/{repo}/git/tags -func (s *GitService) CreateTag(ctx context.Context, owner, repo string, tag *Tag) (*Tag, *Response, error) { - if tag == nil { - return nil, nil, errors.New("tag must be provided") - } - +func (s *GitService) CreateTag(ctx context.Context, owner, repo string, tag CreateTag) (*Tag, *Response, error) { u := fmt.Sprintf("repos/%v/%v/git/tags", owner, repo) - // convert Tag into a createTagRequest - tagRequest := &createTagRequest{ - Tag: tag.Tag, - Message: tag.Message, - Tagger: tag.Tagger, - } - if tag.Object != nil { - tagRequest.Object = tag.Object.SHA - tagRequest.Type = tag.Object.Type - } - - req, err := s.client.NewRequest("POST", u, tagRequest) + req, err := s.client.NewRequest("POST", u, tag) if err != nil { return nil, nil, err } diff --git a/github/git_tags_test.go b/github/git_tags_test.go index 90310ddcf14..8a1e55440ef 100644 --- a/github/git_tags_test.go +++ b/github/git_tags_test.go @@ -54,25 +54,26 @@ func TestGitService_CreateTag(t *testing.T) { t.Parallel() client, mux, _ := setup(t) - input := &createTagRequest{Tag: Ptr("t"), Object: Ptr("s")} + inputTag := CreateTag{ + Tag: "t", + Object: "s", + Type: "commit", + Message: "test message", + } mux.HandleFunc("/repos/o/r/git/tags", func(w http.ResponseWriter, r *http.Request) { - v := new(createTagRequest) + v := new(CreateTag) assertNilError(t, json.NewDecoder(r.Body).Decode(v)) testMethod(t, r, "POST") - if !cmp.Equal(v, input) { - t.Errorf("Request body = %+v, want %+v", v, input) + if !cmp.Equal(*v, inputTag) { + t.Errorf("Request body = %+v, want %+v", *v, inputTag) } fmt.Fprint(w, `{"tag": "t"}`) }) ctx := context.Background() - inputTag := &Tag{ - Tag: input.Tag, - Object: &GitObject{SHA: input.Object}, - } tag, _, err := client.Git.CreateTag(ctx, "o", "r", inputTag) if err != nil { t.Errorf("Git.CreateTag returned error: %v", err) @@ -84,10 +85,6 @@ func TestGitService_CreateTag(t *testing.T) { } const methodName = "CreateTag" - testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Git.CreateTag(ctx, "o", "r", nil) - return err - }) testBadOptions(t, methodName, func() (err error) { _, _, err = client.Git.CreateTag(ctx, "\n", "\n", inputTag) return err @@ -159,15 +156,15 @@ func TestTag_Marshal(t *testing.T) { testJSONMarshal(t, u, want) } -func TestCreateTagRequest_Marshal(t *testing.T) { +func TestCreateTag_Marshal(t *testing.T) { t.Parallel() - testJSONMarshal(t, &createTagRequest{}, "{}") + testJSONMarshal(t, CreateTag{}, "{}") - u := &createTagRequest{ - Tag: Ptr("tag"), - Message: Ptr("msg"), - Object: Ptr("obj"), - Type: Ptr("type"), + u := CreateTag{ + Tag: "tag", + Message: "msg", + Object: "obj", + Type: "type", Tagger: &CommitAuthor{ Date: &Timestamp{referenceTime}, Name: Ptr("name"), diff --git a/github/github-accessors.go b/github/github-accessors.go index 34b9ce166d5..02035759a79 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -6030,6 +6030,14 @@ func (c *CreateRunnerGroupRequest) GetVisibility() string { return *c.Visibility } +// GetTagger returns the Tagger field. +func (c *CreateTag) GetTagger() *CommitAuthor { + if c == nil { + return nil + } + return c.Tagger +} + // GetCanAdminsBypass returns the CanAdminsBypass field if it's non-nil, zero value otherwise. func (c *CreateUpdateEnvironment) GetCanAdminsBypass() bool { if c == nil || c.CanAdminsBypass == nil { @@ -27926,6 +27934,14 @@ func (u *UpdateEnterpriseRunnerGroupRequest) GetVisibility() string { return *u.Visibility } +// GetForce returns the Force field if it's non-nil, zero value otherwise. +func (u *UpdateRef) GetForce() bool { + if u == nil || u.Force == nil { + return false + } + return *u.Force +} + // GetAllowsPublicRepositories returns the AllowsPublicRepositories field if it's non-nil, zero value otherwise. func (u *UpdateRunnerGroupRequest) GetAllowsPublicRepositories() bool { if u == nil || u.AllowsPublicRepositories == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index b4db59226e9..3fd50944499 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -7864,6 +7864,14 @@ func TestCreateRunnerGroupRequest_GetVisibility(tt *testing.T) { c.GetVisibility() } +func TestCreateTag_GetTagger(tt *testing.T) { + tt.Parallel() + c := &CreateTag{} + c.GetTagger() + c = nil + c.GetTagger() +} + func TestCreateUpdateEnvironment_GetCanAdminsBypass(tt *testing.T) { tt.Parallel() var zeroValue bool @@ -35928,6 +35936,17 @@ func TestUpdateEnterpriseRunnerGroupRequest_GetVisibility(tt *testing.T) { u.GetVisibility() } +func TestUpdateRef_GetForce(tt *testing.T) { + tt.Parallel() + var zeroValue bool + u := &UpdateRef{Force: &zeroValue} + u.GetForce() + u = &UpdateRef{} + u.GetForce() + u = nil + u.GetForce() +} + func TestUpdateRunnerGroupRequest_GetAllowsPublicRepositories(tt *testing.T) { tt.Parallel() var zeroValue bool