-
Notifications
You must be signed in to change notification settings - Fork 34
/
filterShare.go
184 lines (147 loc) · 5.76 KB
/
filterShare.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
package jira
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
)
type FilterShareService struct{ client *Client }
type shareFilterScopeScheme struct {
Scope string `json:"scope"`
}
// Returns the default sharing settings for new filters and dashboards for a user.
// Docs: https://docs.go-atlassian.io/jira-software-cloud/filters/sharing#get-default-share-scope
func (f *FilterShareService) Scope(ctx context.Context) (scope string, response *Response, err error) {
var endpoint = "rest/api/3/filter/defaultShareScope"
request, err := f.client.newRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return
}
response, err = f.client.Do(request)
if err != nil {
return
}
result := new(shareFilterScopeScheme)
if err = json.Unmarshal(response.BodyAsBytes, &result); err != nil {
return "", response, fmt.Errorf("unable to marshall the response body, error: %v", err.Error())
}
scope = result.Scope
return
}
// Sets the default sharing for new filters and dashboards for a user.
// Docs: https://docs.go-atlassian.io/jira-software-cloud/filters/sharing#set-default-share-scope
// Valid values: GLOBAL, AUTHENTICATED, PRIVATE
func (f *FilterShareService) SetScope(ctx context.Context, scope string) (response *Response, err error) {
//Valid the share filter scope
var (
validScopeValuesAsList = []string{"GLOBAL", "AUTHENTICATED", "PRIVATE"}
isValid bool
)
for _, validScope := range validScopeValuesAsList {
if validScope == scope {
isValid = true
break
}
}
if !isValid {
//Join the valid values and create the custom error
var validScopeValuesAsString = strings.Join(validScopeValuesAsList, ",")
return nil, fmt.Errorf("invalid scope, please provide one of the following: %v", validScopeValuesAsString)
}
var endpoint = "rest/api/3/filter/defaultShareScope"
request, err := f.client.newRequest(ctx, http.MethodPut, endpoint, shareFilterScopeScheme{Scope: scope})
if err != nil {
return
}
response, err = f.client.Do(request)
if err != nil {
return
}
return
}
// Returns the share permissions for a filter.
// A filter can be shared with groups, projects, all logged-in users, or the public.
// Sharing with all logged-in users or the public is known as a global share permission.
// Docs: https://docs.go-atlassian.io/jira-software-cloud/filters/sharing#get-share-permissions
func (f *FilterShareService) Gets(ctx context.Context, filterID int) (result *[]SharePermissionScheme, response *Response, err error) {
var endpoint = fmt.Sprintf("rest/api/3/filter/%v/permission", filterID)
request, err := f.client.newRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return
}
response, err = f.client.Do(request)
if err != nil {
return
}
result = new([]SharePermissionScheme)
if err = json.Unmarshal(response.BodyAsBytes, &result); err != nil {
return nil, response, fmt.Errorf("unable to marshall the response body, error: %v", err.Error())
}
return
}
type PermissionFilterBodyScheme struct {
Type string `json:"type,omitempty"`
ProjectID string `json:"projectId,omitempty"`
GroupName string `json:"groupname,omitempty"`
ProjectRoleID string `json:"projectRoleId,omitempty"`
}
// Add a share permissions to a filter.
// If you add a global share permission (one for all logged-in users or the public)
// it will overwrite all share permissions for the filter.
// Docs: https://docs.go-atlassian.io/jira-software-cloud/filters/sharing#add-share-permission
func (f *FilterShareService) Add(ctx context.Context, filterID int, payload *PermissionFilterBodyScheme) (result *[]SharePermissionScheme, response *Response, err error) {
if payload == nil {
return nil, nil, fmt.Errorf("error, payload value is nil, please provide a valid PermissionFilterBodyScheme pointer")
}
var endpoint = fmt.Sprintf("rest/api/3/filter/%v/permission", filterID)
request, err := f.client.newRequest(ctx, http.MethodPost, endpoint, &payload)
if err != nil {
return
}
request.Header.Set("Accept", "application/json")
request.Header.Set("Content-Type", "application/json")
response, err = f.client.Do(request)
if err != nil {
return
}
result = new([]SharePermissionScheme)
if err = json.Unmarshal(response.BodyAsBytes, &result); err != nil {
return nil, response, fmt.Errorf("unable to marshall the response body, error: %v", err.Error())
}
return
}
// Returns a share permission for a filter.
// A filter can be shared with groups, projects, all logged-in users, or the public.
// Sharing with all logged-in users or the public is known as a global share permission.
// Docs: https://docs.go-atlassian.io/jira-software-cloud/filters/sharing#get-share-permission
func (f *FilterShareService) Get(ctx context.Context, filterID, permissionID int) (result *SharePermissionScheme, response *Response, err error) {
var endpoint = fmt.Sprintf("rest/api/3/filter/%v/permission/%v", filterID, permissionID)
request, err := f.client.newRequest(ctx, http.MethodGet, endpoint, nil)
if err != nil {
return
}
response, err = f.client.Do(request)
if err != nil {
return
}
result = new(SharePermissionScheme)
if err = json.Unmarshal(response.BodyAsBytes, &result); err != nil {
return nil, response, fmt.Errorf("unable to marshall the response body, error: %v", err.Error())
}
return
}
// Deletes a share permission from a filter.
// Docs: https://docs.go-atlassian.io/jira-software-cloud/filters/sharing#delete-share-permission
func (f *FilterShareService) Delete(ctx context.Context, filterID, permissionID int) (response *Response, err error) {
var endpoint = fmt.Sprintf("rest/api/3/filter/%v/permission/%v", filterID, permissionID)
request, err := f.client.newRequest(ctx, http.MethodDelete, endpoint, nil)
if err != nil {
return
}
response, err = f.client.Do(request)
if err != nil {
return
}
return
}