Created
January 18, 2019 18:19
-
-
Save claudiosanches/5f3abba82e127ccf03106df5a4ffcd27 to your computer and use it in GitHub Desktop.
Small script to migrate Bitbucket repositories to GitHub.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Migrate repositorioes from Bitbucket to GitHub. | |
* | |
* Usage: php run.php | |
* | |
* @package CS\Bitbucket_to_Github | |
* @license MIT License | |
*/ | |
class CS_Bitbucket_to_Github | |
{ | |
const GITHUB_USERNAME = ''; // Your GitHub username. | |
const GITHUB_TOKEN = ''; // Your GitHub Token. | |
const GITHUB_PRIVATE_REPOS = true; | |
const BITBUCKET_USERNAME = ''; // Your Bitbucket username | |
const BITBUCKET_REPOSITORIES = []; // Copy your repositories names from Bitbucket. @todo use bitbucket API. | |
/** | |
* Script current path. | |
* | |
* @var string | |
*/ | |
protected $current_path = ''; | |
/** | |
* Initialize. | |
*/ | |
public function __construct() | |
{ | |
$this->current_path = exec('pwd'); | |
} | |
/** | |
* Run migration. | |
* | |
* @return void | |
*/ | |
public function run() { | |
foreach (self::BITBUCKET_REPOSITORIES as $repo) { | |
exec(sprintf('git clone [email protected]:%s/%s.git', self::BITBUCKET_USERNAME, $repo)); | |
$this->create_gh_repo($repo); | |
exec(sprintf( | |
'cd %1$s/%2$s' . | |
'&& git remote rename origin bitbucket ' . | |
'&& git remote add origin [email protected]:%3$s/%2$s.git ' . | |
'&& git push origin master ' . | |
'&& git remote remove bitbucket ' . | |
'&& cd %1$s ' . | |
'&& rm -rf %1$s/%2$s', | |
$this->current_path, $repo, self::GITHUB_USERNAME | |
)); | |
} | |
} | |
/** | |
* Create repository in GitHub. | |
* | |
* @param string $name Repository name. | |
* @return void | |
*/ | |
protected function create_gh_repo(string $name) | |
{ | |
$ch = curl_init(); | |
$data = [ | |
'name' => $name, | |
'private' => self::GITHUB_PRIVATE_REPOS, | |
]; | |
curl_setopt_array( | |
$ch, | |
[ | |
CURLOPT_URL => 'https://api.github.com/user/repos', | |
CURLOPT_POST => true, | |
CURLOPT_POSTFIELDS => json_encode($data), | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_HTTPHEADER => [ | |
'Accept: application/json', | |
'Content-Type: application/json', | |
'Authorization: token '. self::GITHUB_USERNAME, | |
'User-Agent: CS-Bitbucket-to-Github' | |
] | |
] | |
); | |
$output = curl_exec($ch); | |
curl_close($ch); | |
} | |
} | |
$migrate = new CS_Bitbucket_to_Github(); | |
$migrate->run(); | |
echo 'DONE!'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment