summaryrefslogtreecommitdiff
path: root/qa/qa/specs/features/api/1_manage/import/import_github_repo_spec.rb
blob: fa64489fb8312545b035c1ea280bbf70bb43fd62 (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# frozen_string_literal: true

module QA
  RSpec.describe 'Manage', product_group: :import_and_integrate,
    quarantine: {
      issue: 'https://gitlab.com/gitlab-org/gitlab/-/issues/407297',
      type: :investigating
    } do
    describe 'GitHub import' do
      include_context 'with github import'

      before do
        QA::Support::Helpers::ImportSource.enable('github')
      end

      context 'when imported via api' do
        it 'imports project', testcase: 'https://gitlab.com/gitlab-org/gitlab/-/quality/test_cases/347670' do
          expect_project_import_finished_successfully

          aggregate_failures do
            verify_status_data
            verify_repository_import
            verify_protected_branches_import
            verify_commits_import
            verify_labels_import
            verify_issues_import
            verify_milestones_import
            verify_wikis_import
            verify_merge_requests_import
            verify_release_import
          end
        end

        def verify_status_data
          stats = imported_project.project_import_status.dig(:stats, :imported)
          expect(stats).to eq(
            issue: 1,
            issue_event: 16,
            pull_request: 1,
            pull_request_review: 2,
            pull_request_review_request: 1,
            diff_note: 1,
            label: 9,
            milestone: 1,
            note: 3,
            release: 1,
            protected_branch: 2
          )
        end

        def verify_repository_import
          expect(imported_project.reload!.description).to eq('Project for github import test')
          expect(imported_project.api_response[:import_error]).to be_nil
        end

        def verify_protected_branches_import
          imported_branches = imported_project.protected_branches.map do |branch|
            branch.slice(:name, :allow_force_push)
          end
          actual_branches = [
            {
              name: 'main',
              allow_force_push: false
            },
            {
              name: 'release',
              allow_force_push: true
            }
          ]

          expect(imported_branches).to match_array(actual_branches)
        end

        def verify_commits_import
          expect(imported_project.commits.length).to eq(2)
        end

        def verify_labels_import
          labels = imported_project.labels.map { |label| label.slice(:name, :color) }

          expect(labels).to include(
            { name: 'bug', color: '#d73a4a' },
            { name: 'documentation', color: '#0075ca' },
            { name: 'duplicate', color: '#cfd3d7' },
            { name: 'enhancement', color: '#a2eeef' },
            { name: 'good first issue', color: '#7057ff' },
            { name: 'help wanted', color: '#008672' },
            { name: 'invalid', color: '#e4e669' },
            { name: 'question', color: '#d876e3' },
            { name: 'wontfix', color: '#ffffff' }
          )
        end

        def verify_milestones_import
          milestones = imported_project.milestones

          expect(milestones.length).to eq(1)
          expect(milestones.first).to include(title: '0.0.1', description: nil, state: 'active')
        end

        def verify_wikis_import
          wikis = imported_project.wikis

          expect(wikis.length).to eq(1)
          expect(wikis.first).to include(title: 'Home', format: 'markdown')
        end

        def verify_issues_import
          issues = imported_project.issues
          issue = Resource::Issue.init do |resource|
            resource.project = imported_project
            resource.iid = issues.first[:iid]
            resource.api_client = user_api_client
          end.reload!
          comments, events = fetch_events_and_comments(issue)

          expect(issues.length).to eq(1)
          expect(issue.api_resource).to include(
            title: 'Test issue',
            description: "*Created by: gitlab-qa-github*\n\nTest issue description",
            labels: ['good first issue', 'help wanted', 'question']
          )
          expect(comments).to match_array(
            [
              "*Created by: gitlab-qa-github*\n\nSome test comment",
              "*Created by: gitlab-qa-github*\n\nAnother test comment"
            ]
          )
          expect(events).to match_array(
            [
              { name: "add_label", label: "question" },
              { name: "add_label", label: "good first issue" },
              { name: "add_label", label: "help wanted" },
              { name: "add_milestone", label: "0.0.1" },
              { name: "closed" },
              { name: "reopened" }
            ]
          )
        end

        def verify_merge_requests_import
          merge_requests = imported_project.merge_requests
          merge_request = Resource::MergeRequest.init do |mr|
            mr.project = imported_project
            mr.iid = merge_requests.first[:iid]
            mr.api_client = user_api_client
          end.reload!
          comments, events = fetch_events_and_comments(merge_request)

          expect(merge_requests.length).to eq(1)
          expect(merge_request.api_resource).to include(
            title: 'Test pull request',
            state: 'opened',
            target_branch: 'main',
            source_branch: 'gitlab-qa-github-patch-1',
            labels: %w[documentation],
            description: "*Created by: gitlab-qa-github*\n\nTest pull request body"
          )
          expect(comments).to match_array(
            [
              "*Created by: gitlab-qa-github*\n\n**Review:** Commented\n\nGood but needs some improvement",
              "*Created by: gitlab-qa-github*\n\n```suggestion:-0+0\nProject for GitHub import test to GitLab\r\n```",
              "*Created by: gitlab-qa-github*\n\nSome test PR comment",
              "*Created by: gitlab-qa*\n\n**Review:** Approved",
              "assigned to @#{user.username}",
              "requested review from @#{user.username}"
            ]
          )
          expect(events).to match_array(
            [
              { name: "add_label", label: "documentation" },
              { name: "add_milestone", label: "0.0.1" }
            ]
          )
          # TODO: reenable once https://gitlab.com/gitlab-org/gitlab/-/issues/386714 fixed
          # currently this doesn't work as expected if reviewer is not matched by public email
          # event for assigning approver is created with reviewer being user doing import but mr actually doesn't
          # contain reviewers or the approved state
          #
          # reviews = merge_request.reviews.map do |review|
          #   {
          #     id: review.dig(:user, :id),
          #     username: review.dig(:user, :username),
          #     state: review[:state]
          #   }
          # end
          # expect(reviews).to eq([{ id: user.id, username: user.username, state: "approved" }])
        end

        def verify_release_import
          releases = imported_project.releases

          expect(releases.length).to eq(1)
          expect(releases.first).to include(
            tag_name: "0.0.1",
            name: "0.0.1",
            description: "Initial release",
            created_at: "2022-03-07T07:59:22.000Z",
            released_at: "2022-03-07T08:02:09.000Z"
          )
        end

        # Fetch events and comments from issue or mr
        #
        # @param [QA::Resource::Issuable] issuable
        # @return [Array]
        def fetch_events_and_comments(issuable)
          comments = issuable.comments.pluck(:body)
          events = [
            *issuable.label_events.map { |e| { name: "#{e[:action]}_label", label: e.dig(:label, :name) } },
            *issuable.state_events.map { |e| { name: e[:state] } },
            *issuable.milestone_events.map { |e| { name: "#{e[:action]}_milestone", label: e.dig(:milestone, :title) } }
          ]

          [comments, events]
        end
      end
    end
  end
end