1
0
mirror of https://github.com/interviewstreet/go-jira.git synced 2025-03-17 20:47:57 +02:00

split out agile endpoint for issue get, add api for pagination support

This commit is contained in:
zenixhuang 2018-03-13 18:49:27 +09:00
parent 958c218987
commit 1d38c6cae2
3 changed files with 120 additions and 15 deletions

View File

@ -20,6 +20,28 @@ type groupMembersResult struct {
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
@ -55,6 +77,7 @@ type GroupMember struct {
// 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)
req, err := s.client.NewRequest("GET", apiEndpoint, nil)
@ -68,22 +91,34 @@ func (s *GroupService) Get(name string) ([]GroupMember, *Response, error) {
return nil, resp, err
}
result := group.Members
for group.IsLast != true {
apiEndpoint = group.NextPage
req, err = s.client.NewRequest("GET", apiEndpoint, nil)
if err != nil {
return nil, nil, err
}
return group.Members, resp, nil
}
group = new(groupMembersResult)
resp, err = s.client.Do(req, group)
if err != nil {
return nil, resp, err
}
result = append(result, group.Members...)
// Get returns a paginated list of 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
func (s *GroupService) GetByPage(name string, startAt int, maxResults int, includeInactiveUsers bool) (*groupMembersResult, *Response, error) {
if maxResults < 0 || maxResults > 50 {
maxResults = 50
}
return result, resp, nil
// 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
}
group := new(groupMembersResult)
resp, err := s.client.Do(req, group)
if err != nil {
return nil, resp, err
}
group.Client = s.client
return group, resp, nil
}
// Add adds user to group

View File

@ -21,6 +21,42 @@ func TestGroupService_Get(t *testing.T) {
}
}
func TestGroupService_GetPage(t *testing.T) {
setup()
defer teardown()
testMux.HandleFunc("/rest/api/2/group/member", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
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"}]}`)
} 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"}]}`)
} else {
t.Errorf("startAt %s", startAt)
}
})
if page, _, err := testClient.Group.GetByPage("default", 0, 2, 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 len(page.Members) != 2 {
t.Errorf("Expect 2 members in list. Get %d members", len(page.Members))
}
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")
} else {
if len(page.Members) != 2 {
t.Errorf("Expect 2 members in list. Get %d members", len(page.Members))
}
}
}
}
func TestGroupService_Add(t *testing.T) {
setup()
defer teardown()

View File

@ -527,9 +527,43 @@ type CustomFields map[string]string
//
// JIRA API docs: https://docs.atlassian.com/jira/REST/latest/#api/2/issue-getIssue
func (s *IssueService) Get(issueID string, options *GetQueryOptions) (*Issue, *Response, error) {
apiEndpoint := fmt.Sprintf("rest/api/2/issue/%s", issueID)
req, err := s.client.NewRequest("GET", apiEndpoint, nil)
if err != nil {
return nil, nil, err
}
if options != nil {
q, err := query.Values(options)
if err != nil {
return nil, nil, err
}
req.URL.RawQuery = q.Encode()
}
issue := new(Issue)
resp, err := s.client.Do(req, issue)
if err != nil {
jerr := NewJiraError(resp, err)
return nil, resp, jerr
}
return issue, resp, nil
}
// Get returns a full representation of the issue for the given issue key.
// JIRA will attempt to identify the issue by the issueIdOrKey path parameter.
// This can be an issue id, or an issue key.
// If the issue cannot be found via an exact match, JIRA will also look for the issue in a case-insensitive way, or by looking to see if the issue was moved.
//
// 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)
if !s.client.Authentication.Authenticated() {
apiEndpoint = fmt.Sprintf("rest/api/2/issue/%s", issueID)
return nil, nil, fmt.Errorf("agile endpoints need to be authenticated for testing")
}
req, err := s.client.NewRequest("GET", apiEndpoint, nil)
if err != nil {