-
Notifications
You must be signed in to change notification settings - Fork 2
/
inc.user.php
358 lines (294 loc) · 8.21 KB
/
inc.user.php
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
<?php
class User extends db_generic_model {
static public $_table = 'users';
public static $_config = array(
'index_page_size' => array(
'label' => 'Index page size',
'default' => 10,
'size' => 4,
'type' => 'number',
'required' => true,
),
'agile_view_id' => array(
'label' => 'Agile board ID',
'default' => '',
'size' => 4,
'type' => 'number',
'required' => false,
),
'load_epics' => array(
'label' => 'Load epic issue details',
'default' => 0,
'size' => 1,
'type' => 'checkbox',
'required' => false,
),
'show_thumbnails' => array(
'label' => "Show attachments' thumbnails",
'default' => 0,
'size' => 1,
'type' => 'checkbox',
'required' => false,
),
'agile_view_ids' => FALSE,
'show_custom_fields' => FALSE,
);
public function __construct( array $data = [] ) {
parent::__construct($data);
if ( !$this->last_sync || $this->last_sync + FORCE_JIRA_USER_SYNC < time() ) {
// $this->unsync();
}
$this->syncOverdueVars();
}
function getActiveSprint( $boardId = null ) {
$boardId or $boardId = $this->config('agile_view_id');
if ( !$boardId ) {
return;
}
$sprints = self::getActiveSprints((array) $boardId);
return reset($sprints);
}
static function getActiveSprints( array $boardIds ) {
$activeSprints = [];
foreach ( $boardIds as $id ) {
$activeSprints[$id] = false;
$sprints = jira_get('/rest/greenhopper/1.0/sprintquery/' . $id, array(), $error, $info);
if ( !$sprints ) {
continue;
}
$actives = array_filter($sprints->sprints, function($sprint) {
return $sprint->state == 'ACTIVE';
});
if ( !$actives ) {
continue;
}
$activeSprints[$id] = reset($actives);
}
return $activeSprints;
}
function getAutoVarSprint() {
$sprint = $this->getActiveSprint();
if ( $sprint ) {
return $sprint->id;
}
}
function getAutoVarSprints() {
$boardIds = $this->selected_agile_boards;
if ($boardIds) {
$sprints = array_filter(self::getActiveSprints($boardIds));
return array_map(function($sprint) {
return $sprint->id;
}, $sprints);
}
}
function getCachedAutoVar( $type ) {
foreach ( $this->variables as $var ) {
if ( $var->auto_update_type == $type ) {
return array_map('trim', array_filter(explode(',', $var->value)));
}
}
}
function syncAnyAutoVar( $type, $id = null ) {
global $db;
if ( !$id ) {
$id = $db->select_one('variables', 'id', array('user_id' => $this->id, 'auto_update_type' => $type));
}
$function = 'getAutoVar' . $type;
if ( method_exists($this, $function) ) {
$value = $this->$function() ?: '0';
$db->update('variables', array(
'value' => implode(', ', (array) $value),
'last_update' => time(),
), array(
'id' => $id,
));
}
}
function syncOverdueVars() {
foreach ( $this->overdue_vars as $id => $type ) {
$this->syncAnyAutoVar($type, $id);
}
}
function get_overdue_vars() {
global $db;
return $db->select_fields('variables', 'id, auto_update_type', '
user_id = ? AND
auto_update_type <> ? AND
last_update < ?
', array(
$this->id,
'',
time() - FORCE_AUTO_VARS_SYNC,
));
}
function config( $name, $alt = null ) {
if ( func_num_args() == 1 ) {
$alt = $this::$_config[$name]['default'];
}
$config = $this->config;
return isset($config[$name]) ? $config[$name] : $alt;
}
function get_config() {
global $db;
return $db->select_fields('options', 'name, value', array('user_id' => $this->id));
}
function get_jira_user_short() {
$x = explode('@', $this->jira_user);
return $x[0];
}
function get_jira_domain() {
$url = parse_url($this->jira_url);
return $url['host'];
}
function get_jira_id() {
return $this->jira_user . ' @ ' . $this->jira_domain;
}
function get_variables() {
global $db;
return $db->select('variables', array('user_id' => $this->id))->all();
}
function get_custom_fields() {
$fields = jira_get('field');
return array_values(array_filter($fields, function($f) {
return @$f->custom;
}));
}
function get_custom_field_ids() {
if ( !$this->cache__custom_field_ids ) {
$fields = array();
foreach ( $this->custom_fields as $field ) {
$fields[mb_strtolower($field->name)] = $field->id;
}
ksort($fields);
$this->cache__custom_field_ids = serialize($fields);
$this->update(array('cache__custom_field_ids' => $this->cache__custom_field_ids));
}
return unserialize($this->cache__custom_field_ids);
}
function get_selected_agile_boards() {
$ids = $this->config('agile_view_ids', '');
$ids = array_filter(explode(',', $ids));
return $ids;
}
function get_selected_show_custom_fields() {
$ids = $this->config('show_custom_fields', '');
$ids = array_filter(explode(',', $ids));
return $ids;
}
function get_agile_boards() {
if ( !$this->cache__agile_boards ) {
$boards = jira_get('/rest/agile/1.0/board');
$boardOptions = array_reduce($boards->values, function($options, $board) {
return $options + array($board->id => $board->name);
}, array());
$this->cache__agile_boards = serialize($boardOptions);
$this->update(array('cache__agile_boards' => $this->cache__agile_boards));
}
return unserialize($this->cache__agile_boards);
}
function get_cf_story_points() {
return @$this->custom_field_ids['story points'] ?: '';
}
function get_cf_epic_link() {
return @$this->custom_field_ids['epic link'] ?: '';
}
function get_cf_epic_name() {
return @$this->custom_field_ids['epic name'] ?: '';
}
function get_cf_epic_status() {
return @$this->custom_field_ids['epic status'] ?: '';
}
function get_cf_epic_color() {
return @$this->custom_field_ids['epic colour'] ?: @$this->custom_field_ids['epic color'] ?: '';
}
function unsync() {
global $db;
$db->delete('filters', array('user_id' => $this->id));
unset($this->filters, $this->filter_query_options);
$this->update(array(
'cache__custom_field_ids' => $this->cache__custom_field_ids = '',
'cache__agile_boards' => $this->cache__agile_boards = '',
));
unset(
$this->custom_fields,
$this->custom_field_ids,
$this->agile_boards
);
}
function get_filters() {
global $db;
$filters = $db->select('filters', 'user_id = ? ORDER BY name ASC', array($this->id))->all();
if ( !$filters ) {
$jira_filters = jira_get('filter/favourite', null, $error, $info);
// Error, no need to prettify
if ( $error ) {
var_dump($error, $info);
exit;
}
// No error, but no filters, so return early
if ( !$jira_filters ) {
return array();
}
// Save filters in local db
foreach ( $jira_filters AS $filter ) {
$db->insert('filters', array(
'user_id' => $this->id,
'filter_id' => $filter->id,
'name' => $filter->name,
'jql' => $filter->jql,
));
}
// Re-fetch local filters and save into ->filters via __get magic
unset($this->filters);
$filters = $this->get_filters();
}
return $filters;
}
function get_filter_options( $valueField = 'filter_id', $labelField = 'name' ) {
$filterOptions = array();
foreach ( $this->filters AS $filter ) {
$filterOptions[$filter->$valueField] = $filter->$labelField;
}
return $filterOptions;
}
function get_filter_options_jql() {
return $this->get_filter_options('filter_id', 'jql');
}
function _applyVariables( $variables ) {
$vars = $this->variables;
return array_map(function($jql) use ($vars) {
foreach ( $vars as $var ) {
$jql = preg_replace('#' . $var->regex . '#', str_replace('XXX', $var->value, $var->replacement), $jql);
}
return $jql;
}, $variables);
}
function get_filter_query_options() {
return array_flip($this->_applyVariables($this->get_filter_options('name', 'jql')));
}
function get_index_filter_object() {
if ( $this->index_filter ) {
global $db;
$filter = $db->select('filters', array('user_id' => $this->id, 'filter_id' => $this->index_filter))->first();
$realJQL = $this->_applyVariables(array($filter->jql));
$filter->jql = $realJQL[0];
if ( $filter ) {
return $filter;
}
}
return false;
}
static function load( string $url, string $username ) {
$user = self::first(array(
'jira_url' => $url,
'jira_user' => $username,
));
if ( $user ) {
return $user;
}
return self::create(array(
'jira_url' => $url,
'jira_user' => $username,
));
}
}