Skip to content
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

Add logger API #1113

Merged
merged 9 commits into from
Mar 20, 2024
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
21 changes: 21 additions & 0 deletions packages/docs/site/docs/13-contributing/07-crash-reports.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
sidebar_position: 7
title: Crash Reports
---

# Playground.WordPress.net Crash Reports

When Playground crashes on Playground.WordPress.net users are able to submit a crash report. These reports are stored in the [#playground-logs channel on the Making WordPress Slack](https://wordpress.slack.com/archives/C06Q5DCKZ3L).

**Playground doesn't collect crash reports automatically.** Instead, users are prompted to submit a crash report when a crash occurs and are able to modify it before submitting. The crash report includes a Playground logs, a description, and the url.

## Development

Logs are sent to the [logger API on Playground.WordPress.net](https://github.com/WordPress/wordpress-playground/blob/c52d7dbd94dbe3ffc57adde4d9844545ade97f93/packages/playground/website/public/logger.php). The logger API is a simple REST API that accepts a POST request with a `message` parameter.
The API validates the message and then sends it to the [#playground-logs channel on the Making WordPress Slack(https://wordpress.slack.com/archives/C06Q5DCKZ3L).

### Slack app

[We use the Playground Slack app](https://api.slack.com/apps/A06PEFZ00SG) to send logs to the #playground-logs channel.

The [Slack app OAuth token](https://api.slack.com/apps/A06PEFZ00SG/oauth?) and channel id are stored on Playground.WordPress.net as environment variables in `.htaccess`.
96 changes: 96 additions & 0 deletions packages/playground/website/public/logger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
// Disable error reporting
error_reporting(0);

header('Content-Type: application/json');

$channel = getenv('SLACK_CHANNEL');
$token = getenv('SLACK_TOKEN');

/**
* Send response to the client
*
* @param bool $ok - If the request was successful
* @param string|null $error - Error message if the request was not successful
*/
function response($ok, $error = null)
{
$response_data = array(
'ok' => $ok,
);
if ($error) {
$response_data['error'] = $error;
}
die(json_encode($response_data));
bgrgicak marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Validate the message format
*
* @param string $message - The message to validate
* @return bool - If the message is valid
*/
function validate_message($message)
{
// Validate description. Description is required
preg_match('/(?<=What happened\?\n\n)(.*?)(?=\n\nLogs)/s', $message, $description);
if (empty($description)) {
return false;
}

// Validate logs if exists. Logs need to match the PHP error log format
preg_match('/(?<=Logs\n\n)(.*?)(?=\n\nUrl)/s', $message, $logs);
if (!empty($logs)) {
$logs = $logs[0];
if (preg_match('/\[\d{2}-[A-Za-z]{3}-\d{4} \d{2}:\d{2}:\d{2} UTC\](.*)/s', $logs) !== 1) {
return false;
}
}

// Validate URL if exists
preg_match('/(?<=Url\n\n)(.*)/s', $message, $url);
if (!empty($url)) {
$url = $url[0];
if (filter_var($url, FILTER_VALIDATE_URL) === false) {
return false;
}
}

return true;
}

if (empty($token)) {
response(false, 'No token provided');
}

if (!isset($_POST['message'])) {
response(false, 'No message provided');
}
$text = $_POST['message'];

if (!validate_message($text)) {
response(false, 'Invalid message');
}
$text = urlencode($text);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://slack.com/api/chat.postMessage?channel=$channel&text=$text");
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: Bearer $token"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$response = curl_exec($ch);
$errors = curl_error($ch);
$response_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);


if ($response_code !== 200) {
response(false, 'HTTP Error: ' . $errors);
}

$response_data = json_decode($response, true);
if ($response_data['ok'] !== true) {
response(false, 'Slack Error: ' . $response_data['error']);
}

response(true);
Loading