2016-10-31 14:24:30 +02:00
|
|
|
package jira
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
// UserService handles users for the JIRA instance / API.
|
|
|
|
//
|
|
|
|
// JIRA API docs: https://docs.atlassian.com/jira/REST/cloud/#api/2/user
|
|
|
|
type UserService struct {
|
|
|
|
client *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// User represents a JIRA user.
|
|
|
|
type User struct {
|
|
|
|
Self string `json:"self,omitempty" structs:"self,omitempty"`
|
|
|
|
Name string `json:"name,omitempty" structs:"name,omitempty"`
|
|
|
|
Password string `json:"-"`
|
|
|
|
Key string `json:"key,omitempty" structs:"key,omitempty"`
|
|
|
|
EmailAddress string `json:"emailAddress,omitempty" structs:"emailAddress,omitempty"`
|
|
|
|
AvatarUrls AvatarUrls `json:"avatarUrls,omitempty" structs:"avatarUrls,omitempty"`
|
|
|
|
DisplayName string `json:"displayName,omitempty" structs:"displayName,omitempty"`
|
|
|
|
Active bool `json:"active,omitempty" structs:"active,omitempty"`
|
|
|
|
TimeZone string `json:"timeZone,omitempty" structs:"timeZone,omitempty"`
|
|
|
|
ApplicationKeys []string `json:"applicationKeys,omitempty" structs:"applicationKeys,omitempty"`
|
|
|
|
}
|
|
|
|
|
2017-11-28 04:56:49 +02:00
|
|
|
// UserGroup represents the group list
|
|
|
|
type UserGroup struct {
|
|
|
|
Self string `json:"self,omitempty" structs:"self,omitempty"`
|
|
|
|
Name string `json:"name,omitempty" structs:"name,omitempty"`
|
|
|
|
}
|
|
|
|
|
2018-07-03 04:11:58 +02:00
|
|
|
type userSearchParam struct {
|
|
|
|
name string
|
|
|
|
value string
|
|
|
|
}
|
|
|
|
|
|
|
|
type userSearch []userSearchParam
|
|
|
|
|
|
|
|
type userSearchF func(userSearch) userSearch
|
|
|
|
|
2016-10-31 14:24:30 +02:00
|
|
|
// Get gets user info from JIRA
|
|
|
|
//
|
|
|
|
// JIRA API docs: https://docs.atlassian.com/jira/REST/cloud/#api/2/user-getUser
|
|
|
|
func (s *UserService) Get(username string) (*User, *Response, error) {
|
2016-11-22 23:37:27 +02:00
|
|
|
apiEndpoint := fmt.Sprintf("/rest/api/2/user?username=%s", username)
|
2016-10-31 14:24:30 +02:00
|
|
|
req, err := s.client.NewRequest("GET", apiEndpoint, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
user := new(User)
|
|
|
|
resp, err := s.client.Do(req, user)
|
|
|
|
if err != nil {
|
2017-12-10 20:56:03 +02:00
|
|
|
return nil, resp, NewJiraError(resp, err)
|
2016-10-31 14:24:30 +02:00
|
|
|
}
|
|
|
|
return user, resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create creates an user in JIRA.
|
|
|
|
//
|
|
|
|
// JIRA API docs: https://docs.atlassian.com/jira/REST/cloud/#api/2/user-createUser
|
|
|
|
func (s *UserService) Create(user *User) (*User, *Response, error) {
|
|
|
|
apiEndpoint := "/rest/api/2/user"
|
|
|
|
req, err := s.client.NewRequest("POST", apiEndpoint, user)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := s.client.Do(req, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, err
|
|
|
|
}
|
|
|
|
|
|
|
|
responseUser := new(User)
|
|
|
|
defer resp.Body.Close()
|
|
|
|
data, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2017-12-10 20:56:03 +02:00
|
|
|
e := fmt.Errorf("Could not read the returned data")
|
|
|
|
return nil, resp, NewJiraError(resp, e)
|
2016-10-31 14:24:30 +02:00
|
|
|
}
|
|
|
|
err = json.Unmarshal(data, responseUser)
|
|
|
|
if err != nil {
|
2017-12-10 20:56:03 +02:00
|
|
|
e := fmt.Errorf("Could not unmarshall the data into struct")
|
|
|
|
return nil, resp, NewJiraError(resp, e)
|
2016-10-31 14:24:30 +02:00
|
|
|
}
|
|
|
|
return responseUser, resp, nil
|
|
|
|
}
|
2017-11-28 04:56:49 +02:00
|
|
|
|
2018-07-11 10:53:16 +02:00
|
|
|
// Delete deletes an user from JIRA.
|
|
|
|
// Returns http.StatusNoContent on success.
|
|
|
|
//
|
|
|
|
// JIRA API docs: https://developer.atlassian.com/cloud/jira/platform/rest/#api-api-2-user-delete
|
|
|
|
func (s *UserService) Delete(username string) (*Response, error) {
|
|
|
|
apiEndpoint := fmt.Sprintf("/rest/api/2/user?username=%s", 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 {
|
|
|
|
return resp, NewJiraError(resp, err)
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2017-11-28 04:56:49 +02:00
|
|
|
// 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 {
|
2017-12-10 20:56:03 +02:00
|
|
|
return nil, resp, NewJiraError(resp, err)
|
2017-11-28 04:56:49 +02:00
|
|
|
}
|
|
|
|
return userGroups, resp, nil
|
|
|
|
}
|
2017-12-10 20:54:09 +02:00
|
|
|
|
2018-06-29 00:44:35 +02:00
|
|
|
// Get information about the current logged-in user
|
|
|
|
//
|
|
|
|
// JIRA API docs: https://developer.atlassian.com/cloud/jira/platform/rest/#api-api-2-myself-get
|
|
|
|
func (s *UserService) GetSelf() (*User, *Response, error) {
|
|
|
|
const apiEndpoint = "rest/api/2/myself"
|
|
|
|
req, err := s.client.NewRequest("GET", apiEndpoint, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
var user User
|
|
|
|
resp, err := s.client.Do(req, &user)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, NewJiraError(resp, err)
|
|
|
|
}
|
|
|
|
return &user, resp, nil
|
|
|
|
}
|
|
|
|
|
2018-07-03 04:11:58 +02:00
|
|
|
// WithMaxResults sets the max results to return
|
|
|
|
func WithMaxResults(maxResults int) userSearchF {
|
|
|
|
return func(s userSearch) userSearch {
|
|
|
|
s = append(s, userSearchParam{name: "maxResults", value: fmt.Sprintf("%d", maxResults)})
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithStartAt set the start pager
|
|
|
|
func WithStartAt(startAt int) userSearchF {
|
|
|
|
return func(s userSearch) userSearch {
|
|
|
|
s = append(s, userSearchParam{name: "startAt", value: fmt.Sprintf("%d", startAt)})
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithActive sets the active users lookup
|
|
|
|
func WithActive(active bool) userSearchF {
|
|
|
|
return func(s userSearch) userSearch {
|
|
|
|
s = append(s, userSearchParam{name: "includeActive", value: fmt.Sprintf("%t", active)})
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithInactive sets the inactive users lookup
|
|
|
|
func WithInactive(inactive bool) userSearchF {
|
|
|
|
return func(s userSearch) userSearch {
|
|
|
|
s = append(s, userSearchParam{name: "includeInactive", value: fmt.Sprintf("%t", inactive)})
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-01 08:46:54 +02:00
|
|
|
// Find searches for user info from JIRA:
|
2017-12-10 20:54:09 +02:00
|
|
|
// It can find users by email, username or name
|
2017-11-27 19:25:08 +02:00
|
|
|
//
|
|
|
|
// JIRA API docs: https://docs.atlassian.com/jira/REST/cloud/#api/2/user-findUsers
|
2018-07-03 04:11:58 +02:00
|
|
|
func (s *UserService) Find(property string, tweaks ...userSearchF) ([]User, *Response, error) {
|
|
|
|
search := []userSearchParam{
|
|
|
|
{
|
|
|
|
name: "username",
|
|
|
|
value: property,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, f := range tweaks {
|
|
|
|
search = f(search)
|
|
|
|
}
|
|
|
|
|
|
|
|
var queryString = ""
|
|
|
|
for _, param := range search {
|
|
|
|
queryString += param.name + "=" + param.value + "&"
|
|
|
|
}
|
|
|
|
|
2019-01-14 13:48:03 +02:00
|
|
|
apiEndpoint := fmt.Sprintf("/rest/api/2/user/search?%s", queryString[:len(queryString)-1])
|
2017-11-27 19:25:08 +02:00
|
|
|
req, err := s.client.NewRequest("GET", apiEndpoint, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
users := []User{}
|
|
|
|
resp, err := s.client.Do(req, &users)
|
|
|
|
if err != nil {
|
2017-12-10 20:56:03 +02:00
|
|
|
return nil, resp, NewJiraError(resp, err)
|
2017-11-27 19:25:08 +02:00
|
|
|
}
|
|
|
|
return users, resp, nil
|
|
|
|
}
|