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

Autodetect what language a query is for #915

Merged
merged 3 commits into from
Aug 4, 2021

Conversation

shati-patel
Copy link
Contributor

@shati-patel shati-patel commented Jul 26, 2021

Adds a findLanguage function that uses codeql resolve queries --format bylanguage to detect a query's language. This might not work for some queries, so I've added a manual selection as a fallback.

This is used in two places:

  • When running a query on multiple databases (we now only show the databases that correspond to the query language)
  • When running a remote query (we can now autodetect a language when it isn't specified in the .repositories config. This config file is going to be replaced by a different UI soon.)

Question: Does this need tests? I wasn't sure where/how to go about that!

Checklist

  • CHANGELOG.md has been updated to incorporate all user visible changes made by this pull request.
    • Only the "multiple DBs" part is user-facing for now
  • Issues have been created for any UI or other user-facing changes made by this pull request.
  • @github/docs-content-codeql has been cc'd in all issues for UI or other user-facing changes made by this pull request.

@shati-patel shati-patel marked this pull request as ready for review July 26, 2021 16:32
@shati-patel shati-patel requested a review from a team as a code owner July 26, 2021 16:32
@shati-patel shati-patel marked this pull request as draft July 26, 2021 17:46
Copy link
Contributor

@aeisenberg aeisenberg left a comment

Choose a reason for hiding this comment

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

This looks right to me!

extensions/ql-vscode/src/cli.ts Outdated Show resolved Hide resolved
@shati-patel shati-patel changed the title [WIP] Attempt to autodetect what language a query is for Autodetect what language a query is for Jul 28, 2021
@shati-patel shati-patel marked this pull request as ready for review July 28, 2021 14:53
Copy link
Contributor

@edoardopirovano edoardopirovano left a comment

Choose a reason for hiding this comment

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

Mostly looks good apart from a minor comment inline. I'll hold off on approving it, though, since we paired on this for a bit so it should probably have another set of eyes.

extensions/ql-vscode/src/run-remote-query.ts Show resolved Hide resolved
Copy link
Contributor

@adityasharad adityasharad left a comment

Choose a reason for hiding this comment

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

Looks good! I think the code is mostly ready to go, with one outstanding suggestion from Edoardo about return types, and one from me about logging.

I've added some suggestions about tests, but I think those are fine to add later, since it will require some upfront work to set up tests for this part of the code.

Have we been adding changelog entries for this feature?

extensions/ql-vscode/src/run-remote-query.ts Outdated Show resolved Hide resolved
extensions/ql-vscode/src/run-remote-query.ts Outdated Show resolved Hide resolved
* Resolves the language for a query.
* @param queryUri The URI of the query
*/
async resolveQueryByLanguage(workspaces: string[], queryUri: Uri): Promise<QueryInfoByLanguage> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Since you asked about tests:

  • extensions/ql-vscode/src/vscode-tests/cli-integration/run-cli.test.ts is a good place to add integration tests for the resolveQueryByLanguage and resolveLanguages functions.
  • You can use the existing tests as an example.
  • resolveLanguages is probably easier to write a test for because there's no query as input.
  • resolveQueryByLanguage will require a test query: there are some examples in the data folders, but as far as I can tell none of them depend on an actual language. So you may need to add one, or save this test for later.

* Finds the language that a query targets.
* If it can't be autodetected, prompt the user to specify the language manually.
*/
export async function findLanguage(
Copy link
Contributor

Choose a reason for hiding this comment

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

Since you asked about tests: I agree that having tests for this function would be handy, and give you confidence in its behaviour. However we don't yet have a test module for run-remote-query due to the experimental nature of the entire feature, so adding a test requires some upfront work. (In future it will be easier :) )

I'll record some pointers here about how you could create those tests, but I'm not expecting you to do it in this PR. We can look at test coverage together after this PR.

  • For integration tests (which use a real query and actually run the underlying commands), copy extensions/ql-vscode/src/vscode-tests/cli-integration/queries.test.ts as a template. I think it would make sense to have:
    • one test function that runs findLanguage on a query with a real language dependency and identifies the language
    • one test function that runs it on a dummy query and returns undefined
    • any other cases you can think of
  • For unit tests (which don't run the actual commands, but simulate the underlying behaviour), use extensions/ql-vscode/src/vscode-tests/no-workspace/query-results.test.ts as a template. Those tests mock the behaviour of the CLI server.

Also use autodection in relevant places
- When running on multiple databases
- When running a remote query
- Tweak return types + logging
- Update changelog
@shati-patel
Copy link
Contributor Author

Thank you for the reviews ✨ I've addressed the main comments in 83c68ec and added a minimal changelog entry that covers the user-facing part of this.

I'll look at adding tests in a separate PR!

Copy link
Contributor

@edoardopirovano edoardopirovano left a comment

Choose a reason for hiding this comment

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

A couple more minor comments, but otherwise looks good!

return;
}
}
const quickPickItems = filteredDBs.map<DatabaseQuickPickItem>(dbItem => (
Copy link
Contributor

Choose a reason for hiding this comment

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

Note that if the user has no databases and doesn't pick a language when prompted, we try to display this prompt with nothing in it. There should probably be an extra check to display an error in this case and return, similarly to what happens with the filtered list above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Woops, good point! Thanks 😄

const repositories = config.repositories;

if (!language) {
return;
Copy link
Contributor

Choose a reason for hiding this comment

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

I would add a brief comment stating that we don't need an error message here because findLanguage already displays one.

Copy link
Contributor

@edoardopirovano edoardopirovano 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 addressing all my comments, this LGTM 👍🏼

@shati-patel shati-patel merged commit 2bfcd11 into github:main Aug 4, 2021
@shati-patel shati-patel deleted the language-detection branch August 4, 2021 19:00
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.

4 participants