2020-09-14 18:31:56 +00:00
|
|
|
package jira
|
|
|
|
|
|
|
|
import (
|
2021-10-06 17:33:14 +00:00
|
|
|
"context"
|
2020-09-14 18:31:56 +00:00
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2021-10-06 17:33:14 +00:00
|
|
|
// RoleService handles roles for the Jira instance / API.
|
2020-09-14 18:31:56 +00:00
|
|
|
//
|
2021-10-06 17:33:14 +00:00
|
|
|
// Jira API docs: https://developer.atlassian.com/cloud/jira/platform/rest/v3/#api-group-Role
|
2020-09-14 18:31:56 +00:00
|
|
|
type RoleService struct {
|
|
|
|
client *Client
|
|
|
|
}
|
|
|
|
|
2021-10-06 17:33:14 +00:00
|
|
|
// Role represents a Jira product role
|
2020-09-14 18:31:56 +00:00
|
|
|
type Role struct {
|
|
|
|
Self string `json:"self" structs:"self"`
|
|
|
|
Name string `json:"name" structs:"name"`
|
|
|
|
ID int `json:"id" structs:"id"`
|
|
|
|
Description string `json:"description" structs:"description"`
|
|
|
|
Actors []*Actor `json:"actors" structs:"actors"`
|
|
|
|
}
|
|
|
|
|
2021-10-06 17:33:14 +00:00
|
|
|
// Actor represents a Jira actor
|
2020-09-14 18:31:56 +00:00
|
|
|
type Actor struct {
|
|
|
|
ID int `json:"id" structs:"id"`
|
|
|
|
DisplayName string `json:"displayName" structs:"displayName"`
|
|
|
|
Type string `json:"type" structs:"type"`
|
|
|
|
Name string `json:"name" structs:"name"`
|
|
|
|
AvatarURL string `json:"avatarUrl" structs:"avatarUrl"`
|
|
|
|
ActorUser *ActorUser `json:"actorUser" structs:"actoruser"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ActorUser contains the account id of the actor/user
|
|
|
|
type ActorUser struct {
|
|
|
|
AccountID string `json:"accountId" structs:"accountId"`
|
|
|
|
}
|
|
|
|
|
2021-10-06 17:33:14 +00:00
|
|
|
// GetListWithContext returns a list of all available project roles
|
2020-09-14 18:31:56 +00:00
|
|
|
//
|
2021-10-06 17:33:14 +00:00
|
|
|
// Jira API docs: https://developer.atlassian.com/cloud/jira/platform/rest/v3/#api-api-3-role-get
|
|
|
|
func (s *RoleService) GetListWithContext(ctx context.Context) (*[]Role, *Response, error) {
|
2020-09-14 18:31:56 +00:00
|
|
|
apiEndpoint := "rest/api/3/role"
|
2021-10-06 17:33:14 +00:00
|
|
|
req, err := s.client.NewRequestWithContext(ctx, "GET", apiEndpoint, nil)
|
2020-09-14 18:31:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
roles := new([]Role)
|
|
|
|
resp, err := s.client.Do(req, roles)
|
|
|
|
if err != nil {
|
|
|
|
jerr := NewJiraError(resp, err)
|
|
|
|
return nil, resp, jerr
|
|
|
|
}
|
|
|
|
return roles, resp, err
|
|
|
|
}
|
|
|
|
|
2021-10-06 17:33:14 +00:00
|
|
|
// GetList wraps GetListWithContext using the background context.
|
|
|
|
func (s *RoleService) GetList() (*[]Role, *Response, error) {
|
|
|
|
return s.GetListWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetWithContext retreives a single Role from Jira
|
2020-09-14 18:31:56 +00:00
|
|
|
//
|
2021-10-06 17:33:14 +00:00
|
|
|
// Jira API docs: https://developer.atlassian.com/cloud/jira/platform/rest/v3/#api-api-3-role-id-get
|
|
|
|
func (s *RoleService) GetWithContext(ctx context.Context, roleID int) (*Role, *Response, error) {
|
2020-09-14 18:31:56 +00:00
|
|
|
apiEndpoint := fmt.Sprintf("rest/api/3/role/%d", roleID)
|
2021-10-06 17:33:14 +00:00
|
|
|
req, err := s.client.NewRequestWithContext(ctx, "GET", apiEndpoint, nil)
|
2020-09-14 18:31:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
role := new(Role)
|
|
|
|
resp, err := s.client.Do(req, role)
|
|
|
|
if err != nil {
|
|
|
|
jerr := NewJiraError(resp, err)
|
|
|
|
return nil, resp, jerr
|
|
|
|
}
|
|
|
|
if role.Self == "" {
|
2021-10-06 17:33:14 +00:00
|
|
|
return nil, resp, fmt.Errorf("no role with ID %d found", roleID)
|
2020-09-14 18:31:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return role, resp, err
|
|
|
|
}
|
2021-10-06 17:33:14 +00:00
|
|
|
|
|
|
|
// Get wraps GetWithContext using the background context.
|
|
|
|
func (s *RoleService) Get(roleID int) (*Role, *Response, error) {
|
|
|
|
return s.GetWithContext(context.Background(), roleID)
|
|
|
|
}
|