Commit 32eafb87 authored by Cerdic's avatar Cerdic
Browse files

on itere sur tous les dossier qui ont un .updated et on cree les groupes et...

on itere sur tous les dossier qui ont un .updated et on cree les groupes et les projets dans gitlab a la volee avant de faire un push --mirror
parent cf3b0a0c
#!/usr/bin/php
<?php
include_once __DIR__ . "/vendor/autoload.php";
include_once __DIR__ . "/inc-config.php";
$gitlab_config = parse_ini_file(_GITLAB_CONFIG_FILE);
......@@ -21,16 +22,138 @@ $orgs_to_clone = array_map('trim', $orgs_to_clone);
foreach ($orgs_to_clone as $org_to_clone) {
$dir_org = _DIR_CLONE . $org_to_clone . '/';
$updated_touch = glob($dir_org . '*/.git/updated');
$updated_touch = glob($dir_org . '*/.updated');
foreach ($updated_touch as $updated) {
$dir_repo = dirname(dirname($updated));
$project_name = basename($dir_repo);
$group_name = $org_to_clone;
$dir_repo = dirname($updated);
$project_path = basename($dir_repo);
$group_path = $org_to_clone;
echo "$group_name/$project_name : $dir_repo\n";
echo "$group_path/$project_path : $dir_repo\n";
$gitlab_group = gitlab_get_group($group_path);
$gitlab_project = gitlab_get_project($group_path, $project_path);
$url_ssh = $gitlab_project['ssh_url_to_repo'];
// et il n'y a plus qu'a push en mode mirror
$cmd = "cd $dir_repo && git push --mirror $url_ssh";
echo "$cmd\n";
passthru($cmd);
@unlink($updated);
die('?');
}
}
/*
function git_get_remote($dir_repo, $url) {
$remotes = git_get_remotes($dir_repo);
foreach ($remotes as $remote_name => $remote_url) {
if ($remote_url === $url) {
return $remote_name;
}
}
// on a pas trouver un remote qui correspond, on l'ajoute
$remote_name = 'mirror';
$cpt = '';
while (!empty($remotes[$remote_name.$cpt])) {
$cpt = intval($cpt) + 1;
}
$remote_name = $remote_name . $cpt;
passthru("cd $dir_repo && git remote add $remote_name $url");
$remotes = git_get_remotes($dir_repo);
if (empty($remotes[$remote_name]) or $remotes[$remote_name] !== $url) {
throw new Exception("Echec lors de l'ajout du remote $remote_name $url sur $dir_repo");
}
return $remote_name;
}
function git_get_remotes($dir_repo){
// recuperer les remote du dossier
$ouput = [];
exec("cd $dir_repo && git remote -v", $output);
$remotes = [];
foreach ($output as $o){
$o = preg_replace(",\s+,", " ", $o);
$o = explode(' ', $o);
$remote_name = array_shift($o);
$remote_url = array_shift($o);
$remotes[$remote_name] = $remote_url;
}
return $remotes;
}
*/
function gitlab_client() {
static $client;
if (is_null($client)) {
$client = new Gitlab\Client();
$client->setUrl($GLOBALS['gitlab_config']['url']);
$client->authenticate($GLOBALS['gitlab_config']['token-api'], Gitlab\Client::AUTH_HTTP_TOKEN);
}
return $client;
}
function gitlab_get_group($group_path) {
static $known_groups = [];
if (empty($known_groups[$group_path])) {
$client = gitlab_client();
// un show pour commencer
if ($res = $client->groups->show($group_path)) {
return $known_groups[$group_path] = $res;
}
// sinon un search au cas ou
$res = $client->groups->all(['search' => $group_path]);
if ($res) {
foreach ($res as $r) {
if ($r['full_path'] === $group_path) {
return $known_groups[$group_path] = $r;
}
}
}
// pas de groupe trouve avec ce path, on le cree
$res = $client->groups->create($group_path, $group_path, '', 'public');
if (!$res) {
throw new Exception("Erreur lors de la creation du groupe $group_path");
}
return $known_groups[$group_path] = $res;
}
return $known_groups[$group_path];
}
function gitlab_get_project($group_path, $project_path) {
static $known_projects = [];
if (empty($known_projects[$group_path])) {
$known_projects[$group_path] = [];
}
$gitlab_group = gitlab_get_group($group_path);
if (empty($known_projects[$group_path][$project_path])) {
$client = gitlab_client();
$res = $client->groups->projects($gitlab_group['id'], ['search' => $project_path]);
if ($res) {
foreach ($res as $r) {
if ($r['path_with_namespace'] === "$group_path/$project_path") {
return $known_projects[$group_path][$project_path] = $r;
}
}
}
// pas de projet trouve dans ce groupe, on le cree
// $gitlab_group['full_path'] . '/' .
$res = $client->projects->create($project_path, ['path' => $project_path, 'namespace_id' => $gitlab_group['id'], 'visibility' => 'public']);
if (!$res) {
throw new Exception("Erreur lors de la creation du groupe $group_path");
}
return $known_projects[$group_path][$project_path] = $res;
}
return $known_projects[$group_path][$project_path];
}
/*
// An example API call
$project = $client->projects()->create('My Project', [
'description' => 'This is a project',
'issues_enabled' => false,
]);
*/
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment