Differential D25028 Diff 77 src/applications/differential/field/DifferentialCoAuthorsCommitMessageField.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/differential/field/DifferentialCoAuthorsCommitMessageField.php
- This file was added.
<?php | |||||
final class DifferentialCoAuthorsCommitMessageField | |||||
extends DifferentialCommitMessageField { | |||||
const FIELDKEY = 'co-authors'; | |||||
/** | |||||
* @var array<string, string|null> | |||||
*/ | |||||
protected $userEmailCache = array(); | |||||
/** | |||||
* @return string | |||||
*/ | |||||
public function getFieldName() { | |||||
return pht('Co-Authors'); | |||||
} | |||||
/** | |||||
* @return number | |||||
*/ | |||||
public function getFieldOrder() { | |||||
return 9999999; | |||||
} | |||||
/** | |||||
* @param string $value | |||||
* @return bool | |||||
*/ | |||||
public function validateFieldValue($value) { | |||||
return true; | |||||
} | |||||
/** | |||||
* @return bool | |||||
*/ | |||||
public function isFieldEditable() { | |||||
return false; | |||||
} | |||||
/** | |||||
* @return bool | |||||
*/ | |||||
public function isTemplateField() { | |||||
return false; | |||||
} | |||||
/** | |||||
* @param string $email | |||||
* | |||||
* @return string|null | |||||
*/ | |||||
protected function getUserPHID($email) { | |||||
if (!array_key_exists($email, $this->userEmailCache)) { | |||||
$this->userEmailCache[$email] = (new DiffusionResolveUserQuery()) | |||||
->withName($email) | |||||
->execute(); | |||||
} | |||||
return $this->userEmailCache[$email]; | |||||
} | |||||
/** | |||||
* @param DifferentialRevision<mixed> $revision | |||||
* | |||||
* @return string | |||||
*/ | |||||
public function readFieldValueFromObject(DifferentialRevision $revision) { | |||||
$author_id = $revision->getAuthorPHID(); | |||||
$diff_properties = $revision->getActiveDiff() | |||||
->getDiffAuthorshipDict()['properties']; | |||||
$co_authors = array(); | |||||
if (!array_key_exists('local:commits', $diff_properties)) { | |||||
return ''; | |||||
} | |||||
foreach ($diff_properties['local:commits'] as $commit) { | |||||
if ($this->getUserPHID($commit['authorEmail']) === $author_id) { | |||||
continue; | |||||
} | |||||
$co_authors[] = pht( | |||||
'Co-authored-by: %s <%s>', | |||||
$commit['author'], | |||||
$commit['authorEmail']); | |||||
} | |||||
return implode("\n", array_unique($co_authors)); | |||||
} | |||||
} |
Content licensed under Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) unless otherwise noted; code licensed under Apache 2.0 or other open source licenses. · CC BY-SA 4.0 · Apache 2.0