feat(issues): add dependency relation helpers
All checks were successful
Security Scan / security (push) Successful in 12s
Test / test (push) Successful in 1m15s

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Virgil 2026-04-01 23:35:51 +00:00
parent dd8c6ba998
commit ed4a2c7b0c
2 changed files with 202 additions and 0 deletions

View file

@ -308,6 +308,54 @@ func (s *IssueService) UnsubscribeUser(ctx context.Context, owner, repo string,
return s.client.Delete(ctx, path)
}
// ListDependencies returns all issues that block the given issue.
func (s *IssueService) ListDependencies(ctx context.Context, owner, repo string, index int64) ([]types.Issue, error) {
path := ResolvePath("/api/v1/repos/{owner}/{repo}/issues/{index}/dependencies", pathParams("owner", owner, "repo", repo, "index", int64String(index)))
return ListAll[types.Issue](ctx, s.client, path, nil)
}
// IterDependencies returns an iterator over all issues that block the given issue.
func (s *IssueService) IterDependencies(ctx context.Context, owner, repo string, index int64) iter.Seq2[types.Issue, error] {
path := ResolvePath("/api/v1/repos/{owner}/{repo}/issues/{index}/dependencies", pathParams("owner", owner, "repo", repo, "index", int64String(index)))
return ListIter[types.Issue](ctx, s.client, path, nil)
}
// AddDependency makes another issue block the issue at the given path.
func (s *IssueService) AddDependency(ctx context.Context, owner, repo string, index int64, dependency types.IssueMeta) error {
path := ResolvePath("/api/v1/repos/{owner}/{repo}/issues/{index}/dependencies", pathParams("owner", owner, "repo", repo, "index", int64String(index)))
return s.client.Post(ctx, path, dependency, nil)
}
// RemoveDependency removes an issue dependency from the issue at the given path.
func (s *IssueService) RemoveDependency(ctx context.Context, owner, repo string, index int64, dependency types.IssueMeta) error {
path := ResolvePath("/api/v1/repos/{owner}/{repo}/issues/{index}/dependencies", pathParams("owner", owner, "repo", repo, "index", int64String(index)))
return s.client.DeleteWithBody(ctx, path, dependency)
}
// ListBlocks returns all issues blocked by the given issue.
func (s *IssueService) ListBlocks(ctx context.Context, owner, repo string, index int64) ([]types.Issue, error) {
path := ResolvePath("/api/v1/repos/{owner}/{repo}/issues/{index}/blocks", pathParams("owner", owner, "repo", repo, "index", int64String(index)))
return ListAll[types.Issue](ctx, s.client, path, nil)
}
// IterBlocks returns an iterator over all issues blocked by the given issue.
func (s *IssueService) IterBlocks(ctx context.Context, owner, repo string, index int64) iter.Seq2[types.Issue, error] {
path := ResolvePath("/api/v1/repos/{owner}/{repo}/issues/{index}/blocks", pathParams("owner", owner, "repo", repo, "index", int64String(index)))
return ListIter[types.Issue](ctx, s.client, path, nil)
}
// AddBlock makes the issue at the given path block another issue.
func (s *IssueService) AddBlock(ctx context.Context, owner, repo string, index int64, blockedIssue types.IssueMeta) error {
path := ResolvePath("/api/v1/repos/{owner}/{repo}/issues/{index}/blocks", pathParams("owner", owner, "repo", repo, "index", int64String(index)))
return s.client.Post(ctx, path, blockedIssue, nil)
}
// RemoveBlock removes an issue block from the issue at the given path.
func (s *IssueService) RemoveBlock(ctx context.Context, owner, repo string, index int64, blockedIssue types.IssueMeta) error {
path := ResolvePath("/api/v1/repos/{owner}/{repo}/issues/{index}/blocks", pathParams("owner", owner, "repo", repo, "index", int64String(index)))
return s.client.DeleteWithBody(ctx, path, blockedIssue)
}
// toAnySlice converts a slice of int64 to a slice of any for IssueLabelsOption.
func toAnySlice(ids []int64) []any {
out := make([]any, len(ids))

View file

@ -506,6 +506,160 @@ func TestIssueService_UnsubscribeUser_Good(t *testing.T) {
}
}
func TestIssueService_ListDependencies_Good(t *testing.T) {
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/issues/1/dependencies" {
t.Errorf("wrong path: %s", r.URL.Path)
http.NotFound(w, r)
return
}
w.Header().Set("X-Total-Count", "1")
json.NewEncoder(w).Encode([]types.Issue{{ID: 11, Index: 11, Title: "blocking issue"}})
}))
defer srv.Close()
f := NewForge(srv.URL, "tok")
issues, err := f.Issues.ListDependencies(context.Background(), "core", "go-forge", 1)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(issues, []types.Issue{{ID: 11, Index: 11, Title: "blocking issue"}}) {
t.Fatalf("got %#v", issues)
}
}
func TestIssueService_AddDependency_Good(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("expected POST, got %s", r.Method)
}
if r.URL.Path != "/api/v1/repos/core/go-forge/issues/1/dependencies" {
t.Errorf("wrong path: %s", r.URL.Path)
http.NotFound(w, r)
return
}
var body types.IssueMeta
json.NewDecoder(r.Body).Decode(&body)
if body.Owner != "core" || body.Name != "go-forge" || body.Index != 2 {
t.Fatalf("got body=%#v", body)
}
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(types.Issue{ID: 11, Index: 11, Title: "blocking issue"})
}))
defer srv.Close()
f := NewForge(srv.URL, "tok")
if err := f.Issues.AddDependency(context.Background(), "core", "go-forge", 1, types.IssueMeta{Owner: "core", Name: "go-forge", Index: 2}); err != nil {
t.Fatal(err)
}
}
func TestIssueService_RemoveDependency_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.Path != "/api/v1/repos/core/go-forge/issues/1/dependencies" {
t.Errorf("wrong path: %s", r.URL.Path)
http.NotFound(w, r)
return
}
var body types.IssueMeta
json.NewDecoder(r.Body).Decode(&body)
if body.Owner != "core" || body.Name != "go-forge" || body.Index != 2 {
t.Fatalf("got body=%#v", body)
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(types.Issue{ID: 11, Index: 11, Title: "blocking issue"})
}))
defer srv.Close()
f := NewForge(srv.URL, "tok")
if err := f.Issues.RemoveDependency(context.Background(), "core", "go-forge", 1, types.IssueMeta{Owner: "core", Name: "go-forge", Index: 2}); err != nil {
t.Fatal(err)
}
}
func TestIssueService_ListBlocks_Good(t *testing.T) {
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/issues/1/blocks" {
t.Errorf("wrong path: %s", r.URL.Path)
http.NotFound(w, r)
return
}
w.Header().Set("X-Total-Count", "1")
json.NewEncoder(w).Encode([]types.Issue{{ID: 22, Index: 22, Title: "blocked issue"}})
}))
defer srv.Close()
f := NewForge(srv.URL, "tok")
issues, err := f.Issues.ListBlocks(context.Background(), "core", "go-forge", 1)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(issues, []types.Issue{{ID: 22, Index: 22, Title: "blocked issue"}}) {
t.Fatalf("got %#v", issues)
}
}
func TestIssueService_AddBlock_Good(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("expected POST, got %s", r.Method)
}
if r.URL.Path != "/api/v1/repos/core/go-forge/issues/1/blocks" {
t.Errorf("wrong path: %s", r.URL.Path)
http.NotFound(w, r)
return
}
var body types.IssueMeta
json.NewDecoder(r.Body).Decode(&body)
if body.Owner != "core" || body.Name != "go-forge" || body.Index != 3 {
t.Fatalf("got body=%#v", body)
}
w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(types.Issue{ID: 22, Index: 22, Title: "blocked issue"})
}))
defer srv.Close()
f := NewForge(srv.URL, "tok")
if err := f.Issues.AddBlock(context.Background(), "core", "go-forge", 1, types.IssueMeta{Owner: "core", Name: "go-forge", Index: 3}); err != nil {
t.Fatal(err)
}
}
func TestIssueService_RemoveBlock_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.Path != "/api/v1/repos/core/go-forge/issues/1/blocks" {
t.Errorf("wrong path: %s", r.URL.Path)
http.NotFound(w, r)
return
}
var body types.IssueMeta
json.NewDecoder(r.Body).Decode(&body)
if body.Owner != "core" || body.Name != "go-forge" || body.Index != 3 {
t.Fatalf("got body=%#v", body)
}
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(types.Issue{ID: 22, Index: 22, Title: "blocked issue"})
}))
defer srv.Close()
f := NewForge(srv.URL, "tok")
if err := f.Issues.RemoveBlock(context.Background(), "core", "go-forge", 1, types.IssueMeta{Owner: "core", Name: "go-forge", Index: 3}); err != nil {
t.Fatal(err)
}
}
func TestIssueService_Pin_Good(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {