[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/resources/sql/patches/ -> 20131121.repocredentials.2.mig.php (source)

   1  <?php
   2  
   3  $table = new PhabricatorRepository();
   4  $conn_w = $table->establishConnection('w');
   5  $viewer = PhabricatorUser::getOmnipotentUser();
   6  
   7  $map = array();
   8  foreach (new LiskMigrationIterator($table) as $repository) {
   9    $callsign = $repository->getCallsign();
  10    echo "Examining repository {$callsign}...\n";
  11  
  12    if ($repository->getCredentialPHID()) {
  13      echo "...already has a Credential.\n";
  14      continue;
  15    }
  16  
  17    $raw_uri = $repository->getRemoteURI();
  18    if (!$raw_uri) {
  19      echo "...no remote URI.\n";
  20      continue;
  21    }
  22  
  23    $uri = new PhutilURI($raw_uri);
  24  
  25    $proto = strtolower($uri->getProtocol());
  26    if ($proto == 'http' || $proto == 'https' || $proto == 'svn') {
  27      $username = $repository->getDetail('http-login');
  28      $secret = $repository->getDetail('http-pass');
  29      $type = PassphraseCredentialTypePassword::CREDENTIAL_TYPE;
  30    } else {
  31      $username = $repository->getDetail('ssh-login');
  32      if (!$username) {
  33        // If there's no explicit username, check for one in the URI. This is
  34        // possible with older repositories.
  35        $username = $uri->getUser();
  36        if (!$username) {
  37          // Also check for a Git/SCP-style URI.
  38          $git_uri = new PhutilGitURI($raw_uri);
  39          $username = $git_uri->getUser();
  40        }
  41      }
  42      $file = $repository->getDetail('ssh-keyfile');
  43      if ($file) {
  44        $secret = $file;
  45        $type = PassphraseCredentialTypeSSHPrivateKeyFile::CREDENTIAL_TYPE;
  46      } else {
  47        $secret = $repository->getDetail('ssh-key');
  48        $type = PassphraseCredentialTypeSSHPrivateKeyText::CREDENTIAL_TYPE;
  49      }
  50    }
  51  
  52    if (!$username || !$secret) {
  53      echo "...no credentials set.\n";
  54      continue;
  55    }
  56  
  57    $map[$type][$username][$secret][] = $repository;
  58    echo "...will migrate.\n";
  59  }
  60  
  61  $passphrase = new PassphraseSecret();
  62  $passphrase->openTransaction();
  63  $table->openTransaction();
  64  
  65  foreach ($map as $credential_type => $credential_usernames) {
  66    $type = PassphraseCredentialType::getTypeByConstant($credential_type);
  67    foreach ($credential_usernames as $username => $credential_secrets) {
  68      foreach ($credential_secrets as $secret_plaintext => $repositories) {
  69        $callsigns = mpull($repositories, 'getCallsign');
  70  
  71        $signs = implode(', ', $callsigns);
  72  
  73        $name = pht(
  74          'Migrated Repository Credential (%s)',
  75          id(new PhutilUTF8StringTruncator())
  76            ->setMaximumGlyphs(128)
  77            ->truncateString($signs));
  78  
  79        echo "Creating: {$name}...\n";
  80  
  81        $secret = id(new PassphraseSecret())
  82          ->setSecretData($secret_plaintext)
  83          ->save();
  84  
  85        $secret_id = $secret->getID();
  86  
  87        $credential = PassphraseCredential::initializeNewCredential($viewer)
  88          ->setCredentialType($type->getCredentialType())
  89          ->setProvidesType($type->getProvidesType())
  90          ->setViewPolicy(PhabricatorPolicies::POLICY_ADMIN)
  91          ->setEditPolicy(PhabricatorPolicies::POLICY_ADMIN)
  92          ->setName($name)
  93          ->setUsername($username)
  94          ->setSecretID($secret_id);
  95  
  96        $credential->setPHID($credential->generatePHID());
  97  
  98        queryfx(
  99          $credential->establishConnection('w'),
 100          'INSERT INTO %T (name, credentialType, providesType, viewPolicy,
 101            editPolicy, description, username, secretID, isDestroyed,
 102            phid, dateCreated, dateModified)
 103            VALUES (%s, %s, %s, %s, %s, %s, %s, %d, %d, %s, %d, %d)',
 104          $credential->getTableName(),
 105          $credential->getName(),
 106          $credential->getCredentialType(),
 107          $credential->getProvidesType(),
 108          $credential->getViewPolicy(),
 109          $credential->getEditPolicy(),
 110          $credential->getDescription(),
 111          $credential->getUsername(),
 112          $credential->getSecretID(),
 113          $credential->getIsDestroyed(),
 114          $credential->getPHID(),
 115          time(),
 116          time());
 117  
 118        foreach ($repositories as $repository) {
 119          queryfx(
 120            $conn_w,
 121            'UPDATE %T SET credentialPHID = %s WHERE id = %d',
 122            $table->getTableName(),
 123            $credential->getPHID(),
 124            $repository->getID());
 125  
 126          $edge_type = PhabricatorEdgeConfig::TYPE_OBJECT_USES_CREDENTIAL;
 127  
 128          id(new PhabricatorEdgeEditor())
 129            ->addEdge($repository->getPHID(), $edge_type, $credential->getPHID())
 130            ->save();
 131        }
 132      }
 133    }
 134  }
 135  
 136  $table->saveTransaction();
 137  $passphrase->saveTransaction();
 138  
 139  echo "Done.\n";


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1