feat(repos): add topic iterator
All checks were successful
Security Scan / security (push) Successful in 13s
Test / test (push) Successful in 1m25s

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-02 00:12:11 +00:00
parent cb54bfeea2
commit 61eb6dedbb
2 changed files with 48 additions and 0 deletions

View file

@ -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})

View file

@ -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) {