Add commit diff download helper
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
8a7f969780
commit
d09236ea2a
2 changed files with 30 additions and 0 deletions
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue