Skip to content

Cache users from Bitbucket Server

Madelein van Niekerk requested to merge 433008-cache-users into master

What does this MR do and why?

This is part of 2 MRs that addresses BitBucket Server PR mentions in comments map by... (#433008 - closed):

  1. Cache user usernames and emails from Bitbucket Server 👈 this MR
  2. Convert mentions on pull requests from Bitbucket Server: !139221 (merged)

This MR runs a pipeline right after the repository has been created which fetches users from Bitbucket Server with access to the project and stores every user's username and email in a cache.

  • UsersImporter is called from ImportUsersWorker if the feature flag is enabled for the project's creator
  • Adds a users method to BitbucketServer::Client which calls this API endpoint and returns them as BitbucketServer::Representation::User objects which extracts email and username.
  • Creates a BitbucketServer::UsersMapper which has two methods:
    • write_source_email_to_cache: writes to the cache with key = 'bitbucket_server/project/{project.id}/source/username/{username}' and value = email. The cache expires after 24 hours.
    • read_source_email_from_cache: fetches email cache entry for a given username
  • Steps through users and calls write_source_email_to_cache for each

NOTE: for a user to be returned via the API, they need to have at least read access to the project. If the project is public, it will only return users who are invited to the project. We fetch users in batches of 100 until the response is empty so that we don't risk overloading with thousands of users at once.

How to set up and validate locally

  1. Enable the feature flag
    Feature.enable(:bitbucket_server_convert_mentions_to_users)
  2. Setup bitbucket server by following these instructions
  3. Create another user at http://localhost:7990/admin/users with email john@gmail.com and username doe
  4. Import the project from bitbucket server
  5. Verify that there are cache entries containing the username and email:
     Gitlab::Cache::Import::Caching.with_redis { |redis| redis.keys.select { |k| k.match?(/.*bitbucket_server.*source\/username/) }.map { |k| {"#{k}": redis.get(k) } } }

MR acceptance checklist

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

Related to #433008 (closed)

Edited by Madelein van Niekerk

Merge request reports