diff --git a/asanaapi/asanaapi.go b/asanaapi/asanaapi.go index bc7b132..c6f02f2 100644 --- a/asanaapi/asanaapi.go +++ b/asanaapi/asanaapi.go @@ -151,7 +151,9 @@ func (s service) canEdit(isCollaborator bool, isCollaboratorErr error, authorID } } -func (s service) ListComments(ctx context.Context, _ issues.RepoSpec, id uint64, opt interface{}) ([]issues.Comment, error) { +func (s service) ListComments(ctx context.Context, _ issues.RepoSpec, id uint64, opt *issues.ListOptions) ([]issues.Comment, error) { + // TODO: Pagination. Respect opt.Start and opt.Length, if given. + var comments []issues.Comment // TODO: Figure this out. @@ -215,7 +217,9 @@ func (s service) ListComments(ctx context.Context, _ issues.RepoSpec, id uint64, return comments, nil } -func (s service) ListEvents(ctx context.Context, _ issues.RepoSpec, id uint64, opt interface{}) ([]issues.Event, error) { +func (s service) ListEvents(ctx context.Context, _ issues.RepoSpec, id uint64, opt *issues.ListOptions) ([]issues.Event, error) { + // TODO: Pagination. Respect opt.Start and opt.Length, if given. + stories, err := s.cl.ListTaskStories(ctx, int64(id), &asana.Filter{OptExpand: []string{"created_by"}}) if err != nil { return nil, err diff --git a/fs/fs.go b/fs/fs.go index 3bbac34..d4083f5 100644 --- a/fs/fs.go +++ b/fs/fs.go @@ -150,7 +150,7 @@ func (s service) Get(ctx context.Context, repo issues.RepoSpec, id uint64) (issu }, nil } -func (s service) ListComments(ctx context.Context, repo issues.RepoSpec, id uint64, opt interface{}) ([]issues.Comment, error) { +func (s service) ListComments(ctx context.Context, repo issues.RepoSpec, id uint64, opt *issues.ListOptions) ([]issues.Comment, error) { currentUser, err := s.users.GetAuthenticated(ctx) if err != nil { return nil, err @@ -162,7 +162,7 @@ func (s service) ListComments(ctx context.Context, repo issues.RepoSpec, id uint if err != nil { return comments, err } - for _, fi := range fis { + for _, fi := range paginate(fis, opt) { var comment comment err = jsonDecodeFile(s.fs, issueCommentPath(repo, id, fi.ID), &comment) if err != nil { @@ -203,14 +203,14 @@ func (s service) ListComments(ctx context.Context, repo issues.RepoSpec, id uint return comments, nil } -func (s service) ListEvents(ctx context.Context, repo issues.RepoSpec, id uint64, opt interface{}) ([]issues.Event, error) { +func (s service) ListEvents(ctx context.Context, repo issues.RepoSpec, id uint64, opt *issues.ListOptions) ([]issues.Event, error) { var events []issues.Event fis, err := readDirIDs(s.fs, issueEventsDir(repo, id)) if err != nil { return events, err } - for _, fi := range fis { + for _, fi := range paginate(fis, opt) { var event event err = jsonDecodeFile(s.fs, issueEventPath(repo, id, fi.ID), &event) if err != nil { @@ -697,6 +697,21 @@ func (s service) EditComment(ctx context.Context, repo issues.RepoSpec, id uint6 }, nil } +func paginate(fis []fileInfoID, opt *issues.ListOptions) []fileInfoID { + if opt == nil { + return fis + } + start := opt.Start + if start > len(fis) { + start = len(fis) + } + end := opt.Start + opt.Length + if end > len(fis) { + end = len(fis) + } + return fis[start:end] +} + // toggleReaction toggles reaction emojiID to comment c for specified user u. // If user is creating a new reaction, they get added to the end of reaction authors. func toggleReaction(c *comment, u users.UserSpec, emojiID reactions.EmojiID) error { diff --git a/githubapi/githubapi.go b/githubapi/githubapi.go index 5e04582..7a805d7 100644 --- a/githubapi/githubapi.go +++ b/githubapi/githubapi.go @@ -214,7 +214,9 @@ func (s service) Get(ctx context.Context, rs issues.RepoSpec, id uint64) (issues }, nil } -func (s service) ListComments(ctx context.Context, rs issues.RepoSpec, id uint64, opt interface{}) ([]issues.Comment, error) { +func (s service) ListComments(ctx context.Context, rs issues.RepoSpec, id uint64, opt *issues.ListOptions) ([]issues.Comment, error) { + // TODO: Respect opt.Start and opt.Length, if given. + repo, err := ghRepoSpec(rs) if err != nil { return nil, err @@ -296,14 +298,16 @@ func (s service) ListComments(ctx context.Context, rs issues.RepoSpec, id uint64 return comments, nil } -func (s service) ListEvents(_ context.Context, rs issues.RepoSpec, id uint64, opt interface{}) ([]issues.Event, error) { +func (s service) ListEvents(_ context.Context, rs issues.RepoSpec, id uint64, opt *issues.ListOptions) ([]issues.Event, error) { + // TODO: Pagination. Respect opt.Start and opt.Length, if given. + repo, err := ghRepoSpec(rs) if err != nil { return nil, err } var events []issues.Event - ghEvents, _, err := s.cl.Issues.ListIssueEvents(repo.Owner, repo.Repo, int(id), nil) // TODO: Pagination. + ghEvents, _, err := s.cl.Issues.ListIssueEvents(repo.Owner, repo.Repo, int(id), nil) if err != nil { return events, err } diff --git a/issues.go b/issues.go index 6b82bbe..1f8c7bd 100644 --- a/issues.go +++ b/issues.go @@ -33,9 +33,9 @@ type Service interface { Get(ctx context.Context, repo RepoSpec, id uint64) (Issue, error) // ListComments lists comments for specified issue id. - ListComments(ctx context.Context, repo RepoSpec, id uint64, opt interface{}) ([]Comment, error) + ListComments(ctx context.Context, repo RepoSpec, id uint64, opt *ListOptions) ([]Comment, error) // ListEvents lists events for specified issue id. - ListEvents(ctx context.Context, repo RepoSpec, id uint64, opt interface{}) ([]Event, error) + ListEvents(ctx context.Context, repo RepoSpec, id uint64, opt *ListOptions) ([]Event, error) // Create a new issue. Create(ctx context.Context, repo RepoSpec, issue Issue) (Issue, error) diff --git a/options.go b/options.go index 47559da..8c657a6 100644 --- a/options.go +++ b/options.go @@ -12,3 +12,12 @@ const ( // AllStates is a state filter that includes all issues. AllStates StateFilter = "all" ) + +// ListOptions controls pagination. +type ListOptions struct { + // Start is the index of first result to retrieve, zero-indexed. + Start int + + // Length is the number of results to include. + Length int +}