1
0
mirror of https://github.com/interviewstreet/go-jira.git synced 2024-11-28 08:39:03 +02:00

use pagination from design

This commit is contained in:
zenixhuang 2018-03-16 19:01:13 +09:00
parent 1d38c6cae2
commit 04cbb2fe5c
4 changed files with 60 additions and 48 deletions

View File

@ -2,6 +2,7 @@ package jira
import (
"fmt"
"net/url"
)
// GroupService handles Groups for the JIRA instance / API.
@ -18,30 +19,6 @@ type groupMembersResult struct {
MaxResults int `json:"maxResults"`
Total int `json:"total"`
Members []GroupMember `json:"values"`
IsLast bool `json:"isLast"`
NextPage string `json:"nextPage"`
Client *Client `json:"-"`
}
func (g *groupMembersResult) GetNextPage() (*groupMembersResult, *Response, error) {
if g.IsLast {
return nil, nil, nil
}
if g.Client == nil {
return nil, nil, fmt.Errorf("GroupService not assigned")
}
apiEndpoint := g.NextPage
req, err := g.Client.NewRequest("GET", apiEndpoint, nil)
if err != nil {
return nil, nil, err
}
group := new(groupMembersResult)
resp, err := g.Client.Do(req, group)
if err != nil {
return nil, resp, err
}
group.Client = g.Client
return group, resp, nil
}
// Group represents a JIRA group
@ -72,14 +49,21 @@ type GroupMember struct {
TimeZone string `json:"timeZone,omitempty"`
}
type GroupSearchOptions struct {
StartAt int
MaxResults int
IncludeInactiveUsers bool
}
// Get returns a paginated list of users who are members of the specified group and its subgroups.
// Users in the page are ordered by user names.
// User of this resource is required to have sysadmin or admin permissions.
//
// JIRA API docs: https://docs.atlassian.com/jira/REST/server/#api/2/group-getUsersFromGroup
//
// WARNING: This API only returns the first page of group members
func (s *GroupService) Get(name string) ([]GroupMember, *Response, error) {
apiEndpoint := fmt.Sprintf("/rest/api/2/group/member?groupname=%s", name)
apiEndpoint := fmt.Sprintf("/rest/api/2/group/member?groupname=%s", url.QueryEscape(name))
req, err := s.client.NewRequest("GET", apiEndpoint, nil)
if err != nil {
return nil, nil, err
@ -99,14 +83,19 @@ func (s *GroupService) Get(name string) ([]GroupMember, *Response, error) {
// User of this resource is required to have sysadmin or admin permissions.
//
// JIRA API docs: https://docs.atlassian.com/jira/REST/server/#api/2/group-getUsersFromGroup
func (s *GroupService) GetByPage(name string, startAt int, maxResults int, includeInactiveUsers bool) (*groupMembersResult, *Response, error) {
if maxResults < 0 || maxResults > 50 {
maxResults = 50
func (s *GroupService) GetByOption(name string, options *GroupSearchOptions) ([]GroupMember, *Response, error) {
var apiEndpoint string
if options == nil {
apiEndpoint = fmt.Sprintf("/rest/api/2/group/member?groupname=%s", url.QueryEscape(name))
} else {
apiEndpoint = fmt.Sprintf(
"/rest/api/2/group/member?groupname=%s&startAt=%d&maxResults=%d&includeInactiveUsers=%t",
url.QueryEscape(name),
options.StartAt,
options.MaxResults,
options.IncludeInactiveUsers,
)
}
// TODO: should we escape the name here?
apiEndpoint := fmt.Sprintf(
"/rest/api/2/group/member?groupname=%s&startAt=%d&maxResults=%d&includeInactiveUsers=%t",
name, startAt, maxResults, includeInactiveUsers)
req, err := s.client.NewRequest("GET", apiEndpoint, nil)
if err != nil {
return nil, nil, err
@ -117,8 +106,7 @@ func (s *GroupService) GetByPage(name string, startAt int, maxResults int, inclu
if err != nil {
return nil, resp, err
}
group.Client = s.client
return group, resp, nil
return group.Members, resp, nil
}
// Add adds user to group

View File

@ -29,29 +29,48 @@ func TestGroupService_GetPage(t *testing.T) {
testRequestURL(t, r, "/rest/api/2/group/member?groupname=default")
startAt := r.URL.Query().Get("startAt")
if startAt == "0" {
fmt.Fprint(w, `{"self":"http://www.example.com/jira/rest/api/2/group/member?includeInactiveUsers=false&maxResults=2&groupname=default&startAt=0","nextPage":"`+testServer.URL+`/rest/api/2/group/member?groupname=default&includeInactiveUsers=false&maxResults=2&startAt=2","maxResults":2,"startAt":0,"total":2,"isLast":false,"values":[{"self":"http://www.example.com/jira/rest/api/2/user?username=michael","name":"michael","key":"michael","emailAddress":"michael@example.com","displayName":"MichaelScofield","active":true,"timeZone":"Australia/Sydney"},{"self":"http://www.example.com/jira/rest/api/2/user?username=alex","name":"alex","key":"alex","emailAddress":"alex@example.com","displayName":"AlexanderMahone","active":true,"timeZone":"Australia/Sydney"}]}`)
fmt.Fprint(w, `{"self":"http://www.example.com/jira/rest/api/2/group/member?includeInactiveUsers=false&maxResults=2&groupname=default&startAt=0","nextPage":"`+testServer.URL+`/rest/api/2/group/member?groupname=default&includeInactiveUsers=false&maxResults=2&startAt=2","maxResults":2,"startAt":0,"total":4,"isLast":false,"values":[{"self":"http://www.example.com/jira/rest/api/2/user?username=michael","name":"michael","key":"michael","emailAddress":"michael@example.com","displayName":"MichaelScofield","active":true,"timeZone":"Australia/Sydney"},{"self":"http://www.example.com/jira/rest/api/2/user?username=alex","name":"alex","key":"alex","emailAddress":"alex@example.com","displayName":"AlexanderMahone","active":true,"timeZone":"Australia/Sydney"}]}`)
} else if startAt == "2" {
fmt.Fprint(w, `{"self":"http://www.example.com/jira/rest/api/2/group/member?includeInactiveUsers=false&maxResults=2&groupname=default&startAt=2","maxResults":2,"startAt":0,"total":2,"isLast":true,"values":[{"self":"http://www.example.com/jira/rest/api/2/user?username=michael","name":"michael","key":"michael","emailAddress":"michael@example.com","displayName":"MichaelScofield","active":true,"timeZone":"Australia/Sydney"},{"self":"http://www.example.com/jira/rest/api/2/user?username=alex","name":"alex","key":"alex","emailAddress":"alex@example.com","displayName":"AlexanderMahone","active":true,"timeZone":"Australia/Sydney"}]}`)
fmt.Fprint(w, `{"self":"http://www.example.com/jira/rest/api/2/group/member?includeInactiveUsers=false&maxResults=2&groupname=default&startAt=2","maxResults":2,"startAt":2,"total":4,"isLast":true,"values":[{"self":"http://www.example.com/jira/rest/api/2/user?username=michael","name":"michael","key":"michael","emailAddress":"michael@example.com","displayName":"MichaelScofield","active":true,"timeZone":"Australia/Sydney"},{"self":"http://www.example.com/jira/rest/api/2/user?username=alex","name":"alex","key":"alex","emailAddress":"alex@example.com","displayName":"AlexanderMahone","active":true,"timeZone":"Australia/Sydney"}]}`)
} else {
t.Errorf("startAt %s", startAt)
}
})
if page, _, err := testClient.Group.GetByPage("default", 0, 2, false); err != nil {
if page, resp, err := testClient.Group.GetByOption("default", &GroupSearchOptions{
StartAt: 0,
MaxResults: 2,
IncludeInactiveUsers: false,
}); err != nil {
t.Errorf("Error given: %s %s", err, testServer.URL)
} else if page == nil {
t.Error("Expected members. Group.Members is nil")
} else if page == nil || len(page) != 2 {
t.Error("Expected members. Group.Members is not 2 or is nil")
} else {
if len(page.Members) != 2 {
t.Errorf("Expect 2 members in list. Get %d members", len(page.Members))
if resp.StartAt != 0 {
t.Errorf("Expect Result StartAt to be 0, but is %d", resp.StartAt)
}
url := page.NextPage
if page, _, err = page.GetNextPage(); err != nil {
t.Errorf("Error given: %s %s", err, url)
} else if page == nil {
t.Error("Expected members. Group.Members is nil")
if resp.MaxResults != 2 {
t.Errorf("Expect Result MaxResults to be 2, but is %d", resp.MaxResults)
}
if resp.Total != 4 {
t.Errorf("Expect Result Total to be 4, but is %d", resp.Total)
}
if page, resp, err := testClient.Group.GetByOption("default", &GroupSearchOptions{
StartAt: 2,
MaxResults: 2,
IncludeInactiveUsers: false,
}); err != nil {
t.Errorf("Error give: %s %s", err, testServer.URL)
} else if page == nil || len(page) != 2 {
t.Error("Expected members. Group.Members is not 2 or is nil")
} else {
if len(page.Members) != 2 {
t.Errorf("Expect 2 members in list. Get %d members", len(page.Members))
if resp.StartAt != 2 {
t.Errorf("Expect Result StartAt to be 2, but is %d", resp.StartAt)
}
if resp.MaxResults != 2 {
t.Errorf("Expect Result MaxResults to be 2, but is %d", resp.MaxResults)
}
if resp.Total != 4 {
t.Errorf("Expect Result Total to be 4, but is %d", resp.Total)
}
}
}

View File

@ -559,6 +559,7 @@ func (s *IssueService) Get(issueID string, options *GetQueryOptions) (*Issue, *R
// The given options will be appended to the query string
//
// JIRA API docs: https://docs.atlassian.com/jira-software/REST/7.3.1/#agile/1.0/issue-getIssue
//
// TODO: create agile service for holding all agile apis' implementation
func (s *IssueService) GetWithAgile(issueID string, options *GetQueryOptions) (*Issue, *Response, error) {
apiEndpoint := fmt.Sprintf("rest/agile/1.0/issue/%s", issueID)

View File

@ -280,6 +280,10 @@ func (r *Response) populatePageValues(v interface{}) {
r.StartAt = value.StartAt
r.MaxResults = value.MaxResults
r.Total = value.Total
case *groupMembersResult:
r.StartAt = value.StartAt
r.MaxResults = value.MaxResults
r.Total = value.Total
}
return
}