Created
November 5, 2015 17:04
-
-
Save anonymous/5ea70b650f0288eb1c9c to your computer and use it in GitHub Desktop.
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 | |
/* | |
* @author Andy Foster | |
* @company Sparks Interactive | |
* @date 4 Nov, 2015 | |
* | |
* GitLab Migration Script | |
* We wrote this script to mass-migrate our git repositories from our local | |
* hosted version of Gitorious over to GitLab. | |
* | |
* Run from the command line: | |
* $ php migration_script.php 20 client_project | |
* | |
*/ | |
define("SERVERNAME", "localhost"); | |
define("USERNAME", "root"); | |
define("PASSWORD", "password"); | |
define("DBNAME", "the_gitorious_database"); | |
// No more than 2 parameters allowed | |
if ($argc > 3) { | |
echo <<<'EOT' | |
ERROR | |
Usage: $ php script.php {LIMIT} {PROJECT_SLUG} | |
EOT; | |
exit; | |
} | |
// Defaults for input parameters | |
$project_limit = isset($argv[1]) ? $argv[1] : 1; | |
$project_query = isset($argv[2]) ? | |
"WHERE slug = {$argv[2]}" : | |
"WHERE 1=1"; | |
// Create connection | |
$conn = new mysqli(SERVERNAME, USERNAME, PASSWORD, DBNAME); | |
// Check connection | |
if ($conn->connect_error) { | |
die("Connection failed: " . $conn->connect_error); | |
} | |
// Count how many rows are migrated | |
$count = 0; | |
// Select a project from the database | |
$projects_query = "SELECT * FROM projects $project_query AND slug NOT LIKE " . | |
"'%-migrated' ORDER BY LIMIT $project_limit"; | |
// Loop through each project | |
$result = $conn->query($projects_query); | |
if ($result->num_rows > 0) { | |
while($project = $result->fetch_assoc()) { | |
$project_id = $project['id']; | |
// append -migrated tag in Gitorious | |
echo "\n" . 'appending "-migrated" to project ' . $project['slug'] . | |
"... \n\n"; | |
mark_as_migrated($conn, $project['slug'], $project_id); | |
$repo_query = "SELECT * from repositories WHERE " . | |
"project_id = '$project_id' AND name NOT LIKE '%-wiki' " . | |
"AND name NOT LIKE '%-migrated%'"; | |
echo $repo_query . "\n\n"; | |
$repoResult = $conn->query($repo_query); | |
while($repo = $repoResult->fetch_assoc()) { | |
create_bare_repo_on_gitlab( | |
$project['slug'], | |
$project['description'], | |
$repo['name'], | |
$repo['description']); | |
clone_and_push_repo($project['slug'], $repo['name']); | |
$count++; | |
} | |
} | |
echo <<<EOT | |
================================== | |
Script finished. Migrated $count repos. | |
================================== | |
EOT; | |
} else { | |
echo "0 results\n"; | |
} | |
$conn->close(); | |
// Makes a repo with admin as the owner. only admin can delete | |
function create_bare_repo_on_gitlab( | |
$project_slug, | |
$project_description, | |
$repo_name, | |
$repo_description) { | |
// 1. create a bare repo on GitLab with the correct title and description | |
$description = trim( | |
urlencode($project_description . " -- " . $repo_description)); | |
$curl = 'curl --header "PRIVATE-TOKEN: B7ababab78ab78ab78cq" -X POST ' . | |
'"https://gitlab.com/api/v3/projects?name=' . $project_slug . '.' . | |
$repo_name . '&namespace_id=12345&description=' . $description . '"'; | |
// echo $curl."\n\n"; | |
shell_exec($curl); | |
} | |
function clone_and_push_repo($project_slug, $repo_name){ | |
$new_folder = $project_slug . "-" . $repo_name; | |
// Use the git tools to upload to the Repo | |
$mkdir = "mkdir {$new_folder}"; | |
echo $mkdir . "\n"; | |
shell_exec($mkdir); | |
$git_clone = "cd {$new_folder}; git clone " . | |
"--mirror https://git.sparksinteractive.co.nz/{$project_slug}-migrated/" . | |
"{$repo_name}.git .git"; | |
echo $git_clone . "\n"; | |
shell_exec($git_clone); | |
shell_exec("cd {$new_folder}; git config --bool core.bare false"); | |
$remote_add = "cd {$new_folder}; git remote add gitlab " . | |
"[email protected]:sparksi/{$project_slug}-{$repo_name}.git"; | |
echo $remote_add . "\n"; | |
shell_exec($remote_add); | |
$git_push = "cd {$new_folder}; git push --mirror gitlab"; | |
echo $git_push . "\n"; | |
shell_exec($git_push); | |
} | |
/** | |
* | |
* suspend each PROJECT not individual repos. | |
* LIMIT 1 is for testing | |
*/ | |
function mark_as_migrated($conn, $project_slug, $project_id){ | |
$project_slug_migrated = $project_slug . "-migrated"; | |
$suspend_project = "UPDATE projects SET slug = '$project_slug_migrated' " . | |
"WHERE id = '$project_id' LIMIT 1"; | |
$conn->query($suspend_project); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment