Differential D25028 Diff 81 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(); | |||||
/** | |||||
* @var string | |||||
*/ | |||||
protected $authorPHID = null; | |||||
/** | |||||
* @var string[] | |||||
*/ | |||||
protected $coAuthors = 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) { | |||||
$this->authorPHID = $revision->getAuthorPHID(); | |||||
$diff_properties = $revision->getActiveDiff() | |||||
->getDiffAuthorshipDict()['properties']; | |||||
if (!array_key_exists('local:commits', $diff_properties)) { | |||||
return ''; | |||||
} | |||||
foreach ($diff_properties['local:commits'] as $commit) { | |||||
$this->addCoAuthor($commit['author'], $commit['authorEmail']); | |||||
if (array_key_exists('coAuthors', $commit)) { | |||||
foreach ($commit['coAuthors'] as $co_author) { | |||||
$this->addCoAuthor($co_author['name'], $co_author['email']); | |||||
} | |||||
} | |||||
} | |||||
return implode("\n", $this->getCoAuthors()); | |||||
} | |||||
/** | |||||
* @param string $name | |||||
* @param string $email | |||||
* | |||||
* @return void | |||||
*/ | |||||
protected function addCoAuthor($name, $email) { | |||||
if ($this->getUserPHID($email) === $this->authorPHID) { | |||||
return; | |||||
} | |||||
$this->coAuthors[] = pht('Co-authored-by: %s <%s>', $name, $email); | |||||
} | |||||
/** | |||||
* @return string[] | |||||
*/ | |||||
protected function getCoAuthors() { | |||||
return array_unique($this->coAuthors); | |||||
} | |||||
} |
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