This repository has been archived by the owner on May 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
bootstrap.php
107 lines (89 loc) · 3.08 KB
/
bootstrap.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?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('netlify')->extend([
'fetchDeploys' => function ($limit = 50) {
$settings = $this->app->config['netlify'] ?? FALSE;
if (!$settings || !isset($settings['api_url'],
$settings['site_id'],
$settings['access_token'])) {
return [];
}
if (isset($settings['limit'])) {
$limit = $settings['limit'];
}
$url = $settings['api_url'] . '/sites/' . $settings['site_id'] . '/deploys';
$url .= '?access_token=' . $settings['access_token'];
$url .= "&per_page={$limit}";
if (!empty($settings['branch'])) {
$url .= '&branch=' . $settings['branch'];
}
$headers = [
'Content-Type: application/json',
];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$deploys = curl_exec($ch);
curl_close($ch);
$deploys = json_decode($deploys);
if ($deploys && is_array($deploys)) {
$deploys = array_slice($deploys, 0, $limit);
} else {
$deploys = [];
}
// 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->state, ['building', 'enqueued', 'uploaded', 'uploaded', 'uploading', 'processing'])) {
$building = true;
$deploys[$idx]->building = true;
}
$deploys[$idx]->created_at = date('Y-m-d H:i', strtotime($deploy->created_at));
$deploys[$idx]->updated_at = date('Y-m-d H:i', strtotime($deploy->updated_at));
}
return [
'deploys' => $deploys,
'building' => $building,
];
},
'createDeploy' => function () {
$settings = $this->app->config['netlify'];
$url = $settings['build_hook_url'];
$data = json_encode([]);
$headers = [
'Content-Type: application/json',
'Content-Length: ' . strlen($data),
];
$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);
curl_close($ch);
return json_decode($result);
},
]);
// Include admin.
include_once __DIR__ . '/admin.php';
}
// Include REST API files.
if (COCKPIT_API_REQUEST) {
$this->on('cockpit.rest.init', function ($routes) {
$routes['netlify'] = 'Netlify\\Controller\\RestApi';
});
}