-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c91b567
commit 0e8c8b9
Showing
10 changed files
with
503 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of CaptainHook | ||
* | ||
* (c) Sebastian Feldmann <sf@sebastian-feldmann.info> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace CaptainHook\App\Hook\Notify\Action; | ||
|
||
use CaptainHook\App\Config; | ||
use CaptainHook\App\Console\IO; | ||
use CaptainHook\App\Exception\ActionFailed; | ||
use CaptainHook\App\Hook\Action; | ||
use CaptainHook\App\Hook\Constrained; | ||
use CaptainHook\App\Hook\Notify\Extractor; | ||
use CaptainHook\App\Hook\Notify\Notification; | ||
use CaptainHook\App\Hook\Restriction; | ||
use CaptainHook\App\Hook\Util; | ||
use CaptainHook\App\Hooks; | ||
use SebastianFeldmann\Cli\Processor\ProcOpen as Processor; | ||
use SebastianFeldmann\Git\Repository; | ||
|
||
/** | ||
* Class Notify | ||
* | ||
* @package CaptainHook | ||
* @author Sebastian Feldmann <sf@sebastian-feldmann.info> | ||
* @link https://github.com/captainhookphp/captainhook | ||
* @since Class available since Release 5.4.5 | ||
*/ | ||
class Notify implements Action, Constrained | ||
{ | ||
private const DEFAULT_PREFIX = 'git-notify:'; | ||
|
||
/** | ||
* git-notify trigger | ||
* | ||
* @var string | ||
*/ | ||
private $prefix; | ||
|
||
/** | ||
* Returns a list of applicable hooks | ||
* | ||
* @return \CaptainHook\App\Hook\Restriction | ||
*/ | ||
public static function getRestriction(): Restriction | ||
{ | ||
return Restriction::fromArray([Hooks::POST_CHECKOUT, Hooks::POST_MERGE, Hooks::POST_REWRITE]); | ||
} | ||
|
||
/** | ||
* Executes the action | ||
* | ||
* @param \CaptainHook\App\Config $config | ||
* @param \CaptainHook\App\Console\IO $io | ||
* @param \SebastianFeldmann\Git\Repository $repository | ||
* @param \CaptainHook\App\Config\Action $action | ||
* @return void | ||
* @throws \Exception | ||
*/ | ||
public function execute(Config $config, IO $io, Repository $repository, Config\Action $action): void | ||
{ | ||
$this->prefix = $action->getOptions()->get('prefix', self::DEFAULT_PREFIX); | ||
$oldHash = Util::findPreviousHead($io); | ||
$newHash = $io->getArgument('newHead', 'HEAD'); | ||
|
||
$logOp = $repository->getLogOperator(); | ||
$log = $logOp->getCommitsBetween($oldHash, $newHash); | ||
|
||
foreach ($log as $commit) { | ||
$message = $commit->getSubject() . PHP_EOL . $commit->getBody(); | ||
if ($this->containsNotification($message)) { | ||
$notification = Extractor::extractNotification($message, $this->prefix); | ||
$this->notify($io, $notification); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Checks if the commit message contains the notification prefix 'git-notify:' | ||
* | ||
* @param string $message | ||
* @return bool | ||
*/ | ||
private function containsNotification(string $message): bool | ||
{ | ||
return strpos($message, $this->prefix) !== false; | ||
} | ||
|
||
/** | ||
* Write the notification to the | ||
* | ||
* @param \CaptainHook\App\Console\IO $io | ||
* @param \CaptainHook\App\Hook\Notify\Notification $notification | ||
*/ | ||
private function notify(IO $io, Notification $notification) | ||
{ | ||
$io->write($notification->banner()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of CaptainHook | ||
* | ||
* (c) Sebastian Feldmann <sf@sebastian-feldmann.info> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace CaptainHook\App\Hook\Notify; | ||
|
||
/** | ||
* Class Extractor | ||
* | ||
* @package CaptainHook | ||
* @author Sebastian Feldmann <sf@sebastian-feldmann.info> | ||
* @link https://github.com/captainhookphp/captainhook | ||
* @since Class available since Release 5.4.5 | ||
*/ | ||
class Extractor | ||
{ | ||
public static function extractNotification(string $message, string $prefix = 'git-notify:'): Notification | ||
{ | ||
return new Notification(self::getLines($message, $prefix)); | ||
} | ||
|
||
private static function getLines(string $message, string $prefix): array | ||
{ | ||
$matches = []; | ||
if (preg_match('#' . $prefix . '(.*)#is', $message, $matches)) { | ||
$split = preg_split("/\r\n|\n|\r/", $matches[1]); | ||
|
||
return is_array($split) ? array_map('trim', $split) : []; | ||
} | ||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
/** | ||
* This file is part of CaptainHook | ||
* | ||
* (c) Sebastian Feldmann <sf@sebastian-feldmann.info> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace CaptainHook\App\Hook\Notify; | ||
|
||
/** | ||
* Class Notification | ||
* | ||
* @package CaptainHook | ||
* @author Sebastian Feldmann <sf@sebastian-feldmann.info> | ||
* @link https://github.com/captainhookphp/captainhook | ||
* @since Class available since Release 5.4.5 | ||
*/ | ||
class Notification | ||
{ | ||
/** | ||
* List of rules to check | ||
* | ||
* @var string[] | ||
*/ | ||
private $lines = []; | ||
|
||
/** | ||
* Max line length | ||
* | ||
* @var int | ||
*/ | ||
private $maxLineLength = 0; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param string[] $lines | ||
*/ | ||
public function __construct(array $lines) | ||
{ | ||
$this->lines = $lines; | ||
foreach ($this->lines as $line) { | ||
$lineLength = mb_strlen($line); | ||
if ($lineLength > $this->maxLineLength) { | ||
$this->maxLineLength = $lineLength; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Return line count | ||
* | ||
* @return int | ||
*/ | ||
public function length(): int | ||
{ | ||
return count($this->lines); | ||
} | ||
|
||
/** | ||
* Returns the string to display | ||
* | ||
* @return string | ||
*/ | ||
public function banner(): string | ||
{ | ||
$text = []; | ||
$text[] = '<error>' . str_repeat(' ', $this->maxLineLength + 6) . '</error>'; | ||
$text[] = '<error> </error> ' . str_repeat(' ', $this->maxLineLength) . ' <error> </error>'; | ||
foreach ($this->lines as $line) { | ||
$text[] = $this->formatLine($line); | ||
} | ||
$text[] = '<error> </error> ' . str_repeat(' ', $this->maxLineLength) . ' <error> </error>'; | ||
$text[] = '<error>' . str_repeat(' ', $this->maxLineLength + 6) . '</error>'; | ||
|
||
return PHP_EOL . implode(PHP_EOL, $text) . PHP_EOL; | ||
} | ||
|
||
private function formatLine(string $line): string | ||
{ | ||
$length = mb_strlen($line); | ||
$left = ''; | ||
$right = ''; | ||
if ($length < $this->maxLineLength) { | ||
$space = $this->maxLineLength - $length; | ||
$left = str_repeat(' ', (int) floor($space / 2)); | ||
$right = str_repeat(' ', (int) ceil($space / 2)); | ||
} | ||
return '<error> </error> ' . $left . $line . $right . ' <error> </error>'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.