mirror of
https://github.com/interviewstreet/go-jira.git
synced 2025-06-23 00:07:40 +02:00
Merge pull request #96 from b4b4r07/groups
New API endpoints for JIRA group functionality: GET/POST for /rest/api/2/group/user and GET for /rest/api/2/user/groups
This commit is contained in:
62
group.go
62
group.go
@ -20,6 +20,23 @@ type groupMembersResult struct {
|
|||||||
Members []GroupMember `json:"values"`
|
Members []GroupMember `json:"values"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Group represents a JIRA group
|
||||||
|
type Group struct {
|
||||||
|
ID string `json:"id"`
|
||||||
|
Title string `json:"title"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
Properties groupProperties `json:"properties"`
|
||||||
|
AdditionalProperties bool `json:"additionalProperties"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type groupProperties struct {
|
||||||
|
Name groupPropertiesName `json:"name"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type groupPropertiesName struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
}
|
||||||
|
|
||||||
// GroupMember reflects a single member of a group
|
// GroupMember reflects a single member of a group
|
||||||
type GroupMember struct {
|
type GroupMember struct {
|
||||||
Self string `json:"self,omitempty"`
|
Self string `json:"self,omitempty"`
|
||||||
@ -37,7 +54,7 @@ type GroupMember struct {
|
|||||||
//
|
//
|
||||||
// JIRA API docs: https://docs.atlassian.com/jira/REST/server/#api/2/group-getUsersFromGroup
|
// JIRA API docs: https://docs.atlassian.com/jira/REST/server/#api/2/group-getUsersFromGroup
|
||||||
func (s *GroupService) Get(name string) ([]GroupMember, *Response, error) {
|
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", name)
|
||||||
req, err := s.client.NewRequest("GET", apiEndpoint, nil)
|
req, err := s.client.NewRequest("GET", apiEndpoint, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
@ -51,3 +68,46 @@ func (s *GroupService) Get(name string) ([]GroupMember, *Response, error) {
|
|||||||
|
|
||||||
return group.Members, resp, nil
|
return group.Members, resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add adds user to group
|
||||||
|
//
|
||||||
|
// JIRA API docs: https://docs.atlassian.com/jira/REST/cloud/#api/2/group-addUserToGroup
|
||||||
|
func (s *GroupService) Add(groupname string, username string) (*Group, *Response, error) {
|
||||||
|
apiEndpoint := fmt.Sprintf("/rest/api/2/group/user?groupname=%s", groupname)
|
||||||
|
var user struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
}
|
||||||
|
user.Name = username
|
||||||
|
req, err := s.client.NewRequest("POST", apiEndpoint, &user)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
responseGroup := new(Group)
|
||||||
|
resp, err := s.client.Do(req, responseGroup)
|
||||||
|
if err != nil {
|
||||||
|
jerr := NewJiraError(resp, err)
|
||||||
|
return nil, resp, jerr
|
||||||
|
}
|
||||||
|
|
||||||
|
return responseGroup, resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove removes user from group
|
||||||
|
//
|
||||||
|
// JIRA API docs: https://docs.atlassian.com/jira/REST/cloud/#api/2/group-removeUserFromGroup
|
||||||
|
func (s *GroupService) Remove(groupname string, username string) (*Response, error) {
|
||||||
|
apiEndpoint := fmt.Sprintf("/rest/api/2/group/user?groupname=%s&username=%s", groupname, username)
|
||||||
|
req, err := s.client.NewRequest("DELETE", apiEndpoint, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := s.client.Do(req, nil)
|
||||||
|
if err != nil {
|
||||||
|
jerr := NewJiraError(resp, err)
|
||||||
|
return resp, jerr
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
56
group_test.go
Normal file
56
group_test.go
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
package jira
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGroupService_Get(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")
|
||||||
|
fmt.Fprint(w, `{"self":"http://www.example.com/jira/rest/api/2/group/member?includeInactiveUsers=false&maxResults=50&groupname=default&startAt=0","maxResults":50,"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"}]}`)
|
||||||
|
})
|
||||||
|
if members, _, err := testClient.Group.Get("default"); err != nil {
|
||||||
|
t.Errorf("Error given: %s", err)
|
||||||
|
} else if members == nil {
|
||||||
|
t.Error("Expected members. Group.Members is nil")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGroupService_Add(t *testing.T) {
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
|
testMux.HandleFunc("/rest/api/2/group/user", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
testMethod(t, r, "POST")
|
||||||
|
testRequestURL(t, r, "/rest/api/2/group/user?groupname=default")
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
fmt.Fprint(w, `{"name":"default","self":"http://www.example.com/jira/rest/api/2/group?groupname=default","users":{"size":1,"items":[],"max-results":50,"start-index":0,"end-index":0},"expand":"users"}`)
|
||||||
|
})
|
||||||
|
|
||||||
|
if group, _, err := testClient.Group.Add("default", "theodore"); err != nil {
|
||||||
|
t.Errorf("Error given: %s", err)
|
||||||
|
} else if group == nil {
|
||||||
|
t.Error("Expected group. Group is nil")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGroupService_Remove(t *testing.T) {
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
|
testMux.HandleFunc("/rest/api/2/group/user", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
testMethod(t, r, "DELETE")
|
||||||
|
testRequestURL(t, r, "/rest/api/2/group/user?groupname=default")
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
fmt.Fprint(w, `{"name":"default","self":"http://www.example.com/jira/rest/api/2/group?groupname=default","users":{"size":1,"items":[],"max-results":50,"start-index":0,"end-index":0},"expand":"users"}`)
|
||||||
|
})
|
||||||
|
|
||||||
|
if _, err := testClient.Group.Remove("default", "theodore"); err != nil {
|
||||||
|
t.Errorf("Error given: %s", err)
|
||||||
|
}
|
||||||
|
}
|
24
user.go
24
user.go
@ -27,6 +27,12 @@ type User struct {
|
|||||||
ApplicationKeys []string `json:"applicationKeys,omitempty" structs:"applicationKeys,omitempty"`
|
ApplicationKeys []string `json:"applicationKeys,omitempty" structs:"applicationKeys,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UserGroup represents the group list
|
||||||
|
type UserGroup struct {
|
||||||
|
Self string `json:"self,omitempty" structs:"self,omitempty"`
|
||||||
|
Name string `json:"name,omitempty" structs:"name,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
// Get gets user info from JIRA
|
// Get gets user info from JIRA
|
||||||
//
|
//
|
||||||
// JIRA API docs: https://docs.atlassian.com/jira/REST/cloud/#api/2/user-getUser
|
// JIRA API docs: https://docs.atlassian.com/jira/REST/cloud/#api/2/user-getUser
|
||||||
@ -72,3 +78,21 @@ func (s *UserService) Create(user *User) (*User, *Response, error) {
|
|||||||
}
|
}
|
||||||
return responseUser, resp, nil
|
return responseUser, resp, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetGroups returns the groups which the user belongs to
|
||||||
|
//
|
||||||
|
// JIRA API docs: https://docs.atlassian.com/jira/REST/cloud/#api/2/user-getUserGroups
|
||||||
|
func (s *UserService) GetGroups(username string) (*[]UserGroup, *Response, error) {
|
||||||
|
apiEndpoint := fmt.Sprintf("/rest/api/2/user/groups?username=%s", username)
|
||||||
|
req, err := s.client.NewRequest("GET", apiEndpoint, nil)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
userGroups := new([]UserGroup)
|
||||||
|
resp, err := s.client.Do(req, userGroups)
|
||||||
|
if err != nil {
|
||||||
|
return nil, resp, err
|
||||||
|
}
|
||||||
|
return userGroups, resp, nil
|
||||||
|
}
|
||||||
|
18
user_test.go
18
user_test.go
@ -55,3 +55,21 @@ func TestUserService_Create(t *testing.T) {
|
|||||||
t.Error("Expected user. User is nil")
|
t.Error("Expected user. User is nil")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUserService_GetGroups(t *testing.T) {
|
||||||
|
setup()
|
||||||
|
defer teardown()
|
||||||
|
testMux.HandleFunc("/rest/api/2/user/groups", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
testMethod(t, r, "GET")
|
||||||
|
testRequestURL(t, r, "/rest/api/2/user/groups?username=fred")
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
fmt.Fprint(w, `[{"name":"jira-software-users","self":"http://www.example.com/jira/rest/api/2/user?username=fred"}]`)
|
||||||
|
})
|
||||||
|
|
||||||
|
if groups, _, err := testClient.User.GetGroups("fred"); err != nil {
|
||||||
|
t.Errorf("Error given: %s", err)
|
||||||
|
} else if groups == nil {
|
||||||
|
t.Error("Expected user groups. []UserGroup is nil")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user