update-masterversions.php 3.09 KB
Newer Older
David Morley's avatar
David Morley committed
1
<?php
2 3 4 5 6 7

/**
 * Pull master software versions.
 */

declare(strict_types=1);
David Morley's avatar
David Morley committed
8

noplanman's avatar
noplanman committed
9 10
use RedBeanPHP\R;

11
require_once __DIR__ . '/../boot.php';
David Morley's avatar
David Morley committed
12

dmorley's avatar
dmorley committed
13 14 15 16 17
if (!is_cli()) {
    header('HTTP/1.0 403 Forbidden');
    exit;
}

dmorley's avatar
dmorley committed
18
$softwares = c('softwares');
David Morley's avatar
David Morley committed
19 20

foreach ($softwares as $software => $details) {
21
    if ($details['gittype'] === 'github') {
dmorley's avatar
dmorley committed
22 23
        $rjson       = curl('https://' . $details['gitsite'] . '/repos/' . $details['repo'] . '/releases/latest');
        $releasejson = json_decode($rjson['body'] ?: '');
24
        if ($details['devbranch']) {
dmorley's avatar
dmorley committed
25 26
            $cjson      = curl('https://' . $details['gitsite'] . '/repos/' . $details['repo'] . '/commits/' . $details['devbranch']);
            $commitjson = json_decode($cjson['body'] ?: '');
27
        }
dmorley's avatar
dmorley committed
28
        if (isset($releasejson->tag_name) && $masterversion = $releasejson->tag_name ? str_replace('v', '', $releasejson->tag_name) : '') {
29 30 31 32
            try {
                $m             = R::dispense('masterversions');
                $m['software'] = $software;
                $m['version']  = $masterversion;
dmorley's avatar
dmorley committed
33
                if (isset($releasejson->published_at) && $releasedate = $releasejson ? $releasejson->published_at : '') {
34 35
                    $m['releasedate'] = $releasedate;
                }
dmorley's avatar
dmorley committed
36
                if (isset($commitjson->commit->author->date) && $devlastcommit = $commitjson ? $commitjson->commit->author->date : '') {
37 38 39 40
                    $m['devlastcommit'] = $devlastcommit;
                }
                R::store($m);
            } catch (\RedBeanPHP\RedException $e) {
41
                debug('Error in SQL query: ' . sprintf($e->getMessage()));
42 43
            }
        }
44
    } elseif ($details['gittype'] === 'gitlab') {
dmorley's avatar
dmorley committed
45 46
        $rjson       = curl('https://' . $details['gitsite'] . '/api/v4/projects/' . $details['repo'] . '/repository/tags');
        $releasejson = json_decode($rjson['body'] ?: '');
47
        if ($details['devbranch']) {
dmorley's avatar
dmorley committed
48 49
            $cjson      = curl('https://' . $details['gitsite'] . '/api/v4/projects/' . $details['repo'] . '/repository/commits/' . $details['devbranch']);
            $commitjson = json_decode($cjson['body'] ?: '');
50
        }
dmorley's avatar
dmorley committed
51
        if (isset($releasejson[0]->name) && $masterversion = $releasejson[0]->name ? str_replace('v', '', $releasejson[0]->name) : '') {
52
            try {
53
                $m             = R::dispense('masterversions');
54
                $m['software'] = $software;
55
                $m['version']  = $masterversion;
dmorley's avatar
dmorley committed
56
                if (isset($releasejson[0]->commit->created_at) && $releasedate = $releasejson[0] ? $releasejson[0]->commit->created_at : '') {
57 58
                    $m['releasedate'] = $releasedate;
                }
dmorley's avatar
dmorley committed
59
                if (isset($commitjson->created_at) && $devlastcommit = $commitjson ? $commitjson->created_at : '') {
60 61 62 63
                    $m['devlastcommit'] = $devlastcommit;
                }
                R::store($m);
            } catch (\RedBeanPHP\RedException $e) {
64
                debug('Error in SQL query: ' . sprintf($e->getMessage()));
65 66
            }
        }
noplanman's avatar
noplanman committed
67
    }
David Morley's avatar
David Morley committed
68

69
    printf('%s:%s:%s ', $software, $masterversion, $devlastcommit ?: 'n/a');
David Morley's avatar
David Morley committed
70
}
71 72

updateMeta('masterversions_updated');