Handle invalid URI exception in bitbucket import service

What does this MR do and why?

This merge request is to fix this bug: #377340 (closed)

While importing a project from bitbucket, if the URI is invalid the server responds with a 500 error. It looks like the corresponding network error code is not included in the following code:

Logs:

Kibana: https://log.gprd.gitlab.net/goto/075bcf70-7d4b-11ed-85ed-e7557b0a598c

json.exception.backtrace

lib/gitlab/patch/uri.rb:10:in `parse', lib/gitlab/http.rb:54:in `perform_request', lib/bitbucket_server/connection.rb:33:in `get', lib/bitbucket_server/client.rb:22:in `repo', app/services/import/bitbucket_server_service.rb:50:in `repo', app/services/import/bitbucket_server_service.rb:16:in `execute', lib/api/import_bitbucket_server.rb:46:in `block in <class:ImportBitbucketServer>', ee/lib/gitlab/middleware/ip_restrictor.rb:14:in `block in call', ee/lib/gitlab/ip_address_state.rb:10:in `with', ee/lib/gitlab/middleware/ip_restrictor.rb:13:in `call', lib/api/api_guard.rb:215:in `call', ee/lib/omni_auth/strategies/group_saml.rb:41:in `other_phase', lib/gitlab/metrics/elasticsearch_rack_middleware.rb:16:in `call', lib/gitlab/middleware/memory_report.rb:13:in `call', lib/gitlab/middleware/speedscope.rb:13:in `call', lib/gitlab/database/load_balancing/rack_middleware.rb:23:in `call', lib/gitlab/middleware/rails_queue_duration.rb:33:in `call', lib/gitlab/metrics/rack_middleware.rb:16:in `block in call', lib/gitlab/metrics/web_transaction.rb:46:in `run', lib/gitlab/metrics/rack_middleware.rb:16:in `call', lib/gitlab/jira/middleware.rb:19:in `call', lib/gitlab/middleware/go.rb:20:in `call', lib/gitlab/etag_caching/middleware.rb:21:in `call', lib/gitlab/middleware/query_analyzer.rb:11:in `block in call', lib/gitlab/database/query_analyzer.rb:37:in `within', lib/gitlab/middleware/query_analyzer.rb:11:in `call', lib/gitlab/middleware/multipart.rb:173:in `call', lib/gitlab/middleware/read_only/controller.rb:50:in `call', lib/gitlab/middleware/read_only.rb:18:in `call', lib/gitlab/middleware/same_site_cookies.rb:27:in `call', lib/gitlab/middleware/handle_malformed_strings.rb:21:in `call', lib/gitlab/middleware/basic_health_check.rb:25:in `call', lib/gitlab/middleware/handle_ip_spoof_attack_error.rb:25:in `call', lib/gitlab/middleware/request_context.rb:21:in `call', lib/gitlab/middleware/webhook_recursion_detection.rb:15:in `call', config/initializers/fix_local_cache_middleware.rb:11:in `call', lib/gitlab/middleware/compressed_json.rb:37:in `call', lib/gitlab/middleware/rack_multipart_tempfile_factory.rb:19:in `call', lib/gitlab/middleware/sidekiq_web_static.rb:20:in `call', lib/gitlab/metrics/requests_rack_middleware.rb:77:in `call', lib/gitlab/middleware/release_env.rb:13:in `call'
    NETWORK_ERRORS = [
      SocketError,
      OpenSSL::SSL::SSLError,
      Errno::ECONNRESET,
      Errno::ECONNREFUSED,
      Errno::EHOSTUNREACH,
      Net::OpenTimeout,
      Net::ReadTimeout,
      Gitlab::HTTP::BlockedUrlError
    ].freeze

Adding URI::InvalidURIError, to that list of network error codes will handle the exception gracefully.

Screenshots or screen recordings

Before:

 curl --request POST \
  --url "https://gdk.test:3443/api/v4/import/bitbucket_server" \
  --header "content-type: application/json" \
  --header "PRIVATE-TOKEN: TOKEN" \
  --data '{
    "bitbucket_server_url": "https://bitbucket.org",
    "bitbucket_server_username": "USERNAME",
    "personal_access_token": "TOKEN",
    "bitbucket_server_project": "EXAMPLE",
    "bitbucket_server_repo": "my repo"
}'


{"message":"500 Internal Server Error"}%

After:


 curl --request POST \
  --url "https://gdk.test:3443/api/v4/import/bitbucket_server" \
  --header "content-type: application/json" \
  --header "PRIVATE-TOKEN: TOKEN" \
  --data '{
    "bitbucket_server_url": "https://bitbucket.org",
    "bitbucket_server_username": "USERNAME",
    "personal_access_token": "TOKEN",
    "bitbucket_server_project": "EXAMPLE",
    "bitbucket_server_repo": "my repo"
}'


{"message":{"error":"Import failed due to a BitBucket Server error: bad URI(is not URI?): \"https://bitbucket.org/rest/api/1.0/projects/TES/repos/my repo\""}}%

Screenshots are required for UI changes, and strongly recommended for all other merge requests.

How to set up and validate locally

  1. Checkout the feature branch branch
  2. Import a project from bitbucket server using the API - docs: https://docs.gitlab.com/ee/api/import.html#import-repository-from-bitbucket-server.
  3. Notice that with an invalid URI the server responds with a 4XX instead of 500

Numbered steps to set up and validate the change are strongly suggested.

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 sameer shaik

Merge request reports

Loading