1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-25 00:02:41 +01:00
phorge-phorge/src/applications/differential/storage/DifferentialDiff.php

722 lines
20 KiB
PHP
Raw Normal View History

2011-01-24 20:01:53 +01:00
<?php
final class DifferentialDiff
extends DifferentialDAO
implements
PhabricatorPolicyInterface,
PhabricatorExtendedPolicyInterface,
HarbormasterBuildableInterface,
HarbormasterCircleCIBuildableInterface,
PhabricatorApplicationTransactionInterface,
PhabricatorDestructibleInterface {
2011-01-24 20:01:53 +01:00
protected $revisionID;
protected $authorPHID;
protected $repositoryPHID;
protected $commitPHID;
2011-01-24 20:01:53 +01:00
protected $sourceMachine;
protected $sourcePath;
protected $sourceControlSystem;
protected $sourceControlBaseRevision;
protected $sourceControlPath;
protected $lintStatus;
protected $unitStatus;
protected $lineCount;
protected $branch;
protected $bookmark;
2011-01-24 20:01:53 +01:00
protected $creationMethod;
protected $repositoryUUID;
2011-01-24 20:01:53 +01:00
protected $description;
protected $viewPolicy;
2011-01-24 20:36:53 +01:00
private $unsavedChangesets = array();
private $changesets = self::ATTACHABLE;
private $revision = self::ATTACHABLE;
private $properties = array();
private $buildable = self::ATTACHABLE;
2011-01-24 20:36:53 +01:00
private $unitMessages = self::ATTACHABLE;
protected function getConfiguration() {
return array(
self::CONFIG_AUX_PHID => true,
self::CONFIG_COLUMN_SCHEMA => array(
'revisionID' => 'id?',
'authorPHID' => 'phid?',
'repositoryPHID' => 'phid?',
'sourceMachine' => 'text255?',
'sourcePath' => 'text255?',
'sourceControlSystem' => 'text64?',
'sourceControlBaseRevision' => 'text255?',
'sourceControlPath' => 'text255?',
'lintStatus' => 'uint32',
'unitStatus' => 'uint32',
'lineCount' => 'uint32',
'branch' => 'text255?',
'bookmark' => 'text255?',
'repositoryUUID' => 'text64?',
'commitPHID' => 'phid?',
// T6203/NULLABILITY
// These should be non-null; all diffs should have a creation method
// and the description should just be empty.
'creationMethod' => 'text255?',
'description' => 'text255?',
),
self::CONFIG_KEY_SCHEMA => array(
'revisionID' => array(
'columns' => array('revisionID'),
),
'key_commit' => array(
'columns' => array('commitPHID'),
),
),
) + parent::getConfiguration();
}
public function generatePHID() {
return PhabricatorPHID::generateNewPHID(
DifferentialDiffPHIDType::TYPECONST);
}
2011-01-24 20:36:53 +01:00
public function addUnsavedChangeset(DifferentialChangeset $changeset) {
2011-01-25 00:52:35 +01:00
if ($this->changesets === null) {
$this->changesets = array();
}
2011-01-24 20:36:53 +01:00
$this->unsavedChangesets[] = $changeset;
2011-01-25 00:52:35 +01:00
$this->changesets[] = $changeset;
return $this;
}
public function attachChangesets(array $changesets) {
assert_instances_of($changesets, 'DifferentialChangeset');
2011-01-25 00:52:35 +01:00
$this->changesets = $changesets;
2011-01-24 20:36:53 +01:00
return $this;
}
2011-01-25 00:52:35 +01:00
public function getChangesets() {
return $this->assertAttached($this->changesets);
2011-01-25 00:52:35 +01:00
}
2011-01-24 20:01:53 +01:00
public function loadChangesets() {
if (!$this->getID()) {
return array();
}
$changesets = id(new DifferentialChangeset())->loadAllWhere(
2011-01-24 20:01:53 +01:00
'diffID = %d',
$this->getID());
foreach ($changesets as $changeset) {
$changeset->attachDiff($this);
}
return $changesets;
2011-01-24 20:01:53 +01:00
}
2011-01-24 20:36:53 +01:00
public function save() {
Provide a script to completely destroy revisions Summary: Someone may or may not have accidentally uploaded secrets to Differential. Provide an administrative mechanism to permanently destroy a revision. Also fix some of the transaction handling code. Test Plan: $ ./scripts/differential/destroy_revision.php --trace D1 >>> [0] <connect> <<< [0] <connect> 1,060 us >>> [1] <query> SELECT * FROM `differential_revision` WHERE `id` = 1 <<< [1] <query> 473 us Really destroy 'D1: asdbas' forever? [y/N] y >>> [2] <connect> <<< [2] <connect> 628 us >>> [3] <query> START TRANSACTION <<< [3] <query> 190 us >>> [4] <query> SELECT * FROM `differential_diff` WHERE revisionID = 1 <<< [4] <query> 510 us >>> [5] <query> SAVEPOINT Aphront_Savepoint_1 <<< [5] <query> 122 us >>> [6] <query> SELECT * FROM `differential_changeset` WHERE diffID = 1 <<< [6] <query> 307 us >>> [7] <query> SAVEPOINT Aphront_Savepoint_2 <<< [7] <query> 241 us >>> [8] <query> SELECT * FROM `differential_hunk` WHERE changesetID = 1 <<< [8] <query> 212 us >>> [9] <query> DELETE FROM `differential_hunk` WHERE `id` = 1 <<< [9] <query> 216 us >>> [10] <query> DELETE FROM `differential_changeset` WHERE `id` = 1 <<< [10] <query> 154 us >>> [11] <query> SAVEPOINT Aphront_Savepoint_2 <<< [11] <query> 118 us >>> [12] <query> SELECT * FROM `differential_hunk` WHERE changesetID = 2 <<< [12] <query> 194 us >>> [13] <query> DELETE FROM `differential_hunk` WHERE `id` = 2 <<< [13] <query> 179 us >>> [14] <query> DELETE FROM `differential_changeset` WHERE `id` = 2 <<< [14] <query> 163 us >>> [15] <query> SAVEPOINT Aphront_Savepoint_2 <<< [15] <query> 105 us >>> [16] <query> SELECT * FROM `differential_hunk` WHERE changesetID = 3 <<< [16] <query> 211 us >>> [17] <query> DELETE FROM `differential_hunk` WHERE `id` = 3 <<< [17] <query> 159 us >>> [18] <query> DELETE FROM `differential_changeset` WHERE `id` = 3 <<< [18] <query> 152 us >>> [19] <query> SAVEPOINT Aphront_Savepoint_2 <<< [19] <query> 124 us >>> [20] <query> SELECT * FROM `differential_hunk` WHERE changesetID = 4 <<< [20] <query> 191 us >>> [21] <query> DELETE FROM `differential_hunk` WHERE `id` = 4 <<< [21] <query> 155 us >>> [22] <query> DELETE FROM `differential_changeset` WHERE `id` = 4 <<< [22] <query> 149 us >>> [23] <query> SELECT * FROM `differential_diffproperty` WHERE diffID = 1 <<< [23] <query> 242 us >>> [24] <query> DELETE FROM `differential_diffproperty` WHERE `id` = 1 <<< [24] <query> 196 us >>> [25] <query> DELETE FROM `differential_diff` WHERE `id` = 1 <<< [25] <query> 169 us >>> [26] <query> DELETE FROM `differential_relationship` WHERE revisionID = 1 <<< [26] <query> 178 us >>> [27] <query> DELETE FROM `differential_commit` WHERE revisionID = 1 <<< [27] <query> 164 us >>> [28] <query> SELECT * FROM `differential_comment` WHERE revisionID = 1 <<< [28] <query> 221 us >>> [29] <query> DELETE FROM `differential_comment` WHERE `id` = 1 <<< [29] <query> 172 us >>> [30] <query> SELECT * FROM `differential_inlinecomment` WHERE revisionID = 1 <<< [30] <query> 296 us >>> [31] <query> SELECT * FROM `differential_auxiliaryfield` WHERE revisionPHID = 'PHID-DREV-ooky7ozqukpmwget32oc' <<< [31] <query> 308 us >>> [32] <query> SELECT * FROM `differential_affectedpath` WHERE revisionID = 1 <<< [32] <query> 4,173 us >>> [33] <query> DELETE FROM `differential_revision` WHERE `id` = 1 <<< [33] <query> 231 us >>> [34] <query> COMMIT <<< [34] <query> 686 us OK, destroyed revision. Reviewers: csilvers, vrana, jungejason Reviewed By: csilvers CC: aran Differential Revision: https://secure.phabricator.com/D2796
2012-06-19 20:52:50 +02:00
$this->openTransaction();
2011-01-24 20:36:53 +01:00
$ret = parent::save();
foreach ($this->unsavedChangesets as $changeset) {
2011-01-24 21:07:34 +01:00
$changeset->setDiffID($this->getID());
$changeset->save();
2011-01-24 20:36:53 +01:00
}
Provide a script to completely destroy revisions Summary: Someone may or may not have accidentally uploaded secrets to Differential. Provide an administrative mechanism to permanently destroy a revision. Also fix some of the transaction handling code. Test Plan: $ ./scripts/differential/destroy_revision.php --trace D1 >>> [0] <connect> <<< [0] <connect> 1,060 us >>> [1] <query> SELECT * FROM `differential_revision` WHERE `id` = 1 <<< [1] <query> 473 us Really destroy 'D1: asdbas' forever? [y/N] y >>> [2] <connect> <<< [2] <connect> 628 us >>> [3] <query> START TRANSACTION <<< [3] <query> 190 us >>> [4] <query> SELECT * FROM `differential_diff` WHERE revisionID = 1 <<< [4] <query> 510 us >>> [5] <query> SAVEPOINT Aphront_Savepoint_1 <<< [5] <query> 122 us >>> [6] <query> SELECT * FROM `differential_changeset` WHERE diffID = 1 <<< [6] <query> 307 us >>> [7] <query> SAVEPOINT Aphront_Savepoint_2 <<< [7] <query> 241 us >>> [8] <query> SELECT * FROM `differential_hunk` WHERE changesetID = 1 <<< [8] <query> 212 us >>> [9] <query> DELETE FROM `differential_hunk` WHERE `id` = 1 <<< [9] <query> 216 us >>> [10] <query> DELETE FROM `differential_changeset` WHERE `id` = 1 <<< [10] <query> 154 us >>> [11] <query> SAVEPOINT Aphront_Savepoint_2 <<< [11] <query> 118 us >>> [12] <query> SELECT * FROM `differential_hunk` WHERE changesetID = 2 <<< [12] <query> 194 us >>> [13] <query> DELETE FROM `differential_hunk` WHERE `id` = 2 <<< [13] <query> 179 us >>> [14] <query> DELETE FROM `differential_changeset` WHERE `id` = 2 <<< [14] <query> 163 us >>> [15] <query> SAVEPOINT Aphront_Savepoint_2 <<< [15] <query> 105 us >>> [16] <query> SELECT * FROM `differential_hunk` WHERE changesetID = 3 <<< [16] <query> 211 us >>> [17] <query> DELETE FROM `differential_hunk` WHERE `id` = 3 <<< [17] <query> 159 us >>> [18] <query> DELETE FROM `differential_changeset` WHERE `id` = 3 <<< [18] <query> 152 us >>> [19] <query> SAVEPOINT Aphront_Savepoint_2 <<< [19] <query> 124 us >>> [20] <query> SELECT * FROM `differential_hunk` WHERE changesetID = 4 <<< [20] <query> 191 us >>> [21] <query> DELETE FROM `differential_hunk` WHERE `id` = 4 <<< [21] <query> 155 us >>> [22] <query> DELETE FROM `differential_changeset` WHERE `id` = 4 <<< [22] <query> 149 us >>> [23] <query> SELECT * FROM `differential_diffproperty` WHERE diffID = 1 <<< [23] <query> 242 us >>> [24] <query> DELETE FROM `differential_diffproperty` WHERE `id` = 1 <<< [24] <query> 196 us >>> [25] <query> DELETE FROM `differential_diff` WHERE `id` = 1 <<< [25] <query> 169 us >>> [26] <query> DELETE FROM `differential_relationship` WHERE revisionID = 1 <<< [26] <query> 178 us >>> [27] <query> DELETE FROM `differential_commit` WHERE revisionID = 1 <<< [27] <query> 164 us >>> [28] <query> SELECT * FROM `differential_comment` WHERE revisionID = 1 <<< [28] <query> 221 us >>> [29] <query> DELETE FROM `differential_comment` WHERE `id` = 1 <<< [29] <query> 172 us >>> [30] <query> SELECT * FROM `differential_inlinecomment` WHERE revisionID = 1 <<< [30] <query> 296 us >>> [31] <query> SELECT * FROM `differential_auxiliaryfield` WHERE revisionPHID = 'PHID-DREV-ooky7ozqukpmwget32oc' <<< [31] <query> 308 us >>> [32] <query> SELECT * FROM `differential_affectedpath` WHERE revisionID = 1 <<< [32] <query> 4,173 us >>> [33] <query> DELETE FROM `differential_revision` WHERE `id` = 1 <<< [33] <query> 231 us >>> [34] <query> COMMIT <<< [34] <query> 686 us OK, destroyed revision. Reviewers: csilvers, vrana, jungejason Reviewed By: csilvers CC: aran Differential Revision: https://secure.phabricator.com/D2796
2012-06-19 20:52:50 +02:00
$this->saveTransaction();
2011-01-24 20:36:53 +01:00
return $ret;
}
public static function initializeNewDiff(PhabricatorUser $actor) {
$app = id(new PhabricatorApplicationQuery())
->setViewer($actor)
->withClasses(array('PhabricatorDifferentialApplication'))
->executeOne();
$view_policy = $app->getPolicy(
DifferentialDefaultViewCapability::CAPABILITY);
$diff = id(new DifferentialDiff())
->setViewPolicy($view_policy);
return $diff;
}
public static function newFromRawChanges(
PhabricatorUser $actor,
array $changes) {
assert_instances_of($changes, 'ArcanistDiffChange');
2011-01-24 20:01:53 +01:00
$diff = self::initializeNewDiff($actor);
return self::buildChangesetsFromRawChanges($diff, $changes);
}
public static function newEphemeralFromRawChanges(array $changes) {
assert_instances_of($changes, 'ArcanistDiffChange');
$diff = id(new DifferentialDiff())->makeEphemeral();
return self::buildChangesetsFromRawChanges($diff, $changes);
}
private static function buildChangesetsFromRawChanges(
DifferentialDiff $diff,
array $changes) {
// There may not be any changes; initialize the changesets list so that
// we don't throw later when accessing it.
$diff->attachChangesets(array());
2011-01-24 20:01:53 +01:00
$lines = 0;
foreach ($changes as $change) {
if ($change->getType() == ArcanistDiffChangeType::TYPE_MESSAGE) {
// If a user pastes a diff into Differential which includes a commit
// message (e.g., they ran `git show` to generate it), discard that
// change when constructing a DifferentialDiff.
continue;
}
2011-01-24 20:01:53 +01:00
$changeset = new DifferentialChangeset();
$add_lines = 0;
$del_lines = 0;
$first_line = PHP_INT_MAX;
$hunks = $change->getHunks();
if ($hunks) {
foreach ($hunks as $hunk) {
$dhunk = new DifferentialModernHunk();
$dhunk->setOldOffset($hunk->getOldOffset());
$dhunk->setOldLen($hunk->getOldLength());
$dhunk->setNewOffset($hunk->getNewOffset());
$dhunk->setNewLen($hunk->getNewLength());
$dhunk->setChanges($hunk->getCorpus());
$changeset->addUnsavedHunk($dhunk);
$add_lines += $hunk->getAddLines();
$del_lines += $hunk->getDelLines();
$added_lines = $hunk->getChangedLines('new');
if ($added_lines) {
$first_line = min($first_line, head_key($added_lines));
}
}
$lines += $add_lines + $del_lines;
} else {
// This happens when you add empty files.
$changeset->attachHunks(array());
2011-01-24 20:01:53 +01:00
}
$metadata = $change->getAllMetadata();
if ($first_line != PHP_INT_MAX) {
$metadata['line:first'] = $first_line;
}
2011-01-24 20:01:53 +01:00
$changeset->setOldFile($change->getOldPath());
$changeset->setFilename($change->getCurrentPath());
$changeset->setChangeType($change->getType());
$changeset->setFileType($change->getFileType());
$changeset->setMetadata($metadata);
2011-01-24 20:01:53 +01:00
$changeset->setOldProperties($change->getOldProperties());
$changeset->setNewProperties($change->getNewProperties());
$changeset->setAwayPaths($change->getAwayPaths());
$changeset->setAddLines($add_lines);
$changeset->setDelLines($del_lines);
2011-01-24 20:36:53 +01:00
$diff->addUnsavedChangeset($changeset);
2011-01-24 20:01:53 +01:00
}
$diff->setLineCount($lines);
$parser = new DifferentialChangesetParser();
$changesets = $parser->detectCopiedCode(
$diff->getChangesets(),
$min_width = 30,
$min_lines = 3);
$diff->attachChangesets($changesets);
2011-01-24 20:01:53 +01:00
return $diff;
}
public function getDiffDict() {
$dict = array(
'id' => $this->getID(),
'revisionID' => $this->getRevisionID(),
'dateCreated' => $this->getDateCreated(),
'dateModified' => $this->getDateModified(),
'sourceControlBaseRevision' => $this->getSourceControlBaseRevision(),
'sourceControlPath' => $this->getSourceControlPath(),
'sourceControlSystem' => $this->getSourceControlSystem(),
'branch' => $this->getBranch(),
'bookmark' => $this->getBookmark(),
'creationMethod' => $this->getCreationMethod(),
2012-06-16 01:16:03 +02:00
'description' => $this->getDescription(),
'unitStatus' => $this->getUnitStatus(),
'lintStatus' => $this->getLintStatus(),
'changes' => array(),
);
$dict['changes'] = $this->buildChangesList();
return $dict + $this->getDiffAuthorshipDict();
}
public function getDiffAuthorshipDict() {
$dict = array('properties' => array());
$properties = id(new DifferentialDiffProperty())->loadAllWhere(
'diffID = %d',
$this->getID());
foreach ($properties as $property) {
$dict['properties'][$property->getName()] = $property->getData();
if ($property->getName() == 'local:commits') {
foreach ($property->getData() as $commit) {
$dict['authorName'] = $commit['author'];
$dict['authorEmail'] = idx($commit, 'authorEmail');
break;
}
}
}
return $dict;
}
public function buildChangesList() {
$changes = array();
foreach ($this->getChangesets() as $changeset) {
$hunks = array();
foreach ($changeset->getHunks() as $hunk) {
$hunks[] = array(
'oldOffset' => $hunk->getOldOffset(),
'newOffset' => $hunk->getNewOffset(),
'oldLength' => $hunk->getOldLen(),
'newLength' => $hunk->getNewLen(),
'addLines' => null,
'delLines' => null,
'isMissingOldNewline' => null,
'isMissingNewNewline' => null,
'corpus' => $hunk->getChanges(),
);
}
$change = array(
'id' => $changeset->getID(),
'metadata' => $changeset->getMetadata(),
'oldPath' => $changeset->getOldFile(),
'currentPath' => $changeset->getFilename(),
'awayPaths' => $changeset->getAwayPaths(),
'oldProperties' => $changeset->getOldProperties(),
'newProperties' => $changeset->getNewProperties(),
'type' => $changeset->getChangeType(),
'fileType' => $changeset->getFileType(),
'commitHash' => null,
'addLines' => $changeset->getAddLines(),
'delLines' => $changeset->getDelLines(),
'hunks' => $hunks,
);
$changes[] = $change;
}
return $changes;
}
public function hasRevision() {
return $this->revision !== self::ATTACHABLE;
}
public function getRevision() {
return $this->assertAttached($this->revision);
}
public function attachRevision(DifferentialRevision $revision = null) {
$this->revision = $revision;
return $this;
}
public function attachProperty($key, $value) {
$this->properties[$key] = $value;
return $this;
}
public function getProperty($key) {
return $this->assertAttachedKey($this->properties, $key);
}
public function hasDiffProperty($key) {
$properties = $this->getDiffProperties();
return array_key_exists($key, $properties);
}
public function attachDiffProperties(array $properties) {
$this->properties = $properties;
return $this;
}
public function getDiffProperties() {
return $this->assertAttached($this->properties);
}
public function attachBuildable(HarbormasterBuildable $buildable = null) {
$this->buildable = $buildable;
return $this;
}
public function getBuildable() {
return $this->assertAttached($this->buildable);
}
public function getBuildTargetPHIDs() {
$buildable = $this->getBuildable();
if (!$buildable) {
return array();
}
$target_phids = array();
foreach ($buildable->getBuilds() as $build) {
foreach ($build->getBuildTargets() as $target) {
$target_phids[] = $target->getPHID();
}
}
return $target_phids;
}
public function loadCoverageMap(PhabricatorUser $viewer) {
$target_phids = $this->getBuildTargetPHIDs();
if (!$target_phids) {
return array();
}
$unit = id(new HarbormasterBuildUnitMessage())->loadAllWhere(
'buildTargetPHID IN (%Ls)',
$target_phids);
$map = array();
foreach ($unit as $message) {
$coverage = $message->getProperty('coverage', array());
foreach ($coverage as $path => $coverage_data) {
$map[$path][] = $coverage_data;
}
}
foreach ($map as $path => $coverage_items) {
$map[$path] = ArcanistUnitTestResult::mergeCoverage($coverage_items);
}
return $map;
}
public function getURI() {
$id = $this->getID();
return "/differential/diff/{$id}/";
}
public function attachUnitMessages(array $unit_messages) {
$this->unitMessages = $unit_messages;
return $this;
}
public function getUnitMessages() {
return $this->assertAttached($this->unitMessages);
}
/* -( PhabricatorPolicyInterface )----------------------------------------- */
public function getCapabilities() {
return array(
PhabricatorPolicyCapability::CAN_VIEW,
);
}
public function getPolicy($capability) {
if ($this->hasRevision()) {
return PhabricatorPolicies::getMostOpenPolicy();
}
return $this->viewPolicy;
}
public function hasAutomaticCapability($capability, PhabricatorUser $viewer) {
if ($this->hasRevision()) {
return $this->getRevision()->hasAutomaticCapability($capability, $viewer);
}
return ($this->getAuthorPHID() == $viewer->getPHID());
}
public function describeAutomaticCapability($capability) {
if ($this->hasRevision()) {
return pht(
'This diff is attached to a revision, and inherits its policies.');
}
return pht('The author of a diff can see it.');
}
/* -( PhabricatorExtendedPolicyInterface )--------------------------------- */
public function getExtendedPolicy($capability, PhabricatorUser $viewer) {
$extended = array();
switch ($capability) {
case PhabricatorPolicyCapability::CAN_VIEW:
if ($this->hasRevision()) {
$extended[] = array(
$this->getRevision(),
PhabricatorPolicyCapability::CAN_VIEW,
);
}
break;
}
return $extended;
}
/* -( HarbormasterBuildableInterface )------------------------------------- */
public function getHarbormasterBuildableDisplayPHID() {
$container_phid = $this->getHarbormasterContainerPHID();
if ($container_phid) {
return $container_phid;
}
return $this->getHarbormasterBuildablePHID();
}
public function getHarbormasterBuildablePHID() {
return $this->getPHID();
}
public function getHarbormasterContainerPHID() {
if ($this->getRevisionID()) {
$revision = id(new DifferentialRevision())->load($this->getRevisionID());
if ($revision) {
return $revision->getPHID();
}
}
return null;
}
public function getHarbormasterPublishablePHID() {
return $this->getHarbormasterContainerPHID();
}
public function getBuildVariables() {
$results = array();
$results['buildable.diff'] = $this->getID();
Add "Autoplans" to Harbormaster Summary: Ref T8095. Two general problems: - I want Harbormaster to own all lint and unit test results. - I don't want users to have to configure anything for `arc` to keep working automatically. These are in conflict because generic lint/unit test ownership in Harbormaster requires that build targets exist which we can attach build results to. However, we can't currently create build targets on demand: Harbormaster assumes it is responsible for creating targets, then running code or making third-party service calls to actually run the builds. I considered two broad approaches to let `arc` push results into Harbormaster without requiring administrators to configure some kind of "arc results" build plan: # Add magic target PHIDs like `PHID-MAGIC-this-is-really-arc-unit`. # Add new code to build real targets with real PHIDs. (1) is probably a bit less work to get off the ground, but I think it's worse overall and very likely to create more problems in the long run. I particularly worry that it will lead to a small amount of special casing in a very large number of places, which seems more fragile. (2) is more work upfront but I think does a better job of putting all the special casing in one place that we can, e.g., more reasonably unit test, and letting the rest of the code rarely/never care about this case since it's just dealing with normal plans/steps/targets as far as it can tell. This diff introduces "autoplans", which are source templates for plans/steps. This let us "push" these targets into Harbormaster. Hypthetically, any process "like" arc can use autoplans to upload test/lint/etc results. In practice, probably only `arc` will ever use this, but I think it's still quite a bit cleaner than the alternative despite all the generality. Workflow is basically: - `arc` creates a diff. - `arc` calls `harbormaster.queryautotargets`, passing the diff PHID and saying "I have some lint and unit results I want to stick on this thing". - Harbormaster builds the plan, steps, and targets (if any of them don't already exist), and hands back the target PHIDs so `arc` has a completely standard-looking place to put results. - `arc` uploads the test results to the right targets, as though Harbormaster had asked it to run unit/lint in the first place. (This doesn't actually do any of that yet, just sets things up.) I'll maybe doc turn that ^^^^^^ into a doc for posterity since I think it's hard to guess what an "autotarget" is, but I'm going to grab some lunch first. Test Plan: - Added unit tests to make sure we can build these things properly. - Used `harbormaster.queryautotargets` to build autotargets for a bunch of diffs. - Verified targets come up in "waiting for message" state. - Verified plans and steps are not editable. Reviewers: btrahan Reviewed By: btrahan Subscribers: hach-que, epriestley Maniphest Tasks: T8095 Differential Revision: https://secure.phabricator.com/D13345
2015-06-21 18:04:21 +02:00
if ($this->revisionID) {
$revision = $this->getRevision();
$results['buildable.revision'] = $revision->getID();
$repo = $revision->getRepository();
if ($repo) {
$results['repository.callsign'] = $repo->getCallsign();
$results['repository.phid'] = $repo->getPHID();
Add "Autoplans" to Harbormaster Summary: Ref T8095. Two general problems: - I want Harbormaster to own all lint and unit test results. - I don't want users to have to configure anything for `arc` to keep working automatically. These are in conflict because generic lint/unit test ownership in Harbormaster requires that build targets exist which we can attach build results to. However, we can't currently create build targets on demand: Harbormaster assumes it is responsible for creating targets, then running code or making third-party service calls to actually run the builds. I considered two broad approaches to let `arc` push results into Harbormaster without requiring administrators to configure some kind of "arc results" build plan: # Add magic target PHIDs like `PHID-MAGIC-this-is-really-arc-unit`. # Add new code to build real targets with real PHIDs. (1) is probably a bit less work to get off the ground, but I think it's worse overall and very likely to create more problems in the long run. I particularly worry that it will lead to a small amount of special casing in a very large number of places, which seems more fragile. (2) is more work upfront but I think does a better job of putting all the special casing in one place that we can, e.g., more reasonably unit test, and letting the rest of the code rarely/never care about this case since it's just dealing with normal plans/steps/targets as far as it can tell. This diff introduces "autoplans", which are source templates for plans/steps. This let us "push" these targets into Harbormaster. Hypthetically, any process "like" arc can use autoplans to upload test/lint/etc results. In practice, probably only `arc` will ever use this, but I think it's still quite a bit cleaner than the alternative despite all the generality. Workflow is basically: - `arc` creates a diff. - `arc` calls `harbormaster.queryautotargets`, passing the diff PHID and saying "I have some lint and unit results I want to stick on this thing". - Harbormaster builds the plan, steps, and targets (if any of them don't already exist), and hands back the target PHIDs so `arc` has a completely standard-looking place to put results. - `arc` uploads the test results to the right targets, as though Harbormaster had asked it to run unit/lint in the first place. (This doesn't actually do any of that yet, just sets things up.) I'll maybe doc turn that ^^^^^^ into a doc for posterity since I think it's hard to guess what an "autotarget" is, but I'm going to grab some lunch first. Test Plan: - Added unit tests to make sure we can build these things properly. - Used `harbormaster.queryautotargets` to build autotargets for a bunch of diffs. - Verified targets come up in "waiting for message" state. - Verified plans and steps are not editable. Reviewers: btrahan Reviewed By: btrahan Subscribers: hach-que, epriestley Maniphest Tasks: T8095 Differential Revision: https://secure.phabricator.com/D13345
2015-06-21 18:04:21 +02:00
$results['repository.vcs'] = $repo->getVersionControlSystem();
$results['repository.uri'] = $repo->getPublicCloneURI();
$results['repository.staging.uri'] = $repo->getStagingURI();
$results['repository.staging.ref'] = $this->getStagingRef();
Add "Autoplans" to Harbormaster Summary: Ref T8095. Two general problems: - I want Harbormaster to own all lint and unit test results. - I don't want users to have to configure anything for `arc` to keep working automatically. These are in conflict because generic lint/unit test ownership in Harbormaster requires that build targets exist which we can attach build results to. However, we can't currently create build targets on demand: Harbormaster assumes it is responsible for creating targets, then running code or making third-party service calls to actually run the builds. I considered two broad approaches to let `arc` push results into Harbormaster without requiring administrators to configure some kind of "arc results" build plan: # Add magic target PHIDs like `PHID-MAGIC-this-is-really-arc-unit`. # Add new code to build real targets with real PHIDs. (1) is probably a bit less work to get off the ground, but I think it's worse overall and very likely to create more problems in the long run. I particularly worry that it will lead to a small amount of special casing in a very large number of places, which seems more fragile. (2) is more work upfront but I think does a better job of putting all the special casing in one place that we can, e.g., more reasonably unit test, and letting the rest of the code rarely/never care about this case since it's just dealing with normal plans/steps/targets as far as it can tell. This diff introduces "autoplans", which are source templates for plans/steps. This let us "push" these targets into Harbormaster. Hypthetically, any process "like" arc can use autoplans to upload test/lint/etc results. In practice, probably only `arc` will ever use this, but I think it's still quite a bit cleaner than the alternative despite all the generality. Workflow is basically: - `arc` creates a diff. - `arc` calls `harbormaster.queryautotargets`, passing the diff PHID and saying "I have some lint and unit results I want to stick on this thing". - Harbormaster builds the plan, steps, and targets (if any of them don't already exist), and hands back the target PHIDs so `arc` has a completely standard-looking place to put results. - `arc` uploads the test results to the right targets, as though Harbormaster had asked it to run unit/lint in the first place. (This doesn't actually do any of that yet, just sets things up.) I'll maybe doc turn that ^^^^^^ into a doc for posterity since I think it's hard to guess what an "autotarget" is, but I'm going to grab some lunch first. Test Plan: - Added unit tests to make sure we can build these things properly. - Used `harbormaster.queryautotargets` to build autotargets for a bunch of diffs. - Verified targets come up in "waiting for message" state. - Verified plans and steps are not editable. Reviewers: btrahan Reviewed By: btrahan Subscribers: hach-que, epriestley Maniphest Tasks: T8095 Differential Revision: https://secure.phabricator.com/D13345
2015-06-21 18:04:21 +02:00
}
}
return $results;
}
public function getAvailableBuildVariables() {
return array(
'buildable.diff' =>
pht('The differential diff ID, if applicable.'),
'buildable.revision' =>
pht('The differential revision ID, if applicable.'),
'repository.callsign' =>
pht('The callsign of the repository in Phabricator.'),
'repository.phid' =>
pht('The PHID of the repository in Phabricator.'),
'repository.vcs' =>
pht('The version control system, either "svn", "hg" or "git".'),
'repository.uri' =>
pht('The URI to clone or checkout the repository from.'),
'repository.staging.uri' =>
pht('The URI of the staging repository.'),
'repository.staging.ref' =>
pht('The ref name for this change in the staging repository.'),
);
}
/* -( HarbormasterCircleCIBuildableInterface )----------------------------- */
public function getCircleCIGitHubRepositoryURI() {
$diff_phid = $this->getPHID();
$repository_phid = $this->getRepositoryPHID();
if (!$repository_phid) {
throw new Exception(
pht(
'This diff ("%s") is not associated with a repository. A diff '.
'must belong to a tracked repository to be built by CircleCI.',
$diff_phid));
}
$repository = id(new PhabricatorRepositoryQuery())
->setViewer(PhabricatorUser::getOmnipotentUser())
->withPHIDs(array($repository_phid))
->executeOne();
if (!$repository) {
throw new Exception(
pht(
'This diff ("%s") is associated with a repository ("%s") which '.
'could not be loaded.',
$diff_phid,
$repository_phid));
}
$staging_uri = $repository->getStagingURI();
if (!$staging_uri) {
throw new Exception(
pht(
'This diff ("%s") is associated with a repository ("%s") that '.
'does not have a Staging Area configured. You must configure a '.
'Staging Area to use CircleCI integration.',
$diff_phid,
$repository_phid));
}
$path = HarbormasterCircleCIBuildStepImplementation::getGitHubPath(
$staging_uri);
if (!$path) {
throw new Exception(
pht(
'This diff ("%s") is associated with a repository ("%s") that '.
'does not have a Staging Area ("%s") that is hosted on GitHub. '.
'CircleCI can only build from GitHub, so the Staging Area for '.
'the repository must be hosted there.',
$diff_phid,
$repository_phid,
$staging_uri));
}
return $staging_uri;
}
public function getCircleCIBuildIdentifierType() {
return 'tag';
}
public function getCircleCIBuildIdentifier() {
$ref = $this->getStagingRef();
$ref = preg_replace('(^refs/tags/)', '', $ref);
return $ref;
}
public function getStagingRef() {
// TODO: We're just hoping to get lucky. Instead, `arc` should store
// where it sent changes and we should only provide staging details
// if we reasonably believe they are accurate.
return 'refs/tags/phabricator/diff/'.$this->getID();
}
public function loadTargetBranch() {
// TODO: This is sketchy, but just eat the query cost until this can get
// cleaned up.
// For now, we're only returning a target if there's exactly one and it's
// a branch, since we don't support landing to more esoteric targets like
// tags yet.
$property = id(new DifferentialDiffProperty())->loadOneWhere(
'diffID = %d AND name = %s',
$this->getID(),
'arc:onto');
if (!$property) {
return null;
}
$data = $property->getData();
if (!$data) {
return null;
}
if (!is_array($data)) {
return null;
}
if (count($data) != 1) {
return null;
}
$onto = head($data);
if (!is_array($onto)) {
return null;
}
$type = idx($onto, 'type');
if ($type != 'branch') {
return null;
}
return idx($onto, 'name');
}
/* -( PhabricatorApplicationTransactionInterface )------------------------- */
public function getApplicationTransactionEditor() {
return new DifferentialDiffEditor();
}
public function getApplicationTransactionObject() {
return $this;
}
public function getApplicationTransactionTemplate() {
return new DifferentialDiffTransaction();
}
public function willRenderTimeline(
PhabricatorApplicationTransactionView $timeline,
AphrontRequest $request) {
return $timeline;
}
/* -( PhabricatorDestructibleInterface )----------------------------------- */
public function destroyObjectPermanently(
PhabricatorDestructionEngine $engine) {
$this->openTransaction();
$this->delete();
foreach ($this->loadChangesets() as $changeset) {
$changeset->delete();
}
$properties = id(new DifferentialDiffProperty())->loadAllWhere(
'diffID = %d',
$this->getID());
foreach ($properties as $prop) {
$prop->delete();
}
$this->saveTransaction();
}
2011-01-24 20:01:53 +01:00
}