Created
April 30, 2019 12:24
-
-
Save viezel/2a44990ba0dccc04842c09d262cf0a26 to your computer and use it in GitHub Desktop.
Gitlab Project Migrations
This file contains hidden or 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
{ | |
"name": "napp/gitlab-migration", | |
"require": { | |
"php-http/guzzle6-adapter": "^1.0", | |
"m4tthumphrey/php-gitlab-api": "^9.13" | |
} | |
} |
This file contains hidden or 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 | |
ini_set('max_execution_time', 0); | |
require __DIR__ . '/vendor/autoload.php'; | |
// Gitlab Current Server | |
$currentServerUrl = 'https://example.mygitlab.com'; | |
$currentServerAuthToken = 'current_server_token'; | |
$currentServerGroupId = 123; | |
$username = 'myuser'; | |
$password = 'mypass'; | |
// Gitlab Receiving Server | |
$receivingServerUrl = 'https://gitlab.com'; | |
$receivingServerAuthToken = 'new_server_token'; | |
$receivingServerGroupId = 311; | |
// connect to current server | |
$client = \Gitlab\Client::create($currentServerUrl) | |
->authenticate($currentServerAuthToken,\Gitlab\Client::AUTH_URL_TOKEN); | |
// using pager to get all projects in group | |
$pager = new \Gitlab\ResultPager($client); | |
$projects = $pager->fetchAll($client->groups(), 'projects', [$currentServerGroupId, ['simple' => true]]); | |
$filtered = array_map(function ($element) use ($username, $password) { | |
return [ | |
'name' => $element['name'], | |
'description' => $element['description'], | |
'repo_url' => str_replace('https://', "https://{$username}:{$password}@", $element['http_url_to_repo']) | |
]; | |
}, $projects); | |
/////////////////////////// | |
/// MIGRATE TO NEW SERVER | |
/////////////////////////// | |
$receivingClient = \Gitlab\Client::create($receivingServerUrl) | |
->authenticate($receivingServerAuthToken,\Gitlab\Client::AUTH_URL_TOKEN); | |
foreach ($filtered as $project) { | |
try { | |
$response = $receivingClient->projects()->create($project['name'], [ | |
'description' => $project['description'], | |
'import_url' => $project['repo_url'], | |
'namespace_id' => $receivingServerGroupId | |
]); | |
echo $project['name'] . ' is imported <br>'; | |
} catch(\Exception $exception){ | |
// error | |
echo $project['name'] . ' could not be imported <br>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment