init
This commit is contained in:
133
bootstrap.php
Normal file
133
bootstrap.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Implements bootstrap functions.
|
||||
*/
|
||||
|
||||
// Include addon functions only if its an admin request.
|
||||
if (COCKPIT_ADMIN && !COCKPIT_API_REQUEST) {
|
||||
// Extend addon functions.
|
||||
$this->module('drone')->extend([
|
||||
'fetchDeploys' => function ($limit = 50) {
|
||||
$settings = $this->app->config['drone'] ?? FALSE;
|
||||
|
||||
if (!$settings || !isset($settings['url'],
|
||||
$settings['owner'],
|
||||
$settings['project'],
|
||||
$settings['environment'],
|
||||
$settings['token'])) {
|
||||
return [];
|
||||
}
|
||||
|
||||
// $url = $settings['api_url'] . '/sites/' . $settings['site_id'] . '/deploys' . '?access_token=' . $settings['access_token'];
|
||||
$url = trim($settings['url'], "/") . '/api/repos/' . $settings['owner'] . '/' . $settings['project'] . '/builds';
|
||||
$branch = $settings['branch'] ? $settings['branch'] : 'master';
|
||||
|
||||
$headers = [
|
||||
'Content-Type: application/json',
|
||||
'Accept: application/json',
|
||||
'Authorization: Bearer ' . $settings['token']
|
||||
];
|
||||
|
||||
$ch = curl_init($url);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
|
||||
$deploys = [];
|
||||
$unfiltered_deploys = curl_exec($ch);
|
||||
curl_close($ch);
|
||||
$unfiltered_deploys = json_decode($unfiltered_deploys);
|
||||
$build = $settings['build'];
|
||||
if ($unfiltered_deploys && is_array($unfiltered_deploys)) {
|
||||
foreach($unfiltered_deploys as $deploy) {
|
||||
if ($deploy->{target} == $branch && ($deploy->event == 'push' || ($deploy->event == 'promote' && $deploy->deploy_to == $settings['environment']))) {
|
||||
// find latest successful build
|
||||
if (!$build) {
|
||||
if ($deploy->event == 'push' && $deploy->status == 'success') {
|
||||
$build = $deploy->number;
|
||||
} else {
|
||||
$build = $deploy->parent;
|
||||
}
|
||||
}
|
||||
$limit--;
|
||||
if ($limit>0) {
|
||||
$deploys[] = $deploy;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Parse dates and check if any deploy is on building status.
|
||||
$building = false;
|
||||
foreach ($deploys as $idx => $deploy) {
|
||||
$deploys[$idx]->building = false;
|
||||
if (!in_array($deploy->status, ['success', 'failure'])) {
|
||||
$building = true;
|
||||
$deploys[$idx]->building = true;
|
||||
}
|
||||
$deploys[$idx]->created_at = date('Y-m-d H:i', $deploy->created);
|
||||
$deploys[$idx]->updated_at = date('Y-m-d H:i', $deploy->updated);
|
||||
if ($deploy->finished) {
|
||||
$deploys[$idx]->deploy_time = $deploy->finished - $deploy->started;
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
'deploys' => $deploys,
|
||||
'building' => $building,
|
||||
'build' => $build
|
||||
];
|
||||
},
|
||||
|
||||
'createDeploy' => function ($fromBuild) {
|
||||
$settings = $this->app->config['drone'];
|
||||
|
||||
if (!$fromBuild || !$settings || !isset($settings['url'],
|
||||
$settings['owner'],
|
||||
$settings['project'],
|
||||
$settings['environment'],
|
||||
$settings['token'])) {
|
||||
|
||||
return array(
|
||||
"error" => "missing settings"
|
||||
);
|
||||
}
|
||||
|
||||
$url = trim($settings['url'], "/") . '/api/repos/' . $settings['owner'] . '/' . $settings['project'] . '/builds/' . $fromBuild . '/promote?target=' . $settings['environment'];
|
||||
|
||||
$headers = [
|
||||
'Content-Type: application/json',
|
||||
'Accept: application/json',
|
||||
'Authorization: Bearer ' . $settings['token']
|
||||
];
|
||||
|
||||
$ch = curl_init($url);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
|
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
|
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
||||
// curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
|
||||
$result = curl_exec($ch);
|
||||
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
$decoded = json_decode($result);
|
||||
|
||||
if ($httpcode >= 400) {
|
||||
$decoded->error = "ERROR: $httpcode";
|
||||
}
|
||||
|
||||
return $decoded;
|
||||
},
|
||||
]);
|
||||
|
||||
// Include admin.
|
||||
include_once __DIR__ . '/admin.php';
|
||||
}
|
||||
Reference in New Issue
Block a user