From 26ea87556b4cc90813be2debabc1b74887f388c9 Mon Sep 17 00:00:00 2001 From: Virgil Date: Wed, 1 Apr 2026 22:43:54 +0000 Subject: [PATCH] Add issue time tracking helpers Co-Authored-By: Virgil --- docs/api-contract.md | 3 ++ issues.go | 35 ++++++++++++++++ issues_test.go | 96 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+) diff --git a/docs/api-contract.md b/docs/api-contract.md index a61ea72..4a11a3e 100644 --- a/docs/api-contract.md +++ b/docs/api-contract.md @@ -109,17 +109,20 @@ Coverage notes: rows list direct tests when a symbol is named in test names or r | method | IssueService.AddLabels | `func (s *IssueService) AddLabels(ctx context.Context, owner, repo string, index int64, labelIDs []int64) error` | AddLabels adds labels to an issue. | No direct tests. | | method | IssueService.AddReaction | `func (s *IssueService) AddReaction(ctx context.Context, owner, repo string, index int64, reaction string) error` | AddReaction adds a reaction to an issue. | No direct tests. | | method | IssueService.CreateComment | `func (s *IssueService) CreateComment(ctx context.Context, owner, repo string, index int64, body string) (*types.Comment, error)` | CreateComment creates a comment on an issue. | `TestIssueService_Good_CreateComment` | +| method | IssueService.AddTime | `func (s *IssueService) AddTime(ctx context.Context, owner, repo string, index int64, opts *types.AddTimeOption) (*types.TrackedTime, error)` | AddTime adds tracked time to an issue. | `TestIssueService_AddTime_Good` | | method | IssueService.DeleteStopwatch | `func (s *IssueService) DeleteStopwatch(ctx context.Context, owner, repo string, index int64) error` | DeleteStopwatch deletes an issue's existing stopwatch. | `TestIssueService_DeleteStopwatch_Good` | | method | IssueService.DeleteReaction | `func (s *IssueService) DeleteReaction(ctx context.Context, owner, repo string, index int64, reaction string) error` | DeleteReaction removes a reaction from an issue. | No direct tests. | | method | IssueService.IterComments | `func (s *IssueService) IterComments(ctx context.Context, owner, repo string, index int64) iter.Seq2[types.Comment, error]` | IterComments returns an iterator over all comments on an issue. | No direct tests. | | method | IssueService.ListComments | `func (s *IssueService) ListComments(ctx context.Context, owner, repo string, index int64) ([]types.Comment, error)` | ListComments returns all comments on an issue. | No direct tests. | | method | IssueService.IterTimeline | `func (s *IssueService) IterTimeline(ctx context.Context, owner, repo string, index int64, since, before *time.Time) iter.Seq2[types.TimelineComment, error]` | IterTimeline returns an iterator over all comments and events on an issue. | `TestIssueService_IterTimeline_Good` | | method | IssueService.ListTimeline | `func (s *IssueService) ListTimeline(ctx context.Context, owner, repo string, index int64, since, before *time.Time) ([]types.TimelineComment, error)` | ListTimeline returns all comments and events on an issue. | `TestIssueService_ListTimeline_Good` | +| method | IssueService.ListTimes | `func (s *IssueService) ListTimes(ctx context.Context, owner, repo string, index int64, user string, since, before *time.Time) ([]types.TrackedTime, error)` | ListTimes returns all tracked times on an issue. | `TestIssueService_ListTimes_Good` | | method | IssueService.Pin | `func (s *IssueService) Pin(ctx context.Context, owner, repo string, index int64) error` | Pin pins an issue. | `TestIssueService_Good_Pin` | | method | IssueService.RemoveLabel | `func (s *IssueService) RemoveLabel(ctx context.Context, owner, repo string, index int64, labelID int64) error` | RemoveLabel removes a single label from an issue. | No direct tests. | | method | IssueService.SetDeadline | `func (s *IssueService) SetDeadline(ctx context.Context, owner, repo string, index int64, deadline string) error` | SetDeadline sets or updates the deadline on an issue. | No direct tests. | | method | IssueService.StartStopwatch | `func (s *IssueService) StartStopwatch(ctx context.Context, owner, repo string, index int64) error` | StartStopwatch starts the stopwatch on an issue. | No direct tests. | | method | IssueService.StopStopwatch | `func (s *IssueService) StopStopwatch(ctx context.Context, owner, repo string, index int64) error` | StopStopwatch stops the stopwatch on an issue. | No direct tests. | +| method | IssueService.ResetTime | `func (s *IssueService) ResetTime(ctx context.Context, owner, repo string, index int64) error` | ResetTime removes all tracked time from an issue. | `TestIssueService_ResetTime_Good` | | method | IssueService.Unpin | `func (s *IssueService) Unpin(ctx context.Context, owner, repo string, index int64) error` | Unpin unpins an issue. | No direct tests. | | method | LabelService.CreateOrgLabel | `func (s *LabelService) CreateOrgLabel(ctx context.Context, org string, opts *types.CreateLabelOption) (*types.Label, error)` | CreateOrgLabel creates a new label in an organisation. | `TestLabelService_Good_CreateOrgLabel` | | method | LabelService.CreateRepoLabel | `func (s *LabelService) CreateRepoLabel(ctx context.Context, owner, repo string, opts *types.CreateLabelOption) (*types.Label, error)` | CreateRepoLabel creates a new label in a repository. | `TestLabelService_Good_CreateRepoLabel` | diff --git a/issues.go b/issues.go index 88d674d..e8b3d16 100644 --- a/issues.go +++ b/issues.go @@ -83,6 +83,41 @@ func (s *IssueService) DeleteStopwatch(ctx context.Context, owner, repo string, return s.client.Delete(ctx, path) } +// ListTimes returns all tracked times on an issue. +func (s *IssueService) ListTimes(ctx context.Context, owner, repo string, index int64, user string, since, before *time.Time) ([]types.TrackedTime, error) { + path := ResolvePath("/api/v1/repos/{owner}/{repo}/issues/{index}/times", pathParams("owner", owner, "repo", repo, "index", int64String(index))) + query := make(map[string]string, 3) + if user != "" { + query["user"] = user + } + if since != nil { + query["since"] = since.Format(time.RFC3339) + } + if before != nil { + query["before"] = before.Format(time.RFC3339) + } + if len(query) == 0 { + query = nil + } + return ListAll[types.TrackedTime](ctx, s.client, path, query) +} + +// AddTime adds tracked time to an issue. +func (s *IssueService) AddTime(ctx context.Context, owner, repo string, index int64, opts *types.AddTimeOption) (*types.TrackedTime, error) { + path := ResolvePath("/api/v1/repos/{owner}/{repo}/issues/{index}/times", pathParams("owner", owner, "repo", repo, "index", int64String(index))) + var out types.TrackedTime + if err := s.client.Post(ctx, path, opts, &out); err != nil { + return nil, err + } + return &out, nil +} + +// ResetTime removes all tracked time from an issue. +func (s *IssueService) ResetTime(ctx context.Context, owner, repo string, index int64) error { + path := ResolvePath("/api/v1/repos/{owner}/{repo}/issues/{index}/times", pathParams("owner", owner, "repo", repo, "index", int64String(index))) + return s.client.Delete(ctx, path) +} + // AddLabels adds labels to an issue. func (s *IssueService) AddLabels(ctx context.Context, owner, repo string, index int64, labelIDs []int64) error { path := ResolvePath("/api/v1/repos/{owner}/{repo}/issues/{index}/labels", pathParams("owner", owner, "repo", repo, "index", int64String(index))) diff --git a/issues_test.go b/issues_test.go index 96470ca..39aa611 100644 --- a/issues_test.go +++ b/issues_test.go @@ -413,6 +413,102 @@ func TestIssueService_DeleteStopwatch_Good(t *testing.T) { } } +func TestIssueService_ListTimes_Good(t *testing.T) { + since := time.Date(2026, time.March, 3, 9, 15, 0, 0, time.UTC) + before := time.Date(2026, time.March, 4, 9, 15, 0, 0, time.UTC) + + 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/42/times" { + t.Errorf("wrong path: %s", r.URL.Path) + http.NotFound(w, r) + return + } + if got := r.URL.Query().Get("user"); got != "alice" { + t.Errorf("got user=%q, want %q", got, "alice") + } + if got := r.URL.Query().Get("since"); got != since.Format(time.RFC3339) { + t.Errorf("got since=%q, want %q", got, since.Format(time.RFC3339)) + } + if got := r.URL.Query().Get("before"); got != before.Format(time.RFC3339) { + t.Errorf("got before=%q, want %q", got, before.Format(time.RFC3339)) + } + w.Header().Set("X-Total-Count", "2") + json.NewEncoder(w).Encode([]types.TrackedTime{ + {ID: 11, Time: 30, UserName: "alice"}, + {ID: 12, Time: 90, UserName: "bob"}, + }) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + times, err := f.Issues.ListTimes(context.Background(), "core", "go-forge", 42, "alice", &since, &before) + if err != nil { + t.Fatal(err) + } + if !reflect.DeepEqual(times, []types.TrackedTime{ + {ID: 11, Time: 30, UserName: "alice"}, + {ID: 12, Time: 90, UserName: "bob"}, + }) { + t.Fatalf("got %#v", times) + } +} + +func TestIssueService_AddTime_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/42/times" { + t.Errorf("wrong path: %s", r.URL.Path) + http.NotFound(w, r) + return + } + var body types.AddTimeOption + json.NewDecoder(r.Body).Decode(&body) + if body.Time != 180 || body.User != "alice" { + t.Fatalf("got body=%#v", body) + } + w.WriteHeader(http.StatusCreated) + json.NewEncoder(w).Encode(types.TrackedTime{ID: 99, Time: body.Time, UserName: body.User}) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + got, err := f.Issues.AddTime(context.Background(), "core", "go-forge", 42, &types.AddTimeOption{ + Time: 180, + User: "alice", + }) + if err != nil { + t.Fatal(err) + } + if got.ID != 99 || got.Time != 180 || got.UserName != "alice" { + t.Fatalf("got %#v", got) + } +} + +func TestIssueService_ResetTime_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/42/times" { + t.Errorf("wrong path: %s", r.URL.Path) + http.NotFound(w, r) + return + } + w.WriteHeader(http.StatusNoContent) + })) + defer srv.Close() + + f := NewForge(srv.URL, "tok") + if err := f.Issues.ResetTime(context.Background(), "core", "go-forge", 42); err != nil { + t.Fatal(err) + } +} + func TestIssueService_List_Bad(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusInternalServerError)