Add repo raw media endpoint
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
3940f8f23e
commit
4489da434c
2 changed files with 46 additions and 0 deletions
16
repos.go
16
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))
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue