1
0
Fork 0

Rework the funding info parsing to avoid requiring graphql permissions

pull/8661/head
Jordi Boggiano 2020-02-28 09:09:23 +01:00
parent f154d5c09d
commit c4f19e51d8
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC
1 changed files with 68 additions and 19 deletions

View File

@ -167,7 +167,6 @@ class GitHubDriver extends VcsDriver
if (!isset($composer['abandoned']) && $this->isArchived) {
$composer['abandoned'] = true;
}
if (!isset($composer['funding']) && $funding = $this->getFundingInfo()) {
$composer['funding'] = $funding;
}
@ -193,28 +192,78 @@ class GitHubDriver extends VcsDriver
return $this->fundingInfo = false;
}
$graphql = 'query{repository(owner:"'.$this->owner.'",name:"'.$this->repository.'"){fundingLinks{platform,url}}}';
foreach (array($this->getApiUrl() . '/repos/'.$this->owner.'/'.$this->repository.'/contents/.github/FUNDING.yml', $this->getApiUrl() . '/repos/'.$this->owner.'/.github/contents/FUNDING.yml') as $file) {
try {
$result = $this->remoteFilesystem->getContents($this->originUrl, 'https://api.github.com/graphql', false, array(
'http' => array(
'method' => 'POST',
'content' => json_encode(array('query' => $graphql)),
'header' => array('Content-Type: application/json'),
),
$result = $this->remoteFilesystem->getContents($this->originUrl, $file, false, array(
'retry-auth-failure' => false,
));
$response = json_decode($result, true);
} catch (TransportException $e) {
return $this->fundingInfo = false;
continue;
}
$result = json_decode($result, true);
if (empty($result['data']['repository']['fundingLinks'])) {
if (empty($response['content']) || $response['encoding'] !== 'base64' || !($funding = base64_decode($response['content']))) {
continue;
}
}
if (empty($funding)) {
return $this->fundingInfo = false;
}
return $this->fundingInfo = array_map(function ($link) {
return array('type' => strtolower($link['platform']), 'url' => $link['url']);
}, $result['data']['repository']['fundingLinks']);
$result = array();
$key = null;
foreach (preg_split('{\r?\n}', $funding) as $line) {
$line = preg_replace('{#.*}', '', $line);
$line = trim($line);
if (preg_match('{^(\w+)\s*:\s*(.+)$}', $line, $match)) {
if (preg_match('{^\[.*\]$}', $match[2])) {
foreach (array_map('trim', preg_split('{[\'"]?\s*,\s*[\'"]?}', substr($match[2], 1, -1))) as $item) {
$result[] = array('type' => $match[1], 'url' => trim($item, '"\''));
}
} else {
$result[] = array('type' => $match[1], 'url' => $match[2]);
}
$key = null;
} elseif (preg_match('{^(\w+)\s*:$}', $line, $match)) {
$key = $match[1];
} elseif ($key && preg_match('{^-\s*(.+)$}', $line, $match)) {
$result[] = array('type' => $key, 'url' => $match[1]);
}
}
foreach ($result as $key => $item) {
switch ($item['type']) {
case 'tidelift':
$result[$key]['url'] = 'https://tidelift.com/funding/github/' . $item['url'];
break;
case 'github':
$result[$key]['url'] = 'https://github.com/' . basename($item['url']);
break;
case 'patreon':
$result[$key]['url'] = 'https://www.patreon.com/' . basename($item['url']);
break;
case 'otechie':
$result[$key]['url'] = 'https://otechie.com/' . basename($item['url']);
break;
case 'open_collective':
$result[$key]['url'] = 'https://opencollective.com/' . basename($item['url']);
break;
case 'liberapay':
$result[$key]['url'] = 'https://liberapay.com/' . basename($item['url']);
break;
case 'ko_fi':
$result[$key]['url'] = 'https://ko-fi.com/' . basename($item['url']);
break;
case 'issuehunt':
$result[$key]['url'] = 'https://issuehunt.io/r/' . $item['url'];
break;
case 'community_bridge':
$result[$key]['url'] = 'https://funding.communitybridge.org/projects/' . basename($item['url']);
break;
}
}
return $this->fundingInfo = $result;
}
/**