-
-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for YouTrack in TodoByTicketRule #51
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c95dc33
feat: Add support for YouTrack in TodoByTicketRule
DannyvdSluijs 2fa2cfc
refactor: Allow for empty authorisation header
DannyvdSluijs 0fd0c3b
test: Add E2E tests
DannyvdSluijs e7707a2
test: Run youtrack E2E as part of GitHub action workflow
DannyvdSluijs 60676d9
Correct old jira reference for YouTrack
DannyvdSluijs 30f6a7d
docs: Replace reference to below formats for YouTrack's permanent token
DannyvdSluijs 7cb2f26
cs
staabm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,88 @@ | ||
<?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; | ||
use function is_null; | ||
|
||
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] = is_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. | ||
DannyvdSluijs marked this conversation as resolved.
Show resolved
Hide resolved
|
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for discussion: in jira we use issue-states to tell when/whether a ticket is resolved. can/should we do the same for youtrack? does youtrack also have tickets flow thru different states?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Youtrack uses columns to track the state.
From the API documentation the resolved field of the
Issue
[1] Entity is explained asThere is something called
IssueCustomField
[2] which allows you to track custom fields such as state of a ticket. But this is specific to each setup and isn't being used in our setup. So adding support for this is challenging. I would rather stick to the definition of YouTrack itself and it people really need to use custom field support creating a PR to extend the youtrack integration can be done.