feat: add iterator for repository flags
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
5d4374847d
commit
02def0d8ad
2 changed files with 45 additions and 0 deletions
6
repos.go
6
repos.go
|
|
@ -325,6 +325,12 @@ func (s *RepoService) ListFlags(ctx context.Context, owner, repo string) ([]stri
|
|||
return out, nil
|
||||
}
|
||||
|
||||
// IterFlags returns an iterator over all flags for a repository.
|
||||
func (s *RepoService) IterFlags(ctx context.Context, owner, repo string) iter.Seq2[string, error] {
|
||||
path := ResolvePath("/api/v1/repos/{owner}/{repo}/flags", pathParams("owner", owner, "repo", repo))
|
||||
return ListIter[string](ctx, s.client, path, nil)
|
||||
}
|
||||
|
||||
// ReplaceFlags replaces all flags for a repository.
|
||||
func (s *RepoService) ReplaceFlags(ctx context.Context, owner, repo string, opts *types.ReplaceFlagsOption) error {
|
||||
path := ResolvePath("/api/v1/repos/{owner}/{repo}/flags", pathParams("owner", owner, "repo", repo))
|
||||
|
|
|
|||
|
|
@ -1176,6 +1176,45 @@ func TestRepoService_ListFlags_Good(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestRepoService_IterFlags_Good(t *testing.T) {
|
||||
requests := 0
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
requests++
|
||||
if r.Method != http.MethodGet {
|
||||
t.Errorf("expected GET, got %s", r.Method)
|
||||
}
|
||||
if r.URL.Path != "/api/v1/repos/core/go-forge/flags" {
|
||||
t.Errorf("wrong path: %s", r.URL.Path)
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if got := r.URL.Query().Get("page"); got != "1" {
|
||||
t.Errorf("got page=%q, want %q", got, "1")
|
||||
}
|
||||
if got := r.URL.Query().Get("limit"); got != "50" {
|
||||
t.Errorf("got limit=%q, want %q", got, "50")
|
||||
}
|
||||
w.Header().Set("X-Total-Count", "2")
|
||||
json.NewEncoder(w).Encode([]string{"alpha", "beta"})
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
f := NewForge(srv.URL, "tok")
|
||||
var got []string
|
||||
for flag, err := range f.Repos.IterFlags(context.Background(), "core", "go-forge") {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got = append(got, flag)
|
||||
}
|
||||
if requests != 1 {
|
||||
t.Fatalf("expected 1 request, got %d", requests)
|
||||
}
|
||||
if !reflect.DeepEqual(got, []string{"alpha", "beta"}) {
|
||||
t.Fatalf("got %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRepoService_ReplaceFlags_Good(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPut {
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue