-
Notifications
You must be signed in to change notification settings - Fork 127
/
client_cloud.go
150 lines (128 loc) Β· 3.93 KB
/
client_cloud.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
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
// See License for license information.
package main
import (
"encoding/json"
"fmt"
"strconv"
jira "github.com/andygrunwald/go-jira"
"github.com/mattermost/mattermost/server/public/plugin"
"github.com/pkg/errors"
)
type jiraCloudClient struct {
JiraClient
}
type IssueTypeWithStatuses struct {
*jira.IssueType
Statuses []*jira.Status `json:"statuses"`
}
func newCloudClient(jiraClient *jira.Client) Client {
return &jiraCloudClient{
JiraClient: JiraClient{
Jira: jiraClient,
},
}
}
// GetCreateMetaInfo returns the metadata needed to implement the UI and validation of
// creating new Jira issues.
func (client jiraCloudClient) GetCreateMetaInfo(api plugin.API, options *jira.GetQueryOptions) (*jira.CreateMetaInfo, error) {
cimd, resp, err := client.Jira.Issue.GetCreateMetaWithOptions(options)
if err != nil {
if resp == nil {
return nil, err
}
// returns a different JSON from all other APIs
result := map[string]string{}
jsonerr := json.NewDecoder(resp.Body).Decode(&result)
resp.Body.Close()
if jsonerr == nil {
err = errors.New(result["error"])
}
return nil, RESTError{err, resp.StatusCode}
}
return cimd, nil
}
// SearchUsersAssignableToIssue finds all users that can be assigned to an issue.
func (client jiraCloudClient) SearchUsersAssignableToIssue(issueKey, query string, maxResults int) ([]jira.User, error) {
return SearchUsersAssignableToIssue(client, issueKey, "query", query, maxResults)
}
// SearchUsersAssignableInProject finds all users that can be assigned to some issue in a given project.
func (client jiraCloudClient) SearchUsersAssignableInProject(projectKey, query string, maxResults int) ([]jira.User, error) {
return SearchUsersAssignableInProject(client, projectKey, "query", query, maxResults)
}
// GetUserGroups returns the list of groups that a user belongs to.
func (client jiraCloudClient) GetUserGroups(connection *Connection) ([]*jira.UserGroup, error) {
groups := []*jira.UserGroup{}
params := map[string]string{
"accountId": connection.AccountID,
}
err := client.RESTGet("3/user/groups", params, &groups)
if err != nil {
return nil, err
}
return groups, nil
}
func (client jiraCloudClient) ListProjects(query string, limit int, expandIssueTypes bool) (jira.ProjectList, error) {
type searchResult struct {
Values jira.ProjectList `json:"values"`
StartAt int `json:"startAt"`
MaxResults int `json:"maxResults"`
Total int `json:"total"`
IsLast bool `json:"isLast"`
}
remaining := 50
fetchAll := false
if limit > 0 {
remaining = limit
}
if limit < 0 {
fetchAll = true
}
var out jira.ProjectList
for {
opts := map[string]string{
"startAt": strconv.Itoa(len(out)),
"maxResults": strconv.Itoa(remaining),
}
if expandIssueTypes {
opts["expand"] = QueryParamIssueTypes
}
var result searchResult
err := client.RESTGet("/3/project/search", opts, &result)
if err != nil {
return nil, err
}
if len(result.Values) > remaining {
result.Values = result.Values[:remaining]
}
out = append(out, result.Values...)
if !fetchAll {
remaining -= len(result.Values)
if remaining == 0 {
// Got enough.
return out, nil
}
}
if len(result.Values) == 0 || result.IsLast {
// Ran out of results.
return out, nil
}
}
}
func (client jiraCloudClient) GetIssueTypes(projectID string) ([]jira.IssueType, error) {
var result []jira.IssueType
opts := map[string]string{
"projectId": projectID,
}
if err := client.RESTGet("3/issuetype/project", opts, &result); err != nil {
return nil, err
}
return result, nil
}
func (client jiraCloudClient) ListProjectStatuses(projectID string) ([]*IssueTypeWithStatuses, error) {
var result []*IssueTypeWithStatuses
if err := client.RESTGet(fmt.Sprintf("3/project/%s/statuses", projectID), nil, &result); err != nil {
return nil, err
}
return result, nil
}