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 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    private $oAuthToken = false;
34
35    public function __construct( array $options ) {
36        parent::__construct( $options );
37        if ( isset( $options['token'] ) ) {
38            $this->oAuthToken = $options['token'];
39        }
40    }
41
42    /**
43     * Cache for an hour
44     *
45     * @return int
46     */
47    protected function getCacheDuration() {
48        return 60 * 60;
49    }
50
51    public function getTarballLocation( $ext, $version ) {
52        return $this->substituteUrlVariables( $this->tarballUrl, $ext, $version );
53    }
54
55    protected function fetchBranches( $name ) {
56        $options = [];
57        if ( $this->proxy ) {
58            $options['proxy'] = $this->proxy;
59        }
60
61        $url = $this->substituteUrlVariables( $this->apiUrl, $name );
62        if ( $this->oAuthToken ) {
63            // See https://developer.github.com/v3/#authentication
64            $url = wfAppendQuery( $url, [ 'access_token' => $this->oAuthToken ] );
65        }
66
67        $req = MediaWikiServices::getInstance()->getHttpRequestFactory()
68            ->create( $url, $options, __METHOD__ );
69        $status = $req->execute();
70        if ( !$status->isOK() ) {
71            $errorText = Status::wrap( $status )->getWikiText( false, false, 'en' );
72            $this->logger->error( __METHOD__ . ": Could not fetch branches for $name" .
73                "received: " . $errorText
74            );
75            return [];
76        }
77
78        $info = wfObjectToArray( FormatJson::decode( $req->getContent() ), true );
79        '@phan-var array[] $info';
80        $branches = [];
81        foreach ( $info as $branch ) {
82            $branches[$branch['name']] = $branch['commit']['sha1'];
83        }
84
85        return $branches;
86    }
87}