Skip to content

Refactor deploy keys retrieval in autocomplete controller

What does this MR do and why?

This merge request refactors the retrieval of deploy keys in AutocompleteController by introducing a new finder DeployKeysFinder which can be later shared for other queries to the deploy_keys table in new endpoints or elsewhere.

Please note that:

  1. The issue suggests we also update Projects::DeployKeysController to use the new finder, but it is in fact looking up deploy_keys_projects which is the join table between deploy_keys and projects tables, therefore, I think there's it is irrelevant for this refactor. Moreover, the deploy key is being retrieved in this controller via the id as can be seen here.

  2. Similar to the above, updating the /projects/:id/deploy_keys endpoint in API::DeployKeys to use the new finder would have been feasible if the endpoint was not returning a DeployKeysProject entity upon success, not to say it is impossible work around this, but the way this API is structured favors the use of DeployKeysProject entities instead, and I doubt the finder should be concerned with how the data is structured, so we likely would have needed to add an intermediate step like below in order to not introduce any breaking changes for that API:

get ":id/deploy_keys" do
  keys = DeployKeysFinder.new(current_user, user_project).execute

  # turn `keys` here into the structure expected for `Entities::DeployKeysProject` which looks something like:
  #
  # [
  #   {
  #     deploy_key: {},
  #     can_push: true/false
  #   },
  #   {
  #     deploy_key: {},
  #     can_push: true/false
  #   }
  #   ...
  # ]

  present paginate(keys), with: Entities::DeployKeysProject
end

That's why, in the spirit of iteration, the focus here is only on introducing DeployKeysFinder which is nested under Autocomplete module. If this finder is to be used for other controllers or API endpoints later on, we could move it up to be under /app/finders directly, and update it accordingly.

Resolves #368527.

Raw SQL & Explain Output

Below is the raw SQL for the query generated by the new finder (project used here is https://gitlab.com/ahmed.hemdan/test-93591).

SELECT
    "keys".*
FROM
    "keys"
    INNER JOIN "deploy_keys_projects" ON "keys"."id" = "deploy_keys_projects"."deploy_key_id"
WHERE
    "keys"."type" = 'DeployKey'
    AND "deploy_keys_projects"."project_id" = 38460852
    AND "deploy_keys_projects"."can_push" = TRUE

And you can check the explain output for this SQL command here:

https://console.postgres.ai/gitlab/gitlab-production-tunnel-pg12/sessions/11408/commands/40780

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Ahmed Hemdan

Merge request reports