summaryrefslogtreecommitdiff
path: root/qa/qa/resource/bulk_import_group.rb
blob: a22529152e120af8bc91bda679ccf8191c25f41e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# frozen_string_literal: true

module QA
  module Resource
    class BulkImportGroup < Group
      attributes :source_group,
                 :destination_group,
                 :import_id

      attribute :access_token do
        api_client.personal_access_token
      end

      # In most cases we will want to set path the same as source group
      # but it can be set to a custom name as well when imported via API
      attribute :destination_group_path do
        source_group.path
      end
      # Can't define path as attribue since @path is set in base class initializer
      alias_method :path, :destination_group_path

      delegate :gitlab_address, to: 'QA::Runtime::Scenario'

      def fabricate_via_browser_ui!
        Page::Main::Menu.perform(&:go_to_create_group)

        Page::Group::New.perform do |group|
          group.switch_to_import_tab
          group.connect_gitlab_instance(gitlab_address, api_client.personal_access_token)
        end

        Page::Group::BulkImport.perform do |import_page|
          import_page.import_group(path, sandbox.path)
        end

        reload!
        visit!
      end

      def fabricate_via_api!
        resource_web_url(api_post)
      end

      def api_post_path
        '/bulk_imports'
      end

      def api_post_body
        {
          configuration: {
            url: gitlab_address,
            access_token: access_token
          },
          entities: [
            {
              source_type: 'group_entity',
              source_full_path: source_group.full_path,
              destination_name: destination_group_path,
              destination_namespace: sandbox.full_path
            }
          ]
        }
      end

      # Get import status
      #
      # @return [String]
      def import_status
        response = get(Runtime::API::Request.new(api_client, "/bulk_imports/#{import_id}").url)

        unless response.code == HTTP_STATUS_OK
          raise ResourceQueryError, "Could not get import status. Request returned (#{response.code}): `#{response}`."
        end

        parse_body(response)[:status]
      end

      # Get import details
      #
      # @return [Array]
      def import_details
        response = get(Runtime::API::Request.new(api_client, "/bulk_imports/#{import_id}/entities").url)

        parse_body(response)
      end

      private

      def transform_api_resource(api_resource)
        return api_resource if api_resource[:web_url]

        # override transformation only for /bulk_imports endpoint which doesn't have web_url in response and
        # ignore others so import_id is not overwritten incorrectly
        api_resource[:web_url] = "#{gitlab_address}/#{full_path}"
        api_resource[:import_id] = api_resource[:id]
        api_resource
      end
    end
  end
end