mantis-matrix-integration – Blame information for rev 3

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 <?php
2 /**
3 * Matrix Integration
4 * Copyright (C) 2024 Wizardry and Steamworks (wizardry.steamworks@outlook.com)
5 * based on: Slack Integration by 2014 Karim Ratib (karim.ratib@gmail.com)
6 * License GPLv2
7 */
8  
9 require_once __DIR__ . '/vendor/autoload.php';
10 use MatrixPhp\MatrixClient;
11  
12 class MatrixPlugin extends MantisPlugin {
13 var $skip = false;
14  
15 function register() {
16 $this->name = plugin_lang_get( 'title' );
17 $this->description = plugin_lang_get( 'description' );
18 $this->page = 'config';
19 $this->version = '1.0';
20 $this->requires = array(
21 'MantisCore' => '1.3.0',
22 );
23 $this->author = 'Wizardry and Steamworks';
24 $this->contact = 'wizardry.stemworks@outlook.com';
25 $this->url = 'https://grimore.org';
26 }
27  
28 function install() {
29 if (version_compare(PHP_VERSION, '5.3.0', '<')) {
30 plugin_error(ERROR_PHP_VERSION, ERROR);
31 return false;
32 }
33 if (!extension_loaded('curl')) {
34 plugin_error(ERROR_NO_CURL, ERROR);
35 return false;
36 }
37 return true;
38 }
39  
40 function config() {
41 return array(
42 'server' => 'https://matrix.org',
43 'username' => 'mantis',
44 'skip_bulk' => true,
45 'room' => '#general'
46 );
47 }
48  
49 function hooks() {
50 return array(
51 'EVENT_REPORT_BUG' => 'bug_report_update',
52 'EVENT_UPDATE_BUG' => 'bug_report_update',
53 'EVENT_BUG_DELETED' => 'bug_deleted',
54 'EVENT_BUG_ACTION' => 'bug_action',
55 'EVENT_BUGNOTE_ADD' => 'bugnote_add_edit',
56 'EVENT_BUGNOTE_EDIT' => 'bugnote_add_edit',
57 'EVENT_BUGNOTE_DELETED' => 'bugnote_deleted',
58 'EVENT_BUGNOTE_ADD_FORM' => 'bugnote_add_form',
59 );
60 }
61  
62 function bugnote_add_form($event, $bug_id) {
63 if ($_SERVER['PHP_SELF'] !== '/bug_update_page.php') return;
64  
65 echo '<tr><td class="center" colspan="6">';
66 echo '<input ', helper_get_tab_index(), ' name="matrix_skip" type="checkbox" >' . plugin_lang_get('skip') . '</input>';
67 echo '</td></tr>';
68 }
69  
70 function bug_report_update($event, $bug, $bug_id) {
71 $this->skip = $this->skip || gpc_get_bool('matrix_skip');
72  
73 $project = project_get_name($bug->project_id);
3 office 74 $url = string_get_bug_view_url_with_fqdn($bug->id);
2 office 75 //$summary = $this->format_summary($bug);
1 office 76 $reporter = '@' . user_get_name(auth_get_current_user_id());
3 office 77 $msg = sprintf(
78 plugin_lang_get($event === 'EVENT_REPORT_BUG' ? 'bug_created' : 'bug_updated'),
79 $project,
80 $reporter,
81 $url,
82 $bug->id
1 office 83 );
84 $this->notify($msg);
85 }
86  
87 function bug_action($event, $action, $bug_id) {
88 $this->skip = $this->skip || gpc_get_bool('matrix_skip') || plugin_config_get('skip_bulk');
89  
90 if ($action !== 'DELETE') {
91 $bug = bug_get($bug_id);
92 $this->bug_report_update('EVENT_UPDATE_BUG', $bug, $bug_id);
93 }
94 }
95  
96 function bug_deleted($event, $bug_id) {
97 $this->skip = $this->skip || gpc_get_bool('matrix_skip');
98  
99 $bug = bug_get($bug_id);
3 office 100 $url = string_get_bug_view_url_with_fqdn($bug->id);
1 office 101 $project = project_get_name($bug->project_id);
102 $reporter = '@' . user_get_name(auth_get_current_user_id());
2 office 103 //$summary = $this->format_summary($bug);
3 office 104 $msg = sprintf(
105 plugin_lang_get('bug_deleted'),
106 $project,
107 $reporter,
108 $url,
109 $bug->id
110 );
1 office 111 $this->notify($msg);
112 }
113  
114 function bugnote_add_edit($event, $bug_id, $bugnote_id) {
115 $this->skip = $this->skip || gpc_get_bool('matrix_skip');
116  
117 $bug = bug_get($bug_id);
3 office 118 $url = string_get_bugnote_view_url_with_fqdn($bug_id, $bugnote_id);
1 office 119 $project = project_get_name($bug->project_id);
2 office 120 //$summary = $this->format_summary($bug);
1 office 121 $reporter = '@' . user_get_name(auth_get_current_user_id());
2 office 122 //$note = bugnote_get_text($bugnote_id);
3 office 123 $msg = sprintf(
124 plugin_lang_get($event === 'EVENT_BUGNOTE_ADD' ? 'bugnote_created' : 'bugnote_updated'),
125 $project,
126 $reporter,
127 $url,
128 $bug_id
1 office 129 );
130 $this->notify($msg);
131 }
132  
133 function bugnote_deleted($event, $bug_id, $bugnote_id) {
134 $this->skip = $this->skip || gpc_get_bool('matrix_skip');
135  
136 $bug = bug_get($bug_id);
137 $project = project_get_name($bug->project_id);
3 office 138 $url = string_get_bug_view_url_with_fqdn($bug->id);
2 office 139 //$summary = $this->format_summary($bug);
1 office 140 $reporter = '@' . user_get_name(auth_get_current_user_id());
3 office 141 $msg = sprintf(
142 plugin_lang_get('bugnote_deleted'),
143 $project,
144 $reporter,
145 $url,
146 $bug->id
147 );
1 office 148 $this->notify($msg);
149 }
150  
151 function format_summary($bug) {
152 $summary = bug_format_id($bug->id) . ': ' . string_display_line_links($bug->summary);
153 return strip_tags(html_entity_decode($summary));
154 }
155  
156 function format_text($bug, $text) {
157 $t = string_display_line_links($text);
158 return strip_tags(html_entity_decode($t));
159 }
160  
161 function format_value($bug, $field_name) {
162 $self = $this;
163 $values = array(
164 'id' => function($bug) { return sprintf('<%s|%s>', string_get_bug_view_url_with_fqdn($bug->id), $bug->id); },
165 'project_id' => function($bug) { return project_get_name($bug->project_id); },
166 'reporter_id' => function($bug) { return '@' . user_get_name($bug->reporter_id); },
167 'handler_id' => function($bug) { return empty($bug->handler_id) ? plugin_lang_get('no_user') : ('@' . user_get_name($bug->handler_id)); },
168 'duplicate_id' => function($bug) { return sprintf('<%s|%s>', string_get_bug_view_url_with_fqdn($bug->duplicate_id), $bug->duplicate_id); },
169 'priority' => function($bug) { return get_enum_element( 'priority', $bug->priority ); },
170 'severity' => function($bug) { return get_enum_element( 'severity', $bug->severity ); },
171 'reproducibility' => function($bug) { return get_enum_element( 'reproducibility', $bug->reproducibility ); },
172 'status' => function($bug) { return get_enum_element( 'status', $bug->status ); },
173 'resolution' => function($bug) { return get_enum_element( 'resolution', $bug->resolution ); },
174 'projection' => function($bug) { return get_enum_element( 'projection', $bug->projection ); },
175 'category_id' => function($bug) { return category_full_name( $bug->category_id, false ); },
176 'eta' => function($bug) { return get_enum_element( 'eta', $bug->eta ); },
177 'view_state' => function($bug) { return $bug->view_state == VS_PRIVATE ? lang_get('private') : lang_get('public'); },
178 'sponsorship_total' => function($bug) { return sponsorship_format_amount( $bug->sponsorship_total ); },
179 'os' => function($bug) { return $bug->os; },
180 'os_build' => function($bug) { return $bug->os_build; },
181 'platform' => function($bug) { return $bug->platform; },
182 'version' => function($bug) { return $bug->version; },
183 'fixed_in_version' => function($bug) { return $bug->fixed_in_version; },
184 'target_version' => function($bug) { return $bug->target_version; },
185 'build' => function($bug) { return $bug->build; },
186 'summary' => function($bug) use($self) { return $self->format_summary($bug); },
187 'last_updated' => function($bug) { return date( config_get( 'short_date_format' ), $bug->last_updated ); },
188 'date_submitted' => function($bug) { return date( config_get( 'short_date_format' ), $bug->date_submitted ); },
189 'due_date' => function($bug) { return date( config_get( 'short_date_format' ), $bug->due_date ); },
190 'description' => function($bug) use($self) { return $self->format_text( $bug, $bug->description ); },
191 'steps_to_reproduce' => function($bug) use($self) { return $self->format_text( $bug, $bug->steps_to_reproduce ); },
192 'additional_information' => function($bug) use($self) { return $self->format_text( $bug, $bug->additional_information ); },
193 );
194 // Discover custom fields.
195 $t_related_custom_field_ids = custom_field_get_linked_ids( $bug->project_id );
196 foreach ( $t_related_custom_field_ids as $t_id ) {
197 $t_def = custom_field_get_definition( $t_id );
198 $values['custom_' . $t_def['name']] = function($bug) use ($t_id) {
199 return custom_field_get_value( $t_id, $bug->id );
200 };
201 }
202 if (isset($values[$field_name])) {
203 $func = $values[$field_name];
204 return $func($bug);
205 }
206 else {
207 return FALSE;
208 }
209 }
210  
211 function notify($msg) {
212 if ($this->skip) return;
213  
214 $client = new MatrixClient(plugin_config_get('server'));
215 $token = $client->login(
216 plugin_config_get('username'),
217 plugin_config_get('password')
218 );
219  
220 $room = $client->joinRoom(plugin_config_get('room'));
221 $response = $room->sendHtml($msg);
222 }
223 }