Skip to content

Instantly share code, notes, and snippets.

@cyberwani
Forked from claudiosanches/run.php
Created October 27, 2021 20:27
Show Gist options
  • Select an option

  • Save cyberwani/baf59cb1e6f5dd36e689211745c1435c to your computer and use it in GitHub Desktop.

Select an option

Save cyberwani/baf59cb1e6f5dd36e689211745c1435c to your computer and use it in GitHub Desktop.

Revisions

  1. @claudiosanches claudiosanches created this gist Jan 18, 2019.
    93 changes: 93 additions & 0 deletions run.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,93 @@
    <?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!';