Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/diffusion/doorkeeper/DiffusionDoorkeeperCommitFeedStoryPublisher.php
12241 views
1
<?php
2
3
final class DiffusionDoorkeeperCommitFeedStoryPublisher
4
extends DoorkeeperFeedStoryPublisher {
5
6
private $auditRequests;
7
private $activePHIDs;
8
private $passivePHIDs;
9
10
private function getAuditRequests() {
11
return $this->auditRequests;
12
}
13
14
public function canPublishStory(PhabricatorFeedStory $story, $object) {
15
return
16
($story instanceof PhabricatorApplicationTransactionFeedStory) &&
17
($object instanceof PhabricatorRepositoryCommit);
18
}
19
20
public function isStoryAboutObjectCreation($object) {
21
// TODO: Although creation stories exist, they currently don't have a
22
// primary object PHID set, so they'll never make it here because they
23
// won't pass `canPublishStory()`.
24
return false;
25
}
26
27
public function isStoryAboutObjectClosure($object) {
28
// TODO: This isn't quite accurate, but pretty close: check if this story
29
// is a close (which clearly is about object closure) or is an "Accept" and
30
// the commit is fully audited (which is almost certainly a closure).
31
// After ApplicationTransactions, we could annotate feed stories more
32
// explicitly.
33
34
$story = $this->getFeedStory();
35
$xaction = $story->getPrimaryTransaction();
36
switch ($xaction->getTransactionType()) {
37
case PhabricatorAuditActionConstants::ACTION:
38
switch ($xaction->getNewValue()) {
39
case PhabricatorAuditActionConstants::CLOSE:
40
return true;
41
case PhabricatorAuditActionConstants::ACCEPT:
42
if ($object->isAuditStatusAudited()) {
43
return true;
44
}
45
break;
46
}
47
}
48
49
return false;
50
}
51
52
public function willPublishStory($commit) {
53
$requests = id(new DiffusionCommitQuery())
54
->setViewer($this->getViewer())
55
->withPHIDs(array($commit->getPHID()))
56
->needAuditRequests(true)
57
->executeOne()
58
->getAudits();
59
60
// TODO: This is messy and should be generalized, but we don't have a good
61
// query for it yet. Since we run in the daemons, just do the easiest thing
62
// we can for the moment. Figure out who all of the "active" (need to
63
// audit) and "passive" (no action necessary) users are.
64
65
$auditor_phids = mpull($requests, 'getAuditorPHID');
66
$objects = id(new PhabricatorObjectQuery())
67
->setViewer($this->getViewer())
68
->withPHIDs($auditor_phids)
69
->execute();
70
71
$active = array();
72
$passive = array();
73
74
foreach ($requests as $request) {
75
$status = $request->getAuditStatus();
76
77
$object = idx($objects, $request->getAuditorPHID());
78
if (!$object) {
79
continue;
80
}
81
82
$request_phids = array();
83
if ($object instanceof PhabricatorUser) {
84
$request_phids = array($object->getPHID());
85
} else if ($object instanceof PhabricatorOwnersPackage) {
86
$request_phids = PhabricatorOwnersOwner::loadAffiliatedUserPHIDs(
87
array($object->getID()));
88
} else if ($object instanceof PhabricatorProject) {
89
$project = id(new PhabricatorProjectQuery())
90
->setViewer($this->getViewer())
91
->withIDs(array($object->getID()))
92
->needMembers(true)
93
->executeOne();
94
$request_phids = $project->getMemberPHIDs();
95
} else {
96
// Dunno what this is.
97
$request_phids = array();
98
}
99
100
switch ($status) {
101
case PhabricatorAuditRequestStatus::AUDIT_REQUIRED:
102
case PhabricatorAuditRequestStatus::AUDIT_REQUESTED:
103
case PhabricatorAuditRequestStatus::CONCERNED:
104
$active += array_fuse($request_phids);
105
break;
106
default:
107
$passive += array_fuse($request_phids);
108
break;
109
}
110
}
111
112
113
// Remove "Active" users from the "Passive" list.
114
$passive = array_diff_key($passive, $active);
115
116
$this->activePHIDs = $active;
117
$this->passivePHIDs = $passive;
118
$this->auditRequests = $requests;
119
120
return $commit;
121
}
122
123
public function getOwnerPHID($object) {
124
return $object->getAuthorPHID();
125
}
126
127
public function getActiveUserPHIDs($object) {
128
return $this->activePHIDs;
129
}
130
131
public function getPassiveUserPHIDs($object) {
132
return $this->passivePHIDs;
133
}
134
135
public function getCCUserPHIDs($object) {
136
return PhabricatorSubscribersQuery::loadSubscribersForPHID(
137
$object->getPHID());
138
}
139
140
public function getObjectTitle($object) {
141
$prefix = $this->getTitlePrefix($object);
142
143
$repository = $object->getRepository();
144
$name = $repository->formatCommitName($object->getCommitIdentifier());
145
146
$title = $object->getSummary();
147
148
return ltrim("{$prefix} {$name}: {$title}");
149
}
150
151
public function getObjectURI($object) {
152
$repository = $object->getRepository();
153
$name = $repository->formatCommitName($object->getCommitIdentifier());
154
return PhabricatorEnv::getProductionURI('/'.$name);
155
}
156
157
public function getObjectDescription($object) {
158
$data = $object->loadCommitData();
159
if ($data) {
160
return $data->getCommitMessage();
161
}
162
return null;
163
}
164
165
public function isObjectClosed($object) {
166
return $object->getAuditStatusObject()->getIsClosed();
167
}
168
169
public function getResponsibilityTitle($object) {
170
$prefix = $this->getTitlePrefix($object);
171
return pht('%s Audit', $prefix);
172
}
173
174
private function getTitlePrefix(PhabricatorRepositoryCommit $commit) {
175
return pht('[Diffusion]');
176
}
177
178
}
179
180