update-masterversions.php 4.06 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

9 10
use RedBeanPHP\R;

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

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

David Morley's avatar
David Morley committed
18
$softwares = [
19 20 21 22 23 24
    'diaspora'     => ['repo' => 'diaspora/diaspora', 'gitsite' => 'api.github.com', 'gittype' => 'github', 'devbranch' => 'develop'],
    'friendica'    => ['repo' => 'friendica/friendica', 'gitsite' => 'api.github.com', 'gittype' => 'github', 'devbranch' => 'develop'],
    'hubzilla'     => ['repo' => 'hubzilla%2fcore', 'gitsite' => 'framagit.org', 'gittype' => 'gitlab', 'devbranch' => 'dev'],
    'pleroma'      => ['repo' => 'pleroma%2fpleroma', 'gitsite' => 'git.pleroma.social', 'gittype' => 'gitlab', 'devbranch' => 'develop'],
    'socialhome'   => ['repo' => 'jaywink/socialhome', 'gitsite' => 'api.github.com', 'gittype' => 'github', 'devbranch' => ''],
    'social-relay' => ['repo' => 'jaywink/social-relay', 'gitsite' => 'api.github.com', 'gittype' => 'github', 'devbranch' => ''],
David Morley's avatar
David Morley committed
25
    'ganggo'       => ['repo' => 'ganggo%2fganggo', 'gitsite' => 'git.feneas.org', 'gittype' => 'gitlab', 'devbranch' => ''],
David Morley's avatar
David Morley committed
26
    'funkwhale'    => ['repo' => 'funkwhale%2ffunkwhale', 'gitsite' => 'code.eliotberriot.com', 'gittype' => 'gitlab', 'devbranch' => 'develop'],
27 28 29
];

$opts = [
30
    'http' => ['method' => 'GET', 'header' => ['User-Agent: Poduptime']],
David Morley's avatar
David Morley committed
31 32 33
];

foreach ($softwares as $software => $details) {
34
    if ($details['gittype'] === 'github') {
35
        $context     = stream_context_create($opts);
36 37 38
        $releasejson = json_decode(file_get_contents('https://' . $details['gitsite'] . '/repos/' . $details['repo'] . '/releases/latest', false, $context));
        if ($details['devbranch']) {
            $commitjson = json_decode(file_get_contents('https://' . $details['gitsite'] . '/repos/' . $details['repo'] . '/commits/' . $details['devbranch'], false, $context));
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
        } else {
            $commitjson = '';
        }
        if ($masterversion = $releasejson->tag_name ? str_replace('v', '', $releasejson->tag_name) : '') {
            try {
                $m             = R::dispense('masterversions');
                $m['software'] = $software;
                $m['version']  = $masterversion;
                if ($releasedate = $releasejson ? $releasejson->published_at : '') {
                    $m['releasedate'] = $releasedate;
                }
                if ($devlastcommit = $commitjson ? $commitjson->commit->author->date : '') {
                    $m['devlastcommit'] = $devlastcommit;
                }
                R::store($m);
            } catch (\RedBeanPHP\RedException $e) {
                die('Error in SQL query: ' . $e->getMessage());
            }
        }
58
    } elseif ($details['gittype'] === 'gitlab') {
59
        $context     = stream_context_create($opts);
60 61 62
        $releasejson = json_decode(file_get_contents('https://' . $details['gitsite'] . '/api/v4/projects/' . $details['repo'] . '/repository/tags', false, $context));
        if ($details['devbranch']) {
            $commitjson = json_decode(file_get_contents('https://' . $details['gitsite'] . '/api/v4/projects/' . $details['repo'] . '/repository/commits/' . $details['devbranch'], false, $context));
63 64 65 66 67
        } else {
            $commitjson = '';
        }
        if ($masterversion = $releasejson[0]->name ? str_replace('v', '', $releasejson[0]->name) : '') {
            try {
68
                $m             = R::dispense('masterversions');
69
                $m['software'] = $software;
70
                $m['version']  = $masterversion;
71 72 73 74 75 76 77 78 79 80 81
                if ($releasedate = $releasejson[0] ? $releasejson[0]->commit->created_at : '') {
                    $m['releasedate'] = $releasedate;
                }
                if ($devlastcommit = $commitjson ? $commitjson->created_at : '') {
                    $m['devlastcommit'] = $devlastcommit;
                }
                R::store($m);
            } catch (\RedBeanPHP\RedException $e) {
                die('Error in SQL query: ' . $e->getMessage());
            }
        }
82
    }
David Morley's avatar
David Morley committed
83

84
    printf('%s:%s:%s ', $software, $masterversion, $devlastcommit ?: 'n/a');
David Morley's avatar
David Morley committed
85
}