Skip to content

Instantly share code, notes, and snippets.

@itc-lab
Created December 15, 2021 10:13
Show Gist options
  • Save itc-lab/dd3635dabae2bbfc633c9cbf1323d251 to your computer and use it in GitHub Desktop.
Save itc-lab/dd3635dabae2bbfc633c9cbf1323d251 to your computer and use it in GitHub Desktop.
<?php
define("URL", "http://gitlab.example.com/");
define("PER_PAGE", 100);
$set_hook_param = array(
'url' => 'http://gitlab-download-app.example.com/update_projects_json.php',
'token' => '',
'push_events' => 'TRUE',
'push_events_branch_filter' => '',
'tag_push_events' => 'FALSE',
'note_events' => 'FALSE',
'confidential_note_events' => 'FALSE',
'issues_events' => 'FALSE',
'confidential_issues_events' => 'FALSE',
'merge_requests_events' => 'FALSE',
'job_events' => 'FALSE',
'pipeline_events' => 'FALSE',
'wiki_page_events' => 'FALSE',
'deployment_events' => 'FALSE',
'releases_events' => 'FALSE',
'enable_ssl_verification' => 'FALSE'
);
define("SET_HOOK_PARAM", $set_hook_param);
$headers = array(
"private-token: XXXXXXXXXXXXXXXXXXXX"
);
static $set_hook_param = SET_HOOK_PARAM;
if ($argc > 1) {
add_hook_project($argv[1], $headers);
} else {
add_hook_all_projects($headers);
}
function add_hook_all_projects($headers)
{
$ret = request_projects($headers);
if ($ret == false) {
return;
}
$projects_arr = $ret;
for ($n = 0; $n < count($projects_arr); $n ++) {
if ($projects_arr[$n]["visibility"]=="internal") {
unset($projects_arr[$n]);
$projects_arr = array_values($projects_arr);
}
}
foreach ($projects_arr as $project) {
$ret = add_hook_project($project["path_with_namespace"], $headers);
if ($ret == false) {
return;
}
}
}
function add_hook_project($path_with_namespace, $headers)
{
$ret = request_project_hooks($path_with_namespace, $headers);
if ($ret == false) {
return false;
}
$hooks = $ret;
$set_hook = true;
if (! empty($hooks)) {
$hooks_arr = json_decode($hooks, true);
foreach ($hooks_arr as $hook_arr) {
if ($hook_arr["url"] == SET_HOOK_PARAM["url"]) {
$set_hook = false;
break;
}
}
}
if ($set_hook == true) {
$ret = set_project_hook($path_with_namespace, $headers);
if ($ret == false) {
return false;
}
}
return true;
}
function request_projects($headers)
{
$projects = array();
$url = URL . 'api/v4/projects/';
for ($n = 1; ; $n ++) {
$params = [
'order_by' => 'name',
'simple' => 'false',
'sort' => 'asc',
'd_after' => 0,
'per_page' => PER_PAGE,
'page' => $n
];
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$ret = curl_exec($curl);
$rcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($rcode != 200) {
return false;
}
$projects_arr = json_decode($ret, true);
if (0 < count($projects_arr)) {
$projects = array_merge_recursive($projects, $projects_arr);
}
if (count($projects_arr) < PER_PAGE) {
break;
}
}
return($projects);
}
function request_project_hooks($path_with_namespace, $headers)
{
$url = URL . 'api/v4/projects/' . urlencode($path_with_namespace) . '/hooks';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$ret = curl_exec($curl);
$rcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($rcode == 0) {
return false;
}
if ($rcode >= 400) {
return false;
}
return($ret);
}
function set_project_hook($path_with_namespace, $headers)
{
$url = URL . 'api/v4/projects/' . urlencode($path_with_namespace) . '/hooks';
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, SET_HOOK_PARAM);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$ret = curl_exec($curl);
$rcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
if ($rcode == 0) {
return false;
}
if ($rcode >= 400) {
return false;
}
return($ret);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment