mirror of
				https://github.com/interviewstreet/go-jira.git
				synced 2025-10-30 23:47:46 +02:00 
			
		
		
		
	Add some APIs on a JIRA group
This commit is contained in:
		
							
								
								
									
										64
									
								
								group.go
									
									
									
									
									
								
							
							
						
						
									
										64
									
								
								group.go
									
									
									
									
									
								
							| @@ -20,6 +20,23 @@ type groupMembersResult struct { | ||||
| 	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 | ||||
| type GroupMember struct { | ||||
| 	Self         string `json:"self,omitempty"` | ||||
| @@ -51,3 +68,50 @@ func (s *GroupService) Get(name string) ([]GroupMember, *Response, error) { | ||||
|  | ||||
| 	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) | ||||
| 	// var user struct { | ||||
| 	// 	Name string `json:"name"` | ||||
| 	// } | ||||
| 	// user.Name = 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 | ||||
| } | ||||
|   | ||||
							
								
								
									
										24
									
								
								user.go
									
									
									
									
									
								
							
							
						
						
									
										24
									
								
								user.go
									
									
									
									
									
								
							| @@ -27,6 +27,12 @@ type User struct { | ||||
| 	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 | ||||
| // | ||||
| // 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 | ||||
| } | ||||
|  | ||||
| // 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") | ||||
| 	} | ||||
| } | ||||
|  | ||||
| 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