Better handling of error with git forges APIs

Note to self: Gitlab deprecated APIv3, had to update the link to v4
This commit is contained in:
Geoffrey Frogeye 2019-04-24 08:11:51 +02:00
parent 62df3fddb5
commit 17c4ebe5f6

View file

@ -348,7 +348,8 @@ function updateGitKeys($api, $keys)
function apiRequest($api, $route, $meth = 'GET', $data = null) function apiRequest($api, $route, $meth = 'GET', $data = null)
{ {
$ch = curl_init(); $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api['api'].'/'.$route); $url = $api['api'].'/'.$route;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $meth); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $meth);
curl_setopt($ch, CURLOPT_USERAGENT, 'Machines Frogeye'); curl_setopt($ch, CURLOPT_USERAGENT, 'Machines Frogeye');
if ($data) { if ($data) {
@ -366,7 +367,12 @@ function updateGitKeys($api, $keys)
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$raw = curl_exec($ch); $raw = curl_exec($ch);
curl_close($ch); curl_close($ch);
return json_decode($raw); $json = json_decode($raw);
if ($json !== null && isset($json->{"error"})) {
echo("Error with API ".$url.": ".$json->{"error"}."\n");
return null;
}
return $json;
} }
global $SSH_KEY_REGEX; global $SSH_KEY_REGEX;
@ -403,7 +409,6 @@ function updateGitKeys($api, $keys)
} }
foreach ($toDelete as $id => $ekey) { foreach ($toDelete as $id => $ekey) {
echo "387 deleting $id $ekey\n";
apiRequest($api, 'user/keys/'.$id, 'DELETE'); apiRequest($api, 'user/keys/'.$id, 'DELETE');
} }
} }
@ -657,7 +662,7 @@ case 'config':
foreach ($machines as $dMachineName => $dMachine) { foreach ($machines as $dMachineName => $dMachine) {
if ($network['secure'] == 'true') { if ($network['secure'] == 'true') {
} }
} }