-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommitReact.php
364 lines (307 loc) · 13.5 KB
/
CommitReact.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
359
360
361
362
363
364
<?php
# Copyright ( c ) 2019 Scott Meesseman
# Licensed under GPL3
$g_commitreact_prev_user_id = NO_USER;
class CommitReactPlugin extends MantisPlugin
{
public function register( )
{
$this->name = plugin_lang_get( "title" );
$this->description = plugin_lang_get( "description" );
$this->page = 'config';
$this->version = "1.2.0";
$this->requires = array(
"MantisCore" => "2.0.1",
"Source" => "2.2.0"
);
$this->author = "Scott Meesseman";
$this->contact = "spmeesseman@gmail.com";
$this->url = "https://github.com/mantisbt-plugins/CommitReact";
}
function config( )
{
return array(
'set_fixed_in_version' => 1,
'unattach_tags' => '',
'attach_tags' => '',
'resolved_unattach_tags' => '',
'resolved_attach_tags' => ''
);
}
public function hooks( )
{
return array(
#"EVENT_SOURCE_PRECOMMIT" => "pre_commit",
"EVENT_SOURCE_COMMITS" => "post_commit",
"EVENT_SOURCE_FIXED" => "post_commit_fixed"
);
}
public function pre_commit( $event )
{
return array();
}
public function post_commit( $p_event, $p_changesets )
{
log_event( LOG_PLUGIN, "CommitReact: post_commit" );
$t_resolved_threshold = config_get('bug_resolved_status_threshold');
#$t_fixed_threshold = config_get('bug_resolution_fixed_threshold');
#$t_notfixed_threshold = config_get('bug_resolution_not_fixed_threshold');
try {
foreach ( $p_changesets as $t_changeset )
{
foreach ( $t_changeset->bugs as $t_key => $t_bug_id )
{
$this->cr_set_user($t_bug_id, $t_changeset);
$t_bug = bug_get($t_bug_id);
if ($t_bug->status < $t_resolved_threshold ) {
$this->handle_tags( $t_changeset->user_id, $t_bug_id, $t_changeset->message, plugin_config_get( 'attach_tags' ), plugin_config_get( 'unattach_tags' ) );
}
else {
log_event( LOG_PLUGIN, "CommitReact: bug id %d is resolved, skip event", $t_bug_id );
}
}
}
}
catch ( Exception $e ) {
log_event( LOG_PLUGIN, "CommitReact: Exception handle_tags post commit - %s", $e->getMessage() );
}
$this->cr_reset_user();
return;
}
public function post_commit_fixed( $p_event, $p_fixed_bugs )
{
log_event( LOG_PLUGIN, "CommitReact: post_commit_fixed" );
foreach ( $p_fixed_bugs as $t_bug_id => $t_changeset )
{
$t_project_id = bug_get_field( $t_bug_id, 'project_id' );
if ( $t_project_id == null ) {
continue;
}
$this->cr_set_user($t_bug_id, $t_changeset);
if ( plugin_config_get( 'set_fixed_in_version' ) == 1 )
{
$t_version = null;
$t_versions = version_get_all_rows( $t_project_id, VERSION_FUTURE );
foreach ( $t_versions as $v )
{
if ( $t_version != null && strpos( $t_version, '.' ) != false && version_compare( $v['version'], $t_version ) < 0 ) {
$t_version = $v['version'];
}
else if ( $t_version != null && strpos( $t_version, '.' ) == false && strcmp( $v['version'], $t_version ) < 0 ) {
$t_version = $v['version'];
}
else if ( $t_version == null ) {
$t_version = $v['version'];
}
}
if ( !is_blank( $t_version ) ) {
bug_set_field( $t_bug_id, 'fixed_in_version', $t_version );
}
}
try {
$this->handle_tags( $t_changeset->user_id, $t_bug_id, $t_changeset->message, plugin_config_get( 'resolved_attach_tags' ), plugin_config_get( 'resolved_unattach_tags' ) );
}
catch ( Exception $e ) {
log_event( LOG_PLUGIN, "CommitReact: Exception handle_tags post commit resolve - %s", $e->getMessage() );
}
}
$this->cr_reset_user();
return;
}
private function get_commit_subject( $p_commit_msg )
{
$t_subject = '';
if (strstr( $p_commit_msg, ':' ) === false ) {
return $t_subject;
}
$t_end = strpos( $p_commit_msg, ':' );
# scope
if (strstr( $p_commit_msg, '(' ) === false ) {
$t_end = strpos( $p_commit_msg, '(' );
}
$t_subject = substr(0, $t_end);
#subject should contain no spaces
if (strstr( $t_subject, ' ' ) === false ) {
return '';
}
return strtolower( trim( $t_subject ) );
}
private function cr_reset_user()
{
global $g_commitreact_prev_user_id;
global $g_cache_current_user_id;
$g_cache_current_user_id = $g_commitreact_prev_user_id;
log_event( LOG_PLUGIN, "CommitReact: reset user id - %d", $g_cache_current_user_id );
}
private function cr_set_user($p_bug_id, $p_changeset)
{
global $g_commitreact_prev_user_id;
global $g_cache_current_user_id;
$g_commitreact_prev_user_id = $g_cache_current_user_id;
$t_handle_bug_threshold = config_get( 'handle_bug_threshold' );
#
# From source-integration plugin
#
# Determine the Mantis user to associate with the issue referenced in
# the changeset:
# - use Author if they can handle the issue
# - use Committer if not
# - if Committer can't handle issue either, it will not be resolved.
# This is used to generate the history entries and set the bug handler
# if the changeset fixes the issue.
$t_user_id = null;
if ( $p_changeset->user_id > 0 ) {
$t_can_handle_bug = access_has_bug_level( $t_handle_bug_threshold, $p_bug_id, $p_changeset->user_id );
if( $t_can_handle_bug ) {
$t_user_id = $p_changeset->user_id;
}
}
$t_handler_id = $t_user_id;
if( $t_handler_id === null && $p_changeset->committer_id > 0 ) {
$t_user_id = $p_changeset->committer_id;
$t_can_handle_bug = access_has_bug_level( $t_handle_bug_threshold, $p_bug_id, $t_user_id );
if( $t_can_handle_bug ) {
$t_handler_id = $t_user_id;
}
}
if ( !is_null( $t_user_id ) ) {
$g_cache_current_user_id = $t_user_id;
} else if ( !is_null( $g_commitreact_prev_user_id ) ) {
$g_cache_current_user_id = $g_commitreact_prev_user_id;
} else {
$g_cache_current_user_id = 0;
}
log_event( LOG_PLUGIN, "CommitReact: set user id - %d", $g_cache_current_user_id );
return;
}
private function handle_tags( $p_user_id, $p_bug_id, $p_commit_msg, $p_attach_tags, $p_unattach_tags )
{
log_event( LOG_PLUGIN, "CommitReact: Handle tags - bugid %d userid %d", $p_bug_id, $p_user_id );
$this->handle_tag_type( 'attach', $p_user_id, $p_bug_id, $p_commit_msg, $p_attach_tags );
$this->handle_tag_type( 'detach', $p_user_id, $p_bug_id, $p_commit_msg, $p_unattach_tags );
return;
}
private function cr_get_tag_id( $p_tage_name, $p_user_id )
{
$t_tag_id = 0;
$t_tag = tag_get_by_name( $p_tage_name );
if ( $t_tag === false ) {
if ( access_has_global_level( config_get( 'tag_create_threshold' ), $p_user_id ) ) {
$t_tag_id = tag_create( $p_tage_name, $p_user_id );
}
else {
log_event( LOG_PLUGIN, "CommitReact: Access level cannot create tag", $p_tage_name );
}
}
else {
$t_tag_id = $t_tag['id'];
}
return $t_tag_id;
}
private function handle_tag( $p_type, $p_tag_id, $p_user_id, $p_bug_id )
{
if ($p_tag_id > 0)
{
if ( $p_type == 'attach' )
{
if ( !tag_bug_is_attached( $p_tag_id, $p_bug_id ) )
{
if ( access_has_global_level( config_get('tag_attach_threshold'), $p_user_id ) ) {
tag_bug_attach( $p_tag_id, $p_bug_id, $p_user_id );
}
}
}
else if ( $p_type == 'detach' )
{
if ( tag_bug_is_attached( $p_tag_id, $p_bug_id ) )
{
if ( access_has_global_level( config_get('tag_detach_threshold'), $p_user_id ) ) {
tag_bug_detach( $p_tag_id, $p_bug_id, true, $p_user_id );
}
}
}
}
}
private function handle_tag_type( $p_type, $p_user_id, $p_bug_id, $p_commit_msg, $p_tags )
{
if ( !is_blank( $p_tags ) )
{
log_event( LOG_PLUGIN, "CommitReact: Handle tags (%s) - %s", $p_type, $p_tags);
$t_tag_strings = explode( config_get( 'tag_separator' ), $p_tags );
#
# fixes:bug,review pending|<8,manager checked|8,admin checked|16
#
foreach ( $t_tag_strings as $t_tag_string )
{
log_event( LOG_PLUGIN, "CommitReact: Process tag %s", $t_tag_string );
$t_tag_string_parts = explode( ':', $t_tag_string );
if ( count( $t_tag_string_parts ) === 1 )
{
$t_tag_string_parts_2 = explode( '|', $t_tag_string );
if ( count( $t_tag_string_parts_2 ) === 1 )
{
$t_tag_id = $this->cr_get_tag_id( $t_tag_string_parts_2[0], $p_user_id );
$this->handle_tag( $p_type, $t_tag_id, $p_user_id, $p_bug_id );
}
else if ( count( $t_tag_string_parts_2 ) === 2 )
{
try
{
if ( substr( $t_tag_string_parts_2[1], 0, 2 ) == "<=" )
{
$t_access_level = substr( $t_tag_string_parts_2[1], 2 );
if ( user_get_access_level( $p_user_id ) <= (int)$t_access_level ) {
$t_tag_id = $this->cr_get_tag_id( $t_tag_string_parts_2[0], $p_user_id );
$this->handle_tag( $p_type, $t_tag_id, $p_user_id, $p_bug_id );
}
}
else if ( substr( $t_tag_string_parts_2[1], 0, 2 ) == ">=" )
{
$t_access_level = substr( $t_tag_string_parts_2[1], 2 );
if ( user_get_access_level( $p_user_id ) >= (int)$t_access_level ) {
$t_tag_id = $this->cr_get_tag_id( $t_tag_string_parts_2[0], $p_user_id );
$this->handle_tag( $p_type, $t_tag_id, $p_user_id, $p_bug_id );
}
}
else if ( substr( $t_tag_string_parts_2[1], 0, 1 ) == "<" )
{
$t_access_level = substr( $t_tag_string_parts_2[1], 1 );
if ( user_get_access_level( $p_user_id ) < (int)$t_access_level ) {
$t_tag_id = $this->cr_get_tag_id( $t_tag_string_parts_2[0], $p_user_id );
$this->handle_tag( $p_type, $t_tag_id, $p_user_id, $p_bug_id );
}
}
else if ( substr( $t_tag_string_parts_2[1], 0, 1 ) == ">" )
{
$t_access_level = substr( $t_tag_string_parts_2[1], 1 );
if ( user_get_access_level( $p_user_id ) > (int)$t_access_level ) {
$t_tag_id = $this->cr_get_tag_id( $t_tag_string_parts_2[0], $p_user_id );
$this->handle_tag( $p_type, $t_tag_id, $p_user_id, $p_bug_id );
}
}
else
{
$t_access_level = $t_tag_string_parts_2[1];
if ( user_get_access_level( $p_user_id ) == (int)$t_access_level ) {
$t_tag_id = $this->cr_get_tag_id( $t_tag_string_parts_2[0], $p_user_id );
$this->handle_tag( $p_type, $t_tag_id, $p_user_id, $p_bug_id );
}
}
}
catch (Exception $e) {
log_event( LOG_PLUGIN, "CommitReact: Exception handle_tag_type - %s", $e->getMessage() );
}
}
}
else if ( count( $t_tag_string_parts ) === 2 )
{
if ( !is_blank( $t_tag_string_parts[0] ) && strtolower( trim( $t_tag_string_parts[0] ) ) === $this->get_commit_subject( $p_commit_msg ) )
{
$t_tag_id = $this->cr_get_tag_id( $t_tag_string_parts[1], $p_user_id );
$this->handle_tag( $p_type, $t_tag_id, $p_user_id, $p_bug_id );
}
}
}
}
}
}