Skip to content
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

Ticket-rule: add "see $url" tip #84

Merged
merged 2 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/e2e-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ jobs:
php-version: '8.2'
test-dir: tests-e2e/github/
script: |
diff <(vendor/bin/phpstan analyse --error-format=raw --no-progress | sed "s|$(pwd)/||") expected-errors.txt
diff -w <(vendor/bin/phpstan analyse --error-format=prettyJson --no-progress | sed "s|$(pwd)/||") expected-errors.json
- os: ubuntu-latest
php-version: '8.2'
test-dir: tests-e2e/jira/
script: |
diff <(vendor/bin/phpstan analyse --error-format=raw --no-progress | sed "s|$(pwd)/||") expected-errors.txt
diff -w <(vendor/bin/phpstan analyse --error-format=prettyJson --no-progress | sed "s|$(pwd)/||") expected-errors.json
- os: ubuntu-latest
php-version: '8.2'
test-dir: tests-e2e/youtrack/
script: |
diff <(vendor/bin/phpstan analyse --error-format=raw --no-progress | sed "s|$(pwd)/||") expected-errors.txt
diff -w <(vendor/bin/phpstan analyse --error-format=prettyJson --no-progress | sed "s|$(pwd)/||") expected-errors.json

steps:
- name: Checkout
Expand Down
2 changes: 1 addition & 1 deletion src/TodoByTicketRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function processNode(Node $node, Scope $scope): array
$errors[] = $this->errorBuilder->buildFileError(
$this->commentFromJson($json),
$errorMessage,
null,
"See {$this->configuration->getFetcher()->resolveTicketUrl($ticketKey)}",
$wholeMatchStartOffset,
$file,
$line
Expand Down
27 changes: 21 additions & 6 deletions src/utils/ticket/GitHubTicketStatusFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,8 @@ public function fetchTicketStatus(array $ticketKeys): array
{
$ticketUrls = [];

$keyRegex = self::KEY_REGEX;
foreach ($ticketKeys as $ticketKey) {
preg_match_all("/$keyRegex/ix", $ticketKey, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);

$owner = $matches[0]['githubOwner'][0] ?: $this->defaultOwner;
$repo = $matches[0]['githubRepo'][0] ?: $this->defaultRepo;
$number = $matches[0]['githubNumber'][0];
[$owner, $repo, $number] = $this->processKey($ticketKey);

$ticketUrls[$ticketKey] = "https://api.github.com/repos/$owner/$repo/issues/$number";
}
Expand Down Expand Up @@ -91,4 +86,24 @@ public static function getKeyPattern(): string
{
return self::KEY_REGEX;
}

public function resolveTicketUrl(string $ticketKey): string
{
[$owner, $repo, $number] = $this->processKey($ticketKey);

return "https://github.com/$owner/$repo/issues/$number";
}

/** @return array{string,string,string} */
private function processKey(string $ticketKey): array
{
$keyRegex = self::KEY_REGEX;
preg_match_all("/$keyRegex/ix", $ticketKey, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);

$owner = $matches[0]['githubOwner'][0] ?: $this->defaultOwner;
$repo = $matches[0]['githubRepo'][0] ?: $this->defaultRepo;
$number = $matches[0]['githubNumber'][0];

return [$owner, $repo, $number];
}
}
5 changes: 5 additions & 0 deletions src/utils/ticket/JiraTicketStatusFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public static function getKeyPattern(): string
return '[A-Z0-9]+-\d+';
}

public function resolveTicketUrl(string $ticketKey): string
{
return "$this->host/browse/$ticketKey";
}

/** @return array{fields: array{status: array{name: string}}} */
private static function decodeAndValidateResponse(string $body): array
{
Expand Down
7 changes: 7 additions & 0 deletions src/utils/ticket/TicketStatusFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ public function fetchTicketStatus(array $ticketKeys): array;

/** @return non-empty-string */
public static function getKeyPattern(): string;

/**
* @param non-empty-string $ticketKey
*
* @return non-empty-string
*/
public function resolveTicketUrl(string $ticketKey): string;
}
5 changes: 5 additions & 0 deletions src/utils/ticket/YouTrackTicketStatusFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public static function getKeyPattern(): string
return '[A-Z0-9]+-\d+';
}

public function resolveTicketUrl(string $ticketKey): string
{
return "https://youtrack.jetbrains.com/issue/$ticketKey";
}

private static function createAuthorizationHeader(string $credentials): string
{
return "Bearer $credentials";
Expand Down
38 changes: 38 additions & 0 deletions tests-e2e/github/expected-errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"totals": {
"errors": 0,
"file_errors": 4
},
"files": {
"src/github-issues-and-prs.php": {
"errors": 4,
"messages": [
{
"message": "Should have been resolved in #59: fix me.",
"line": 3,
"ignorable": false,
"tip": "See https://github.com/staabm/phpstan-todo-by/issues/59"
},
{
"message": "Should have been resolved in staabm/phpstan-dba#452: change me after https://github.com/staabm/phpstan-dba/issues/452 is closed.",
"line": 4,
"ignorable": false,
"tip": "See https://github.com/staabm/phpstan-dba/issues/452"
},
{
"message": "Comment should have been resolved in phpstan/phpstan#3.",
"line": 5,
"ignorable": false,
"tip": "See https://github.com/phpstan/phpstan/issues/3"
},
{
"message": "Should have been resolved in #26: needs https://github.com/staabm/phpstan-todo-by/pull/26.",
"line": 6,
"ignorable": false,
"tip": "See https://github.com/staabm/phpstan-todo-by/issues/26"
}
]
}
},
"errors": []
}
4 changes: 0 additions & 4 deletions tests-e2e/github/expected-errors.txt

This file was deleted.

20 changes: 20 additions & 0 deletions tests-e2e/jira/expected-errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"totals": {
"errors": 0,
"file_errors": 1
},
"files": {
"src/tickets.php": {
"errors": 1,
"messages": [
{
"message": "Should have been resolved in JRA-9: fix me.",
"line": 3,
"ignorable": false,
"tip": "See https://jira.atlassian.com/browse/JRA-9"
}
]
}
},
"errors": []
}
1 change: 0 additions & 1 deletion tests-e2e/jira/expected-errors.txt

This file was deleted.

20 changes: 20 additions & 0 deletions tests-e2e/youtrack/expected-errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"totals": {
"errors": 0,
"file_errors": 1
},
"files": {
"src/tickets.php": {
"errors": 1,
"messages": [
{
"message": "Should have been resolved in WI-1: fix me.",
"line": 3,
"ignorable": false,
"tip": "See https://youtrack.jetbrains.com/issue/WI-1"
}
]
}
},
"errors": []
}
1 change: 0 additions & 1 deletion tests-e2e/youtrack/expected-errors.txt

This file was deleted.

26 changes: 22 additions & 4 deletions tests/TodoByTicketRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,26 @@ private function getTicketConfiguration(): TicketRuleConfiguration
public function testRule(): void
{
$this->analyse([__DIR__ . '/data/ticket.php'], [
['Should have been resolved in APP-123: rename this to doBar().', 5],
['Comment should have been resolved in APP-4444.', 11],
['Comment should have been resolved in FOO-0001.', 12],
['Should have been resolved in F01-12345: please change me.', 13],
[
'Should have been resolved in APP-123: rename this to doBar().',
5,
'See https://issue-tracker.com/APP-123',
],
[
'Comment should have been resolved in APP-4444.',
11,
'See https://issue-tracker.com/APP-4444',
],
[
'Comment should have been resolved in FOO-0001.',
12,
'See https://issue-tracker.com/FOO-0001',
],
[
'Should have been resolved in F01-12345: please change me.',
13,
'See https://issue-tracker.com/F01-12345',
],
]);
}

Expand All @@ -71,10 +87,12 @@ public function testBug64(): void
[
'Should have been resolved in F01-12345: please change me.',
9,
'See https://issue-tracker.com/F01-12345',
],
[
'Should have been resolved in F01-12345: please * change me.',
13,
'See https://issue-tracker.com/F01-12345',
],
]);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/utils/StaticTicketStatusFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,9 @@ public static function getKeyPattern(): string
{
return '[A-Z0-9]+-\d+';
}

public function resolveTicketUrl(string $ticketKey): string
{
return "https://issue-tracker.com/$ticketKey";
}
}
Loading