Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/audit/storage/PhabricatorAuditTransaction.php
12256 views
1
<?php
2
3
final class PhabricatorAuditTransaction
4
extends PhabricatorModularTransaction {
5
6
const TYPE_COMMIT = 'audit:commit';
7
8
const MAILTAG_ACTION_CONCERN = 'audit-action-concern';
9
const MAILTAG_ACTION_ACCEPT = 'audit-action-accept';
10
const MAILTAG_ACTION_RESIGN = 'audit-action-resign';
11
const MAILTAG_ACTION_CLOSE = 'audit-action-close';
12
const MAILTAG_ADD_AUDITORS = 'audit-add-auditors';
13
const MAILTAG_ADD_CCS = 'audit-add-ccs';
14
const MAILTAG_COMMENT = 'audit-comment';
15
const MAILTAG_COMMIT = 'audit-commit';
16
const MAILTAG_PROJECTS = 'audit-projects';
17
const MAILTAG_OTHER = 'audit-other';
18
19
public function getApplicationName() {
20
return 'audit';
21
}
22
23
public function getBaseTransactionClass() {
24
return 'DiffusionCommitTransactionType';
25
}
26
27
public function getApplicationTransactionType() {
28
return PhabricatorRepositoryCommitPHIDType::TYPECONST;
29
}
30
31
public function getApplicationTransactionCommentObject() {
32
return new PhabricatorAuditTransactionComment();
33
}
34
35
public function getRemarkupBlocks() {
36
$blocks = parent::getRemarkupBlocks();
37
38
switch ($this->getTransactionType()) {
39
case self::TYPE_COMMIT:
40
$data = $this->getNewValue();
41
$blocks[] = $data['description'];
42
break;
43
}
44
45
return $blocks;
46
}
47
48
public function getActionStrength() {
49
$type = $this->getTransactionType();
50
51
switch ($type) {
52
case self::TYPE_COMMIT:
53
return 300;
54
}
55
56
return parent::getActionStrength();
57
}
58
59
public function getRequiredHandlePHIDs() {
60
$phids = parent::getRequiredHandlePHIDs();
61
62
$type = $this->getTransactionType();
63
64
switch ($type) {
65
case self::TYPE_COMMIT:
66
$phids[] = $this->getObjectPHID();
67
$data = $this->getNewValue();
68
if ($data['authorPHID']) {
69
$phids[] = $data['authorPHID'];
70
}
71
if ($data['committerPHID']) {
72
$phids[] = $data['committerPHID'];
73
}
74
break;
75
case PhabricatorAuditActionConstants::ADD_CCS:
76
case PhabricatorAuditActionConstants::ADD_AUDITORS:
77
$old = $this->getOldValue();
78
$new = $this->getNewValue();
79
80
if (!is_array($old)) {
81
$old = array();
82
}
83
if (!is_array($new)) {
84
$new = array();
85
}
86
87
foreach (array_keys($old + $new) as $phid) {
88
$phids[] = $phid;
89
}
90
break;
91
}
92
93
return $phids;
94
}
95
96
public function getActionName() {
97
98
switch ($this->getTransactionType()) {
99
case PhabricatorAuditActionConstants::ACTION:
100
switch ($this->getNewValue()) {
101
case PhabricatorAuditActionConstants::CONCERN:
102
return pht('Raised Concern');
103
case PhabricatorAuditActionConstants::ACCEPT:
104
return pht('Accepted');
105
case PhabricatorAuditActionConstants::RESIGN:
106
return pht('Resigned');
107
case PhabricatorAuditActionConstants::CLOSE:
108
return pht('Closed');
109
}
110
break;
111
case PhabricatorAuditActionConstants::ADD_AUDITORS:
112
return pht('Added Auditors');
113
case self::TYPE_COMMIT:
114
return pht('Committed');
115
}
116
117
return parent::getActionName();
118
}
119
120
public function getColor() {
121
122
$type = $this->getTransactionType();
123
124
switch ($type) {
125
case PhabricatorAuditActionConstants::ACTION:
126
switch ($this->getNewValue()) {
127
case PhabricatorAuditActionConstants::CONCERN:
128
return 'red';
129
case PhabricatorAuditActionConstants::ACCEPT:
130
return 'green';
131
case PhabricatorAuditActionConstants::RESIGN:
132
return 'black';
133
case PhabricatorAuditActionConstants::CLOSE:
134
return 'indigo';
135
}
136
}
137
138
return parent::getColor();
139
}
140
141
public function getIcon() {
142
143
$type = $this->getTransactionType();
144
145
switch ($type) {
146
case PhabricatorAuditActionConstants::ACTION:
147
switch ($this->getNewValue()) {
148
case PhabricatorAuditActionConstants::CONCERN:
149
return 'fa-exclamation-circle';
150
case PhabricatorAuditActionConstants::ACCEPT:
151
return 'fa-check';
152
case PhabricatorAuditActionConstants::RESIGN:
153
return 'fa-plane';
154
case PhabricatorAuditActionConstants::CLOSE:
155
return 'fa-check';
156
}
157
}
158
159
return parent::getIcon();
160
}
161
162
public function getTitle() {
163
$old = $this->getOldValue();
164
$new = $this->getNewValue();
165
166
$author_handle = $this->renderHandleLink($this->getAuthorPHID());
167
168
$type = $this->getTransactionType();
169
170
switch ($type) {
171
case PhabricatorAuditActionConstants::ADD_CCS:
172
case PhabricatorAuditActionConstants::ADD_AUDITORS:
173
if (!is_array($old)) {
174
$old = array();
175
}
176
if (!is_array($new)) {
177
$new = array();
178
}
179
$add = array_keys(array_diff_key($new, $old));
180
$rem = array_keys(array_diff_key($old, $new));
181
break;
182
}
183
184
switch ($type) {
185
case self::TYPE_COMMIT:
186
$author = null;
187
if ($new['authorPHID']) {
188
$author = $this->renderHandleLink($new['authorPHID']);
189
} else {
190
$author = $new['authorName'];
191
}
192
193
$committer = null;
194
if ($new['committerPHID']) {
195
$committer = $this->renderHandleLink($new['committerPHID']);
196
} else if ($new['committerName']) {
197
$committer = $new['committerName'];
198
}
199
200
$commit = $this->renderHandleLink($this->getObjectPHID());
201
202
if (!$committer) {
203
$committer = $author;
204
$author = null;
205
}
206
207
if ($author) {
208
$title = pht(
209
'%s committed %s (authored by %s).',
210
$committer,
211
$commit,
212
$author);
213
} else {
214
$title = pht(
215
'%s committed %s.',
216
$committer,
217
$commit);
218
}
219
return $title;
220
221
case PhabricatorAuditActionConstants::INLINE:
222
return pht(
223
'%s added inline comments.',
224
$author_handle);
225
226
case PhabricatorAuditActionConstants::ADD_CCS:
227
if ($add && $rem) {
228
return pht(
229
'%s edited subscribers; added: %s, removed: %s.',
230
$author_handle,
231
$this->renderHandleList($add),
232
$this->renderHandleList($rem));
233
} else if ($add) {
234
return pht(
235
'%s added subscribers: %s.',
236
$author_handle,
237
$this->renderHandleList($add));
238
} else if ($rem) {
239
return pht(
240
'%s removed subscribers: %s.',
241
$author_handle,
242
$this->renderHandleList($rem));
243
} else {
244
return pht(
245
'%s added subscribers...',
246
$author_handle);
247
}
248
249
case PhabricatorAuditActionConstants::ADD_AUDITORS:
250
if ($add && $rem) {
251
return pht(
252
'%s edited auditors; added: %s, removed: %s.',
253
$author_handle,
254
$this->renderHandleList($add),
255
$this->renderHandleList($rem));
256
} else if ($add) {
257
return pht(
258
'%s added auditors: %s.',
259
$author_handle,
260
$this->renderHandleList($add));
261
} else if ($rem) {
262
return pht(
263
'%s removed auditors: %s.',
264
$author_handle,
265
$this->renderHandleList($rem));
266
} else {
267
return pht(
268
'%s added auditors...',
269
$author_handle);
270
}
271
272
case PhabricatorAuditActionConstants::ACTION:
273
switch ($new) {
274
case PhabricatorAuditActionConstants::ACCEPT:
275
return pht(
276
'%s accepted this commit.',
277
$author_handle);
278
case PhabricatorAuditActionConstants::CONCERN:
279
return pht(
280
'%s raised a concern with this commit.',
281
$author_handle);
282
case PhabricatorAuditActionConstants::RESIGN:
283
return pht(
284
'%s resigned from this audit.',
285
$author_handle);
286
case PhabricatorAuditActionConstants::CLOSE:
287
return pht(
288
'%s closed this audit.',
289
$author_handle);
290
}
291
292
}
293
294
return parent::getTitle();
295
}
296
297
public function getTitleForFeed() {
298
$old = $this->getOldValue();
299
$new = $this->getNewValue();
300
301
$author_handle = $this->renderHandleLink($this->getAuthorPHID());
302
$object_handle = $this->renderHandleLink($this->getObjectPHID());
303
304
$type = $this->getTransactionType();
305
306
switch ($type) {
307
case PhabricatorAuditActionConstants::ADD_CCS:
308
case PhabricatorAuditActionConstants::ADD_AUDITORS:
309
if (!is_array($old)) {
310
$old = array();
311
}
312
if (!is_array($new)) {
313
$new = array();
314
}
315
$add = array_keys(array_diff_key($new, $old));
316
$rem = array_keys(array_diff_key($old, $new));
317
break;
318
}
319
320
switch ($type) {
321
case self::TYPE_COMMIT:
322
$author = null;
323
if ($new['authorPHID']) {
324
$author = $this->renderHandleLink($new['authorPHID']);
325
} else {
326
$author = $new['authorName'];
327
}
328
329
$committer = null;
330
if ($new['committerPHID']) {
331
$committer = $this->renderHandleLink($new['committerPHID']);
332
} else if ($new['committerName']) {
333
$committer = $new['committerName'];
334
}
335
336
if (!$committer) {
337
$committer = $author;
338
$author = null;
339
}
340
341
if ($author) {
342
$title = pht(
343
'%s committed %s (authored by %s).',
344
$committer,
345
$object_handle,
346
$author);
347
} else {
348
$title = pht(
349
'%s committed %s.',
350
$committer,
351
$object_handle);
352
}
353
return $title;
354
355
case PhabricatorAuditActionConstants::INLINE:
356
return pht(
357
'%s added inline comments to %s.',
358
$author_handle,
359
$object_handle);
360
361
case PhabricatorAuditActionConstants::ADD_AUDITORS:
362
if ($add && $rem) {
363
return pht(
364
'%s edited auditors for %s; added: %s, removed: %s.',
365
$author_handle,
366
$object_handle,
367
$this->renderHandleList($add),
368
$this->renderHandleList($rem));
369
} else if ($add) {
370
return pht(
371
'%s added auditors to %s: %s.',
372
$author_handle,
373
$object_handle,
374
$this->renderHandleList($add));
375
} else if ($rem) {
376
return pht(
377
'%s removed auditors from %s: %s.',
378
$author_handle,
379
$object_handle,
380
$this->renderHandleList($rem));
381
} else {
382
return pht(
383
'%s added auditors to %s...',
384
$author_handle,
385
$object_handle);
386
}
387
388
case PhabricatorAuditActionConstants::ACTION:
389
switch ($new) {
390
case PhabricatorAuditActionConstants::ACCEPT:
391
return pht(
392
'%s accepted %s.',
393
$author_handle,
394
$object_handle);
395
case PhabricatorAuditActionConstants::CONCERN:
396
return pht(
397
'%s raised a concern with %s.',
398
$author_handle,
399
$object_handle);
400
case PhabricatorAuditActionConstants::RESIGN:
401
return pht(
402
'%s resigned from auditing %s.',
403
$author_handle,
404
$object_handle);
405
case PhabricatorAuditActionConstants::CLOSE:
406
return pht(
407
'%s closed the audit of %s.',
408
$author_handle,
409
$object_handle);
410
}
411
412
}
413
414
return parent::getTitleForFeed();
415
}
416
417
public function getBodyForFeed(PhabricatorFeedStory $story) {
418
switch ($this->getTransactionType()) {
419
case self::TYPE_COMMIT:
420
$data = $this->getNewValue();
421
return $story->renderSummary($data['summary']);
422
}
423
return parent::getBodyForFeed($story);
424
}
425
426
public function isInlineCommentTransaction() {
427
switch ($this->getTransactionType()) {
428
case PhabricatorAuditActionConstants::INLINE:
429
return true;
430
}
431
432
return parent::isInlineCommentTransaction();
433
}
434
435
public function getBodyForMail() {
436
switch ($this->getTransactionType()) {
437
case self::TYPE_COMMIT:
438
$data = $this->getNewValue();
439
return $data['description'];
440
}
441
442
return parent::getBodyForMail();
443
}
444
445
public function getMailTags() {
446
$tags = array();
447
switch ($this->getTransactionType()) {
448
case DiffusionCommitAcceptTransaction::TRANSACTIONTYPE:
449
$tags[] = self::MAILTAG_ACTION_ACCEPT;
450
break;
451
case DiffusionCommitConcernTransaction::TRANSACTIONTYPE:
452
$tags[] = self::MAILTAG_ACTION_CONCERN;
453
break;
454
case DiffusionCommitResignTransaction::TRANSACTIONTYPE:
455
$tags[] = self::MAILTAG_ACTION_RESIGN;
456
break;
457
case DiffusionCommitAuditorsTransaction::TRANSACTIONTYPE:
458
$tags[] = self::MAILTAG_ADD_AUDITORS;
459
break;
460
case PhabricatorAuditActionConstants::ACTION:
461
switch ($this->getNewValue()) {
462
case PhabricatorAuditActionConstants::CONCERN:
463
$tags[] = self::MAILTAG_ACTION_CONCERN;
464
break;
465
case PhabricatorAuditActionConstants::ACCEPT:
466
$tags[] = self::MAILTAG_ACTION_ACCEPT;
467
break;
468
case PhabricatorAuditActionConstants::RESIGN:
469
$tags[] = self::MAILTAG_ACTION_RESIGN;
470
break;
471
case PhabricatorAuditActionConstants::CLOSE:
472
$tags[] = self::MAILTAG_ACTION_CLOSE;
473
break;
474
}
475
break;
476
case PhabricatorAuditActionConstants::ADD_AUDITORS:
477
$tags[] = self::MAILTAG_ADD_AUDITORS;
478
break;
479
case PhabricatorAuditActionConstants::ADD_CCS:
480
$tags[] = self::MAILTAG_ADD_CCS;
481
break;
482
case PhabricatorAuditActionConstants::INLINE:
483
case PhabricatorTransactions::TYPE_COMMENT:
484
$tags[] = self::MAILTAG_COMMENT;
485
break;
486
case self::TYPE_COMMIT:
487
$tags[] = self::MAILTAG_COMMIT;
488
break;
489
case PhabricatorTransactions::TYPE_EDGE:
490
switch ($this->getMetadataValue('edge:type')) {
491
case PhabricatorProjectObjectHasProjectEdgeType::EDGECONST:
492
$tags[] = self::MAILTAG_PROJECTS;
493
break;
494
case PhabricatorObjectHasSubscriberEdgeType::EDGECONST:
495
$tags[] = self::MAILTAG_ADD_CCS;
496
break;
497
default:
498
$tags[] = self::MAILTAG_OTHER;
499
break;
500
}
501
break;
502
default:
503
$tags[] = self::MAILTAG_OTHER;
504
break;
505
}
506
return $tags;
507
}
508
509
public function shouldDisplayGroupWith(array $group) {
510
// Make the "This commit now requires audit." state message stand alone.
511
$type_state = DiffusionCommitStateTransaction::TRANSACTIONTYPE;
512
513
if ($this->getTransactionType() == $type_state) {
514
return false;
515
}
516
517
foreach ($group as $xaction) {
518
if ($xaction->getTransactionType() == $type_state) {
519
return false;
520
}
521
}
522
523
return parent::shouldDisplayGroupWith($group);
524
}
525
526
}
527
528