-
Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathindex.ts
More file actions
385 lines (327 loc) · 9.72 KB
/
index.ts
File metadata and controls
385 lines (327 loc) · 9.72 KB
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// Copyright (c) 2019-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {getCurrentChannelId, getCurrentUserId} from 'mattermost-redux/selectors/entities/common';
import {PostTypes} from 'mattermost-redux/action_types';
import {AnyAction, Dispatch} from 'redux';
import Client from '../client';
import ActionTypes from '../action_types';
import {APIError, ConnectedData, GitlabUsersData, LHSData, ShowRhsPluginActionData, SubscriptionData} from '../types';
import {Item} from '../types/gitlab_items';
import {GlobalState} from '../types/store';
import {getPluginState} from '../selectors';
import {CommentBody, IssueBody} from '../types/gitlab_types';
export function getConnected(reminder = false) {
return async (dispatch: Dispatch<AnyAction>) => {
let data: ConnectedData;
try {
data = await Client.getConnected(reminder);
} catch (error) {
return {error};
}
dispatch({
type: ActionTypes.RECEIVED_CONNECTED,
data,
});
return {data};
};
}
function checkAndHandleNotConnected(data: APIError) {
return async (dispatch: Dispatch<AnyAction>) => {
if (data && data.id === 'not_connected') {
dispatch({
type: ActionTypes.RECEIVED_CONNECTED,
data: {
connected: false,
gitlab_username: '',
gitlab_client_id: '',
settings: {},
} as ConnectedData,
});
return false;
}
return true;
};
}
export function getReviewDetails(prList: Item[]) {
return async (dispatch: Dispatch<AnyAction>) => {
let data: Item | APIError;
try {
data = await Client.getPrsDetails(prList);
} catch (error) {
return {error};
}
const connected = await checkAndHandleNotConnected(data as APIError)(dispatch);
if (!connected) {
return {error: data};
}
dispatch({
type: ActionTypes.RECEIVED_REVIEW_DETAILS,
data,
});
return {data};
};
}
export function getYourPrDetails(prList: Item[]) {
return async (dispatch: Dispatch<AnyAction>) => {
let data: Item | APIError;
try {
data = await Client.getPrsDetails(prList);
} catch (error) {
return {error};
}
const connected = await checkAndHandleNotConnected(data as APIError)(dispatch);
if (!connected) {
return {error: data};
}
dispatch({
type: ActionTypes.RECEIVED_YOUR_PR_DETAILS,
data,
});
return {data};
};
}
export function getLHSData() {
return async (dispatch: Dispatch<AnyAction>) => {
let data: LHSData | APIError;
try {
data = await Client.getLHSData();
} catch (error) {
return {error};
}
const connected = await checkAndHandleNotConnected(data as APIError)(dispatch);
if (!connected) {
return {error: data};
}
dispatch({
type: ActionTypes.RECEIVED_LHS_DATA,
data,
});
return {data};
};
}
/**
* Stores "showRHSPlugin" action returned by
* "registerRightHandSidebarComponent" in plugin initialization.
*/
export function setShowRHSAction(showRHSPluginAction: () => ShowRhsPluginActionData) {
return {
type: ActionTypes.RECEIVED_SHOW_RHS_ACTION,
showRHSPluginAction,
};
}
export function updateRHSState(rhsState: string) {
return {
type: ActionTypes.UPDATE_RHS_STATE,
state: rhsState,
};
}
const GITLAB_USER_GET_TIMEOUT_MILLISECONDS = 1000 * 60 * 60; // 1 hour
export function getGitlabUser(userID: string) {
return async (dispatch: Dispatch<AnyAction>, getState: () => GlobalState) => {
if (!userID) {
return {};
}
const user = getPluginState(getState()).gitlabUsers[userID];
if (
user &&
user.last_try &&
Date.now() - user.last_try < GITLAB_USER_GET_TIMEOUT_MILLISECONDS
) {
return {};
}
if (user && user.username) {
return {data: user};
}
let data: GitlabUsersData;
try {
data = await Client.getGitlabUser(userID);
} catch (error: unknown) {
if ((error as APIError).status === 404) {
dispatch({
type: ActionTypes.RECEIVED_GITLAB_USER,
userID,
data: {last_try: Date.now()},
});
}
return {error};
}
dispatch({
type: ActionTypes.RECEIVED_GITLAB_USER,
userID,
data,
});
return {data};
};
}
export function openCreateIssueModal(postId: string) {
return {
type: ActionTypes.OPEN_CREATE_ISSUE_MODAL,
data: {
postId,
},
};
}
export function openCreateIssueModalWithoutPost(title: string, channelId: string) {
return {
type: ActionTypes.OPEN_CREATE_ISSUE_MODAL_WITHOUT_POST,
data: {
title,
channelId,
},
};
}
export function closeCreateIssueModal() {
return {
type: ActionTypes.CLOSE_CREATE_ISSUE_MODAL,
};
}
export function createIssue(payload: IssueBody) {
return async (dispatch: Dispatch<AnyAction>) => {
let data;
try {
data = await Client.createIssue(payload);
} catch (error) {
return {error};
}
const connected = await checkAndHandleNotConnected(data as APIError)(dispatch);
if (!connected) {
return {error: data};
}
return {data};
};
}
export function openAttachCommentToIssueModal(postId: string) {
return {
type: ActionTypes.OPEN_ATTACH_COMMENT_TO_ISSUE_MODAL,
data: {
postId,
},
};
}
export function closeAttachCommentToIssueModal() {
return {
type: ActionTypes.CLOSE_ATTACH_COMMENT_TO_ISSUE_MODAL,
};
}
export function attachCommentToIssue(payload: CommentBody) {
return async (dispatch: Dispatch<AnyAction>) => {
let data;
try {
data = await Client.attachCommentToIssue(payload);
} catch (error) {
return {error};
}
const connected = await checkAndHandleNotConnected(data as APIError)(dispatch);
if (!connected) {
return {error: data};
}
return {data};
};
}
export function getProjects() {
return async (dispatch: Dispatch<AnyAction>) => {
let data;
try {
data = await Client.getProjects();
} catch (error) {
return {error};
}
const connected = await checkAndHandleNotConnected(data as APIError)(dispatch);
if (!connected) {
return {error: data};
}
dispatch({
type: ActionTypes.RECEIVED_PROJECTS,
data,
});
return {data};
};
}
export function getLabelOptions(projectID: number) {
return async (dispatch: Dispatch<AnyAction>) => {
let data;
try {
data = await Client.getLabels(projectID);
} catch (error) {
return {error};
}
const connected = await checkAndHandleNotConnected(data as APIError)(dispatch);
if (!connected) {
return {error: data};
}
return {data};
};
}
export function getMilestoneOptions(projectID: number) {
return async (dispatch: Dispatch<AnyAction>) => {
let data;
try {
data = await Client.getMilestones(projectID);
} catch (error) {
return {error};
}
const connected = await checkAndHandleNotConnected(data as APIError)(dispatch);
if (!connected) {
return {error: data};
}
return {data};
};
}
export function getAssigneeOptions(projectID: number) {
return async (dispatch: Dispatch<AnyAction>, getState: () => GlobalState) => {
let data;
try {
data = await Client.getAssignees(projectID);
} catch (error) {
return {error};
}
const connected = await checkAndHandleNotConnected(data as APIError)(dispatch);
if (!connected) {
return {error: data};
}
return {data};
};
}
export function getChannelSubscriptions(channelId: string) {
return async (dispatch: Dispatch<AnyAction>) => {
if (!channelId) {
return {};
}
let subscriptions: SubscriptionData;
try {
subscriptions = await Client.getChannelSubscriptions(channelId);
} catch (error) {
return {error};
}
dispatch({
type: ActionTypes.RECEIVED_CHANNEL_SUBSCRIPTIONS,
data: {
channelId,
subscriptions,
},
});
return {subscriptions};
};
}
export function sendEphemeralPost(message: string) {
return (dispatch: Dispatch<AnyAction>, getState: () => GlobalState) => {
const timestamp = Date.now();
const state = getState();
const post = {
id: 'gitlabPlugin' + Date.now(),
user_id: getCurrentUserId(state),
channel_id: getCurrentChannelId(state),
message,
type: 'system_ephemeral',
create_at: timestamp,
update_at: timestamp,
root_id: '',
parent_id: '',
props: {},
};
dispatch({
type: PostTypes.RECEIVED_NEW_POST,
data: post,
});
};
}