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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
|
# frozen_string_literal: true
module QA
module Resource
class MergeRequest < Issuable
include ApprovalConfiguration
attr_accessor :approval_rules,
:source_branch,
:target_new_branch,
:update_existing_file,
:assignee,
:milestone,
:labels,
:file_name,
:file_content,
:reviewer_ids
attr_writer :no_preparation,
:wait_for_merge,
:template
attributes :iid,
:title,
:description,
:merge_when_pipeline_succeeds,
:merge_status,
:state,
:reviewers
attribute :project do
Project.fabricate_via_api! do |resource|
resource.name = 'project-with-merge-request'
resource.initialize_with_readme = true
resource.api_client = api_client
end
end
attribute :target_branch do
project.default_branch
end
attribute :target do
Repository::Commit.fabricate_via_api! do |resource|
resource.project = project
resource.api_client = api_client
resource.commit_message = 'This is a test commit'
resource.add_files([{ 'file_path': "file-#{SecureRandom.hex(8)}.txt", 'content': 'MR init' }])
resource.branch = target_branch
resource.start_branch = project.default_branch if target_branch != project.default_branch
end
end
attribute :source do
Repository::Commit.fabricate_via_api! do |resource|
resource.project = project
resource.api_client = api_client
resource.commit_message = 'This is a test commit'
resource.branch = source_branch
resource.start_branch = target_branch
files = [{ 'file_path': file_name, 'content': file_content }]
update_existing_file ? resource.update_files(files) : resource.add_files(files)
end
end
def initialize
@approval_rules = nil
@title = 'QA test - merge request'
@description = 'This is a test merge request'
@source_branch = "qa-test-feature-#{SecureRandom.hex(8)}"
@assignee = nil
@milestone = nil
@labels = []
@file_name = "added_file-#{SecureRandom.hex(8)}.txt"
@file_content = "File Added"
@target_new_branch = true
@update_existing_file = false
@no_preparation = false
@wait_for_merge = true
end
def fabricate!
return fabricate_large_merge_request if large_setup?
populate_target_and_source_if_required
project.visit!
Flow::MergeRequest.create_new(source_branch: source_branch)
Page::MergeRequest::New.perform do |new_page|
new_page.fill_title(@title)
new_page.choose_template(@template) if @template
new_page.fill_description(@description) if @description && !@template
new_page.choose_milestone(@milestone) if @milestone
new_page.assign_to_me if @assignee == 'me'
labels.each do |label|
new_page.select_label(label)
end
new_page.add_approval_rules(approval_rules) if approval_rules
new_page.create_merge_request
end
end
def fabricate_via_api!
return fabricate_large_merge_request if large_setup?
resource_web_url(api_get)
rescue ResourceNotFoundError, NoValueError # rescue if iid not populated
populate_target_and_source_if_required
super
end
def api_merge_path
"/projects/#{project.id}/merge_requests/#{iid}/merge"
end
def api_get_path
"/projects/#{project.id}/merge_requests/#{iid}"
end
def api_post_path
"/projects/#{project.id}/merge_requests"
end
def api_reviewers_path
"#{api_get_path}/reviewers"
end
def api_approve_path
"#{api_get_path}/approve"
end
def api_post_body
{
description: description,
source_branch: source_branch,
target_branch: target_branch,
title: title,
reviewer_ids: reviewer_ids
}
end
def merge_via_api!
Support::Waiter.wait_until(sleep_interval: 1) do
QA::Runtime::Logger.debug("Waiting until merge request with id '#{iid}' can be merged")
reload!.merge_status == 'can_be_merged'
end
Support::Retrier.retry_on_exception do
response = put(Runtime::API::Request.new(api_client, api_merge_path).url)
unless response.code == HTTP_STATUS_OK
raise ResourceUpdateFailedError, "Could not merge. Request returned (#{response.code}): `#{response}`."
end
result = parse_body(response)
project.wait_for_merge(result[:title]) if @wait_for_merge
result
end
end
# Approve merge request
#
# Due to internal implementation of api client, project needs to have
# setting 'Prevent approval by author' set to false since we use same user that created merge request which
# is set through approval configuration
#
# @return [void]
def approve
api_post_to(api_approve_path, {})
end
def fabricate_large_merge_request
@project = Resource::ImportProject.fabricate_via_browser_ui!
# Setting the name here, since otherwise some tests will look for an existing file in
# the proejct without ever knowing what is in it.
@file_name = "added_file-00000000.txt"
@source_branch = "large_merge_request"
visit("#{project.web_url}/-/merge_requests/1")
current_url
end
# Return subset of fields for comparing merge requests
#
# @return [Hash]
def comparable
reload! if api_response.nil?
api_resource.except(
:id,
:web_url,
:project_id,
:source_project_id,
:target_project_id,
:merge_status,
# these can differ depending on user fetching mr
:user,
:subscribed,
:first_contribution
).merge({ references: api_resource[:references].except(:full) })
end
private
def large_setup?
Runtime::Scenario.large_setup?
rescue ArgumentError
false
end
def transform_api_resource(api_resource)
raise ResourceNotFoundError if api_resource.blank?
super(api_resource)
end
# Create source and target and commits if necessary
#
# @return [void]
def populate_target_and_source_if_required
return if @no_preparation
populate(:target) if create_target?
populate(:source)
end
# Check if target needs to be created
#
# Return false if project was already initialized and mr target is default branch
# Return false if target_new_branch is explicitly set to false
#
# @return [Boolean]
def create_target?
!(project.initialize_with_readme && target_branch == project.default_branch) && target_new_branch
end
end
end
end
|