From 4489da434cf4dac387aa99ccf1d6c0c1e9e01a19 Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 00:19:59 +0000 Subject: [PATCH] Add repo raw media endpoint Co-Authored-By: Virgil --- repos.go | 16 ++++++++++++++++ repos_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/repos.go b/repos.go index 3652181..334ed93 100644 --- a/repos.go +++ b/repos.go @@ -221,6 +221,22 @@ func (s *RepoService) GetRawFile(ctx context.Context, owner, repo, filepath stri return s.client.GetRaw(ctx, path) } +// GetRawFileOrLFS returns the raw content or LFS object for a repository file as bytes. +func (s *RepoService) GetRawFileOrLFS(ctx context.Context, owner, repo, filepath, ref string) ([]byte, error) { + path := ResolvePath("/api/v1/repos/{owner}/{repo}/media/{filepath}", pathParams("owner", owner, "repo", repo, "filepath", filepath)) + if ref != "" { + u, err := url.Parse(path) + if err != nil { + return nil, err + } + q := u.Query() + q.Set("ref", ref) + u.RawQuery = q.Encode() + path = u.String() + } + return s.client.GetRaw(ctx, path) +} + // GetLanguages returns the byte counts per language for a repository. func (s *RepoService) GetLanguages(ctx context.Context, owner, repo string) (map[string]int64, error) { path := ResolvePath("/api/v1/repos/{owner}/{repo}/languages", pathParams("owner", owner, "repo", repo)) diff --git a/repos_test.go b/repos_test.go index 2b4b375..af0d6a1 100644 --- a/repos_test.go +++ b/repos_test.go @@ -1,6 +1,7 @@ package forge import ( + "bytes" "context" json "github.com/goccy/go-json" "net/http" @@ -417,6 +418,35 @@ func TestRepoService_GetLanguages_Good(t *testing.T) { } } +func TestRepoService_GetRawFileOrLFS_Good(t *testing.T) { + want := []byte("lfs-pointer-or-content") + 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/media/README.md" { + t.Errorf("wrong path: %s", r.URL.Path) + http.NotFound(w, r) + return + } + if got := r.URL.Query().Get("ref"); got != "main" { + t.Errorf("wrong ref: %s", got) + } + w.WriteHeader(http.StatusOK) + _, _ = w.Write(want) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + got, err := f.Repos.GetRawFileOrLFS(context.Background(), "core", "go-forge", "README.md", "main") + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(got, want) { + t.Fatalf("got %q, want %q", got, want) + } +} + func TestRepoService_ListForks_Good(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet {