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

Add TodoByTicketRule (issue tracker support) #26

Merged
merged 25 commits into from
Dec 27, 2023
Merged

Add TodoByTicketRule (issue tracker support) #26

merged 25 commits into from
Dec 27, 2023

Conversation

EmilMassey
Copy link
Contributor

Hi!

First of all, thank you for this extension, because it allowed me to easily create something I needed for my personal project: to analyze codebase for todos with JIRA ticket keys and to make sure commented code has been fixed in the referenced tickets.

I noticed that your extension is getting more and more features so I decided to create this Pull Request with the changes I made to support my need to fetch statuses from JIRA. Maybe someone else is looking for such a feature and maybe you'll find it useful for your project.

I'm not sure though if it is expected from static analyzer to make API calls to some external provider but I made this rule optional (can be allowed with a flag in parameters). Currently only JIRA is supported but in the future this could also include other issue trackers. At the moment only basic authentication with Personal Access Token or user password is supported which was enough for my personal use case on local machine but most of organizations run their phpstan analysis in CI so if this PR is accepted, I'll work on changes to support OAuth tokens which are more appropriate for this use case.

If you would like to include this feature in your project, please feel free to share your comments and suggestions so I can work on this to match the vision you have for this extension.

Copy link
Owner

@staabm staabm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the contribution.

I noticed you forked the repo and had already seen the commits coming :-).

I haven't forseen expiring a comment by external source, but why not.

'Authorization' => "Basic {$this->credentials}",
]
]);
} catch (ClientException $exception) {
Copy link
Owner

@staabm staabm Dec 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean the issue with the given jira-key does not exist? If so we could report that for the comment (as it might be a typo) as a rule error

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to API's response message, 404 means

Issue does not exist or you do not have permission to see it

So I'd say we shouldn't assume it's an error to be reported.

$credentials = self::getCredentials($credentialsFilePath);

$this->credentials = base64_encode($credentials);
$this->client = new Client([
Copy link
Owner

@staabm staabm Dec 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we utilize some kind of caching and/or cache revalidation?

I remember a dedicated jira api client which supports it, so we could do similar stuff I think.

I don't say we neeed the jira api client but maybe we can take inspiration from it. I am also fine with adopting the jira api client.. I like the jira client, as it does not depend on a http client package

wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder that maybe phpstan's cache is enough? I don't know exactly how phpstan's cache works but I assume that if it's not invalidated altogether for some reason (e.g. configuration change), then the same comment won't be analyzed twice if it doesn't change. This means that the API call is done only once.

If we wanted to use cache for Jira requests in our extension how do you think it should work? Should we introduce some parameter that developers could adjust to their needs (e.g. keep the status in cache for 5 minutes)? I tried to quickly analyze the client you mentioned but I don't see how it caches API calls. Can you please give me a hint where to look?

I like my dev tools to use as few dependencies as possible to minimize risks of conflicts, etc. Maybe it would be a good idea to not depend on jira client nor on guzzle and use low-level ext-curl and ext-json functions to make these calls? This should be quite simple to do and there would be less things to worry during the maintenance of the extension. If we add support in the future for more issue trackers, then using some client dependency for each of them could lead to quite complex dependency graph which isn't necessarily required for such a simple task as fetching a status from APIs. What do you think?

Copy link
Owner

@staabm staabm Dec 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. As simple as possible would be great. Plain curl sounds good.

It seems I misremembered the cache thing with a client to a different api. I can't find it too :-)

extension.neon Outdated Show resolved Hide resolved
src/TodoByTicketRule.php Outdated Show resolved Hide resolved
src/TodoByTicketRule.php Show resolved Hide resolved
tests/data/example.php Outdated Show resolved Hide resolved
src/TodoByTicketRule.php Outdated Show resolved Hide resolved
@DannyvdSluijs
Copy link
Contributor

👀 This seems like an interesting development. I’m currently using Youtrack. So once this PR is merged (or about to) I could check if I can add support for Youtrack in a new PR.

@@ -57,24 +57,24 @@ private static function decodeAndValidateResponse(string $body): array
$data = json_decode($body, true);

if (!is_array($data) || !array_key_exists('fields', $data)) {
throw self::throwInvalidResponse();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

opened a phpstan issue on that one, as it should be detected as a error:

phpstan/phpstan#10344

@EmilMassey
Copy link
Contributor Author

I think, it's ready to be merged now unless anyone has some more suggestions or see potential issues. I ran it on some of my projects and it works well.

continue;
}

$ticketStatus = $this->fetcher->fetchTicketStatus($ticketKey);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wdyt about prevent duplicate requests to the api, for the same key used in several comments?

src/TodoByTicketRule.php Outdated Show resolved Hide resolved
@staabm
Copy link
Owner

staabm commented Dec 27, 2023

just a few nits. will merge after that

@staabm staabm merged commit 98d37d9 into staabm:main Dec 27, 2023
11 checks passed
@staabm
Copy link
Owner

staabm commented Dec 27, 2023

thank you - released in https://github.com/staabm/phpstan-todo-by/releases/tag/0.1.16

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants