Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Small script to migrate Bitbucket repositories to GitHub.
<?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 git@bitbucket.org:%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 git@github.com:%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
You can’t perform that action at this time.