Commit 2560d75c authored by Mark Lapierre's avatar Mark Lapierre Committed by Andrejs Cunskis

Update existing protected branch

This sets the option to require code owner approval on an existing
protected branch instead of deleting it and creating it again.
parent cb8a5b23
......@@ -61,6 +61,8 @@ module QA
end
def fabricate_via_api!
resource_web_url(api_get)
rescue ResourceNotFoundError
populate_new_branch_if_required
super
......@@ -75,7 +77,11 @@ module QA
end
def api_delete_path
"/projects/#{project.id}/protected_branches/#{branch_name}"
api_get_path
end
def api_put_path
api_get_path
end
def api_post_path
......@@ -107,6 +113,16 @@ module QA
# this particular resource does not expose a web_url property
end
def set_require_code_owner_approval(require = true)
response = patch(Runtime::API::Request.new(api_client, api_put_path).url, { code_owner_approval_required: require })
return if response.code == HTTP_STATUS_OK
raise(
ResourceUpdateFailedError,
"Could not update code_owner_approval_required to #{require}. Request returned (#{response.code}): `#{response}`."
)
end
class Roles
NO_ONE = { description: 'No one', access_level: 0 }.freeze
DEVS_AND_MAINTAINERS = { description: 'Developers + Maintainers', access_level: 30 }.freeze
......
......@@ -44,6 +44,18 @@ module QA
end
end
def patch(url, payload = nil)
with_retry_on_too_many_requests do
RestClient::Request.execute(
method: :patch,
url: url,
payload: payload,
verify_ssl: false)
rescue RestClient::ExceptionWithResponse => e
return_response_or_raise(e)
end
end
def put(url, payload = nil)
with_retry_on_too_many_requests do
RestClient::Request.execute(
......
......@@ -31,19 +31,13 @@ module QA
end
# Require approval from code owners on the default branch
# The default branch is already protected, and we can't update a protected branch via the API (yet)
# so we unprotect it first and then protect it again with the desired parameters
Resource::ProtectedBranch.unprotect_via_api! do |protected_branch|
protected_branch.project = project
protected_branch.branch_name = project.default_branch
end
Resource::ProtectedBranch.fabricate_via_api! do |protected_branch|
protected_branch.project = project
protected_branch.branch_name = project.default_branch
protected_branch.new_branch = false
protected_branch.require_code_owner_approval = true
protected_branch = Resource::ProtectedBranch.fabricate_via_api! do |branch|
branch.project = project
branch.branch_name = project.default_branch
branch.new_branch = false
branch.require_code_owner_approval = true
end
protected_branch.set_require_code_owner_approval
# Push a change to the file with a CODEOWNERS rule
Resource::Repository::Push.fabricate! do |push|
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment