/
field_impl.go
190 lines (152 loc) · 5.6 KB
/
field_impl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package internal
import (
"context"
"fmt"
model "github.com/ctreminiom/go-atlassian/pkg/infra/models"
"github.com/ctreminiom/go-atlassian/service"
"github.com/ctreminiom/go-atlassian/service/jira"
"net/http"
"net/url"
"strconv"
"strings"
)
func NewIssueFieldService(client service.Connector, version string, configuration *IssueFieldConfigService, context *IssueFieldContextService,
trash *IssueFieldTrashService) (*IssueFieldService, error) {
if version == "" {
return nil, model.ErrNoVersionProvided
}
return &IssueFieldService{
internalClient: &internalIssueFieldServiceImpl{c: client, version: version},
Configuration: configuration,
Context: context,
Trash: trash,
}, nil
}
type IssueFieldService struct {
internalClient jira.FieldConnector
Configuration *IssueFieldConfigService
Context *IssueFieldContextService
Trash *IssueFieldTrashService
}
// Gets returns system and custom issue fields according to the following rules:
//
// 1. Fields that cannot be added to the issue navigator are always returned.
//
// 2. Fields that cannot be placed on an issue screen are always returned.
//
// 3. Fields that depend on global Jira settings are only returned if the setting is enabled.
// That is, timetracking fields, subtasks, votes, and watches.
//
// 4. For all other fields, this operation only returns the fields that the user has permission to view
//
// GET /rest/api/{2-3}/field
//
// https://docs.go-atlassian.io/jira-software-cloud/issues/fields#get-fields
func (i *IssueFieldService) Gets(ctx context.Context) ([]*model.IssueFieldScheme, *model.ResponseScheme, error) {
return i.internalClient.Gets(ctx)
}
// Create creates a custom field.
//
// POST /rest/api/{2-3}/field
//
// https://docs.go-atlassian.io/jira-software-cloud/issues/fields#create-custom-field
func (i *IssueFieldService) Create(ctx context.Context, payload *model.CustomFieldScheme) (*model.IssueFieldScheme, *model.ResponseScheme, error) {
return i.internalClient.Create(ctx, payload)
}
// Search returns a paginated list of fields for Classic Jira projects.
//
// GET /rest/api/{2-3}/field/search
//
// https://docs.go-atlassian.io/jira-software-cloud/issues/fields#get-fields-paginated
func (i *IssueFieldService) Search(ctx context.Context, options *model.FieldSearchOptionsScheme, startAt, maxResults int) (*model.FieldSearchPageScheme, *model.ResponseScheme, error) {
return i.internalClient.Search(ctx, options, startAt, maxResults)
}
// Delete deletes a custom field. The custom field is deleted whether it is in the trash or not.
//
// See Edit or delete a custom field for more information on trashing and deleting custom fields.
//
// DELETE /rest/api/{2-3}/field/{id}
//
// https://docs.go-atlassian.io/jira-software-cloud/issues/fields#delete-field
func (i *IssueFieldService) Delete(ctx context.Context, fieldId string) (*model.TaskScheme, *model.ResponseScheme, error) {
return i.internalClient.Delete(ctx, fieldId)
}
type internalIssueFieldServiceImpl struct {
c service.Connector
version string
}
func (i *internalIssueFieldServiceImpl) Gets(ctx context.Context) ([]*model.IssueFieldScheme, *model.ResponseScheme, error) {
endpoint := fmt.Sprintf("rest/api/%v/field", i.version)
request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil)
if err != nil {
return nil, nil, err
}
var fields []*model.IssueFieldScheme
response, err := i.c.Call(request, &fields)
if err != nil {
return nil, response, err
}
return fields, response, nil
}
func (i *internalIssueFieldServiceImpl) Create(ctx context.Context, payload *model.CustomFieldScheme) (*model.IssueFieldScheme, *model.ResponseScheme, error) {
endpoint := fmt.Sprintf("rest/api/%v/field", i.version)
request, err := i.c.NewRequest(ctx, http.MethodPost, endpoint, "", payload)
if err != nil {
return nil, nil, err
}
field := new(model.IssueFieldScheme)
response, err := i.c.Call(request, field)
if err != nil {
return nil, response, err
}
return field, response, nil
}
func (i *internalIssueFieldServiceImpl) Search(ctx context.Context, options *model.FieldSearchOptionsScheme, startAt, maxResults int) (*model.FieldSearchPageScheme, *model.ResponseScheme, error) {
params := url.Values{}
params.Add("startAt", strconv.Itoa(startAt))
params.Add("maxResults", strconv.Itoa(maxResults))
if options != nil {
if len(options.Expand) != 0 {
params.Add("expand", strings.Join(options.Expand, ","))
}
if len(options.Types) != 0 {
params.Add("type", strings.Join(options.Types, ","))
}
if len(options.IDs) != 0 {
params.Add("id", strings.Join(options.IDs, ","))
}
if len(options.OrderBy) != 0 {
params.Add("orderBy", options.OrderBy)
}
if len(options.Query) != 0 {
params.Add("query", options.Query)
}
}
endpoint := fmt.Sprintf("rest/api/%v/field/search?%v", i.version, params.Encode())
request, err := i.c.NewRequest(ctx, http.MethodGet, endpoint, "", nil)
if err != nil {
return nil, nil, err
}
page := new(model.FieldSearchPageScheme)
response, err := i.c.Call(request, page)
if err != nil {
return nil, response, err
}
return page, response, nil
}
func (i *internalIssueFieldServiceImpl) Delete(ctx context.Context, fieldId string) (*model.TaskScheme, *model.ResponseScheme, error) {
if fieldId == "" {
return nil, nil, model.ErrNoFieldIDError
}
endpoint := fmt.Sprintf("rest/api/%v/field/%v", i.version, fieldId)
request, err := i.c.NewRequest(ctx, http.MethodDelete, endpoint, "", nil)
if err != nil {
return nil, nil, err
}
task := new(model.TaskScheme)
response, err := i.c.Call(request, task)
if err != nil {
return nil, response, err
}
return task, response, nil
}