Skip to content

Use MySQL as cache for GitHub client to reduce API requests. #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
### Added
- Code checkers to ensure coding standard.
- When releasing a new version of the Support Bot, automatically fetch the latest code and install with composer.
- MySQL cache for GitHub client.
### Changed
- Bumped Manager to 1.5.
- Logging is now decoupled with custom Monolog logger.
Expand Down
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@
"require": {
"php": ">= 7.1",
"ext-json": "*",
"ext-pdo": "*",
"php-telegram-bot/telegram-bot-manager": "^1.5",
"noplanman/service-webhook-handler": "^0.2",
"vlucas/phpdotenv": "^3.4",
"php-http/guzzle6-adapter": "^1.1",
"knplabs/github-api": "^2.11",
"elvanto/litemoji": "^1.4",
"monolog/monolog": "^1.24"
"monolog/monolog": "^1.24",
"matthiasmullie/scrapbook": "^1.4"
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.4",
Expand Down
140 changes: 138 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions public/webhooks/github.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Longman\TelegramBot\Request;
use Longman\TelegramBot\Telegram;
use Longman\TelegramBot\TelegramLog;
use MatthiasMullie\Scrapbook\Adapters\MySQL;
use MatthiasMullie\Scrapbook\Psr6\Pool;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
Expand Down Expand Up @@ -108,6 +110,10 @@ function parseReleaseBody($body, $user, $repo): string
}, $body);

$github_client = new Client();
$github_client->addCache(new Pool(new MySQL(
new PDO('mysql:dbname=' . getenv('TG_DB_DATABASE') . ';host=' . getenv('TG_DB_HOST'), getenv('TG_DB_USER'), getenv('TG_DB_PASSWORD'))
)));

// Replace any ID links with the corresponding issue or pull request link.
$body = preg_replace_callback('~(?:(?<user>[0-9a-z\-]*)/(?<repo>[0-9a-z\-]*))?#(?<id>\d*)~i', static function ($matches) use ($github_client, $user, $repo) {
$text = $matches[0];
Expand All @@ -118,7 +124,7 @@ function parseReleaseBody($body, $user, $repo): string
// Check if this ID is an issue.
try {
/** @var Issue $issue */
$issue = $github_client->api('issue')->show($user, $repo, $id);
$issue = $github_client->issue()->show($user, $repo, $id);
return "[{$text}]({$issue['html_url']})";
} catch (Throwable $e) {
// Silently ignore.
Expand All @@ -127,7 +133,7 @@ function parseReleaseBody($body, $user, $repo): string
// Check if this ID is a pull request.
try {
/** @var PullRequest $pr */
$pr = $github_client->api('pull_request')->show($user, $repo, $id);
$pr = $github_client->pr()->show($user, $repo, $id);
return "[{$text}]({$pr['html_url']})";
} catch (Throwable $e) {
// Silently ignore.
Expand Down