-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjira-list-functions.js
211 lines (183 loc) · 4.3 KB
/
jira-list-functions.js
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
const { default: axios } = require("axios");
const { getJiraClient, resolveHost } = require("./helpers");
const DEFAULT_FIELDS = ["created", "description", "summary", "status", "priority"];
const DEFAULT_MAX_RESULTS = 50;
const DEFAULT_START_AT = 0;
function listIssues({
host,
email,
apiToken,
project,
status,
startDate,
endDate,
overrideJql: customJqlString,
maxResults = DEFAULT_MAX_RESULTS,
startAt = DEFAULT_START_AT,
fields = DEFAULT_FIELDS,
}) {
const jiraClient = getJiraClient({ host, email, apiToken });
const jqlSegments = [];
if (customJqlString) {
jqlSegments.push(customJqlString);
}
if (!customJqlString && project) {
jqlSegments.push(`project = ${project}`);
}
if (!customJqlString && status) {
jqlSegments.push(`status = ${status}`);
}
if (!customJqlString && startDate) {
jqlSegments.push(`updated >= ${startDate}`);
}
if (!customJqlString && endDate) {
jqlSegments.push(`updated <= ${endDate}`);
}
const searchOptions = { maxResults, startAt };
if (fields !== "*") {
searchOptions.fields = fields;
}
return jiraClient.searchJira(jqlSegments.join(" AND "), searchOptions);
}
function listAssigneeDetails({
host,
email,
apiToken,
startDate,
endDate,
overrideJql: customJqlString,
users: assignee,
maxResults = DEFAULT_MAX_RESULTS,
startAt = DEFAULT_START_AT,
fields = DEFAULT_FIELDS,
}) {
const jiraClient = getJiraClient({ host, email, apiToken });
const jqlSegments = [];
if (customJqlString) {
jqlSegments.push(customJqlString);
}
if (!customJqlString && assignee) {
jqlSegments.push(`assignee in ('${assignee}')`);
}
if (!customJqlString && startDate) {
jqlSegments.push(`created >= ${startDate}`);
}
if (!customJqlString && endDate) {
jqlSegments.push(`created <= ${endDate}`);
}
const jql = jqlSegments.join(" AND ");
const searchOptions = { maxResults, startAt };
if (fields && fields !== "*") {
searchOptions.fields = fields;
}
return jiraClient.searchJira(jql, searchOptions);
}
function listTransitions({
host,
email,
apiToken,
issue,
}) {
const jiraClient = getJiraClient({ host, email, apiToken });
return jiraClient.listTransitions(issue);
}
function listProjects({
host,
email,
apiToken,
}) {
const jiraClient = getJiraClient({ host, email, apiToken });
return jiraClient.listProjects();
}
function listProjectVersions({
host,
email,
apiToken,
project,
}) {
const jiraClient = getJiraClient({ host, email, apiToken });
return jiraClient.getVersions(project);
}
async function listIssueTypes({
host,
email,
apiToken,
project,
}) {
const client = getJiraClient({ host, email, apiToken });
if (!project) {
return client.listIssueTypes();
}
const { issueTypes } = await client.getProject(project);
return issueTypes;
}
async function listUsers({
host,
email,
apiToken,
group,
}) {
const client = getJiraClient({ host, email, apiToken });
const result = await (group ? client.getUsersInGroup(group) : client.getUsers());
return (group ? result.users.items : result) ?? [];
}
async function listStatus({
host,
email,
apiToken,
project,
}) {
const client = getJiraClient({ host, email, apiToken });
const statuses = await client.listStatus();
if (!project) {
return statuses;
}
const mappedStatuses = statuses.map((status) => ({
...status,
isGlobal: !Reflect.has(status, "scope"),
}));
const filteredStatuses = mappedStatuses.filter((status) => (
status.isGlobal
|| status.scope.project?.id === project
));
return filteredStatuses;
}
async function listGroups(params) {
const {
host,
email,
apiToken,
query,
} = params;
const { host: resolvedHost, protocol } = resolveHost(host);
const fullBaseUrl = `${protocol}://${resolvedHost}`;
const urlParams = {};
if (query) {
urlParams.query = query;
}
const { data } = await axios({
method: "GET",
baseURL: fullBaseUrl,
url: "/rest/api/2/groups/picker",
auth: {
password: apiToken,
username: email,
},
headers: {
Accept: "application/json",
},
params: urlParams,
});
return data;
}
module.exports = {
listIssues,
listTransitions,
listProjects,
listProjectVersions,
listAssigneeDetails,
listIssueTypes,
listUsers,
listStatus,
listGroups,
};