Skip to content

Commit

Permalink
[TASK] Make scruntizier a bit more happy
Browse files Browse the repository at this point in the history
  • Loading branch information
NeoBlack committed Jul 23, 2017
1 parent 3f897ce commit b8a0819
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 57 deletions.
2 changes: 1 addition & 1 deletion Classes/Commands/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ abstract class AbstractCommand
*
* @param Payload $payload
* @param RealTimeClient $client
* @param array $configuration
* @param array|null $configuration
*/
public function __construct(Payload $payload, RealTimeClient $client, array $configuration = null)
{
Expand Down
83 changes: 47 additions & 36 deletions Classes/Controller/GerritHookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,42 +69,7 @@ public function process($hook, $input = 'php://input')
$message = $this->buildMessage(':white_check_mark: [MERGED] ' . $item->subject, $text, Message\Attachment::COLOR_GOOD);
$this->sendMessageToChannel($hook, $message);

$files = $this->getFilesForPatch($patchId, $commit);
$rstFiles = [];
if (is_array($files)) {
foreach ($files as $fileName => $changeInfo) {
if ($this->endsWith(strtolower($fileName), '.rst')) {
$rstFiles[$fileName] = $changeInfo;
}
}
}
if (count($rstFiles) > 0) {
$message = new Message();
$message->setText(' ');
foreach ($rstFiles as $fileName => $changeInfo) {
$attachment = new Message\Attachment();
$status = !empty($changeInfo['status']) ? $changeInfo['status'] : null;
switch ($status) {
case 'A':
$attachment->setColor(Message\Attachment::COLOR_GOOD);
$attachment->setTitle('A new documentation file has been added');
break;
case 'D':
$attachment->setColor(Message\Attachment::COLOR_WARNING);
$attachment->setTitle('A documentation file has been removed');
break;
default:
$attachment->setColor(Message\Attachment::COLOR_WARNING);
$attachment->setTitle('A documentation file has been updated');
break;
}
$text = ':link: <https://git.typo3.org/Packages/TYPO3.CMS.git/blob/HEAD:/' . $fileName . '|' . $fileName . '>';
$attachment->setText($text);
$attachment->setFallback($text);
$message->addAttachment($attachment);
}
$this->sendMessageToChannel('rst-merged', $message);
}
$this->checkFiles($patchId, $commit);
break;
}
}
Expand All @@ -131,6 +96,52 @@ protected function buildMessage(string $title, string $text, string $color = Mes
return $message;
}

/**
* @param int $patchId
* @param int $commit
*
* @throws \Doctrine\DBAL\DBALException
*/
protected function checkFiles($patchId, $commit)
{
$files = $this->getFilesForPatch($patchId, $commit);
$rstFiles = [];
if (is_array($files)) {
foreach ($files as $fileName => $changeInfo) {
if ($this->endsWith(strtolower($fileName), '.rst')) {
$rstFiles[$fileName] = $changeInfo;
}
}
}
if (count($rstFiles) > 0) {
$message = new Message();
$message->setText(' ');
foreach ($rstFiles as $fileName => $changeInfo) {
$attachment = new Message\Attachment();
$status = !empty($changeInfo['status']) ? $changeInfo['status'] : null;
switch ($status) {
case 'A':
$attachment->setColor(Message\Attachment::COLOR_GOOD);
$attachment->setTitle('A new documentation file has been added');
break;
case 'D':
$attachment->setColor(Message\Attachment::COLOR_WARNING);
$attachment->setTitle('A documentation file has been removed');
break;
default:
$attachment->setColor(Message\Attachment::COLOR_WARNING);
$attachment->setTitle('A documentation file has been updated');
break;
}
$text = ':link: <https://git.typo3.org/Packages/TYPO3.CMS.git/blob/HEAD:/' . $fileName . '|' . $fileName . '>';
$attachment->setText($text);
$attachment->setFallback($text);
$message->addAttachment($attachment);
}
$this->sendMessageToChannel('rst-merged', $message);
}
}

/**
* @param string $hook
* @param Message $message
Expand Down
32 changes: 12 additions & 20 deletions Classes/Controller/WebHookController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@
*/
class WebHookController extends AbstractHookController
{
/**
* @var array
*/
protected $colorMap = [
'info' => Message\Attachment::COLOR_INFO,
'ok' => Message\Attachment::COLOR_GOOD,
'warning' => Message\Attachment::COLOR_WARNING,
'danger' => Message\Attachment::COLOR_DANGER,
'notice' => Message\Attachment::COLOR_NOTICE,
];

/**
* public method to start processing the request.
*
Expand Down Expand Up @@ -46,26 +57,7 @@ public function process($hook, $input = 'php://input')
return;
}

switch ($json->color) {
case 'info':
$color = Message\Attachment::COLOR_INFO;
break;
case 'ok':
$color = Message\Attachment::COLOR_GOOD;
break;
case 'warning':
$color = Message\Attachment::COLOR_WARNING;
break;
case 'danger':
$color = Message\Attachment::COLOR_DANGER;
break;
case 'notice':
$color = Message\Attachment::COLOR_NOTICE;
break;
default:
$color = $json->color;
break;
}
$color = $this->colorMap[$json->color] ?? $json->color;

$message = new Message();
$message->setText(' ');
Expand Down

0 comments on commit b8a0819

Please sign in to comment.