Record new commits in the push log

Summary:
Ref T4195. Like the previous diffs, these both create a useful log and give us an object to hand off to Herald.

Surface this information in Diffusion, too, and clean things up a little bit.

Test Plan: {F87565}

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T4195

Differential Revision: https://secure.phabricator.com/D7718
This commit is contained in:
epriestley
2013-12-05 11:59:41 -08:00
parent 3f50460149
commit 2ff5541fc5
4 changed files with 144 additions and 40 deletions

View File

@@ -18,6 +18,7 @@ final class DiffusionCommitHookEngine extends Phobject {
private $subversionRepository;
private $remoteAddress;
private $remoteProtocol;
private $transactionKey;
public function setRemoteProtocol($remote_protocol) {
$this->remoteProtocol = $remote_protocol;
@@ -37,6 +38,23 @@ final class DiffusionCommitHookEngine extends Phobject {
return $this->remoteAddress;
}
private function getRemoteAddressForLog() {
// If whatever we have here isn't a valid IPv4 address, just store `null`.
// Older versions of PHP return `-1` on failure instead of `false`.
$remote_address = $this->getRemoteAddress();
$remote_address = max(0, ip2long($remote_address));
$remote_address = nonempty($remote_address, null);
return $remote_address;
}
private function getTransactionKey() {
if (!$this->transactionKey) {
$entropy = Filesystem::readRandomBytes(64);
$this->transactionKey = PhabricatorHash::digestForIndex($entropy);
}
return $this->transactionKey;
}
public function setSubversionTransactionInfo($transaction, $repository) {
$this->subversionTransaction = $transaction;
$this->subversionRepository = $repository;
@@ -89,6 +107,18 @@ final class DiffusionCommitHookEngine extends Phobject {
return $err;
}
private function newPushLog() {
return PhabricatorRepositoryPushLog::initializeNewLog($this->getViewer())
->setRepositoryPHID($this->getRepository()->getPHID())
->setEpoch(time())
->setRemoteAddress($this->getRemoteAddressForLog())
->setRemoteProtocol($this->getRemoteProtocol())
->setTransactionKey($this->getTransactionKey())
->setRejectCode(PhabricatorRepositoryPushLog::REJECT_ACCEPT)
->setRejectDetails(null);
}
/**
* @task git
*/
@@ -106,34 +136,17 @@ final class DiffusionCommitHookEngine extends Phobject {
// TODO: Now, do content checks.
// TODO: Generalize this; just getting some data in the database for now.
$transaction_key = PhabricatorHash::digestForIndex(
Filesystem::readRandomBytes(64));
// If whatever we have here isn't a valid IPv4 address, just store `null`.
// Older versions of PHP return `-1` on failure instead of `false`.
$remote_address = $this->getRemoteAddress();
$remote_address = max(0, ip2long($remote_address));
$remote_address = nonempty($remote_address, null);
$remote_protocol = $this->getRemoteProtocol();
$logs = array();
foreach ($updates as $update) {
$log = PhabricatorRepositoryPushLog::initializeNewLog($this->getViewer())
->setRepositoryPHID($this->getRepository()->getPHID())
->setEpoch(time())
->setRemoteAddress($remote_address)
->setRemoteProtocol($remote_protocol)
->setTransactionKey($transaction_key)
$log = $this->newPushLog()
->setRefType($update['type'])
->setRefNameHash(PhabricatorHash::digestForIndex($update['ref']))
->setRefNameRaw($update['ref'])
->setRefNameEncoding(phutil_is_utf8($update['ref']) ? 'utf8' : null)
->setRefOld($update['old'])
->setRefNew($update['new'])
->setMergeBase(idx($update, 'merge-base'))
->setRejectCode(PhabricatorRepositoryPushLog::REJECT_ACCEPT)
->setRejectDetails(null);
->setMergeBase(idx($update, 'merge-base'));
$flags = 0;
if ($update['operation'] == 'create') {
@@ -151,6 +164,18 @@ final class DiffusionCommitHookEngine extends Phobject {
$logs[] = $log;
}
// Now, build logs for all the commits.
// TODO: Generalize this, too.
$commits = array_mergev(ipull($updates, 'commits'));
$commits = array_unique($commits);
foreach ($commits as $commit) {
$log = $this->newPushLog()
->setRefType(PhabricatorRepositoryPushLog::REFTYPE_COMMIT)
->setRefNew($commit)
->setChangeFlags(PhabricatorRepositoryPushLog::CHANGEFLAG_ADD);
$logs[] = $log;
}
foreach ($logs as $log) {
$log->save();
}