Browse Source

Retrieve funding info from github into composer.json if not declared there

Jordi Boggiano 5 years ago
parent
commit
b033a2ae81
1 changed files with 34 additions and 0 deletions
  1. 34 0
      src/Composer/Repository/Vcs/GitHubDriver.php

+ 34 - 0
src/Composer/Repository/Vcs/GitHubDriver.php

@@ -35,6 +35,7 @@ class GitHubDriver extends VcsDriver
     protected $infoCache = array();
     protected $isPrivate = false;
     private $isArchived = false;
+    private $fundingInfo;
 
     /**
      * Git Driver
@@ -166,6 +167,10 @@ class GitHubDriver extends VcsDriver
                 if (!isset($composer['abandoned']) && $this->isArchived) {
                     $composer['abandoned'] = true;
                 }
+
+                if (!isset($composer['funding']) && $funding = $this->getFundingInfo()) {
+                    $composer['funding'] = $funding;
+                }
             }
 
             if ($this->shouldCache($identifier)) {
@@ -178,6 +183,35 @@ class GitHubDriver extends VcsDriver
         return $this->infoCache[$identifier];
     }
 
+    private function getFundingInfo()
+    {
+        if (null !== $this->fundingInfo) {
+            return $this->fundingInfo;
+        }
+
+        if ($this->originUrl !== 'github.com') {
+            return $this->fundingInfo = false;
+        }
+
+        $graphql = 'query{repository(owner:"'.$this->owner.'",name:"'.$this->repository.'"){fundingLinks{platform,url}}}';
+        $result = $this->remoteFilesystem->getContents($this->originUrl, 'https://api.github.com/graphql', false, [
+            'http' => [
+                'method' => 'POST',
+                'content' => json_encode(['query' => $graphql]),
+                'header' => ['Content-Type: application/json'],
+            ],
+        ]);
+        $result = json_decode($result, true);
+
+        if (empty($result['data']['repository']['fundingLinks'])) {
+            return $this->fundingInfo = false;
+        }
+
+        return $this->fundingInfo = array_map(function ($link) {
+            return array('type' => strtolower($link['platform']), 'url' => $link['url']);
+        }, $result['data']['repository']['fundingLinks']);
+    }
+
     /**
      * {@inheritdoc}
      */