2016-10-31 14:24:30 +02:00
|
|
|
package jira
|
|
|
|
|
|
|
|
import (
|
2020-05-03 15:38:32 +02:00
|
|
|
"context"
|
2016-10-31 14:24:30 +02:00
|
|
|
"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 {
|
2019-07-22 19:49:43 +02:00
|
|
|
Self string `json:"self,omitempty" structs:"self,omitempty"`
|
|
|
|
AccountID string `json:"accountId,omitempty" structs:"accountId,omitempty"`
|
|
|
|
AccountType string `json:"accountType,omitempty" structs:"accountType,omitempty"`
|
2019-04-16 16:44:10 +02:00
|
|
|
// TODO: name & key are deprecated, see:
|
|
|
|
// https://developer.atlassian.com/cloud/jira/platform/api-changes-for-user-privacy-announcement/
|
2016-10-31 14:24:30 +02:00
|
|
|
Name string `json:"name,omitempty" structs:"name,omitempty"`
|
|
|
|
Key string `json:"key,omitempty" structs:"key,omitempty"`
|
2019-04-16 16:44:10 +02:00
|
|
|
Password string `json:"-"`
|
2016-10-31 14:24:30 +02:00
|
|
|
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"`
|
2019-07-22 19:49:43 +02:00
|
|
|
Locale string `json:"locale,omitempty" structs:"locale,omitempty"`
|
2016-10-31 14:24:30 +02:00
|
|
|
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
|
|
|
|
|
2020-05-03 15:38:32 +02:00
|
|
|
// GetWithContext gets user info from JIRA
|
2016-10-31 14:24:30 +02:00
|
|
|
//
|
|
|
|
// JIRA API docs: https://docs.atlassian.com/jira/REST/cloud/#api/2/user-getUser
|
2020-03-10 18:03:59 +02:00
|
|
|
//
|
|
|
|
// /!\ Deprecation notice: https://developer.atlassian.com/cloud/jira/platform/deprecation-notice-user-privacy-api-migration-guide/
|
|
|
|
// By 29 April 2019, we will remove personal data from the API that is used to identify users,
|
|
|
|
// such as username and userKey, and instead use the Atlassian account ID (accountId).
|
2020-05-03 15:38:32 +02:00
|
|
|
func (s *UserService) GetWithContext(ctx context.Context, username string) (*User, *Response, error) {
|
2016-11-22 23:37:27 +02:00
|
|
|
apiEndpoint := fmt.Sprintf("/rest/api/2/user?username=%s", username)
|
2020-05-03 15:38:32 +02:00
|
|
|
req, err := s.client.NewRequestWithContext(ctx, "GET", apiEndpoint, nil)
|
2016-10-31 14:24:30 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-03 15:38:32 +02:00
|
|
|
// Get wraps GetWithContext using the background context.
|
|
|
|
func (s *UserService) Get(username string) (*User, *Response, error) {
|
|
|
|
return s.GetWithContext(context.Background(), username)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetByAccountIDWithContext gets user info from JIRA
|
2020-03-10 18:03:59 +02:00
|
|
|
//
|
|
|
|
// JIRA API docs: https://docs.atlassian.com/jira/REST/cloud/#api/2/user-getUser
|
2020-05-03 15:38:32 +02:00
|
|
|
func (s *UserService) GetByAccountIDWithContext(ctx context.Context, accountID string) (*User, *Response, error) {
|
2020-03-10 18:03:59 +02:00
|
|
|
apiEndpoint := fmt.Sprintf("/rest/api/2/user?accountId=%s", accountID)
|
2020-05-03 15:38:32 +02:00
|
|
|
req, err := s.client.NewRequestWithContext(ctx, "GET", apiEndpoint, nil)
|
2020-03-10 18:03:59 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
user := new(User)
|
|
|
|
resp, err := s.client.Do(req, user)
|
|
|
|
if err != nil {
|
|
|
|
return nil, resp, NewJiraError(resp, err)
|
|
|
|
}
|
|
|
|
return user, resp, nil
|
|
|
|
}
|
|
|
|
|
2020-05-03 15:38:32 +02:00
|
|
|
// GetByAccountID wraps GetByAccountIDWithContext using the background context.
|
|
|
|
func (s *UserService) GetByAccountID(accountID string) (*User, *Response, error) {
|
|
|
|
return s.GetByAccountIDWithContext(context.Background(), accountID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateWithContext creates an user in JIRA.
|
2016-10-31 14:24:30 +02:00
|
|
|
//
|
|
|
|
// JIRA API docs: https://docs.atlassian.com/jira/REST/cloud/#api/2/user-createUser
|
2020-05-03 15:38:32 +02:00
|
|
|
func (s *UserService) CreateWithContext(ctx context.Context, user *User) (*User, *Response, error) {
|
2016-10-31 14:24:30 +02:00
|
|
|
apiEndpoint := "/rest/api/2/user"
|
2020-05-03 15:38:32 +02:00
|
|
|
req, err := s.client.NewRequestWithContext(ctx, "POST", apiEndpoint, user)
|
2016-10-31 14:24:30 +02:00
|
|
|
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 {
|
style: Fix staticcheck (static analysis) errors for this library (#283)
* style: Fix staticcheck errors for "error strings should not be capitalized (ST1005)"
staticcheck is a static analysis tool for go.
It reports several "error strings should not be capitalized (ST1005)" messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "printf-style function with dynamic format ... (SA1006)"
staticcheck is a static analysis tool for go.
It reports several "printf-style function with dynamic format string and no further arguments should use print-style function instead (SA1006)" messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "type X is unused (U1000)"
staticcheck is a static analysis tool for go.
It reports several "type X is unused (U1000)" messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "should use X instead (S1003 & SA6005)"
staticcheck is a static analysis tool for go.
It reports several
- should use !bytes.Contains(b, []byte(`"password":"bar"`)) instead (S1003)
- should use strings.EqualFold instead (SA6005)
messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "unnecessary use of fmt.Sprintf (S1039)"
staticcheck is a static analysis tool for go.
It report several "unnecessary use of fmt.Sprintf (S1039)" messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "this value of X is never used (SA4006)"
staticcheck is a static analysis tool for go.
It report several "this value of X is never used (SA4006)" messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "redundant return statement (S1023)"
staticcheck is a static analysis tool for go.
It report several "redundant return statement (S1023)" messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "possible nil pointer dereference (SA5011)"
staticcheck is a static analysis tool for go.
It report several
file.go:Line:character: possible nil pointer dereference (SA5011)
file.go:Line:character: this check suggests that the pointer can be nil
messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "this value of X is never used (SA4006)"
staticcheck is a static analysis tool for go.
It report several "this value of X is never used (SA4006)" messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
2020-05-02 23:08:01 +02:00
|
|
|
e := fmt.Errorf("could not read the returned data")
|
2017-12-10 20:56:03 +02:00
|
|
|
return nil, resp, NewJiraError(resp, e)
|
2016-10-31 14:24:30 +02:00
|
|
|
}
|
|
|
|
err = json.Unmarshal(data, responseUser)
|
|
|
|
if err != nil {
|
style: Fix staticcheck (static analysis) errors for this library (#283)
* style: Fix staticcheck errors for "error strings should not be capitalized (ST1005)"
staticcheck is a static analysis tool for go.
It reports several "error strings should not be capitalized (ST1005)" messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "printf-style function with dynamic format ... (SA1006)"
staticcheck is a static analysis tool for go.
It reports several "printf-style function with dynamic format string and no further arguments should use print-style function instead (SA1006)" messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "type X is unused (U1000)"
staticcheck is a static analysis tool for go.
It reports several "type X is unused (U1000)" messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "should use X instead (S1003 & SA6005)"
staticcheck is a static analysis tool for go.
It reports several
- should use !bytes.Contains(b, []byte(`"password":"bar"`)) instead (S1003)
- should use strings.EqualFold instead (SA6005)
messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "unnecessary use of fmt.Sprintf (S1039)"
staticcheck is a static analysis tool for go.
It report several "unnecessary use of fmt.Sprintf (S1039)" messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "this value of X is never used (SA4006)"
staticcheck is a static analysis tool for go.
It report several "this value of X is never used (SA4006)" messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "redundant return statement (S1023)"
staticcheck is a static analysis tool for go.
It report several "redundant return statement (S1023)" messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "possible nil pointer dereference (SA5011)"
staticcheck is a static analysis tool for go.
It report several
file.go:Line:character: possible nil pointer dereference (SA5011)
file.go:Line:character: this check suggests that the pointer can be nil
messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
* style: Fix staticcheck errors for "this value of X is never used (SA4006)"
staticcheck is a static analysis tool for go.
It report several "this value of X is never used (SA4006)" messages.
Here, we fix it to be more compliant with the go coding styleguide.
Related: #280
2020-05-02 23:08:01 +02:00
|
|
|
e := fmt.Errorf("could not unmarshall the data into struct")
|
2017-12-10 20:56:03 +02:00
|
|
|
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
|
|
|
|
2020-05-03 15:38:32 +02:00
|
|
|
// Create wraps CreateWithContext using the background context.
|
|
|
|
func (s *UserService) Create(user *User) (*User, *Response, error) {
|
|
|
|
return s.CreateWithContext(context.Background(), user)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteWithContext deletes an user from JIRA.
|
2018-07-11 10:53:16 +02:00
|
|
|
// Returns http.StatusNoContent on success.
|
|
|
|
//
|
|
|
|
// JIRA API docs: https://developer.atlassian.com/cloud/jira/platform/rest/#api-api-2-user-delete
|
2020-05-03 15:38:32 +02:00
|
|
|
func (s *UserService) DeleteWithContext(ctx context.Context, username string) (*Response, error) {
|
2018-07-11 10:53:16 +02:00
|
|
|
apiEndpoint := fmt.Sprintf("/rest/api/2/user?username=%s", username)
|
2020-05-03 15:38:32 +02:00
|
|
|
req, err := s.client.NewRequestWithContext(ctx, "DELETE", apiEndpoint, nil)
|
2018-07-11 10:53:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := s.client.Do(req, nil)
|
|
|
|
if err != nil {
|
|
|
|
return resp, NewJiraError(resp, err)
|
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
2020-05-03 15:38:32 +02:00
|
|
|
// Delete wraps DeleteWithContext using the background context.
|
|
|
|
func (s *UserService) Delete(username string) (*Response, error) {
|
|
|
|
return s.DeleteWithContext(context.Background(), username)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetGroupsWithContext returns the groups which the user belongs to
|
2017-11-28 04:56:49 +02:00
|
|
|
//
|
|
|
|
// JIRA API docs: https://docs.atlassian.com/jira/REST/cloud/#api/2/user-getUserGroups
|
2020-05-03 15:38:32 +02:00
|
|
|
func (s *UserService) GetGroupsWithContext(ctx context.Context, username string) (*[]UserGroup, *Response, error) {
|
2017-11-28 04:56:49 +02:00
|
|
|
apiEndpoint := fmt.Sprintf("/rest/api/2/user/groups?username=%s", username)
|
2020-05-03 15:38:32 +02:00
|
|
|
req, err := s.client.NewRequestWithContext(ctx, "GET", apiEndpoint, nil)
|
2017-11-28 04:56:49 +02:00
|
|
|
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
|
|
|
|
2020-05-03 15:38:32 +02:00
|
|
|
// GetGroups wraps GetGroupsWithContext using the background context.
|
|
|
|
func (s *UserService) GetGroups(username string) (*[]UserGroup, *Response, error) {
|
|
|
|
return s.GetGroupsWithContext(context.Background(), username)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSelfWithContext information about the current logged-in user
|
2018-06-29 00:44:35 +02:00
|
|
|
//
|
|
|
|
// JIRA API docs: https://developer.atlassian.com/cloud/jira/platform/rest/#api-api-2-myself-get
|
2020-05-03 15:38:32 +02:00
|
|
|
func (s *UserService) GetSelfWithContext(ctx context.Context) (*User, *Response, error) {
|
2018-06-29 00:44:35 +02:00
|
|
|
const apiEndpoint = "rest/api/2/myself"
|
2020-05-03 15:38:32 +02:00
|
|
|
req, err := s.client.NewRequestWithContext(ctx, "GET", apiEndpoint, nil)
|
2018-06-29 00:44:35 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-03 15:38:32 +02:00
|
|
|
// GetSelf wraps GetSelfWithContext using the background context.
|
|
|
|
func (s *UserService) GetSelf() (*User, *Response, error) {
|
|
|
|
return s.GetSelfWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-03 15:38:32 +02:00
|
|
|
// FindWithContext 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
|
2020-05-03 15:38:32 +02:00
|
|
|
func (s *UserService) FindWithContext(ctx context.Context, property string, tweaks ...userSearchF) ([]User, *Response, error) {
|
2018-07-03 04:11:58 +02:00
|
|
|
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])
|
2020-05-03 15:38:32 +02:00
|
|
|
req, err := s.client.NewRequestWithContext(ctx, "GET", apiEndpoint, nil)
|
2017-11-27 19:25:08 +02:00
|
|
|
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
|
|
|
|
}
|
2020-05-03 15:38:32 +02:00
|
|
|
|
|
|
|
// Find wraps FindWithContext using the background context.
|
|
|
|
func (s *UserService) Find(property string, tweaks ...userSearchF) ([]User, *Response, error) {
|
|
|
|
return s.FindWithContext(context.Background(), property, tweaks...)
|
|
|
|
}
|