-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add TodoByTicketRule (issue tracker support) (#26)
Co-authored-by: Markus Staab <maggus.staab@googlemail.com> Co-authored-by: Emil Masiakowski <emil.masiakowski@amsterdamstandard.com> Co-authored-by: Markus Staab <markus.staab@redaxo.de>
- Loading branch information
1 parent
829088b
commit 98d37d9
Showing
12 changed files
with
534 additions
and
0 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
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,106 @@ | ||
<?php | ||
|
||
namespace staabm\PHPStanTodoBy; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use staabm\PHPStanTodoBy\utils\CommentMatcher; | ||
use staabm\PHPStanTodoBy\utils\ExpiredCommentErrorBuilder; | ||
use staabm\PHPStanTodoBy\utils\TicketStatusFetcher; | ||
|
||
use function in_array; | ||
use function strlen; | ||
use function trim; | ||
|
||
/** | ||
* @implements Rule<Node> | ||
*/ | ||
final class TodoByTicketRule implements Rule | ||
{ | ||
private const PATTERN = <<<'REGEXP' | ||
{ | ||
@?TODO # possible @ prefix | ||
@?[a-zA-Z0-9_-]* # optional username | ||
\s*[:-]?\s* # optional colon or hyphen | ||
\s+ # keyword/ticket separator | ||
(?P<ticketKey>[A-Z0-9]+-\d+) # ticket key consisting of ABC-123 or F01-12345 format | ||
\s*[:-]?\s* # optional colon or hyphen | ||
(?P<comment>.*) # rest of line as comment text | ||
}ix | ||
REGEXP; | ||
|
||
/** @var list<non-empty-string> */ | ||
private array $resolvedStatuses; | ||
/** @var list<non-empty-string> */ | ||
private array $keyPrefixes; | ||
private TicketStatusFetcher $fetcher; | ||
private ExpiredCommentErrorBuilder $errorBuilder; | ||
|
||
/** | ||
* @param list<non-empty-string> $resolvedStatuses | ||
* @param list<non-empty-string> $keyPrefixes | ||
*/ | ||
public function __construct(array $resolvedStatuses, array $keyPrefixes, TicketStatusFetcher $fetcher, ExpiredCommentErrorBuilder $errorBuilder) | ||
{ | ||
$this->resolvedStatuses = $resolvedStatuses; | ||
$this->keyPrefixes = $keyPrefixes; | ||
$this->fetcher = $fetcher; | ||
$this->errorBuilder = $errorBuilder; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$it = CommentMatcher::matchComments($node, self::PATTERN); | ||
|
||
$errors = []; | ||
foreach ($it as $comment => $matches) { | ||
/** @var array<int, array<array{0: string, 1: int}>> $matches */ | ||
foreach ($matches as $match) { | ||
$ticketKey = $match['ticketKey'][0]; | ||
$todoText = trim($match['comment'][0]); | ||
|
||
if (!$this->hasPrefix($ticketKey)) { | ||
continue; | ||
} | ||
|
||
$ticketStatus = $this->fetcher->fetchTicketStatus($ticketKey); | ||
|
||
if (null === $ticketStatus || !in_array($ticketStatus, $this->resolvedStatuses, true)) { | ||
continue; | ||
} | ||
|
||
if ('' !== $todoText) { | ||
$errorMessage = "Should have been resolved in {$ticketKey}: ". rtrim($todoText, '.') .'.'; | ||
} else { | ||
$errorMessage = "Comment should have been resolved in {$ticketKey}."; | ||
} | ||
|
||
$errors[] = $this->errorBuilder->buildError( | ||
$comment, | ||
$errorMessage, | ||
null, | ||
$match[0][1] | ||
); | ||
} | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
private function hasPrefix(string $ticketKey): bool | ||
{ | ||
foreach ($this->keyPrefixes as $prefix) { | ||
if (substr($ticketKey, 0, strlen($prefix)) === $prefix) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
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,9 @@ | ||
<?php | ||
|
||
namespace staabm\PHPStanTodoBy\utils; | ||
|
||
interface TicketStatusFetcher | ||
{ | ||
/** @return string|null Status name or null if ticket doesn't exist */ | ||
public function fetchTicketStatus(string $ticketKey): ?string; | ||
} |
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,36 @@ | ||
<?php | ||
|
||
namespace staabm\PHPStanTodoBy\utils\jira; | ||
|
||
use RuntimeException; | ||
|
||
final class JiraAuthorization | ||
{ | ||
public static function getCredentials(?string $credentials, ?string $credentialsFilePath): string | ||
{ | ||
if (null !== $credentials) { | ||
return trim($credentials); | ||
} | ||
|
||
if (null === $credentialsFilePath) { | ||
throw new RuntimeException('Either credentials or credentialsFilePath parameter must be configured'); | ||
} | ||
|
||
$credentials = file_get_contents($credentialsFilePath); | ||
|
||
if (false === $credentials) { | ||
throw new RuntimeException("Cannot read $credentialsFilePath file"); | ||
} | ||
|
||
return trim($credentials); | ||
} | ||
|
||
public static function createAuthorizationHeader(string $credentials): string | ||
{ | ||
if (str_contains($credentials, ':')) { | ||
return 'Basic ' . base64_encode($credentials); | ||
} | ||
|
||
return "Bearer $credentials"; | ||
} | ||
} |
Oops, something went wrong.