summaryrefslogtreecommitdiff
path: root/qa/qa/resource/project_imported_from_github.rb
blob: 214e8f517bbd2378d63d84b8ef7dea3dfbd721d5 (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
# frozen_string_literal: true

require 'octokit'

module QA
  module Resource
    class ProjectImportedFromGithub < Resource::Project
      attribute :github_repo_id do
        github_client.repository(github_repository_path).id
      end

      def fabricate!
        self.import = true

        Page::Main::Menu.perform(&:go_to_create_project)

        go_to_import_page

        Page::Project::Import::Github.perform do |import_page|
          import_page.add_personal_access_token(github_personal_access_token)
          import_page.import!(github_repository_path, name)
          import_page.go_to_project(name)
        end
      end

      def go_to_import_page
        Page::Project::New.perform do |project_page|
          project_page.click_import_project
          project_page.click_github_link
        end
      end

      def fabricate_via_api!
        super
      rescue ResourceURLMissingError
        "#{Runtime::Scenario.gitlab_address}/#{group.full_path}/#{name}"
      end

      def api_post_path
        '/import/github'
      end

      def api_trigger_mirror_pull_path
        "#{api_get_path}/mirror/pull"
      end

      def api_post_body
        {
          repo_id: github_repo_id,
          new_name: name,
          target_namespace: group.full_path,
          personal_access_token: github_personal_access_token,
          ci_cd_only: false
        }
      end

      def transform_api_resource(api_resource)
        api_resource
      end

      def trigger_project_mirror
        Runtime::Logger.info "Triggering pull mirror request"

        Support::Retrier.retry_until(max_attempts: 6, sleep_interval: 10) do
          response = post(request_url(api_trigger_mirror_pull_path), nil)

          Runtime::Logger.info "Mirror pull request response: #{response}"
          response.code == Support::Api::HTTP_STATUS_OK
        end
      end

      private

      # Github client
      #
      # @return [Octokit::Client]
      def github_client
        @github_client ||= Octokit::Client.new(access_token: github_personal_access_token)
      end
    end
  end
end