Skip to content
Snippets Groups Projects

Automated Project Update Bot fixes

4 unresolved threads
Files
10
@@ -2,14 +2,13 @@
namespace Drupal\tmgmt_crowdin\Controller;
use Drupal\Core\Utility\Error;
use Drupal\Component\Utility\DeprecationHelper;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Controller\ControllerBase;
use Drupal\tmgmt\Entity\Job;
use Drupal\tmgmt\Entity\JobItem;
use Drupal\tmgmt\JobInterface;
use Drupal\tmgmt\JobItemInterface;
use Drupal\tmgmt\TMGMTException;
use Drupal\tmgmt\TranslatorInterface;
use Drupal\tmgmt_crowdin\Plugin\tmgmt\Translator\CrowdinTranslator;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -17,88 +16,107 @@ use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class CrowdinWebhookController extends ControllerBase
{
/** @var LoggerInterface */
protected $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public static function create(ContainerInterface $container): self
{
return new static($container->get('logger.factory')->get('tmgmt_crowdin'));
/**
* Class for CrowdinWebhookController.
*/
class CrowdinWebhookController extends ControllerBase {
/**
* The protected logger.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* {@inheritdoc}
*/
public function __construct(LoggerInterface $logger) {
$this->logger = $logger;
}
/**
* Public static function for create.
*/
public static function create(ContainerInterface $container): self {
return new static($container->get('logger.factory')->get('tmgmt_crowdin'));
}
/**
* Function for process.
*/
public function process(Request $request): JsonResponse {
$response = ['success' => TRUE, 'translations_updated' => FALSE];
$data = Json::decode($request->getContent());
if (!is_array($data) || !isset($data['file']['path'])) {
$this->logger->warning('Invalid webhook payload received: missing file path.');
return new JsonResponse($response, Response::HTTP_OK);
}
public function process(Request $request): JsonResponse
{
$response = ['success' => false, 'translations_updated' => false];
$data = Json::decode($request->getContent());
$project_id = $data['project_id'] ? $data['project_id'] : $data['file']['project']['id'];
$file_id = $data['file_id'] ? $data['file_id'] : $data['file']['id'];
$target_language = $data['language'] ? $data['language'] : $data['targetLanguage']['id'];
$file_path = explode('/', $data['file']['path']);
$file_path = explode('/', $data['file']['path']);
[$job_id, $job_item_id] = sscanf(end($file_path), CrowdinTranslator::FORMAT_FILE_NAME);
[$job_id, $job_item_id] = sscanf(end($file_path), CrowdinTranslator::FORMAT_FILE_NAME);
if (!$job_id && !$job_item_id) {
return new JsonResponse($response, Response::HTTP_OK);
}
if (!$job_id && !$job_item_id) {
$response['message'] = 'Invalid file path. Job id and job item id are not found.';
return new JsonResponse($response, Response::HTTP_BAD_REQUEST);
}
/** @var \Drupal\tmgmt\Entity\JobInterface $job */
$job = Job::load($job_id);
/** @var \Drupal\tmgmt\Entity\JobItemInterface $job_item */
$job_item = JobItem::load($job_item_id);
/** @var JobInterface $job */
$job = Job::load($job_id);
/** @var JobItemInterface $job_item */
$job_item = JobItem::load($job_item_id);
if ($job->isAborted() || $job_item->isAborted()) {
$message = 'The job (%id) you receive translation from is not active. Please contact your Crowdin manager.';
if ($job->isAborted() || $job_item->isAborted()) {
$message = 'The job (' . $job->id() . ') or job item (' . $job_item->id() . ') is not active.';
$this->logger->warning($message, ['%id' => $job->id()]);
$this->logger->warning($message);
$job->addMessage($message, 'warning');
$job->addMessage($message, ['%id' => $job_item->id()], 'warning');
$response['message'] = $message;
return new JsonResponse(NULL, Response::HTTP_NOT_FOUND);
}
return new JsonResponse($response, Response::HTTP_NOT_FOUND);
}
/** @var \Drupal\tmgmt\TranslatorInterface $translator */
$translator = $job->getTranslator();
/** @var \Drupal\tmgmt_crowdin\Plugin\tmgmt\Translator\CrowdinTranslator $crowdin_translator */
$crowdin_translator = $translator->getPlugin();
/** @var TranslatorInterface $translator */
$translator = $job->getTranslator();
/** @var CrowdinTranslator $crowdin_translator */
$crowdin_translator = $translator->getPlugin();
$webhook_id_by_project_id = unserialize($crowdin_translator->getCrowdinData('webhook_id_by_project_id'));
$webhook_id_by_project_id = unserialize($crowdin_translator->getCrowdinData('webhook_id_by_project_id'));
if (!array_key_exists($data['project_id'], $webhook_id_by_project_id)) {
return new JsonResponse($response, Response::HTTP_OK);
}
if (!array_key_exists($project_id, $webhook_id_by_project_id)) {
$response['message'] = 'Webhook id is not found for project id ' . $project_id;
return new JsonResponse($response, Response::HTTP_BAD_REQUEST);
}
$project = $crowdin_translator->getProject($translator, $data['project_id']);
$response['success'] = true;
$project = $crowdin_translator->getProject($translator, $project_id);
$export_approved_only = $project['data']['exportApprovedOnly'] ?? (bool)$project['data']['exportWithMinApprovalsCount'];
$export_approved_only = $project['data']['exportApprovedOnly'] ?? (bool) $project['data']['exportWithMinApprovalsCount'];
if ($export_approved_only && $data['event'] !== CrowdinTranslator::FILE_APPROVED_EVENT) {
return new JsonResponse($response, Response::HTTP_OK);
}
if ($export_approved_only && $data['event'] !== CrowdinTranslator::FILE_APPROVED_EVENT) {
return new JsonResponse($response, Response::HTTP_OK);
}
try {
if ($crowdin_translator->updateTranslation($job_item, $project, $file_id, $target_language)) {
return new JsonResponse(['success' => true, 'translations_updated' => true], Response::HTTP_OK);
}
} catch (TMGMTException $e) {
$job_item->addMessage($e->getMessage());
} catch (\Exception $e) {
watchdog_exception('tmgmt_crowdin', $e);
return new JsonResponse(NULL, Response::HTTP_INTERNAL_SERVER_ERROR);
}
$file_id = $data['file_id'];
$target_language = $data['language'];
return new JsonResponse(['success' => true, 'translations_updated' => false], Response::HTTP_OK);
try {
if ($crowdin_translator->updateTranslation($job_item, $project, $file_id, $target_language)) {
return new JsonResponse(['success' => TRUE, 'translations_updated' => TRUE], Response::HTTP_OK);
}
}
catch (TMGMTException $e) {
$job_item->addMessage($e->getMessage());
}
catch (\Exception $e) {
DeprecationHelper::backwardsCompatibleCall(\Drupal::VERSION, '10.1.0',
fn() => Error::logException($this->logger, $e),
fn() => $this->logger->error($e->getMessage())
);
return new JsonResponse(NULL, Response::HTTP_INTERNAL_SERVER_ERROR);
}
return new JsonResponse(['success' => TRUE, 'translations_updated' => FALSE], Response::HTTP_OK);
}
}
Loading