Repository navigation
Expand file tree
/
Copy pathgha_queue.php
More file actions
138 lines (111 loc) · 3.91 KB
/
Copy pathgha_queue.php
File metadata and controls
138 lines (111 loc) · 3.91 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/***
* This file allows provides a queue system for artifacts.
* Where a name matches several artifacts - a job re-run uploads a second under the same name - the newest is used.
*
***/
require_once("config.php");
define("CI_SNAPSHOTS", true);
require_once('lib/functions.common.php');
require_once('lib/functions.http.php');
header('Content-Type: text/plain');
if ((!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) && GHA_REQUIRES_AUTH) {
ExitUnauthorized();
}
// bootstrap web-app.
require_once('lib/init.php');
// test given auth credentials.
if (GHA_REQUIRES_AUTH) {
if (!CheckUserCredentials($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])) {
ExitForbidden();
}
}
if (getSnapshotDirectorySize() >= MAX_CAPACITY_BYTES && MAX_CAPACITY_DELETE_OLDEST == false) {
ExitFullStorage();
}
$artifact_name = null;
if ( isset($_REQUEST['artifact_name']) && !empty($_REQUEST['artifact_name']) ) {
if ( false == preg_match('/^[a-z0-9-_\.]+$/iu', $_REQUEST['artifact_name']) ) {
ExitClientError('Invalid artifact name');
} else {
$artifact_name = trim($_REQUEST['artifact_name']);
}
} else {
ExitClientError('Missing required parameter artifact_name');
}
$unzip = false;
if ( isset($_REQUEST['unzip']) ) {
$unzip = filter_var($_REQUEST['unzip'], FILTER_VALIDATE_BOOLEAN);
} else {
ExitClientError('Missing required parameter unzip');
}
$artifact_queue_file = $ScriptPath . DIRECTORY_SEPARATOR . TEMP_DIR . GHA_QFILE_PREFIX . $artifact_name . GHA_QFILE_EXT ;
$gha_list_url = GHA_LIST_URL . '?per_page=100&page=1';
if ( file_exists($artifact_queue_file) ) {
ExitFailedRequest('Artifact is already queued');
}
$list_req = do_curl_get($gha_list_url, array('Authorization: token ' . GHA_AUTH_TOKEN));
if ( $list_req === false ) {
ExitFailedRequest('Error checking artifact list');
}
if ( $list_req[2] !== 200 ) {
ExitFailedRequest('Failed to fetch artifact list - ' . strval($list_req[2]) );
}
$list_data = json_decode($list_req[0], true);
if ( ! isset($list_data['artifacts']) ) {
ExitFailedRequest('Failed to find artifacts');
}
$artifact_id = null;
$artifact_created = -1;
if ( !empty($list_data['artifacts']) ) {
// A name can legitimately match more than once: re-running a build job uploads a second
// artifact under the same name. Pick the newest by creation time rather than trusting the
// order the API happens to return, so a re-run publishes its own build and not the stale
// one from the earlier attempt.
// Only the first 100 entries are searched, so a name can still drop out of the window.
foreach( $list_data['artifacts'] as $idx => $arti ) {
if ( $artifact_name != $arti['name'] ) {
continue;
}
$created = isset($arti['created_at']) ? strtotime($arti['created_at']) : false;
if ( $created === false ) {
$created = 0;
}
if ( $created > $artifact_created ) {
$artifact_created = $created;
$artifact_id = $arti['id'];
}
}
}
$maxdays = 0;
if (isset($_SERVER['HTTP_MAX_DAYS'])) {
if (preg_match('/^[0-9]+$/i', $_SERVER['HTTP_MAX_DAYS']) === 1) {
$maxdays = intval($_SERVER['HTTP_MAX_DAYS']);
}
}
$maxdls = 0;
if (isset($_SERVER['HTTP_MAX_DOWNLOADS'])) {
if (preg_match('/^[0-9]+$/i', $_SERVER['HTTP_MAX_DOWNLOADS']) === 1) {
$maxdls = intval($_SERVER['HTTP_MAX_DOWNLOADS']);
}
}
$artifact_data = array(
'name' => $artifact_name,
'unzip' => $unzip,
'qtime' => time()
);
if ($artifact_id !== null) {
$artifact_data['id'] = $artifact_id;
}
if ($maxdays != 0) {
$artifact_data['maxdays'] = $maxdays;
}
if ($maxdls != 0) {
$artifact_data['maxdls'] = $maxdls;
}
$queue_json = json_encode($artifact_data);
if ( false !== file_put_contents($artifact_queue_file, $queue_json, LOCK_EX) ) {
echo('Artifact enqueued');
} else {
ExitFailedRequest('Failed to enqueue artifact');
}