Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
GithubExtDistProvider
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 4
90
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 getCacheDuration
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTarballLocation
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 fetchBranches
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3namespace MediaWiki\Extension\ExtensionDistributor\Providers;
4
5use MediaWiki\Json\FormatJson;
6use MediaWiki\MediaWikiServices;
7use MediaWiki\Status\Status;
8
9/**
10 * ExtensionDistributor provider for Github.com
11 *
12 * @author Legoktm
13 *
14 * Provider-specific parameters:
15 *  token - OAuth authentication token, see https://github.com/blog/1509-personal-api-tokens
16 *
17 * Example configuration for the Wikimedia Github account:
18 *
19 * use MediaWiki\Extension\ExtensionDistributor\Providers\GithubExtDistProvider;
20 *
21 * $wgExtDistAPIConfig = [
22 *  'class' => GithubExtDistProvider::class,
23 *  'apiUrl' => 'https://api.github.com/repos/wikimedia/mediawiki-$TYPE-$EXT/branches',
24 *  'tarballUrl' => 'https://codeload.github.com/wikimedia/mediawiki-$TYPE-$EXT/legacy.tar.gz/$REF',
25 *  'tarballName' => 'wikimedia-mediawiki-$TYPE-$EXT-$SHA.tar.gz'
26 *  'sourceUrl' => 'https://github.com/wikimedia/mediawiki-$TYPE-$EXT',
27 *  'token' => 'YOUR TOKEN HERE',
28 * ];
29 *
30 */
31class GithubExtDistProvider extends ExtDistProvider {
32
33    /** @var string|false */
34    private $oAuthToken = false;
35
36    public function __construct( array $options ) {
37        parent::__construct( $options );
38        if ( isset( $options['token'] ) ) {
39            $this->oAuthToken = $options['token'];
40        }
41    }
42
43    /**
44     * Cache for an hour
45     *
46     * @return int
47     */
48    protected function getCacheDuration() {
49        return 60 * 60;
50    }
51
52    /** @inheritDoc */
53    public function getTarballLocation( $ext, $version ) {
54        return $this->substituteUrlVariables( $this->tarballUrl, $ext, $version );
55    }
56
57    /** @inheritDoc */
58    protected function fetchBranches( $name ) {
59        $options = [];
60        if ( $this->proxy ) {
61            $options['proxy'] = $this->proxy;
62        }
63
64        $url = $this->substituteUrlVariables( $this->apiUrl, $name );
65        if ( $this->oAuthToken ) {
66            // See https://developer.github.com/v3/#authentication
67            $url = wfAppendQuery( $url, [ 'access_token' => $this->oAuthToken ] );
68        }
69
70        $req = MediaWikiServices::getInstance()->getHttpRequestFactory()
71            ->create( $url, $options, __METHOD__ );
72        $status = $req->execute();
73        if ( !$status->isOK() ) {
74            $errorText = Status::wrap( $status )->getWikiText( false, false, 'en' );
75            $this->logger->error( __METHOD__ . ": Could not fetch branches for $name" .
76                "received: " . $errorText
77            );
78            return [];
79        }
80
81        $info = wfObjectToArray( FormatJson::decode( $req->getContent() ), true );
82        '@phan-var array[] $info';
83        $branches = [];
84        foreach ( $info as $branch ) {
85            $branches[$branch['name']] = $branch['commit']['sha1'];
86        }
87
88        return $branches;
89    }
90}