Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ability to specify fields #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 51 additions & 28 deletions v5/pivotal/stories.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,28 +35,43 @@ const (
)

type Story struct {
Id int `json:"id,omitempty"`
ProjectId int `json:"project_id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Type string `json:"story_type,omitempty"`
State string `json:"current_state,omitempty"`
Estimate *float64 `json:"estimate,omitempty"`
AcceptedAt *time.Time `json:"accepted_at,omitempty"`
Deadline *time.Time `json:"deadline,omitempty"`
RequestedById int `json:"requested_by_id,omitempty"`
OwnerIds []int `json:"owner_ids,omitempty"`
LabelIds []int `json:"label_ids,omitempty"`
Labels []*Label `json:"labels,omitempty"`
TaskIds []int `json:"task_ids,omitempty"`
Tasks []int `json:"tasks,omitempty"`
FollowerIds []int `json:"follower_ids,omitempty"`
CommentIds []int `json:"comment_ids,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
IntegrationId int `json:"integration_id,omitempty"`
ExternalId string `json:"external_id,omitempty"`
URL string `json:"url,omitempty"`
Id int `json:"id,omitempty"`
ProjectId int `json:"project_id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
Type string `json:"story_type,omitempty"`
State string `json:"current_state,omitempty"`
Estimate *float64 `json:"estimate,omitempty"`
AcceptedAt *time.Time `json:"accepted_at,omitempty"`
Deadline *time.Time `json:"deadline,omitempty"`
RequestedById int `json:"requested_by_id,omitempty"`
OwnerIds []int `json:"owner_ids,omitempty"`
LabelIds []int `json:"label_ids,omitempty"`
Labels []*Label `json:"labels,omitempty"`
TaskIds []int `json:"task_ids,omitempty"`
Tasks []int `json:"tasks,omitempty"`
FollowerIds []int `json:"follower_ids,omitempty"`
CommentIds []int `json:"comment_ids,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
UpdatedAt *time.Time `json:"updated_at,omitempty"`
IntegrationId int `json:"integration_id,omitempty"`
ExternalId string `json:"external_id,omitempty"`
URL string `json:"url,omitempty"`
CycleTimeDetails *CycleTimeDetails `json:"cycle_time_details,omitempty"`
}

type CycleTimeDetails struct {
TotalCycleTime int `json:"total_cycle_time,omitempty"`
StartedTime int `json:"started_time,omitempty"`
StartedCount int `json:"started_count,omitempty"`
FinishedTime int `json:"finished_time,omitempty"`
FinishedCount int `json:"finished_count,omitempty"`
DeliveredTime int `json:"delivered_time,omitempty"`
DeliveredCount int `json:"delivered_count,omitempty"`
RejectedTime int `json:"rejected_time,omitempty"`
RejectedCount int `json:"rejected_count,omitempty"`
StoryId int `json:"story_id,omitempty"`
Kind string `json:"kind,omitempty"`
}

type StoryRequest struct {
Expand Down Expand Up @@ -132,8 +147,8 @@ func newStoryService(client *Client) *StoryService {
// to get the right results. Since the response as generated by Pivotal Tracker
// is not always sorted when using a filter, this approach is required to get
// the right data. Not sure whether this is a bug or a feature.
func (service *StoryService) List(projectId int, filter string) ([]*Story, error) {
reqFunc := newStoriesRequestFunc(service.client, projectId, filter)
func (service *StoryService) List(projectId int, filter, fields string) ([]*Story, error) {
reqFunc := newStoriesRequestFunc(service.client, projectId, filter, fields)
cursor, err := newCursor(service.client, reqFunc, 0)
if err != nil {
return nil, err
Expand All @@ -146,11 +161,19 @@ func (service *StoryService) List(projectId int, filter string) ([]*Story, error
return stories, nil
}

func newStoriesRequestFunc(client *Client, projectId int, filter string) func() *http.Request {
func newStoriesRequestFunc(client *Client, projectId int, filter, fields string) func() *http.Request {
return func() *http.Request {
u := fmt.Sprintf("projects/%v/stories", projectId)
q := url.Values{}
if filter != "" {
u += "?filter=" + url.QueryEscape(filter)
q.Set("filter", filter)
}

if fields != "" {
q.Set("fields", fields)
}
if qp := q.Encode(); qp != "" {
u = u + "?" + qp
}
req, _ := client.NewRequest("GET", u, nil)
return req
Expand Down Expand Up @@ -183,8 +206,8 @@ func (c *StoryCursor) Next() (s *Story, err error) {

// Iterate returns a cursor that can be used to iterate over the stories specified
// by the filter. More stories are fetched on demand as needed.
func (service *StoryService) Iterate(projectId int, filter string) (c *StoryCursor, err error) {
reqFunc := newStoriesRequestFunc(service.client, projectId, filter)
func (service *StoryService) Iterate(projectId int, filter, fields string) (c *StoryCursor, err error) {
reqFunc := newStoriesRequestFunc(service.client, projectId, filter, fields)
cursor, err := newCursor(service.client, reqFunc, PageLimit)
if err != nil {
return nil, err
Expand Down