forked from nf-core/website
-
Notifications
You must be signed in to change notification settings - Fork 3
/
update_module_details.php
465 lines (426 loc) · 18.2 KB
/
update_module_details.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<?php
echo "\n\nUpdating module details - " . date('Y-m-d h:i:s') . "\n";
// Allow PHP fopen to work with remote links
ini_set('allow_url_fopen', 1);
// Load yaml parser
require 'vendor/autoload.php';
use Symfony\Component\Yaml\Yaml;
// Get auth secrets
$config = parse_ini_file('config.ini');
$gh_auth = base64_encode($config['github_username'] . ':' . $config['github_access_token']);
$conn = mysqli_connect(
$config['host'],
$config['username'],
$config['password'],
$config['dbname'],
$config['port'],
);
if ($conn === false) {
die('ERROR: Could not connect. ' . mysqli_connect_error());
}
//
//
// GitHub API calls
//
//
function github_query($gh_query_url) {
global $config;
$gh_auth = base64_encode($config['github_username'] . ':' . $config['github_access_token']);
// HTTP header to use on GitHub API GET requests
$gh_api_opts = stream_context_create([
'http' => [
'method' => 'GET',
'header' => ['User-Agent: PHP', "Authorization: Basic $gh_auth"],
],
]);
$first_page = true;
$next_page = false;
$res = [];
while ($first_page || $next_page) {
// reset loop vars
$first_page = false;
// Get GitHub API results
if ($next_page) {
$gh_query_url = $next_page;
}
$tmp_results = json_decode(file_get_contents($gh_query_url, false, $gh_api_opts), true);
if (!preg_match('/HTTP\/\d\.?\d? 200/', $http_response_header[0])) {
var_dump($http_response_header);
echo "\nCould not fetch $gh_query_url";
continue;
}
if (substr($gh_query_url, 0, 29) == 'https://api.github.com/repos/') {
$res = $tmp_results;
} else {
array_push($res, ...$tmp_results);
}
// Look for URL to next page of API results
$next_page = false;
$m_array = preg_grep('/rel="next"/', $http_response_header);
if (count($m_array) > 0) {
preg_match('/<([^>]+)>; rel="next"/', array_values($m_array)[0], $matches);
if (isset($matches[1])) {
$next_page = $matches[1];
}
}
}
return $res;
}
//
// nf-core modules table
//
$gh_modules = github_query('https://api.github.com/repos/nf-core/modules/git/trees/master?recursive=1');
$modules = [];
foreach ($gh_modules['tree'] as $f) {
if (substr($f['path'], -8) == 'meta.yml' && substr($f['path'], 0, 8) == 'modules/') {
$meta = github_query($f['url']);
$meta_content = base64_decode($meta['content']);
$meta_content = Yaml::parse($meta_content);
$meta_content['keywords'] = is_array($meta_content['keywords'])
? implode(';', $meta_content['keywords'])
: $meta_content['keywords'];
$meta_content['authors'] = is_array($meta_content['authors'])
? implode(';', $meta_content['authors'])
: $meta_content['authors'];
$meta_content['sha'] = $meta['sha'];
$meta_content['api_url'] = $meta['url'];
$meta_content['github_path'] = $f['path'];
$modules[] = $meta_content;
}
}
echo "\nModules in nf-core in total: " . count($modules) . "\n\n";
$sql = "CREATE TABLE IF NOT EXISTS nfcore_modules (
id INT AUTO_INCREMENT PRIMARY KEY,
github_sha VARCHAR (400) NOT NULL,
github_path VARCHAR (400) NOT NULL,
api_url VARCHAR (400) NOT NULL,
name VARCHAR (400) NOT NULL,
description VARCHAR (4000) DEFAULT NULL,
keywords VARCHAR (2000) DEFAULT NULL,
tools JSON NOT NULL,
input JSON NOT NULL,
output JSON NOT NULL,
authors VARCHAR (2000) NOT NULL,
date_added TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
date_updated TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)";
if (mysqli_query($conn, $sql)) {
echo "`nfcore_modules` table created successfully.\n";
} else {
echo "ERROR: Could not execute $sql. " . mysqli_error($conn);
}
// Prepare an insert statement
$sql =
'INSERT INTO nfcore_modules (github_sha,github_path,api_url,name,description,keywords,tools,input,output,authors) VALUES (?,?,?,?,?,?,?,?,?,?)';
if ($stmt = mysqli_prepare($conn, $sql)) {
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param(
$stmt,
'ssssssssss',
$github_sha,
$github_path,
$api_url,
$name,
$description,
$keywords,
$tools,
$input,
$output,
$authors,
);
foreach ($modules as $idx => $module) {
// check if module already exists
$check = "SELECT * FROM nfcore_modules WHERE name = '$module[name]'";
$res = mysqli_query($conn, $check);
$github_sha = $module['sha'];
$github_path = $module['github_path'];
$api_url = $module['api_url'];
$name = htmlspecialchars($module['name'], ENT_QUOTES, 'UTF-8');;
$description = $module['description'];
$keywords = $module['keywords'];
$tools = json_encode($module['tools']);
$input = json_encode($module['input']);
$output = json_encode($module['output']);
$authors = $module['authors'];
if ($res->num_rows) {
// check if entry is different
$row = $res->fetch_assoc();
// update existing module
$update = "UPDATE nfcore_modules SET github_sha = ?, github_path = ?, api_url = ?, description = ?, keywords = ?, tools = ?, input = ?, output = ?, authors = ? WHERE name = '$name'";
if ($update_stmt = mysqli_prepare($conn, $update)) {
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param(
$update_stmt,
'sssssssss',
$github_sha,
$github_path,
$api_url,
$description,
$keywords,
$tools,
$input,
$output,
$authors,
);
} else {
echo "ERROR: Could not prepare query: $update. " . mysqli_error($conn);
}
if (!mysqli_stmt_execute($update_stmt)) {
echo "ERROR: Could not execute $update. " . mysqli_error($conn);
}
} else {
if (!mysqli_stmt_execute($stmt)) {
echo "ERROR: Could not execute $sql. " . mysqli_error($conn);
}
}
}
} else {
echo "ERROR: Could not prepare query: $sql. " . mysqli_error($conn);
}
echo "\nInformation for modules saved into database.\n\n";
//
// nf-core pipelines table
//
$gh_pipelines = github_query('https://api.github.com/orgs/sanger-tol/repos?per_page=100');
$ignored_repos = parse_ini_file('ignored_repos.ini')['repos'];
// Drop existing table if query was successful
$sql = "CREATE TABLE IF NOT EXISTS nfcore_pipelines (
id INT AUTO_INCREMENT PRIMARY KEY,
github_id VARCHAR (400) NOT NULL,
html_url VARCHAR (400) NOT NULL,
name VARCHAR (400) NOT NULL,
description VARCHAR (4000) DEFAULT NULL,
gh_created_at datetime NOT NULL,
gh_updated_at datetime NOT NULL,
gh_pushed_at datetime NOT NULL,
stargazers_count INT NOT NULL,
watchers_count INT NOT NULL,
forks_count INT NOT NULL,
open_issues_count INT NOT NULL,
open_pr_count INT NOT NULL,
topics VARCHAR (4000) DEFAULT NULL,
default_branch VARCHAR (400) NOT NULL,
pipeline_type VARCHAR (400) NOT NULL,
archived BOOLEAN NOT NULL,
last_release_date datetime DEFAULT NULL,
date_added datetime DEFAULT current_timestamp
)";
if (mysqli_query($conn, $sql)) {
echo "`nfcore_pipelines` table created successfully.\n\n";
} else {
echo "ERROR: Could not execute $sql. " . mysqli_error($conn);
}
// Prepare an insert statement
$sql =
'INSERT INTO nfcore_pipelines (github_id,html_url,name,description,gh_created_at,gh_updated_at,gh_pushed_at,stargazers_count,watchers_count,forks_count,open_issues_count,open_pr_count,topics,default_branch,pipeline_type,archived,last_release_date) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)';
if ($stmt = mysqli_prepare($conn, $sql)) {
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param(
$stmt,
'sssssssiiiiisssis',
$github_id,
$html_url,
$name,
$description,
$gh_created_at,
$gh_updated_at,
$gh_pushed_at,
$stargazers_count,
$watchers_count,
$forks_count,
$open_issues_count,
$open_pr_count,
$topics,
$default_branch,
$pipeline_type,
$archived,
$last_release_date,
);
foreach ($gh_pipelines as $pipeline) {
if (in_array($pipeline['name'], $ignored_repos)) {
$pipeline_type = 'core_repos';
continue;
} else {
$pipeline_type = 'pipelines';
echo "Process pipeline " . htmlspecialchars($pipeline['name'], ENT_QUOTES, 'UTF-8') . "\n";
}
// check where entries need to be updated and update them
$github_id = $pipeline['id'];
$html_url = $pipeline['html_url'];
$name = htmlspecialchars($pipeline['name'], ENT_QUOTES, 'UTF-8');
$description = $pipeline['description'];
$gh_created_at = date('Y-m-d H:i:s', strtotime($pipeline['created_at']));
$gh_updated_at = date('Y-m-d H:i:s', strtotime($pipeline['updated_at']));
$gh_pushed_at = date('Y-m-d H:i:s', strtotime($pipeline['pushed_at']));
$stargazers_count = $pipeline['stargazers_count'];
$watchers_count = count(
github_query('https://api.github.com/repos/sanger-tol/' . $pipeline['name'] . '/watchers'),
);
$forks_count = $pipeline['forks_count'];
$open_issues_count = $pipeline['open_issues_count'];
$open_pr_count = count(github_query(str_replace('{/number}', '', $pipeline['pulls_url'])));
$topics = is_array($pipeline['topics']) ? implode(';', $pipeline['topics']) : $pipeline['topics'];
$default_branch = $pipeline['default_branch'];
$archived = $pipeline['archived'];
$last_release_date = github_query(str_replace('{/id}', '', $pipeline['releases_url']) . '?per_page=1')[0][
'published_at'
];
$last_release_date = is_null($last_release_date) ? null : date('Y-m-d H:i:s', strtotime($last_release_date));
$check = "SELECT * FROM nfcore_pipelines WHERE name = '" . $pipeline['name'] . "'";
$res = mysqli_query($conn, $check);
if ($res->num_rows > 0) {
// test if the entry needs to be updated
echo " Pipeline $name exists in the database, trying to update it now.\n";
$row = $res->fetch_assoc();
$update = false;
$update = $update || $row['github_id'] != $github_id;
$update = $update || $row['html_url'] != $html_url;
$update = $update || $row['description'] != $description;
$update = $update || $row['gh_created_at'] != $gh_created_at;
$update = $update || $row['gh_updated_at'] != $gh_updated_at;
$update = $update || $row['gh_pushed_at'] != $gh_pushed_at;
$update = $update || $row['stargazers_count'] != $stargazers_count;
$update = $update || $row['watchers_count'] != $watchers_count;
$update = $update || $row['forks_count'] != $forks_count;
$update = $update || $row['open_issues_count'] != $open_issues_count;
$update = $update || $row['open_pr_count'] != $open_pr_count;
$update = $update || $row['topics'] != $topics;
$update = $update || $row['default_branch'] != $default_branch;
$update = $update || $row['pipeline_type'] != $pipeline_type;
$update = $update || $row['archived'] != $archived;
$update = $update || $row['last_release_date'] != $last_release_date;
if ($update) {
$update = "UPDATE
nfcore_pipelines
SET
github_id = ?,
html_url = ?,
description = ?,
gh_created_at = ?,
gh_updated_at = ?,
gh_pushed_at = ?,
stargazers_count = ?,
watchers_count = ?,
forks_count = ?,
open_issues_count = ?,
open_pr_count = ?,
topics = ?,
default_branch = ?,
pipeline_type = ?,
archived = ?,
last_release_date = ?
WHERE
name = '" . htmlspecialchars($pipeline['name'], ENT_QUOTES, 'UTF-8') . "'";
if ($update_stmt = mysqli_prepare($conn, $update)) {
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param(
$update_stmt,
'ssssssiiiiisssis',
$github_id,
$html_url,
$description,
$gh_created_at,
$gh_updated_at,
$gh_pushed_at,
$stargazers_count,
$watchers_count,
$forks_count,
$open_issues_count,
$open_pr_count,
$topics,
$default_branch,
$pipeline_type,
$archived,
$last_release_date,
);
} else {
echo "ERROR: Could not prepare query: $update. " . mysqli_error($conn);
}
if (!mysqli_stmt_execute($update_stmt)) {
echo "ERROR: Could not execute $update. " . mysqli_error($conn);
}
}else{
echo " No need to update pipeline $name.\n";
}
} else {
echo " Pipeline $name is new for database, inserting it now.\n";
if (!mysqli_stmt_execute($stmt)) {
echo "ERROR: Could not execute $sql. " . mysqli_error($conn). "\n";
}
}
}
} else {
echo "ERROR: Could not prepare query: $sql. " . mysqli_error($conn);
}
echo "\nInformation for pipelines saved into database.\n\n";
//
// pipelines modules link table
//
$sql = "SELECT * FROM nfcore_pipelines WHERE pipeline_type = 'pipelines' ORDER BY LOWER(name) ";
$pipelines = [];
if ($result = mysqli_query($conn, $sql)) {
if (mysqli_num_rows($result) > 0) {
$pipelines = mysqli_fetch_all($result, MYSQLI_ASSOC);
// Free result set
mysqli_free_result($result);
} else {
echo 'No pipelines found.';
}
}
// create pipelines_modules table with foreign keys to pipelines and modules
$sql = "CREATE TABLE IF NOT EXISTS pipelines_modules (
id INT AUTO_INCREMENT PRIMARY KEY,
pipeline_id INT NOT NULL,
module_id INT NOT NULL,
FOREIGN KEY (pipeline_id) REFERENCES nfcore_pipelines(id),
FOREIGN KEY (module_id) REFERENCES nfcore_modules(id)
)";
if (mysqli_query($conn, $sql)) {
echo "`pipelines_modules` table created successfully.\n\n";
} else {
echo "ERROR: Could not execute $sql. " . mysqli_error($conn);
}
// Prepare an insert statement
$sql = 'INSERT INTO pipelines_modules (pipeline_id,module_id) VALUES (?,?)';
foreach ($pipelines as $pipeline) {
$modules_json = github_query(
'https://api.github.com/repos/sanger-tol/' . basename($pipeline['name']) . '/contents/modules.json',
);
$modules_json = json_decode(base64_decode($modules_json['content']), true);
$modules = $modules_json['repos']['https://github.com/nf-core/modules.git']['modules']['nf-core'];
// catch repos with no modules.json
if ($modules == null) {
echo "No nf-core modules for pipeline " . htmlspecialchars($pipeline['name'], ENT_QUOTES, 'UTF-8') . "\n";
continue;
}
foreach ($modules as $name => $content) {
$stmt = mysqli_prepare($conn, $sql);
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, 'ii', $pipeline_id, $module_id);
$name = str_replace('/', '_', $name);
$name = htmlspecialchars($name, ENT_QUOTES, 'UTF-8');
// prepare a select statement for nfcore_modules based on name
$get_module = "SELECT * FROM nfcore_modules WHERE name = '$name'";
if ($result = mysqli_query($conn, $get_module)) {
if (mysqli_num_rows($result) > 0) {
$repo_module = mysqli_fetch_all($result, MYSQLI_ASSOC);
$pipeline_id = $pipeline['id'];
$module_id = $repo_module[0]['id'];
# need to check before inserting
$check_sql = "SELECT * FROM pipelines_modules WHERE pipeline_id = '$pipeline_id' AND module_id = '$module_id'";
$res = mysqli_query($conn, $check_sql);
if ($res->num_rows == 0) {
mysqli_stmt_execute($stmt);
print_r($stmt->error);
}
// Free result set
mysqli_free_result($result);
} else {
echo "No modules with the name $name found for pipeline ". htmlspecialchars($pipeline['name'], ENT_QUOTES, 'UTF-8'). "\n";
}
}
}
}
echo "\nPipeline and modules relationship saved into database.\n\n";
mysqli_close($conn);
echo "\nupdate_module_details done " . date('Y-m-d h:i:s') . "\n\n";