From 221c5fa94f1693904f2c19f207540c1f3746c392 Mon Sep 17 00:00:00 2001 From: Virgil Date: Wed, 1 Apr 2026 22:18:01 +0000 Subject: [PATCH] Add repo topic add/delete helpers Co-Authored-By: Virgil --- repos.go | 12 ++++++++++++ repos_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/repos.go b/repos.go index ba3a711..5c08fe1 100644 --- a/repos.go +++ b/repos.go @@ -105,6 +105,18 @@ func (s *RepoService) UpdateTopics(ctx context.Context, owner, repo string, topi return s.client.Put(ctx, path, types.RepoTopicOptions{Topics: topics}, nil) } +// AddTopic adds a topic to a repository. +func (s *RepoService) AddTopic(ctx context.Context, owner, repo, topic string) error { + path := ResolvePath("/api/v1/repos/{owner}/{repo}/topics/{topic}", pathParams("owner", owner, "repo", repo, "topic", topic)) + return s.client.Put(ctx, path, nil, nil) +} + +// DeleteTopic removes a topic from a repository. +func (s *RepoService) DeleteTopic(ctx context.Context, owner, repo, topic string) error { + path := ResolvePath("/api/v1/repos/{owner}/{repo}/topics/{topic}", pathParams("owner", owner, "repo", repo, "topic", topic)) + return s.client.Delete(ctx, path) +} + // GetNewPinAllowed returns whether new issue pins are allowed for a repository. func (s *RepoService) GetNewPinAllowed(ctx context.Context, owner, repo string) (*types.NewIssuePinsAllowed, error) { path := ResolvePath("/api/v1/repos/{owner}/{repo}/new_pin_allowed", pathParams("owner", owner, "repo", repo)) diff --git a/repos_test.go b/repos_test.go index da73252..d8bd358 100644 --- a/repos_test.go +++ b/repos_test.go @@ -64,6 +64,46 @@ func TestRepoService_UpdateTopics_Good(t *testing.T) { } } +func TestRepoService_AddTopic_Good(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPut { + t.Errorf("expected PUT, got %s", r.Method) + } + if r.URL.EscapedPath() != "/api/v1/repos/core/go-forge/topics/release%20candidate" { + t.Errorf("wrong path: %s", r.URL.EscapedPath()) + http.NotFound(w, r) + return + } + w.WriteHeader(http.StatusNoContent) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + if err := f.Repos.AddTopic(context.Background(), "core", "go-forge", "release candidate"); err != nil { + t.Fatal(err) + } +} + +func TestRepoService_DeleteTopic_Good(t *testing.T) { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodDelete { + t.Errorf("expected DELETE, got %s", r.Method) + } + if r.URL.EscapedPath() != "/api/v1/repos/core/go-forge/topics/release%20candidate" { + t.Errorf("wrong path: %s", r.URL.EscapedPath()) + http.NotFound(w, r) + return + } + w.WriteHeader(http.StatusNoContent) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + if err := f.Repos.DeleteTopic(context.Background(), "core", "go-forge", "release candidate"); err != nil { + t.Fatal(err) + } +} + func TestRepoService_GetNewPinAllowed_Good(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodGet {