2016-05-29 18:42:38 +02:00
|
|
|
package jira
|
|
|
|
|
|
|
|
import (
|
2020-05-03 15:38:32 +02:00
|
|
|
"context"
|
2016-06-01 14:13:11 +02:00
|
|
|
"fmt"
|
2018-01-31 17:34:47 +02:00
|
|
|
|
|
|
|
"github.com/google/go-querystring/query"
|
2016-05-29 18:42:38 +02:00
|
|
|
)
|
|
|
|
|
2020-05-14 17:18:31 +02:00
|
|
|
// ProjectService handles projects for the Jira instance / API.
|
2016-06-03 23:25:18 +02:00
|
|
|
//
|
2020-05-14 17:18:31 +02:00
|
|
|
// Jira API docs: https://docs.atlassian.com/jira/REST/latest/#api/2/project
|
2016-05-29 18:42:38 +02:00
|
|
|
type ProjectService struct {
|
|
|
|
client *Client
|
|
|
|
}
|
|
|
|
|
2016-06-03 23:25:18 +02:00
|
|
|
// ProjectList represent a list of Projects
|
2016-05-29 18:42:38 +02:00
|
|
|
type ProjectList []struct {
|
2016-09-23 16:19:07 +02:00
|
|
|
Expand string `json:"expand" structs:"expand"`
|
|
|
|
Self string `json:"self" structs:"self"`
|
|
|
|
ID string `json:"id" structs:"id"`
|
|
|
|
Key string `json:"key" structs:"key"`
|
|
|
|
Name string `json:"name" structs:"name"`
|
|
|
|
AvatarUrls AvatarUrls `json:"avatarUrls" structs:"avatarUrls"`
|
|
|
|
ProjectTypeKey string `json:"projectTypeKey" structs:"projectTypeKey"`
|
|
|
|
ProjectCategory ProjectCategory `json:"projectCategory,omitempty" structs:"projectsCategory,omitempty"`
|
2018-01-31 18:25:35 +02:00
|
|
|
IssueTypes []IssueType `json:"issueTypes,omitempty" structs:"issueTypes,omitempty"`
|
2016-05-29 18:42:38 +02:00
|
|
|
}
|
|
|
|
|
2016-06-03 23:25:18 +02:00
|
|
|
// ProjectCategory represents a single project category
|
2016-06-03 23:14:27 +02:00
|
|
|
type ProjectCategory struct {
|
2016-09-26 17:59:51 +02:00
|
|
|
Self string `json:"self" structs:"self,omitempty"`
|
|
|
|
ID string `json:"id" structs:"id,omitempty"`
|
|
|
|
Name string `json:"name" structs:"name,omitempty"`
|
|
|
|
Description string `json:"description" structs:"description,omitempty"`
|
2016-06-03 23:14:27 +02:00
|
|
|
}
|
|
|
|
|
2020-05-14 17:18:31 +02:00
|
|
|
// Project represents a Jira Project.
|
2016-06-03 23:25:18 +02:00
|
|
|
type Project struct {
|
2018-10-12 11:53:03 +02:00
|
|
|
Expand string `json:"expand,omitempty" structs:"expand,omitempty"`
|
|
|
|
Self string `json:"self,omitempty" structs:"self,omitempty"`
|
|
|
|
ID string `json:"id,omitempty" structs:"id,omitempty"`
|
|
|
|
Key string `json:"key,omitempty" structs:"key,omitempty"`
|
|
|
|
Description string `json:"description,omitempty" structs:"description,omitempty"`
|
|
|
|
Lead User `json:"lead,omitempty" structs:"lead,omitempty"`
|
|
|
|
Components []ProjectComponent `json:"components,omitempty" structs:"components,omitempty"`
|
|
|
|
IssueTypes []IssueType `json:"issueTypes,omitempty" structs:"issueTypes,omitempty"`
|
|
|
|
URL string `json:"url,omitempty" structs:"url,omitempty"`
|
|
|
|
Email string `json:"email,omitempty" structs:"email,omitempty"`
|
|
|
|
AssigneeType string `json:"assigneeType,omitempty" structs:"assigneeType,omitempty"`
|
|
|
|
Versions []Version `json:"versions,omitempty" structs:"versions,omitempty"`
|
|
|
|
Name string `json:"name,omitempty" structs:"name,omitempty"`
|
|
|
|
Roles map[string]string `json:"roles,omitempty" structs:"roles,omitempty"`
|
|
|
|
AvatarUrls AvatarUrls `json:"avatarUrls,omitempty" structs:"avatarUrls,omitempty"`
|
|
|
|
ProjectCategory ProjectCategory `json:"projectCategory,omitempty" structs:"projectCategory,omitempty"`
|
2016-06-03 23:14:27 +02:00
|
|
|
}
|
|
|
|
|
2016-06-03 23:25:18 +02:00
|
|
|
// ProjectComponent represents a single component of a project
|
2016-06-03 23:14:27 +02:00
|
|
|
type ProjectComponent struct {
|
2016-09-26 17:59:51 +02:00
|
|
|
Self string `json:"self" structs:"self,omitempty"`
|
|
|
|
ID string `json:"id" structs:"id,omitempty"`
|
|
|
|
Name string `json:"name" structs:"name,omitempty"`
|
|
|
|
Description string `json:"description" structs:"description,omitempty"`
|
|
|
|
Lead User `json:"lead,omitempty" structs:"lead,omitempty"`
|
|
|
|
AssigneeType string `json:"assigneeType" structs:"assigneeType,omitempty"`
|
|
|
|
Assignee User `json:"assignee" structs:"assignee,omitempty"`
|
|
|
|
RealAssigneeType string `json:"realAssigneeType" structs:"realAssigneeType,omitempty"`
|
|
|
|
RealAssignee User `json:"realAssignee" structs:"realAssignee,omitempty"`
|
|
|
|
IsAssigneeTypeValid bool `json:"isAssigneeTypeValid" structs:"isAssigneeTypeValid,omitempty"`
|
|
|
|
Project string `json:"project" structs:"project,omitempty"`
|
|
|
|
ProjectID int `json:"projectId" structs:"projectId,omitempty"`
|
2016-06-01 14:13:11 +02:00
|
|
|
}
|
|
|
|
|
2018-05-12 09:28:05 +02:00
|
|
|
// PermissionScheme represents the permission scheme for the project
|
|
|
|
type PermissionScheme struct {
|
2018-11-20 13:54:02 +02:00
|
|
|
Expand string `json:"expand" structs:"expand,omitempty"`
|
|
|
|
Self string `json:"self" structs:"self,omitempty"`
|
|
|
|
ID int `json:"id" structs:"id,omitempty"`
|
|
|
|
Name string `json:"name" structs:"name,omitempty"`
|
|
|
|
Description string `json:"description" structs:"description,omitempty"`
|
|
|
|
Permissions []Permission `json:"permissions" structs:"permissions,omitempty"`
|
2018-05-12 09:28:05 +02:00
|
|
|
}
|
|
|
|
|
2020-05-14 17:18:31 +02:00
|
|
|
// GetListWithContext gets all projects form Jira
|
2016-06-01 14:13:11 +02:00
|
|
|
//
|
2020-05-14 17:18:31 +02:00
|
|
|
// Jira API docs: https://docs.atlassian.com/jira/REST/latest/#api/2/project-getAllProjects
|
2020-05-03 15:38:32 +02:00
|
|
|
func (s *ProjectService) GetListWithContext(ctx context.Context) (*ProjectList, *Response, error) {
|
|
|
|
return s.ListWithOptionsWithContext(ctx, &GetQueryOptions{})
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetList wraps GetListWithContext using the background context.
|
2016-06-15 17:09:13 +02:00
|
|
|
func (s *ProjectService) GetList() (*ProjectList, *Response, error) {
|
2020-05-03 15:38:32 +02:00
|
|
|
return s.GetListWithContext(context.Background())
|
2018-01-31 17:34:47 +02:00
|
|
|
}
|
|
|
|
|
2020-05-14 17:18:31 +02:00
|
|
|
// ListWithOptionsWithContext gets all projects form Jira with optional query params, like &GetQueryOptions{Expand: "issueTypes"} to get
|
2018-01-31 17:34:47 +02:00
|
|
|
// a list of all projects and their supported issuetypes
|
|
|
|
//
|
2020-05-14 17:18:31 +02:00
|
|
|
// Jira API docs: https://docs.atlassian.com/jira/REST/latest/#api/2/project-getAllProjects
|
2020-05-03 15:38:32 +02:00
|
|
|
func (s *ProjectService) ListWithOptionsWithContext(ctx context.Context, options *GetQueryOptions) (*ProjectList, *Response, error) {
|
2016-05-29 18:42:38 +02:00
|
|
|
apiEndpoint := "rest/api/2/project"
|
2020-05-03 15:38:32 +02:00
|
|
|
req, err := s.client.NewRequestWithContext(ctx, "GET", apiEndpoint, nil)
|
2016-05-29 18:42:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2018-01-31 17:34:47 +02:00
|
|
|
if options != nil {
|
|
|
|
q, err := query.Values(options)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
req.URL.RawQuery = q.Encode()
|
|
|
|
}
|
|
|
|
|
2016-05-29 18:42:38 +02:00
|
|
|
projectList := new(ProjectList)
|
|
|
|
resp, err := s.client.Do(req, projectList)
|
|
|
|
if err != nil {
|
2017-11-04 00:22:32 +02:00
|
|
|
jerr := NewJiraError(resp, err)
|
|
|
|
return nil, resp, jerr
|
2016-05-29 18:42:38 +02:00
|
|
|
}
|
2017-11-04 00:22:32 +02:00
|
|
|
|
2016-05-29 18:42:38 +02:00
|
|
|
return projectList, resp, nil
|
|
|
|
}
|
2016-06-01 14:13:11 +02:00
|
|
|
|
2020-05-03 15:38:32 +02:00
|
|
|
// ListWithOptions wraps ListWithOptionsWithContext using the background context.
|
|
|
|
func (s *ProjectService) ListWithOptions(options *GetQueryOptions) (*ProjectList, *Response, error) {
|
|
|
|
return s.ListWithOptionsWithContext(context.Background(), options)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetWithContext returns a full representation of the project for the given issue key.
|
2020-05-14 17:18:31 +02:00
|
|
|
// Jira will attempt to identify the project by the projectIdOrKey path parameter.
|
2016-06-01 14:13:11 +02:00
|
|
|
// This can be an project id, or an project key.
|
|
|
|
//
|
2020-05-14 17:18:31 +02:00
|
|
|
// Jira API docs: https://docs.atlassian.com/jira/REST/latest/#api/2/project-getProject
|
2020-05-03 15:38:32 +02:00
|
|
|
func (s *ProjectService) GetWithContext(ctx context.Context, projectID string) (*Project, *Response, error) {
|
2017-02-20 17:09:50 +02:00
|
|
|
apiEndpoint := fmt.Sprintf("rest/api/2/project/%s", projectID)
|
2020-05-03 15:38:32 +02:00
|
|
|
req, err := s.client.NewRequestWithContext(ctx, "GET", apiEndpoint, nil)
|
2016-06-01 14:13:11 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2016-06-03 23:25:18 +02:00
|
|
|
project := new(Project)
|
2016-06-01 14:13:11 +02:00
|
|
|
resp, err := s.client.Do(req, project)
|
|
|
|
if err != nil {
|
2017-11-04 00:22:32 +02:00
|
|
|
jerr := NewJiraError(resp, err)
|
|
|
|
return nil, resp, jerr
|
2016-06-01 14:13:11 +02:00
|
|
|
}
|
2017-11-04 00:22:32 +02:00
|
|
|
|
2016-06-01 14:13:11 +02:00
|
|
|
return project, resp, nil
|
|
|
|
}
|
2018-05-12 09:28:05 +02:00
|
|
|
|
2020-05-03 15:38:32 +02:00
|
|
|
// Get wraps GetWithContext using the background context.
|
|
|
|
func (s *ProjectService) Get(projectID string) (*Project, *Response, error) {
|
|
|
|
return s.GetWithContext(context.Background(), projectID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetPermissionSchemeWithContext returns a full representation of the permission scheme for the project
|
2020-05-14 17:18:31 +02:00
|
|
|
// Jira will attempt to identify the project by the projectIdOrKey path parameter.
|
2018-05-12 09:28:05 +02:00
|
|
|
// This can be an project id, or an project key.
|
|
|
|
//
|
2020-05-14 17:18:31 +02:00
|
|
|
// Jira API docs: https://docs.atlassian.com/jira/REST/latest/#api/2/project-getProject
|
2020-05-03 15:38:32 +02:00
|
|
|
func (s *ProjectService) GetPermissionSchemeWithContext(ctx context.Context, projectID string) (*PermissionScheme, *Response, error) {
|
2018-05-12 09:28:05 +02:00
|
|
|
apiEndpoint := fmt.Sprintf("/rest/api/2/project/%s/permissionscheme", projectID)
|
2020-05-03 15:38:32 +02:00
|
|
|
req, err := s.client.NewRequestWithContext(ctx, "GET", apiEndpoint, nil)
|
2018-05-12 09:28:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ps := new(PermissionScheme)
|
|
|
|
resp, err := s.client.Do(req, ps)
|
|
|
|
if err != nil {
|
|
|
|
jerr := NewJiraError(resp, err)
|
|
|
|
return nil, resp, jerr
|
|
|
|
}
|
|
|
|
|
|
|
|
return ps, resp, nil
|
|
|
|
}
|
2020-05-03 15:38:32 +02:00
|
|
|
|
|
|
|
// GetPermissionScheme wraps GetPermissionSchemeWithContext using the background context.
|
|
|
|
func (s *ProjectService) GetPermissionScheme(projectID string) (*PermissionScheme, *Response, error) {
|
|
|
|
return s.GetPermissionSchemeWithContext(context.Background(), projectID)
|
|
|
|
}
|