Changeset View
Changeset View
Standalone View
Standalone View
resources/sql/patches/20131121.repocredentials.2.mig.php
| <?php | <?php | ||||
| $table = new PhabricatorRepository(); | $table = new PhabricatorRepository(); | ||||
| $conn_w = $table->establishConnection('w'); | $conn_w = $table->establishConnection('w'); | ||||
| $viewer = PhabricatorUser::getOmnipotentUser(); | $viewer = PhabricatorUser::getOmnipotentUser(); | ||||
| $map = array(); | $map = array(); | ||||
| foreach (new LiskMigrationIterator($table) as $repository) { | foreach (new LiskMigrationIterator($table) as $repository) { | ||||
| $callsign = $repository->getCallsign(); | $callsign = $repository->getCallsign(); | ||||
| echo "Examining repository {$callsign}...\n"; | echo pht('Examining repository %s...', $callsign)."\n"; | ||||
| if ($repository->getCredentialPHID()) { | if ($repository->getCredentialPHID()) { | ||||
| echo "...already has a Credential.\n"; | echo pht('...already has a Credential.')."\n"; | ||||
| continue; | continue; | ||||
| } | } | ||||
| $raw_uri = $repository->getRemoteURI(); | $raw_uri = $repository->getRemoteURI(); | ||||
| if (!$raw_uri) { | if (!$raw_uri) { | ||||
| echo "...no remote URI.\n"; | echo pht('...no remote URI.')."\n"; | ||||
| continue; | continue; | ||||
| } | } | ||||
| $uri = new PhutilURI($raw_uri); | $uri = new PhutilURI($raw_uri); | ||||
| $proto = strtolower($uri->getProtocol()); | $proto = strtolower($uri->getProtocol()); | ||||
| if ($proto == 'http' || $proto == 'https' || $proto == 'svn') { | if ($proto == 'http' || $proto == 'https' || $proto == 'svn') { | ||||
| $username = $repository->getDetail('http-login'); | $username = $repository->getDetail('http-login'); | ||||
| Show All 17 Lines | if ($file) { | ||||
| $type = PassphraseCredentialTypeSSHPrivateKeyFile::CREDENTIAL_TYPE; | $type = PassphraseCredentialTypeSSHPrivateKeyFile::CREDENTIAL_TYPE; | ||||
| } else { | } else { | ||||
| $secret = $repository->getDetail('ssh-key'); | $secret = $repository->getDetail('ssh-key'); | ||||
| $type = PassphraseCredentialTypeSSHPrivateKeyText::CREDENTIAL_TYPE; | $type = PassphraseCredentialTypeSSHPrivateKeyText::CREDENTIAL_TYPE; | ||||
| } | } | ||||
| } | } | ||||
| if (!$username || !$secret) { | if (!$username || !$secret) { | ||||
| echo "...no credentials set.\n"; | echo pht('...no credentials set.')."\n"; | ||||
| continue; | continue; | ||||
| } | } | ||||
| $map[$type][$username][$secret][] = $repository; | $map[$type][$username][$secret][] = $repository; | ||||
| echo "...will migrate.\n"; | echo pht('...will migrate.')."\n"; | ||||
| } | } | ||||
| $passphrase = new PassphraseSecret(); | $passphrase = new PassphraseSecret(); | ||||
| $passphrase->openTransaction(); | $passphrase->openTransaction(); | ||||
| $table->openTransaction(); | $table->openTransaction(); | ||||
| foreach ($map as $credential_type => $credential_usernames) { | foreach ($map as $credential_type => $credential_usernames) { | ||||
| $type = PassphraseCredentialType::getTypeByConstant($credential_type); | $type = PassphraseCredentialType::getTypeByConstant($credential_type); | ||||
| foreach ($credential_usernames as $username => $credential_secrets) { | foreach ($credential_usernames as $username => $credential_secrets) { | ||||
| foreach ($credential_secrets as $secret_plaintext => $repositories) { | foreach ($credential_secrets as $secret_plaintext => $repositories) { | ||||
| $callsigns = mpull($repositories, 'getCallsign'); | $callsigns = mpull($repositories, 'getCallsign'); | ||||
| $signs = implode(', ', $callsigns); | $signs = implode(', ', $callsigns); | ||||
| $name = pht( | $name = pht( | ||||
| 'Migrated Repository Credential (%s)', | 'Migrated Repository Credential (%s)', | ||||
| id(new PhutilUTF8StringTruncator()) | id(new PhutilUTF8StringTruncator()) | ||||
| ->setMaximumGlyphs(128) | ->setMaximumGlyphs(128) | ||||
| ->truncateString($signs)); | ->truncateString($signs)); | ||||
| echo "Creating: {$name}...\n"; | echo pht('Creating: %s...', $name)."\n"; | ||||
| $secret = id(new PassphraseSecret()) | $secret = id(new PassphraseSecret()) | ||||
| ->setSecretData($secret_plaintext) | ->setSecretData($secret_plaintext) | ||||
| ->save(); | ->save(); | ||||
| $secret_id = $secret->getID(); | $secret_id = $secret->getID(); | ||||
| $credential = PassphraseCredential::initializeNewCredential($viewer) | $credential = PassphraseCredential::initializeNewCredential($viewer) | ||||
| ▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | foreach ($credential_secrets as $secret_plaintext => $repositories) { | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| $table->saveTransaction(); | $table->saveTransaction(); | ||||
| $passphrase->saveTransaction(); | $passphrase->saveTransaction(); | ||||
| echo "Done.\n"; | echo pht('Done.')."\n"; | ||||