-
Notifications
You must be signed in to change notification settings - Fork 23
/
repo-meta-api.php
296 lines (254 loc) · 6.51 KB
/
repo-meta-api.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
/**
* Support-level label functionality.
*
* @package Automattic/vip-go-ci
*/
declare(strict_types=1);
/**
* Fetch meta-data for repository from
* repo-meta API, cache the results in memory.
*
* @param string $repo_meta_api_base_url URL to repo-meta API.
* @param int|string $repo_meta_api_user_id User ID for repo-meta API.
* @param string $repo_meta_api_access_token Access token for repo-meta API.
* @param string $repo_owner Repository owner.
* @param string $repo_name Repository name.
*
* @return null|array Results as array on success, null on failure.
*/
function vipgoci_repo_meta_api_data_fetch(
string $repo_meta_api_base_url,
int|string $repo_meta_api_user_id,
string $repo_meta_api_access_token,
string $repo_owner,
string $repo_name
) {
$cached_id = array(
__FUNCTION__,
$repo_meta_api_base_url,
$repo_meta_api_user_id,
$repo_meta_api_access_token,
$repo_owner,
$repo_name,
);
$cached_data = vipgoci_cache( $cached_id );
vipgoci_log(
'Fetching repository meta-data from repo-meta API' .
vipgoci_cached_indication_str( $cached_data ),
array(
'repo_meta_api_base_url' => $repo_meta_api_base_url,
'repo_meta_api_user_id' => $repo_meta_api_user_id,
'repo_owner' => $repo_owner,
'repo_name' => $repo_name,
)
);
if ( false !== $cached_data ) {
return $cached_data;
}
$curl_retries = 0;
do {
$resp_data = false;
$resp_data_parsed = null;
$endpoint_url =
$repo_meta_api_base_url .
'/v1' .
'/sites?' .
'active=1&' .
'page=1&' .
'pagesize=20&' .
'source_repo=' . rawurlencode( $repo_owner . '/' . $repo_name );
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $endpoint_url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, VIPGOCI_HTTP_API_LONG_TIMEOUT );
curl_setopt(
$ch,
CURLOPT_USERAGENT,
VIPGOCI_CLIENT_ID
);
$endpoint_send_headers = array();
if ( ! empty( $repo_meta_api_user_id ) ) {
$endpoint_send_headers[] =
'API-User-ID: ' . $repo_meta_api_user_id;
}
if ( ! empty( $repo_meta_api_access_token ) ) {
$endpoint_send_headers[] =
'Access-Token: ' . $repo_meta_api_access_token;
}
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
$endpoint_send_headers
);
curl_setopt(
$ch,
CURLOPT_HEADERFUNCTION,
'vipgoci_curl_headers'
);
vipgoci_curl_set_security_options(
$ch
);
vipgoci_runtime_measure( VIPGOCI_RUNTIME_START, 'repo_meta_data_endpoint_api_request' );
vipgoci_counter_report(
VIPGOCI_COUNTERS_DO,
'repo_meta_data_endpoint_api'
);
$resp_data = curl_exec( $ch );
vipgoci_runtime_measure( VIPGOCI_RUNTIME_STOP, 'repo_meta_data_endpoint_api_request' );
if ( false !== $resp_data ) {
$resp_data_parsed = json_decode(
$resp_data,
true
);
}
$resp_headers = vipgoci_curl_headers(
null,
null
);
if (
( false === $resp_data ) ||
( null === $resp_data_parsed ) ||
(
( isset( $resp_data_parsed['status'] ) ) &&
( 'error' === $resp_data_parsed['status'] )
)
) {
vipgoci_log(
'Failed fetching or parsing data...',
array(
'resp_data' => $resp_data,
'resp_data_parsed' => $resp_data_parsed,
'curl_error' => curl_error( $ch ),
'http_status' => (
isset( $resp_headers['status'] ) ?
$resp_headers['status'] :
null
),
)
);
/*
* For the while() loop below.
*/
if ( ! isset( $resp_data_parsed['status'] ) ) {
$resp_data = false;
}
}
curl_close( $ch );
} while (
( false === $resp_data ) &&
( $curl_retries++ < 2 )
);
vipgoci_cache(
$cached_id,
$resp_data_parsed
);
return $resp_data_parsed;
}
/**
* Fetch data from repo-meta API, then try
* to match fields and their values with
* the data. The fields and values are those
* found in a particular $option parameter
* specified as an argument here ($option_name).
*
* @param array $options Options needed.
* @param string $option_name Option name.
* @param mixed $option_no_match Variable to update when there is a match.
*
* @return bool Return true on match, otherwise return false.
*/
function vipgoci_repo_meta_api_data_match(
array $options,
string $option_name,
mixed &$option_no_match
) :bool {
if (
( empty( $option_name ) ) ||
( empty( $options['repo-meta-api-base-url'] ) ) ||
( empty( $options[ $option_name ] ) )
) {
vipgoci_log(
'Not attempting to match repo-meta API field-value ' .
'to a criteria due to invalid configuration',
array(
'option_name'
=> $option_name,
'repo_meta_api_base_url'
=> isset( $options['repo-meta-api-base-url'] ) ?
$options['repo-meta-api-base-url'] : '',
'repo_meta_match'
=> ( ( ! empty( $option_name ) ) && ( isset( $options[ $option_name ] ) ) ) ?
$options[ $option_name ] : '',
)
);
return false;
} else {
vipgoci_log(
'Attempting to match repo-meta API field-value to a criteria',
array(
'option_name' => $option_name,
'repo_meta_match' => $options[ $option_name ],
'repo_meta_api_base_url' => $options['repo-meta-api-base-url'],
)
);
}
$repo_meta_data = vipgoci_repo_meta_api_data_fetch(
$options['repo-meta-api-base-url'],
$options['repo-meta-api-user-id'],
$options['repo-meta-api-access-token'],
$options['repo-owner'],
$options['repo-name']
);
if (
( empty(
$repo_meta_data['data']
) )
||
( 'error' === $repo_meta_data['status'] )
) {
return false;
}
/*
* Loop through possible match in the
* option array -- bail out once we
* find a match.
*/
foreach (
array_keys( $options[ $option_name ] ) as
$option_name_key_no
) {
$found_fields = vipgoci_find_fields_in_array(
$options[ $option_name ][ $option_name_key_no ],
$repo_meta_data['data']
);
/*
* If we find one data-item that had
* all fields matching the criteria given,
* we return true.
*/
$ret_val = false;
foreach (
$found_fields as
$found_field_item_key => $found_field_item_value
) {
if ( true === $found_field_item_value ) {
$ret_val = true;
}
}
if ( true === $ret_val ) {
$option_no_match = $option_name_key_no;
break;
}
}
vipgoci_log(
'Repo-meta API matching returning',
array(
'found_fields_in_repo_meta_data' => $found_fields,
'repo_meta_data_item_cnt' => count( $repo_meta_data['data'] ),
'ret_val' => $ret_val,
'option_no_match' => $option_no_match,
)
);
return $ret_val;
}