Skip to content

Commit fe9ba6b

Browse files
author
epriestley
committed
Improve DifferentialRevisionQuery and add the ability to query by arcanist project
Summary: - We currently post-filter by branches, but should do this in SQL. See T799. - We currently identify branch-name-matches as being in the working copy even if they belong to a different project (e.g., two different projects with commits on the branch "master"). See T1100. - Denormalize branch and project information into DifferentialRevision. - Expose project information in the API. Test Plan: Ran conduit API queries with branches and arc project IDs, got reasonable results. Reviewers: btrahan, vrana, jungejason Reviewed By: btrahan CC: aran Maniphest Tasks: T1100, T799 Differential Revision: https://secure.phabricator.com/D2190
1 parent b5adde8 commit fe9ba6b

File tree

7 files changed

+109
-30
lines changed

7 files changed

+109
-30
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
ALTER TABLE phabricator_differential.differential_revision
2+
ADD branchName VARCHAR(255) COLLATE utf8_general_ci;
3+
4+
ALTER TABLE phabricator_differential.differential_revision
5+
ADD arcanistProjectPHID VARCHAR(64) COLLATE utf8_bin;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* Copyright 2012 Facebook, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
$table = new DifferentialRevision();
20+
$conn_w = $table->establishConnection('w');
21+
22+
$revisions = id(new DifferentialRevision())->loadAll();
23+
24+
echo "Migrating ".count($revisions)." revisions";
25+
foreach ($revisions as $revision) {
26+
echo ".";
27+
28+
$diff = $revision->loadActiveDiff();
29+
if (!$diff) {
30+
continue;
31+
}
32+
33+
$branch_name = $diff->getBranch();
34+
$arc_project_phid = $diff->getArcanistProjectPHID();
35+
36+
queryfx(
37+
$conn_w,
38+
'UPDATE %T SET branchName = %s, arcanistProjectPHID = %s WHERE id = %d',
39+
$table->getTableName(),
40+
$branch_name,
41+
$arc_project_phid,
42+
$revision->getID());
43+
}
44+
echo "\nDone.\n";

src/applications/conduit/method/differential/query/ConduitAPI_differential_query_Method.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public function defineParamTypes() {
6262
'subscribers' => 'optional list<phid>',
6363
'responsibleUsers' => 'optional list<phid>',
6464
'branches' => 'optional list<string>',
65+
'arcanistProjects' => 'optional list<string>',
6566
);
6667
}
6768

@@ -89,6 +90,7 @@ protected function execute(ConduitAPIRequest $request) {
8990
$subscribers = $request->getValue('subscribers');
9091
$responsible_users = $request->getValue('responsibleUsers');
9192
$branches = $request->getValue('branches');
93+
$arc_projects = $request->getValue('arcanistProjects');
9294

9395
$query = new DifferentialRevisionQuery();
9496
if ($authors) {
@@ -151,6 +153,19 @@ protected function execute(ConduitAPIRequest $request) {
151153
if ($branches) {
152154
$query->withBranches($branches);
153155
}
156+
if ($arc_projects) {
157+
// This is sort of special-cased, but don't make arc do an extra round
158+
// trip.
159+
$projects = id(new PhabricatorRepositoryArcanistProject())
160+
->loadAllWhere(
161+
'name in (%Ls)',
162+
$arc_projects);
163+
if (!$projects) {
164+
return array();
165+
}
166+
167+
$query->withArcanistProjectPHIDs(mpull($projects, 'getPHID'));
168+
}
154169

155170
$query->needRelationships(true);
156171
$query->needCommitPHIDs(true);

src/applications/conduit/method/differential/query/__init__.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
phutil_require_module('phabricator', 'applications/conduit/method/base');
1313
phutil_require_module('phabricator', 'applications/conduit/protocol/exception');
1414
phutil_require_module('phabricator', 'applications/differential/query/revision');
15+
phutil_require_module('phabricator', 'applications/repository/storage/arcanistproject');
1516
phutil_require_module('phabricator', 'infrastructure/env');
1617

18+
phutil_require_module('phutil', 'utils');
19+
1720

1821
phutil_require_source('ConduitAPI_differential_query_Method.php');

src/applications/differential/editor/revision/DifferentialRevisionEditor.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,13 @@ public function save() {
206206

207207
$revision->openTransaction();
208208

209+
if ($diff) {
210+
$revision->setBranchName($diff->getBranch());
211+
$revision->setArcanistProjectPHID($diff->getArcanistProjectPHID());
212+
}
213+
209214
$revision->save();
215+
210216
if ($diff) {
211217
$diff->setRevisionID($revision->getID());
212218
$diff->save();

src/applications/differential/query/revision/DifferentialRevisionQuery.php

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ final class DifferentialRevisionQuery {
5151
private $subscribers = array();
5252
private $responsibles = array();
5353
private $branches = array();
54+
private $arcanistProjectPHIDs = array();
5455

5556
private $order = 'order-modified';
5657
const ORDER_MODIFIED = 'order-modified';
@@ -244,6 +245,20 @@ public function withSubscribers(array $subscriber_phids) {
244245
}
245246

246247

248+
/**
249+
* Filter results to only return revisions with a given set of arcanist
250+
* projects.
251+
*
252+
* @param array List of project PHIDs.
253+
* @return this
254+
* @task config
255+
*/
256+
public function withArcanistProjectPHIDs(array $arc_project_phids) {
257+
$this->arcanistProjectPHIDs = $arc_project_phids;
258+
return $this;
259+
}
260+
261+
247262
/**
248263
* Set result ordering. Provide a class constant, such as
249264
* ##DifferentialRevisionQuery::ORDER_CREATED##.
@@ -368,45 +383,17 @@ public function execute() {
368383
$this->loadCommitPHIDs($conn_r, $revisions);
369384
}
370385

371-
$need_active = $this->needActiveDiffs ||
372-
$this->branches;
373-
386+
$need_active = $this->needActiveDiffs;
374387
$need_ids = $need_active ||
375388
$this->needDiffIDs;
376389

377-
378390
if ($need_ids) {
379391
$this->loadDiffIDs($conn_r, $revisions);
380392
}
381393

382394
if ($need_active) {
383395
$this->loadActiveDiffs($conn_r, $revisions);
384396
}
385-
386-
if ($this->branches) {
387-
388-
// TODO: We could filter this in SQL instead and might get better
389-
// performance in some cases.
390-
391-
$branch_map = array_fill_keys($this->branches, true);
392-
foreach ($revisions as $key => $revision) {
393-
$diff = $revision->getActiveDiff();
394-
if (!$diff) {
395-
unset($revisions[$key]);
396-
continue;
397-
}
398-
399-
// TODO: Old arc uploaded the wrong branch name for Mercurial (i.e.,
400-
// with a trailing "\n"). Once the arc version gets bumped, do a
401-
// migration and remove this.
402-
$branch = trim($diff->getBranch());
403-
404-
if (!$diff || empty($branch_map[$branch])) {
405-
unset($revisions[$key]);
406-
continue;
407-
}
408-
}
409-
}
410397
}
411398

412399
return $revisions;
@@ -431,7 +418,9 @@ private function shouldUseResponsibleFastPath() {
431418
!$this->authors &&
432419
!$this->revIDs &&
433420
!$this->commitHashes &&
434-
!$this->phids) {
421+
!$this->phids &&
422+
!$this->branches &&
423+
!$this->arcanistProjectPHIDs) {
435424
return true;
436425
}
437426
return false;
@@ -656,6 +645,20 @@ private function buildWhereClause($conn_r) {
656645
$this->responsibles);
657646
}
658647

648+
if ($this->branches) {
649+
$where[] = qsprintf(
650+
$conn_r,
651+
'r.branchName in (%Ls)',
652+
$this->branches);
653+
}
654+
655+
if ($this->arcanistProjectPHIDs) {
656+
$where[] = qsprintf(
657+
$conn_r,
658+
'r.arcanistProjectPHID in (%Ls)',
659+
$this->arcanistProjectPHIDs);
660+
}
661+
659662
switch ($this->status) {
660663
case self::STATUS_ANY:
661664
break;

src/applications/differential/storage/revision/DifferentialRevision.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@ final class DifferentialRevision extends DifferentialDAO {
3535
protected $unsubscribed = array();
3636

3737
protected $mailKey;
38+
protected $branchName;
39+
protected $arcanistProjectPHID;
3840

3941
private $relationships;
4042
private $commits;
4143
private $activeDiff = false;
4244
private $diffIDs;
4345

46+
4447
const RELATIONSHIP_TABLE = 'differential_relationship';
4548
const TABLE_COMMIT = 'differential_commit';
4649

0 commit comments

Comments
 (0)