Skip to content

Commit

Permalink
Merge pull request #856 from j0k3r/fix/github-release
Browse files Browse the repository at this point in the history
Add support for GitHub release
  • Loading branch information
j0k3r authored Dec 13, 2021
2 parents 7378bf7 + 18f00f7 commit ac7a967
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 5 deletions.
44 changes: 40 additions & 4 deletions src/Extractor/Github.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class Github extends AbstractExtractor
/** @var string */
protected $githubRepo;
/** @var string */
protected $releaseTag;
/** @var string */
protected $pullNumber;
/** @var string */
protected $issueNumber;
Expand Down Expand Up @@ -64,6 +66,16 @@ public function match(string $url): bool
return true;
}

// find release
preg_match('/^\/([\w\d\.-]+)\/([\w\d\.-]+)\/releases\/(.*)/i', (string) $path, $matches);

if (4 === \count($matches)) {
$this->githubRepo = $matches[1] . '/' . $matches[2];
$this->releaseTag = $matches[3];

return true;
}

return false;
}

Expand Down Expand Up @@ -108,9 +120,7 @@ public function getContent(): string

return '';
}
}

if (null !== $this->issueNumber) {
} elseif (null !== $this->issueNumber) {
try {
$request = $messageFactory
->createRequest(
Expand All @@ -127,13 +137,39 @@ public function getContent(): string
'<h2><a href="' . $data['html_url'] . '">' . $data['title'] . '</a></h2>' .
'<ul><li>by <a href="' . $data['user']['html_url'] . '">' . $data['user']['login'] . '</a></li>' .
'<li>on ' . date('d/m/Y', strtotime($data['created_at'])) . '</li>' .
'<li>' . $data['comments'] . ' comments</li></ul></ul>' .
'<li>' . $data['comments'] . ' comments</li></ul>' .
$data['body_html'] . '</div>';
} catch (\Exception $e) {
$this->logger->error('Github (issue) extract failed for: ' . $this->githubRepo . ' & issue: ' . $this->issueNumber, [
'exception' => $e,
]);

return '';
}
} elseif (null !== $this->releaseTag) {
try {
$request = $messageFactory
->createRequest(
'GET',
'https://api.github.com/repos/' . $this->githubRepo . '/releases/tags/' . $this->releaseTag
)
->withHeader('Accept', 'application/vnd.github.v3.html+json')
->withHeader('User-Agent', 'f43.me / Github Extractor')
;
$response = $this->client->sendRequest($authentication->authenticate($request));
$data = $this->jsonDecode($response);

return '<div><em>Release on Github</em>' .
'<h2><a href="' . $data['html_url'] . '">' . $data['name'] . ' (' . $data['tag_name'] . ')</a></h2>' .
'<ul><li>repo <strong>' . $this->githubRepo . '</strong></li>' .
'<li>on ' . date('d/m/Y', strtotime($data['published_at'])) . '</li>' .
'<li>' . $data['reactions']['total_count'] . ' reactions</li></ul>' .
$data['body_html'] . '</div>';
} catch (\Exception $e) {
$this->logger->error('Github (release) extract failed for: ' . $this->githubRepo . ' & release: ' . $this->releaseTag, [
'exception' => $e,
]);

return '';
}
}
Expand Down
38 changes: 37 additions & 1 deletion tests/Extractor/GithubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function testIssue(): void
$github->match('https://github.com/octocat/Hello-World/issues/212');

// consecutive calls
$this->assertSame('<div><em>Issue on Github</em><h2><a href="http://1.1.1.1">test</a></h2><ul><li>by <a href="http://2.2.2.2">login</a></li><li>on 04/08/2015</li><li>0 comments</li></ul></ul>body</div>', $github->getContent());
$this->assertSame('<div><em>Issue on Github</em><h2><a href="http://1.1.1.1">test</a></h2><ul><li>by <a href="http://2.2.2.2">login</a></li><li>on 04/08/2015</li><li>0 comments</li></ul>body</div>', $github->getContent());
// this one will catch an exception
$this->assertEmpty($github->getContent());
}
Expand Down Expand Up @@ -124,4 +124,40 @@ public function testPR(): void
// this one will catch an exception
$this->assertEmpty($github->getContent());
}

public function testRelease(): void
{
$client = self::getMockClient([
(new Response(200, [], (string) json_encode([
'html_url' => 'http://1.1.1.1',
'name' => 'release 2',
'tag_name' => '2.0.0',
'created_at' => '2015-08-04T13:49:04Z',
'published_at' => '2015-08-05T13:49:04Z',
'body_html' => 'body',
'reactions' => [
'total_count' => 3,
],
'user' => ['html_url' => 'http://2.2.2.2', 'login' => 'login'],
]))),
(new Response(400, [], 'oops')),
]);

$github = new Github('client_id', 'client_secret');
$github->setClient($client);

$logHandler = new TestHandler();
$logger = new Logger('test', [$logHandler]);
$github->setLogger($logger);

// first test fail because we didn't match an url, so GithubId isn't defined
$this->assertEmpty($github->getContent());

$github->match('https://github.com/guzzle/psr7/releases/2.0.0');

// consecutive calls
$this->assertSame('<div><em>Release on Github</em><h2><a href="http://1.1.1.1">release 2 (2.0.0)</a></h2><ul><li>repo <strong>guzzle/psr7</strong></li><li>on 05/08/2015</li><li>3 reactions</li></ul>body</div>', $github->getContent());
// this one will catch an exception
$this->assertEmpty($github->getContent());
}
}

0 comments on commit ac7a967

Please sign in to comment.