diff --git a/forge_test.go b/forge_test.go index 8a4421e..5ed95f5 100644 --- a/forge_test.go +++ b/forge_test.go @@ -185,6 +185,32 @@ func TestRepoService_GetArchive_Good(t *testing.T) { } } +func TestRepoService_GetRawFile_Good(t *testing.T) { + want := []byte("# go-forge\n\nA Go client for Forgejo.") + 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/raw/README.md" { + t.Errorf("wrong path: %s", r.URL.Path) + http.NotFound(w, r) + return + } + w.WriteHeader(http.StatusOK) + _, _ = w.Write(want) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + got, err := f.Repos.GetRawFile(context.Background(), "core", "go-forge", "README.md") + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(got, want) { + t.Fatalf("got %q, want %q", got, want) + } +} + func TestRepoService_ListTags_Good(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet { diff --git a/repos.go b/repos.go index 7850746..be8d8e3 100644 --- a/repos.go +++ b/repos.go @@ -148,6 +148,12 @@ func (s *RepoService) GetArchive(ctx context.Context, owner, repo, archive strin return s.client.GetRaw(ctx, path) } +// GetRawFile returns the raw content of a repository file as bytes. +func (s *RepoService) GetRawFile(ctx context.Context, owner, repo, filepath string) ([]byte, error) { + path := ResolvePath("/api/v1/repos/{owner}/{repo}/raw/{filepath}", pathParams("owner", owner, "repo", repo, "filepath", filepath)) + return s.client.GetRaw(ctx, path) +} + // ListIssueTemplates returns all issue templates available for a repository. func (s *RepoService) ListIssueTemplates(ctx context.Context, owner, repo string) ([]types.IssueTemplate, error) { path := ResolvePath("/api/v1/repos/{owner}/{repo}/issue_templates", pathParams("owner", owner, "repo", repo))