-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathgithub.php
192 lines (163 loc) · 5.85 KB
/
github.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/**
* This file is part of the PHP Telegram Support Bot.
*
* (c) PHP Telegram Bot Team (https://github.com/php-telegram-bot)
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
use Github\Api\Issue;
use Github\Api\PullRequest;
use Github\Client;
use PhpTelegramBot\Core\Entities\ServerResponse;
use PhpTelegramBot\Core\Exception\TelegramException;
use PhpTelegramBot\Core\Request;
use PhpTelegramBot\Core\Telegram;
use PhpTelegramBot\Core\TelegramLog;
use MatthiasMullie\Scrapbook\Adapters\MySQL;
use MatthiasMullie\Scrapbook\Psr6\Pool;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\StreamHandler;
use Monolog\Logger;
use NPM\ServiceWebhookHandler\Handlers\GitHubHandler;
use TelegramBot\SupportBot\Webhooks\Utils;
// Composer autoloader.
require_once __DIR__ . '/../../vendor/autoload.php';
Dotenv\Dotenv::create(__DIR__ . '/../..')->load();
$webhook = new GitHubHandler(getenv('TG_WEBHOOK_SECRET_GITHUB'));
if (!$webhook->validate()) {
http_response_code(404);
die;
}
// Save all incoming data to a log file for future reference.
Utils::logWebhookData(getenv('TG_LOGS_DIR') . '/' . getenv('TG_BOT_USERNAME') . '_webhook_github.log');
// Limit repos and events to serve.
$allowed_repos_events = [
'php-telegram-bot/core' => ['release'],
'php-telegram-bot/support-bot' => ['release'],
'php-telegram-bot/telegram-bot-manager' => ['release'],
];
// Get the incoming webhook data.
$data = $webhook->getData();
// Only react to allowed repos and events.
$repo = $data['repository'];
if (!in_array($webhook->getEvent(), $allowed_repos_events[$repo['full_name']] ?? [], true)) {
die;
}
// Handle event.
if ($webhook->getEvent() === 'release') {
handleRelease($data);
if ($repo['full_name'] === 'php-telegram-bot/support-bot' && getenv('TG_AUTOUPDATE') === '1') {
pullLatestAndUpdate();
}
}
/**
* Handle the "release" event.
*
* @param array $data
*/
function handleRelease(array $data): void
{
$repo = $data['repository'];
$release = $data['release'];
$action = $data['action'];
if ($action === 'published' && !$release['draft'] && !$release['prerelease']) {
$author = $release['author']['login'];
$author_url = $release['author']['html_url'];
$tag = $release['tag_name'];
$url = $release['html_url'];
$body = parseReleaseBody($release['body'], $repo['owner']['login'], $repo['name']);
$message = LitEmoji\LitEmoji::encodeUnicode("
:star: *New Release!* :star:
(_version_ [{$tag}]({$url}) _of_ [{$repo['full_name']}]({$repo['html_url']}) _has just been released by_ [{$author}]({$author_url}))
{$body}
");
// Post the release message!
sendTelegramMessage(getenv('TG_SUPPORT_GROUP_ID'), $message);
}
}
/**
* Make the release message Telegram-friendly and resolve links to GitHub.
*
* @param string $body
* @param string $user
* @param string $repo
*
* @return string
*/
function parseReleaseBody($body, $user, $repo): string
{
// Replace headers with bold text.
$body = preg_replace_callback('~### (?<header>.*)~', static function ($matches) {
$header = trim($matches['header']);
return "*{$header}*";
}, $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];
$id = $matches['id'];
$user = $matches['user'] ?: $user;
$repo = $matches['repo'] ?: $repo;
// Check if this ID is an issue.
try {
/** @var Issue $issue */
$issue = $github_client->issue()->show($user, $repo, $id);
return "[{$text}]({$issue['html_url']})";
} catch (Throwable $e) {
// Silently ignore.
}
// Check if this ID is a pull request.
try {
/** @var PullRequest $pr */
$pr = $github_client->pr()->show($user, $repo, $id);
return "[{$text}]({$pr['html_url']})";
} catch (Throwable $e) {
// Silently ignore.
}
return $text;
}, $body);
return $body;
}
/**
* Send a text to the passed chat.
*
* @param string $chat_id
* @param string $text
*
* @return ServerResponse|null
*/
function sendTelegramMessage($chat_id, $text): ?ServerResponse
{
try {
new Telegram(getenv('TG_API_KEY'));
TelegramLog::initialize(new Logger('telegram_bot_releases', [
(new StreamHandler(getenv('TG_LOGS_DIR') . '/releases.debug.log', Logger::DEBUG))->setFormatter(new LineFormatter(null, null, true)),
(new StreamHandler(getenv('TG_LOGS_DIR') . '/releases.error.log', Logger::ERROR))->setFormatter(new LineFormatter(null, null, true)),
]));
$parse_mode = 'markdown';
return Request::sendMessage(compact('chat_id', 'text', 'parse_mode'));
} catch (TelegramException $e) {
TelegramLog::error($e->getMessage());
} catch (Throwable $e) {
// Silently ignore.
}
return null;
}
/**
* Pull the latest code from the repository and install with composer.
*/
function pullLatestAndUpdate(): void
{
exec('sudo -u www-data /usr/bin/git stash');
exec('sudo -u www-data /usr/bin/git fetch');
exec('sudo -u www-data /usr/bin/git reset --hard');
exec('sudo -u www-data /usr/bin/git rebase');
exec('sudo -u www-data /usr/bin/git pull');
exec('sudo -u www-data /usr/local/bin/composer install --no-dev');
}