/
DifferentialJIRAIssuesField.php
313 lines (249 loc) · 7.92 KB
/
DifferentialJIRAIssuesField.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
<?php
final class DifferentialJIRAIssuesField
extends DifferentialStoredCustomField {
private $error;
public function getFieldKey() {
return 'phabricator:jira-issues';
}
public function getFieldKeyForConduit() {
return 'jira.issues';
}
public function isFieldEnabled() {
return (bool)PhabricatorJIRAAuthProvider::getJIRAProvider();
}
public function canDisableField() {
return false;
}
public function getValueForStorage() {
return json_encode($this->getValue());
}
public function setValueFromStorage($value) {
try {
$this->setValue(phutil_json_decode($value));
} catch (PhutilJSONParserException $ex) {
$this->setValue(array());
}
return $this;
}
public function getFieldName() {
return pht('JIRA Issues');
}
public function getFieldDescription() {
return pht('Lists associated JIRA issues.');
}
public function shouldAppearInPropertyView() {
return true;
}
public function renderPropertyViewLabel() {
return $this->getFieldName();
}
public function renderPropertyViewValue(array $handles) {
$xobjs = $this->loadDoorkeeperExternalObjects($this->getValue());
if (!$xobjs) {
return null;
}
$links = array();
foreach ($xobjs as $xobj) {
$links[] = id(new DoorkeeperTagView())
->setExternalObject($xobj);
}
return phutil_implode_html(phutil_tag('br'), $links);
}
private function buildDoorkeeperRefs($value) {
$provider = PhabricatorJIRAAuthProvider::getJIRAProvider();
$refs = array();
if ($value) {
foreach ($value as $jira_key) {
$refs[] = id(new DoorkeeperObjectRef())
->setApplicationType(DoorkeeperBridgeJIRA::APPTYPE_JIRA)
->setApplicationDomain($provider->getProviderDomain())
->setObjectType(DoorkeeperBridgeJIRA::OBJTYPE_ISSUE)
->setObjectID($jira_key);
}
}
return $refs;
}
private function loadDoorkeeperExternalObjects($value) {
$refs = $this->buildDoorkeeperRefs($value);
if (!$refs) {
return array();
}
$xobjs = id(new DoorkeeperExternalObjectQuery())
->setViewer($this->getViewer())
->withObjectKeys(mpull($refs, 'getObjectKey'))
->execute();
return $xobjs;
}
public function shouldAppearInEditView() {
return PhabricatorJIRAAuthProvider::getJIRAProvider();
}
public function shouldAppearInApplicationTransactions() {
return PhabricatorJIRAAuthProvider::getJIRAProvider();
}
public function readValueFromRequest(AphrontRequest $request) {
$this->setValue($request->getStrList($this->getFieldKey()));
return $this;
}
public function renderEditControl(array $handles) {
return id(new AphrontFormTextControl())
->setLabel(pht('JIRA Issues'))
->setCaption(
pht('Example: %s', phutil_tag('tt', array(), 'JIS-3, JIS-9')))
->setName($this->getFieldKey())
->setValue(implode(', ', nonempty($this->getValue(), array())))
->setError($this->error);
}
public function getOldValueForApplicationTransactions() {
return array_unique(nonempty($this->getValue(), array()));
}
public function getNewValueForApplicationTransactions() {
return array_unique(nonempty($this->getValue(), array()));
}
public function validateApplicationTransactions(
PhabricatorApplicationTransactionEditor $editor,
$type,
array $xactions) {
$this->error = null;
$errors = parent::validateApplicationTransactions(
$editor,
$type,
$xactions);
$transaction = null;
foreach ($xactions as $xaction) {
$old = $xaction->getOldValue();
$new = $xaction->getNewValue();
$add = array_diff($new, $old);
if (!$add) {
continue;
}
// Only check that the actor can see newly added JIRA refs. You're
// allowed to remove refs or make no-op changes even if you aren't
// linked to JIRA.
try {
$refs = id(new DoorkeeperImportEngine())
->setViewer($this->getViewer())
->setRefs($this->buildDoorkeeperRefs($add))
->setThrowOnMissingLink(true)
->execute();
} catch (DoorkeeperMissingLinkException $ex) {
$this->error = pht('Not Linked');
$errors[] = new PhabricatorApplicationTransactionValidationError(
$type,
pht('Not Linked'),
pht(
'You can not add JIRA issues (%s) to this revision because your '.
'Phabricator account is not linked to a JIRA account.',
implode(', ', $add)),
$xaction);
continue;
}
$bad = array();
foreach ($refs as $ref) {
if (!$ref->getIsVisible()) {
$bad[] = $ref->getObjectID();
}
}
if ($bad) {
$bad = implode(', ', $bad);
$this->error = pht('Invalid');
$errors[] = new PhabricatorApplicationTransactionValidationError(
$type,
pht('Invalid'),
pht(
'Some JIRA issues could not be loaded. They may not exist, or '.
'you may not have permission to view them: %s',
$bad),
$xaction);
}
}
return $errors;
}
public function getApplicationTransactionTitle(
PhabricatorApplicationTransaction $xaction) {
$old = $xaction->getOldValue();
if (!is_array($old)) {
$old = array();
}
$new = $xaction->getNewValue();
if (!is_array($new)) {
$new = array();
}
$add = array_diff($new, $old);
$rem = array_diff($old, $new);
$author_phid = $xaction->getAuthorPHID();
if ($add && $rem) {
return pht(
'%s updated JIRA issue(s): added %d %s; removed %d %s.',
$xaction->renderHandleLink($author_phid),
phutil_count($add),
implode(', ', $add),
phutil_count($rem),
implode(', ', $rem));
} else if ($add) {
return pht(
'%s added %d JIRA issue(s): %s.',
$xaction->renderHandleLink($author_phid),
phutil_count($add),
implode(', ', $add));
} else if ($rem) {
return pht(
'%s removed %d JIRA issue(s): %s.',
$xaction->renderHandleLink($author_phid),
phutil_count($rem),
implode(', ', $rem));
}
return parent::getApplicationTransactionTitle($xaction);
}
public function applyApplicationTransactionExternalEffects(
PhabricatorApplicationTransaction $xaction) {
// Update the CustomField storage.
parent::applyApplicationTransactionExternalEffects($xaction);
// Now, synchronize the Doorkeeper edges.
$revision = $this->getObject();
$revision_phid = $revision->getPHID();
$edge_type = PhabricatorJiraIssueHasObjectEdgeType::EDGECONST;
$xobjs = $this->loadDoorkeeperExternalObjects($xaction->getNewValue());
$edge_dsts = mpull($xobjs, 'getPHID');
$edges = PhabricatorEdgeQuery::loadDestinationPHIDs(
$revision_phid,
$edge_type);
$editor = new PhabricatorEdgeEditor();
foreach (array_diff($edges, $edge_dsts) as $rem_edge) {
$editor->removeEdge($revision_phid, $edge_type, $rem_edge);
}
foreach (array_diff($edge_dsts, $edges) as $add_edge) {
$editor->addEdge($revision_phid, $edge_type, $add_edge);
}
$editor->save();
}
public function shouldAppearInCommitMessage() {
return true;
}
public function shouldAppearInCommitMessageTemplate() {
return true;
}
public function getCommitMessageLabels() {
return array(
'JIRA',
'JIRA Issues',
'JIRA Issue',
);
}
public function parseValueFromCommitMessage($value) {
return preg_split('/[\s,]+/', $value, $limit = -1, PREG_SPLIT_NO_EMPTY);
}
public function readValueFromCommitMessage($value) {
$this->setValue($value);
return $this;
}
public function renderCommitMessageValue(array $handles) {
$value = $this->getValue();
if (!$value) {
return null;
}
return implode(', ', $value);
}
public function shouldAppearInConduitDictionary() {
return true;
}
}