Changeset View
Changeset View
Standalone View
Standalone View
src/applications/nuance/item/NuanceGitHubEventItemType.php
| <?php | <?php | ||||
| final class NuanceGitHubEventItemType | final class NuanceGitHubEventItemType | ||||
| extends NuanceItemType { | extends NuanceItemType { | ||||
| const ITEMTYPE = 'github.event'; | const ITEMTYPE = 'github.event'; | ||||
| private $externalObject; | private $externalObject; | ||||
| private $externalActor; | |||||
| public function getItemTypeDisplayName() { | public function getItemTypeDisplayName() { | ||||
| return pht('GitHub Event'); | return pht('GitHub Event'); | ||||
| } | } | ||||
| public function getItemTypeDisplayIcon() { | public function getItemTypeDisplayIcon() { | ||||
| return 'fa-github'; | return 'fa-github'; | ||||
| } | } | ||||
| public function getItemDisplayName(NuanceItem $item) { | public function getItemDisplayName(NuanceItem $item) { | ||||
| return $this->newRawEvent($item)->getEventFullTitle(); | return $this->newRawEvent($item)->getEventFullTitle(); | ||||
| } | } | ||||
| public function canUpdateItems() { | public function canUpdateItems() { | ||||
| return true; | return true; | ||||
| } | } | ||||
| protected function updateItemFromSource(NuanceItem $item) { | protected function updateItemFromSource(NuanceItem $item) { | ||||
| $viewer = $this->getViewer(); | $viewer = $this->getViewer(); | ||||
| $is_dirty = false; | $is_dirty = false; | ||||
| // TODO: Link up the requestor, etc. | |||||
| $is_dirty = false; | |||||
| $xobj = $this->reloadExternalObject($item); | $xobj = $this->reloadExternalObject($item); | ||||
| if ($xobj) { | if ($xobj) { | ||||
| $item->setItemProperty('doorkeeper.xobj.phid', $xobj->getPHID()); | $item->setItemProperty('doorkeeper.xobj.phid', $xobj->getPHID()); | ||||
| $is_dirty = true; | $is_dirty = true; | ||||
| } | } | ||||
| $actor = $this->reloadExternalActor($item); | |||||
| if ($actor) { | |||||
| $item->setRequestorPHID($actor->getPHID()); | |||||
| $is_dirty = true; | |||||
| } | |||||
| if ($item->getStatus() == NuanceItem::STATUS_IMPORTING) { | if ($item->getStatus() == NuanceItem::STATUS_IMPORTING) { | ||||
| $item->setStatus(NuanceItem::STATUS_ROUTING); | $item->setStatus(NuanceItem::STATUS_ROUTING); | ||||
| $is_dirty = true; | $is_dirty = true; | ||||
| } | } | ||||
| if ($is_dirty) { | if ($is_dirty) { | ||||
| $item->save(); | $item->save(); | ||||
| } | } | ||||
| } | } | ||||
| private function getDoorkeeperActorRef(NuanceItem $item) { | |||||
| $raw = $this->newRawEvent($item); | |||||
| $user_id = $raw->getActorGitHubUserID(); | |||||
| if (!$user_id) { | |||||
| return null; | |||||
| } | |||||
| $ref_type = DoorkeeperBridgeGitHubUser::OBJTYPE_GITHUB_USER; | |||||
| return $this->newDoorkeeperRef() | |||||
| ->setObjectType($ref_type) | |||||
| ->setObjectID($user_id); | |||||
| } | |||||
| private function getDoorkeeperRef(NuanceItem $item) { | private function getDoorkeeperRef(NuanceItem $item) { | ||||
| $raw = $this->newRawEvent($item); | $raw = $this->newRawEvent($item); | ||||
| $full_repository = $raw->getRepositoryFullName(); | $full_repository = $raw->getRepositoryFullName(); | ||||
| if (!strlen($full_repository)) { | if (!strlen($full_repository)) { | ||||
| return null; | return null; | ||||
| } | } | ||||
| if ($raw->isIssueEvent()) { | if ($raw->isIssueEvent()) { | ||||
| $ref_type = DoorkeeperBridgeGitHubIssue::OBJTYPE_GITHUB_ISSUE; | $ref_type = DoorkeeperBridgeGitHubIssue::OBJTYPE_GITHUB_ISSUE; | ||||
| $issue_number = $raw->getIssueNumber(); | $issue_number = $raw->getIssueNumber(); | ||||
| $full_ref = "{$full_repository}#{$issue_number}"; | $full_ref = "{$full_repository}#{$issue_number}"; | ||||
| } else { | } else { | ||||
| return null; | return null; | ||||
| } | } | ||||
| return id(new DoorkeeperObjectRef()) | return $this->newDoorkeeperRef() | ||||
| ->setApplicationType(DoorkeeperBridgeGitHub::APPTYPE_GITHUB) | |||||
| ->setApplicationDomain(DoorkeeperBridgeGitHub::APPDOMAIN_GITHUB) | |||||
| ->setObjectType($ref_type) | ->setObjectType($ref_type) | ||||
| ->setObjectID($full_ref); | ->setObjectID($full_ref); | ||||
| } | } | ||||
| private function newDoorkeeperRef() { | |||||
| return id(new DoorkeeperObjectRef()) | |||||
| ->setApplicationType(DoorkeeperBridgeGitHub::APPTYPE_GITHUB) | |||||
| ->setApplicationDomain(DoorkeeperBridgeGitHub::APPDOMAIN_GITHUB); | |||||
| } | |||||
| private function reloadExternalObject(NuanceItem $item, $local = false) { | private function reloadExternalObject(NuanceItem $item, $local = false) { | ||||
| $ref = $this->getDoorkeeperRef($item); | $ref = $this->getDoorkeeperRef($item); | ||||
| if (!$ref) { | if (!$ref) { | ||||
| return null; | return null; | ||||
| } | } | ||||
| $xobj = $this->reloadExternalRef($item, $ref, $local); | |||||
| if ($xobj) { | |||||
| $this->externalObject = $xobj; | |||||
| } | |||||
| return $xobj; | |||||
| } | |||||
| private function reloadExternalActor(NuanceItem $item, $local = false) { | |||||
| $ref = $this->getDoorkeeperActorRef($item); | |||||
| if (!$ref) { | |||||
| return null; | |||||
| } | |||||
| $xobj = $this->reloadExternalRef($item, $ref, $local); | |||||
| if ($xobj) { | |||||
| $this->externalActor = $xobj; | |||||
| } | |||||
| return $xobj; | |||||
| } | |||||
| private function reloadExternalRef( | |||||
| NuanceItem $item, | |||||
| DoorkeeperObjectRef $ref, | |||||
| $local) { | |||||
| $source = $item->getSource(); | $source = $item->getSource(); | ||||
| $token = $source->getSourceProperty('github.token'); | $token = $source->getSourceProperty('github.token'); | ||||
| $token = new PhutilOpaqueEnvelope($token); | $token = new PhutilOpaqueEnvelope($token); | ||||
| $viewer = $this->getViewer(); | $viewer = $this->getViewer(); | ||||
| $ref = id(new DoorkeeperImportEngine()) | $ref = id(new DoorkeeperImportEngine()) | ||||
| ->setViewer($viewer) | ->setViewer($viewer) | ||||
| ->setRefs(array($ref)) | ->setRefs(array($ref)) | ||||
| ->setThrowOnMissingLink(true) | ->setThrowOnMissingLink(true) | ||||
| ->setContextProperty('github.token', $token) | ->setContextProperty('github.token', $token) | ||||
| ->needLocalOnly($local) | ->needLocalOnly($local) | ||||
| ->executeOne(); | ->executeOne(); | ||||
| if ($ref->getSyncFailed()) { | if ($ref->getSyncFailed()) { | ||||
| $xobj = null; | $xobj = null; | ||||
| } else { | } else { | ||||
| $xobj = $ref->getExternalObject(); | $xobj = $ref->getExternalObject(); | ||||
| } | } | ||||
| if ($xobj) { | |||||
| $this->externalObject = $xobj; | |||||
| } | |||||
| return $xobj; | return $xobj; | ||||
| } | } | ||||
| private function getExternalObject(NuanceItem $item) { | private function getExternalObject(NuanceItem $item) { | ||||
| if ($this->externalObject === null) { | if ($this->externalObject === null) { | ||||
| $xobj = $this->reloadExternalObject($item, $local = true); | $xobj = $this->reloadExternalObject($item, $local = true); | ||||
| if ($xobj) { | if ($xobj) { | ||||
| $this->externalObject = $xobj; | $this->externalObject = $xobj; | ||||
| } else { | } else { | ||||
| $this->externalObject = false; | $this->externalObject = false; | ||||
| } | } | ||||
| } | } | ||||
| if ($this->externalObject) { | if ($this->externalObject) { | ||||
| return $this->externalObject; | return $this->externalObject; | ||||
| } | } | ||||
| return null; | return null; | ||||
| } | } | ||||
| private function getExternalActor(NuanceItem $item) { | |||||
| if ($this->externalActor === null) { | |||||
| $xobj = $this->reloadExternalActor($item, $local = true); | |||||
| if ($xobj) { | |||||
| $this->externalActor = $xobj; | |||||
| } else { | |||||
| $this->externalActor = false; | |||||
| } | |||||
| } | |||||
| if ($this->externalActor) { | |||||
| return $this->externalActor; | |||||
| } | |||||
| return null; | |||||
| } | |||||
| private function newRawEvent(NuanceItem $item) { | private function newRawEvent(NuanceItem $item) { | ||||
| $type = $item->getItemProperty('api.type'); | $type = $item->getItemProperty('api.type'); | ||||
| $raw = $item->getItemProperty('api.raw', array()); | $raw = $item->getItemProperty('api.raw', array()); | ||||
| return NuanceGitHubRawEvent::newEvent($type, $raw); | return NuanceGitHubRawEvent::newEvent($type, $raw); | ||||
| } | } | ||||
| public function getItemActions(NuanceItem $item) { | public function getItemActions(NuanceItem $item) { | ||||
| Show All 37 Lines | if ($xobj) { | ||||
| ->executeOne(); | ->executeOne(); | ||||
| if ($task) { | if ($task) { | ||||
| $panels[] = $this->newCurtainPanel($item) | $panels[] = $this->newCurtainPanel($item) | ||||
| ->setHeaderText(pht('Imported As')) | ->setHeaderText(pht('Imported As')) | ||||
| ->appendChild($viewer->renderHandle($task->getPHID())); | ->appendChild($viewer->renderHandle($task->getPHID())); | ||||
| } | } | ||||
| } | } | ||||
| $xactor = $this->getExternalActor($item); | |||||
| if ($xactor) { | |||||
| $panels[] = $this->newCurtainPanel($item) | |||||
| ->setHeaderText(pht('GitHub Actor')) | |||||
| ->appendChild($viewer->renderHandle($xactor->getPHID())); | |||||
| } | |||||
| return $panels; | return $panels; | ||||
| } | } | ||||
| protected function handleAction(NuanceItem $item, $action) { | protected function handleAction(NuanceItem $item, $action) { | ||||
| $viewer = $this->getViewer(); | $viewer = $this->getViewer(); | ||||
| $controller = $this->getController(); | $controller = $this->getController(); | ||||
| switch ($action) { | switch ($action) { | ||||
| ▲ Show 20 Lines • Show All 173 Lines • ▼ Show 20 Lines | private function syncItem( | ||||
| return array( | return array( | ||||
| 'objectPHID' => $task->getPHID(), | 'objectPHID' => $task->getPHID(), | ||||
| 'xactionPHIDs' => mpull($xactions, 'getPHID'), | 'xactionPHIDs' => mpull($xactions, 'getPHID'), | ||||
| ); | ); | ||||
| } | } | ||||
| protected function getActingAsPHID(NuanceItem $item) { | protected function getActingAsPHID(NuanceItem $item) { | ||||
| // TODO: This should be an external account PHID representing the original | $actor_phid = $item->getRequestorPHID(); | ||||
| // GitHub user. | |||||
| if ($actor_phid) { | |||||
| return $actor_phid; | |||||
| } | |||||
| return parent::getActingAsPHID($item); | return parent::getActingAsPHID($item); | ||||
| } | } | ||||
| } | } | ||||