diff --git a/repos.go b/repos.go index fc6d503..51c4263 100644 --- a/repos.go +++ b/repos.go @@ -269,6 +269,22 @@ func (s *RepoService) ListTopics(ctx context.Context, owner, repo string) ([]str return out.TopicNames, nil } +// IterTopics returns an iterator over the topics assigned to a repository. +func (s *RepoService) IterTopics(ctx context.Context, owner, repo string) iter.Seq2[string, error] { + return func(yield func(string, error) bool) { + topics, err := s.ListTopics(ctx, owner, repo) + if err != nil { + yield("", err) + return + } + for _, topic := range topics { + if !yield(topic, nil) { + return + } + } + } +} + // SearchTopics searches topics by keyword. func (s *RepoService) SearchTopics(ctx context.Context, query string) ([]types.TopicResponse, error) { return ListAll[types.TopicResponse](ctx, s.client, "/api/v1/topics/search", map[string]string{"q": query}) diff --git a/repos_test.go b/repos_test.go index dd6b6a2..5755642 100644 --- a/repos_test.go +++ b/repos_test.go @@ -35,6 +35,38 @@ func TestRepoService_ListTopics_Good(t *testing.T) { } } +func TestRepoService_IterTopics_Good(t *testing.T) { + var requests int + 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/topics" { + t.Errorf("wrong path: %s", r.URL.Path) + http.NotFound(w, r) + return + } + json.NewEncoder(w).Encode(types.TopicName{TopicNames: []string{"go", "forge"}}) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + var got []string + for topic, err := range f.Repos.IterTopics(context.Background(), "core", "go-forge") { + if err != nil { + t.Fatal(err) + } + got = append(got, topic) + } + if requests != 1 { + t.Fatalf("expected 1 request, got %d", requests) + } + if !reflect.DeepEqual(got, []string{"go", "forge"}) { + t.Fatalf("got %#v", got) + } +} + func TestRepoService_SearchTopics_Good(t *testing.T) { var requests int srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {