Skip to content

Commit 3f4cc3a

Browse files
author
epriestley
committed
Allow Nuances sources to provide import cursors
Summary: Ref T10537. Some sources (like the future "GitHub Repository" source) need to poll remotes. - Provide a mechanism for sources to emit import cursors. - Hook them into the trigger daemon so they'll fire periodically. - Provide some storage. This diff does nothing useful or interesting, and is pure infrastructure. Test Plan: - Ran `bin/storage upgrade -f`, no adjustment issues. - Poked around Nuance. - Ran the trigger daemon, verified it didn't crash and checked for Nuance stuff to do. Reviewers: chad Reviewed By: chad Maniphest Tasks: T10537 Differential Revision: https://secure.phabricator.com/D15435
1 parent aa5df5f commit 3f4cc3a

File tree

11 files changed

+307
-1
lines changed

11 files changed

+307
-1
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE {$NAMESPACE}_nuance.nuance_source
2+
ADD isDisabled BOOL NOT NULL;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CREATE TABLE {$NAMESPACE}_nuance.nuance_importcursordata (
2+
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
3+
phid VARBINARY(64) NOT NULL,
4+
sourcePHID VARBINARY(64) NOT NULL,
5+
cursorKey VARCHAR(32) NOT NULL COLLATE {$COLLATE_TEXT},
6+
cursorType VARCHAR(32) NOT NULL COLLATE {$COLLATE_TEXT},
7+
properties LONGTEXT NOT NULL COLLATE {$COLLATE_TEXT},
8+
dateCreated INT UNSIGNED NOT NULL,
9+
dateModified INT UNSIGNED NOT NULL,
10+
UNIQUE KEY `key_phid` (phid),
11+
UNIQUE KEY `key_source` (sourcePHID, cursorKey)
12+
) ENGINE=InnoDB, COLLATE {$COLLATE_TEXT};

src/__phutil_library_map__.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,6 +1421,10 @@
14211421
'NuanceController' => 'applications/nuance/controller/NuanceController.php',
14221422
'NuanceCreateItemConduitAPIMethod' => 'applications/nuance/conduit/NuanceCreateItemConduitAPIMethod.php',
14231423
'NuanceDAO' => 'applications/nuance/storage/NuanceDAO.php',
1424+
'NuanceImportCursor' => 'applications/nuance/cursor/NuanceImportCursor.php',
1425+
'NuanceImportCursorData' => 'applications/nuance/storage/NuanceImportCursorData.php',
1426+
'NuanceImportCursorDataQuery' => 'applications/nuance/query/NuanceImportCursorDataQuery.php',
1427+
'NuanceImportCursorPHIDType' => 'applications/nuance/phid/NuanceImportCursorPHIDType.php',
14241428
'NuanceItem' => 'applications/nuance/storage/NuanceItem.php',
14251429
'NuanceItemEditController' => 'applications/nuance/controller/NuanceItemEditController.php',
14261430
'NuanceItemEditor' => 'applications/nuance/editor/NuanceItemEditor.php',
@@ -5661,6 +5665,10 @@
56615665
'NuanceController' => 'PhabricatorController',
56625666
'NuanceCreateItemConduitAPIMethod' => 'NuanceConduitAPIMethod',
56635667
'NuanceDAO' => 'PhabricatorLiskDAO',
5668+
'NuanceImportCursor' => 'Phobject',
5669+
'NuanceImportCursorData' => 'NuanceDAO',
5670+
'NuanceImportCursorDataQuery' => 'NuanceQuery',
5671+
'NuanceImportCursorPHIDType' => 'PhabricatorPHIDType',
56645672
'NuanceItem' => array(
56655673
'NuanceDAO',
56665674
'PhabricatorPolicyInterface',
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
abstract class NuanceImportCursor extends Phobject {
4+
5+
final public function importFromSource() {
6+
// TODO: Perhaps, do something.
7+
return false;
8+
}
9+
10+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
final class NuanceImportCursorPHIDType extends PhabricatorPHIDType {
4+
5+
const TYPECONST = 'NUAC';
6+
7+
public function getTypeName() {
8+
return pht('Import Cursor');
9+
}
10+
11+
public function newObject() {
12+
return new NuanceImportCursorData();
13+
}
14+
15+
public function getPHIDTypeApplicationClass() {
16+
return 'PhabricatorNuanceApplication';
17+
}
18+
19+
protected function buildQueryForObjects(
20+
PhabricatorObjectQuery $query,
21+
array $phids) {
22+
23+
return id(new NuanceImportCursorDataQuery())
24+
->withPHIDs($phids);
25+
}
26+
27+
public function loadHandles(
28+
PhabricatorHandleQuery $query,
29+
array $handles,
30+
array $objects) {
31+
32+
$viewer = $query->getViewer();
33+
foreach ($handles as $phid => $handle) {
34+
$item = $objects[$phid];
35+
}
36+
}
37+
38+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
final class NuanceImportCursorDataQuery
4+
extends NuanceQuery {
5+
6+
private $ids;
7+
private $phids;
8+
private $sourcePHIDs;
9+
10+
public function withIDs(array $ids) {
11+
$this->ids = $ids;
12+
return $this;
13+
}
14+
15+
public function withPHIDs(array $phids) {
16+
$this->phids = $phids;
17+
return $this;
18+
}
19+
20+
public function withSourcePHIDs(array $source_phids) {
21+
$this->sourcePHIDs = $source_phids;
22+
return $this;
23+
}
24+
25+
public function newResultObject() {
26+
return new NuanceImportCursorData();
27+
}
28+
29+
protected function loadPage() {
30+
return $this->loadStandardPage($this->newResultObject());
31+
}
32+
33+
protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
34+
$where = parent::buildWhereClauseParts($conn);
35+
36+
if ($this->sourcePHIDs !== null) {
37+
$where[] = qsprintf(
38+
$conn,
39+
'sourcePHID IN (%Ls)',
40+
$this->sourcePHIDs);
41+
}
42+
43+
if ($this->ids !== null) {
44+
$where[] = qsprintf(
45+
$conn,
46+
'id IN (%Ld)',
47+
$this->ids);
48+
}
49+
50+
if ($this->phids !== null) {
51+
$where[] = qsprintf(
52+
$conn,
53+
'phid IN (%Ls)',
54+
$this->phids);
55+
}
56+
57+
return $where;
58+
}
59+
60+
}

src/applications/nuance/query/NuanceSourceQuery.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ final class NuanceSourceQuery
66
private $ids;
77
private $phids;
88
private $types;
9+
private $isDisabled;
10+
private $hasCursors;
911

1012
public function withIDs(array $ids) {
1113
$this->ids = $ids;
@@ -22,6 +24,16 @@ public function withTypes($types) {
2224
return $this;
2325
}
2426

27+
public function withIsDisabled($disabled) {
28+
$this->isDisabled = $disabled;
29+
return $this;
30+
}
31+
32+
public function withHasImportCursors($has_cursors) {
33+
$this->hasCursors = $has_cursors;
34+
return $this;
35+
}
36+
2537
public function newResultObject() {
2638
return new NuanceSource();
2739
}
@@ -71,6 +83,44 @@ protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
7183
$this->phids);
7284
}
7385

86+
if ($this->isDisabled !== null) {
87+
$where[] = qsprintf(
88+
$conn,
89+
'isDisabled = %d',
90+
(int)$this->isDisabled);
91+
}
92+
93+
if ($this->hasCursors !== null) {
94+
$cursor_types = array();
95+
96+
$definitions = NuanceSourceDefinition::getAllDefinitions();
97+
foreach ($definitions as $key => $definition) {
98+
if ($definition->hasImportCursors()) {
99+
$cursor_types[] = $key;
100+
}
101+
}
102+
103+
if ($this->hasCursors) {
104+
if (!$cursor_types) {
105+
throw new PhabricatorEmptyQueryException();
106+
} else {
107+
$where[] = qsprintf(
108+
$conn,
109+
'type IN (%Ls)',
110+
$cursor_types);
111+
}
112+
} else {
113+
if (!$cursor_types) {
114+
// Apply no constraint.
115+
} else {
116+
$where[] = qsprintf(
117+
$conn,
118+
'type NOT IN (%Ls)',
119+
$cursor_types);
120+
}
121+
}
122+
}
123+
74124
return $where;
75125
}
76126

src/applications/nuance/source/NuanceSourceDefinition.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,23 @@ public static function getAllDefinitions() {
4343
->execute();
4444
}
4545

46+
public function hasImportCursors() {
47+
return false;
48+
}
49+
50+
final public function getImportCursors() {
51+
if (!$this->hasImportCursors()) {
52+
throw new Exception(
53+
pht('This source has no input cursors.'));
54+
}
55+
56+
return $this->newImportCursors();
57+
}
58+
59+
protected function newImportCursors() {
60+
throw new PhutilMethodNotImplementedException();
61+
}
62+
4663
/**
4764
* A human readable string like "Twitter" or "Phabricator Form".
4865
*/
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
final class NuanceImportCursorData
4+
extends NuanceDAO {
5+
6+
protected $sourcePHID;
7+
protected $cursorKey;
8+
protected $cursorType;
9+
protected $properties = array();
10+
11+
protected function getConfiguration() {
12+
return array(
13+
self::CONFIG_AUX_PHID => true,
14+
self::CONFIG_SERIALIZATION => array(
15+
'properties' => self::SERIALIZATION_JSON,
16+
),
17+
self::CONFIG_COLUMN_SCHEMA => array(
18+
'cursorType' => 'text32',
19+
'cursorKey' => 'text32',
20+
),
21+
self::CONFIG_KEY_SCHEMA => array(
22+
'key_source' => array(
23+
'columns' => array('sourcePHID', 'cursorKey'),
24+
'unique' => true,
25+
),
26+
),
27+
) + parent::getConfiguration();
28+
}
29+
30+
public function generatePHID() {
31+
return PhabricatorPHID::generateNewPHID(
32+
NuanceImportCursorPHIDType::TYPECONST);
33+
}
34+
35+
}

src/applications/nuance/storage/NuanceSource.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ final class NuanceSource extends NuanceDAO
1212
protected $viewPolicy;
1313
protected $editPolicy;
1414
protected $defaultQueuePHID;
15+
protected $isDisabled;
1516

1617
private $definition = self::ATTACHABLE;
1718

@@ -25,6 +26,7 @@ protected function getConfiguration() {
2526
'name' => 'text255?',
2627
'type' => 'text32',
2728
'mailKey' => 'bytes20',
29+
'isDisabled' => 'bool',
2830
),
2931
self::CONFIG_KEY_SCHEMA => array(
3032
'key_type' => array(
@@ -66,7 +68,8 @@ public static function initializeNewSource(
6668
->setViewPolicy($view_policy)
6769
->setEditPolicy($edit_policy)
6870
->setType($definition->getSourceTypeConstant())
69-
->attachDefinition($definition);
71+
->attachDefinition($definition)
72+
->setIsDisabled(0);
7073
}
7174

7275
public function getDefinition() {

0 commit comments

Comments
 (0)