-
-
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 support for YouTrack in TodoByTicketRule (#51)
Co-authored-by: Markus Staab <[email protected]>
- Loading branch information
1 parent
00690bb
commit 0a3ce2c
Showing
11 changed files
with
298 additions
and
3 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
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,87 @@ | ||
<?php | ||
|
||
namespace staabm\PHPStanTodoBy\utils\ticket; | ||
|
||
use RuntimeException; | ||
use staabm\PHPStanTodoBy\utils\CredentialsHelper; | ||
use staabm\PHPStanTodoBy\utils\HttpClient; | ||
|
||
use function array_key_exists; | ||
use function is_array; | ||
|
||
final class YouTrackTicketStatusFetcher implements TicketStatusFetcher | ||
{ | ||
private string $host; | ||
private ?string $authorizationHeader; | ||
|
||
private HttpClient $httpClient; | ||
|
||
/** | ||
* @var array<string, ?string> | ||
*/ | ||
private array $cache; | ||
|
||
public function __construct(string $host, ?string $credentials, ?string $credentialsFilePath, HttpClient $httpClient) | ||
{ | ||
$credentials = CredentialsHelper::getCredentials($credentials, $credentialsFilePath); | ||
|
||
$this->host = $host; | ||
$this->authorizationHeader = $credentials ? self::createAuthorizationHeader($credentials) : null; | ||
|
||
$this->cache = []; | ||
$this->httpClient = $httpClient; | ||
} | ||
|
||
public function fetchTicketStatus(string $ticketKey): ?string | ||
{ | ||
if (array_key_exists($ticketKey, $this->cache)) { | ||
return $this->cache[$ticketKey]; | ||
} | ||
|
||
$url = "{$this->host}/api/issues/$ticketKey?fields=resolved"; | ||
$headers = []; | ||
if (null !== $this->authorizationHeader) { | ||
$headers = [ | ||
"Authorization: $this->authorizationHeader", | ||
]; | ||
} | ||
|
||
[$responseCode, $response] = $this->httpClient->get($url, $headers); | ||
|
||
if (200 !== $responseCode) { | ||
throw new RuntimeException("Could not fetch ticket's status from YouTrack with url $url"); | ||
} | ||
|
||
$data = self::decodeAndValidateResponse($response); | ||
|
||
return $this->cache[$ticketKey] = null === $data['resolved'] ? 'open' : 'resolved'; | ||
} | ||
|
||
public static function getKeyPattern(): string | ||
{ | ||
return '[A-Z0-9]+-\d+'; | ||
} | ||
|
||
private static function createAuthorizationHeader(string $credentials): string | ||
{ | ||
return "Bearer $credentials"; | ||
} | ||
|
||
/** @return array{resolved: ?int} */ | ||
private static function decodeAndValidateResponse(string $body): array | ||
{ | ||
$data = json_decode($body, true, 512, JSON_THROW_ON_ERROR); | ||
|
||
if (!is_array($data) || !array_key_exists('resolved', $data)) { | ||
self::throwInvalidResponse(); | ||
} | ||
|
||
return $data; | ||
} | ||
|
||
/** @return never */ | ||
private static function throwInvalidResponse(): void | ||
{ | ||
throw new RuntimeException('YouTrack returned invalid response body'); | ||
} | ||
} |
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 @@ | ||
/vendor/ |
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,11 @@ | ||
{ | ||
"config": { | ||
"allow-plugins": { | ||
"phpstan/extension-installer": true | ||
} | ||
}, | ||
"require-dev": { | ||
"phpstan/phpstan": "^1.10", | ||
"phpstan/extension-installer": "^1.3" | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
src/tickets.php:3:Should have been resolved in WI-1: fix me. |
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,12 @@ | ||
parameters: | ||
level: max | ||
paths: | ||
- src/ | ||
todo_by: | ||
ticket: | ||
enabled: true | ||
tracker: youtrack | ||
keyPrefixes: | ||
- WI | ||
jira: | ||
server: https://youtrack.jetbrains.com/ |
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,3 @@ | ||
<?php | ||
|
||
// TODO: WI-1 fix me |