Skip to content

Commit a0f1e14

Browse files
authored
Merge pull request #32 from noplanman/github_client_cache
Use MySQL as cache for GitHub client to reduce API requests.
2 parents f6a7acd + 186d170 commit a0f1e14

File tree

4 files changed

+150
-5
lines changed

4 files changed

+150
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
77
### Added
88
- Code checkers to ensure coding standard.
99
- When releasing a new version of the Support Bot, automatically fetch the latest code and install with composer.
10+
- MySQL cache for GitHub client.
1011
### Changed
1112
- Bumped Manager to 1.5.
1213
- Logging is now decoupled with custom Monolog logger.

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,15 @@
1919
"require": {
2020
"php": ">= 7.1",
2121
"ext-json": "*",
22+
"ext-pdo": "*",
2223
"php-telegram-bot/telegram-bot-manager": "^1.5",
2324
"noplanman/service-webhook-handler": "^0.2",
2425
"vlucas/phpdotenv": "^3.4",
2526
"php-http/guzzle6-adapter": "^1.1",
2627
"knplabs/github-api": "^2.11",
2728
"elvanto/litemoji": "^1.4",
28-
"monolog/monolog": "^1.24"
29+
"monolog/monolog": "^1.24",
30+
"matthiasmullie/scrapbook": "^1.4"
2931
},
3032
"require-dev": {
3133
"squizlabs/php_codesniffer": "^3.4",

composer.lock

Lines changed: 138 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/webhooks/github.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
use Longman\TelegramBot\Request;
1818
use Longman\TelegramBot\Telegram;
1919
use Longman\TelegramBot\TelegramLog;
20+
use MatthiasMullie\Scrapbook\Adapters\MySQL;
21+
use MatthiasMullie\Scrapbook\Psr6\Pool;
2022
use Monolog\Formatter\LineFormatter;
2123
use Monolog\Handler\StreamHandler;
2224
use Monolog\Logger;
@@ -108,6 +110,10 @@ function parseReleaseBody($body, $user, $repo): string
108110
}, $body);
109111

110112
$github_client = new Client();
113+
$github_client->addCache(new Pool(new MySQL(
114+
new PDO('mysql:dbname=' . getenv('TG_DB_DATABASE') . ';host=' . getenv('TG_DB_HOST'), getenv('TG_DB_USER'), getenv('TG_DB_PASSWORD'))
115+
)));
116+
111117
// Replace any ID links with the corresponding issue or pull request link.
112118
$body = preg_replace_callback('~(?:(?<user>[0-9a-z\-]*)/(?<repo>[0-9a-z\-]*))?#(?<id>\d*)~i', static function ($matches) use ($github_client, $user, $repo) {
113119
$text = $matches[0];
@@ -118,7 +124,7 @@ function parseReleaseBody($body, $user, $repo): string
118124
// Check if this ID is an issue.
119125
try {
120126
/** @var Issue $issue */
121-
$issue = $github_client->api('issue')->show($user, $repo, $id);
127+
$issue = $github_client->issue()->show($user, $repo, $id);
122128
return "[{$text}]({$issue['html_url']})";
123129
} catch (Throwable $e) {
124130
// Silently ignore.
@@ -127,7 +133,7 @@ function parseReleaseBody($body, $user, $repo): string
127133
// Check if this ID is a pull request.
128134
try {
129135
/** @var PullRequest $pr */
130-
$pr = $github_client->api('pull_request')->show($user, $repo, $id);
136+
$pr = $github_client->pr()->show($user, $repo, $id);
131137
return "[{$text}]({$pr['html_url']})";
132138
} catch (Throwable $e) {
133139
// Silently ignore.

0 commit comments

Comments
 (0)