]> khamidou — GitHub mirrors - arcanist.git/commitdiff
arcanist: add postponed linter support
authorAndrew Gallagher <agallagher@fb.com>
Mon, 2 Jul 2012 22:53:22 +0000 (15:53 -0700)
committerAndrew Gallagher <agallagher@fb.com>
Thu, 12 Jul 2012 01:09:05 +0000 (18:09 -0700)
Summary:
This uses a similiar approach as with postponed unittests, allowing
the lint workflow/engine to report postponed linter names.  After
the lint engine is run, a separate method is used to collect any
postponed linters and these are reposted to the diff via the
"arc:lint-postponed" property.

Also, a ##diff.wasCreated## was added allowing hooks to be called
immediately after the call ##differential.creatediff## with the
returned diff ID.

Test Plan:
Created diffs with a dummy lint engine which always reports a
postponed linter.

Reviewers: epriestley, vrana

Reviewed By: epriestley

CC: aran, Korvin
Maniphest Tasks: T1332

Differential Revision: https://secure.phabricator.com/D2933

src/events/constant/ArcanistEventType.php
src/lint/engine/ArcanistLintEngine.php
src/workflow/ArcanistDiffWorkflow.php
src/workflow/ArcanistLintWorkflow.php

index 411dbaaf74b120e65dbc34b7d1a7d74824b102d5..37879396208d1a3cd1b04cd494fdbe7a97c85f16 100644 (file)
@@ -20,5 +20,6 @@ final class ArcanistEventType extends PhutilEventType {
 
   const TYPE_COMMIT_WILLCOMMITSVN   = 'commit.willCommitSVN';
   const TYPE_DIFF_WILLBUILDMESSAGE  = 'diff.willBuildMessage';
+  const TYPE_DIFF_WASCREATED        = 'diff.wasCreated';
 
 }
index aed642ba54a38294b9d2161df3e5b39fabae1581..858285c087be8dc3edda71cd39830ad0a5263eff 100644 (file)
@@ -72,6 +72,9 @@ abstract class ArcanistLintEngine {
   private $commitHookMode = false;
   private $hookAPI;
 
+  private $enableAsyncLint = false;
+  private $postponedLinters = array();
+
   public function __construct() {
 
   }
@@ -126,6 +129,15 @@ abstract class ArcanistLintEngine {
     return $this->hookAPI;
   }
 
+  public function setEnableAsyncLint($enable_async_lint) {
+    $this->enableAsyncLint = $enable_async_lint;
+    return $this;
+  }
+
+  public function getEnableAsyncLint() {
+    return $this->enableAsyncLint;
+  }
+
   public function loadData($path) {
     if (!isset($this->fileData[$path])) {
       if ($this->getCommitHookMode()) {
@@ -295,5 +307,13 @@ abstract class ArcanistLintEngine {
     return array($line, $char);
   }
 
+  public function getPostponedLinters() {
+    return $this->postponedLinters;
+  }
+
+  public function setPostponedLinters(array $linters) {
+    $this->postponedLinters = $linters;
+    return $this;
+  }
 
 }
index b8aab48a6b1617713e7863abf68cc0fd462936a4..73c36f2333c4529bf29b40d42efbed652531aba4 100644 (file)
@@ -36,6 +36,8 @@ final class ArcanistDiffWorkflow extends ArcanistBaseWorkflow {
   private $diffID;
   private $revisionID;
   private $unitWorkflow;
+  private $lintWorkflow;
+  private $postponedLinters;
 
   public function getCommandSynopses() {
     return phutil_console_format(<<<EOTEXT
@@ -387,6 +389,8 @@ EOTEXT
 
     $this->diffID = $diff_info['diffid'];
 
+    $this->dispatchDiffWasCreatedEvent($diff_info['diffid']);
+
     if ($this->unitWorkflow) {
       $this->unitWorkflow->setDifferentialDiffID($diff_info['diffid']);
     }
@@ -1093,6 +1097,7 @@ EOTEXT
         $argv[] = $repository_api->getRelativeCommit();
       }
       $lint_workflow = $this->buildChildWorkflow('lint', $argv);
+      $this->lintWorkflow = $lint_workflow;
 
       if ($this->shouldAmend()) {
         // TODO: We should offer to create a checkpoint commit.
@@ -1118,9 +1123,15 @@ EOTEXT
             "Lint issued unresolved errors!",
             'lint-excuses');
           break;
+        case ArcanistLintWorkflow::RESULT_POSTPONED:
+          echo phutil_console_format(
+            "<bg:yellow>** LINT POSTPONED **</bg> ".
+            "Lint results are postponed.\n");
+          break;
       }
 
       $this->unresolvedLint = $lint_workflow->getUnresolvedMessages();
+      $this->postponedLinters = $lint_workflow->getPostponedLinters();
 
       return $lint_result;
     } catch (ArcanistNoEngineException $ex) {
@@ -1945,6 +1956,7 @@ EOTEXT
       ArcanistLintWorkflow::RESULT_ERRORS     => 'fail',
       ArcanistLintWorkflow::RESULT_WARNINGS   => 'warn',
       ArcanistLintWorkflow::RESULT_SKIP       => 'skip',
+      ArcanistLintWorkflow::RESULT_POSTPONED  => 'postponed',
     );
     return idx($map, $lint_result, 'none');
   }
@@ -2052,28 +2064,33 @@ EOTEXT
    * @task diffprop
    */
   private function updateLintDiffProperty() {
-    if (!$this->unresolvedLint) {
-      return;
-    }
 
-    $data = array();
-    foreach ($this->unresolvedLint as $message) {
-      $data[] = array(
-        'path'        => $message->getPath(),
-        'line'        => $message->getLine(),
-        'char'        => $message->getChar(),
-        'code'        => $message->getCode(),
-        'severity'    => $message->getSeverity(),
-        'name'        => $message->getName(),
-        'description' => $message->getDescription(),
-      );
+    if ($this->unresolvedLint) {
+      $data = array();
+      foreach ($this->unresolvedLint as $message) {
+        $data[] = array(
+          'path'        => $message->getPath(),
+          'line'        => $message->getLine(),
+          'char'        => $message->getChar(),
+          'code'        => $message->getCode(),
+          'severity'    => $message->getSeverity(),
+          'name'        => $message->getName(),
+          'description' => $message->getDescription(),
+        );
+      }
+
+      $this->updateDiffProperty('arc:lint', json_encode($data));
+      if (strlen($this->lintExcuse)) {
+        $this->updateDiffProperty('arc:lint-excuse',
+          json_encode($this->lintExcuse));
+      }
     }
 
-    $this->updateDiffProperty('arc:lint', json_encode($data));
-    if (strlen($this->lintExcuse)) {
-      $this->updateDiffProperty('arc:lint-excuse',
-        json_encode($this->lintExcuse));
+    $postponed = $this->postponedLinters;
+    if ($postponed) {
+      $this->updateDiffProperty('arc:lint-postponed', json_encode($postponed));
     }
+
   }
 
 
@@ -2157,4 +2174,13 @@ EOTEXT
     return $event->getValue('fields');
   }
 
+  private function dispatchDiffWasCreatedEvent($diff_id) {
+    $event = new PhutilEvent(
+      ArcanistEventType::TYPE_DIFF_WASCREATED,
+      array(
+        'diffID' => $diff_id,
+      ));
+
+    PhutilEventEngine::dispatchEvent($event);
+  }
 }
index 2504e7ba52a447409fb4bc11ecb50da05d8f6583..cd8cb8fc2a63c0260f779668e8904830f979473e 100644 (file)
@@ -27,11 +27,14 @@ class ArcanistLintWorkflow extends ArcanistBaseWorkflow {
   const RESULT_WARNINGS   = 1;
   const RESULT_ERRORS     = 2;
   const RESULT_SKIP       = 3;
+  const RESULT_POSTPONED  = 4;
 
   private $unresolvedMessages;
   private $shouldAmendChanges = false;
   private $shouldAmendWithoutPrompt = false;
   private $shouldAmendAutofixesWithoutPrompt = false;
+  private $engine;
+  private $postponedLinters;
 
   public function setShouldAmendChanges($should_amend) {
     $this->shouldAmendChanges = $should_amend;
@@ -173,6 +176,7 @@ EOTEXT
     }
 
     $engine = newv($engine, array());
+    $this->engine = $engine;
     $engine->setWorkingCopy($working_copy);
 
     if ($this->getArgument('advice')) {
@@ -195,8 +199,21 @@ EOTEXT
       }
     }
 
+    // Enable possible async linting only for 'arc diff' not 'arc unit'
+    if ($this->getParentWorkflow()) {
+      $engine->setEnableAsyncLint(true);
+    } else {
+      $engine->setEnableAsyncLint(false);
+    }
+
     $results = $engine->run();
 
+    // It'd be nice to just return a single result from the run method above
+    // which contains both the lint messages and the postponed linters.
+    // However, to maintain compatibility with existing lint subclasses, use
+    // a separate method call to grab the postponed linters.
+    $this->postponedLinters = $engine->getPostponedLinters();
+
     if ($this->getArgument('never-apply-patches')) {
       $apply_patches = false;
     } else {
@@ -342,6 +359,8 @@ EOTEXT
       $result_code = self::RESULT_ERRORS;
     } else if ($has_warnings) {
       $result_code = self::RESULT_WARNINGS;
+    } else if (!empty($this->postponedLinters)) {
+      $result_code = self::RESULT_POSTPONED;
     } else {
       $result_code = self::RESULT_OKAY;
     }
@@ -359,4 +378,8 @@ EOTEXT
     return $this->unresolvedMessages;
   }
 
+  public function getPostponedLinters() {
+    return $this->postponedLinters;
+  }
+
 }