diff --git a/commits.go b/commits.go index d4efcec..499d745 100644 --- a/commits.go +++ b/commits.go @@ -53,6 +53,12 @@ func (s *CommitService) Get(ctx context.Context, params Params) (*types.Commit, return &out, nil } +// GetDiffOrPatch returns a commit diff or patch as raw bytes. +func (s *CommitService) GetDiffOrPatch(ctx context.Context, owner, repo, sha, diffType string) ([]byte, error) { + path := ResolvePath("/api/v1/repos/{owner}/{repo}/git/commits/{sha}.{diffType}", pathParams("owner", owner, "repo", repo, "sha", sha, "diffType", diffType)) + return s.client.GetRaw(ctx, path) +} + // GetPullRequest returns the pull request associated with a commit SHA. // // Usage: diff --git a/commits_test.go b/commits_test.go index f9fbf2b..96c54ef 100644 --- a/commits_test.go +++ b/commits_test.go @@ -3,6 +3,7 @@ package forge import ( "context" json "github.com/goccy/go-json" + "io" "net/http" "net/http/httptest" "testing" @@ -95,6 +96,29 @@ func TestCommitService_Get_Good(t *testing.T) { } } +func TestCommitService_GetDiffOrPatch_Good(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + t.Errorf("expected GET, got %s", r.Method) + } + if r.URL.Path != "/api/v1/repos/core/go-forge/git/commits/abc123.diff" { + t.Errorf("wrong path: %s", r.URL.Path) + } + w.Header().Set("Content-Type", "text/plain") + io.WriteString(w, "diff --git a/README.md b/README.md") + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + data, err := f.Commits.GetDiffOrPatch(context.Background(), "core", "go-forge", "abc123", "diff") + if err != nil { + t.Fatal(err) + } + if string(data) != "diff --git a/README.md b/README.md" { + t.Fatalf("got body=%q", string(data)) + } +} + func TestCommitService_GetPullRequest_Good(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet {