2022-10-11 01:57:18 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Gitlab
|
|
|
|
module GithubImport
|
|
|
|
module Representation
|
|
|
|
class ProtectedBranch
|
|
|
|
include ToHash
|
|
|
|
include ExposeAttribute
|
|
|
|
|
|
|
|
attr_reader :attributes
|
|
|
|
|
2022-11-25 23:54:43 +05:30
|
|
|
expose_attribute :id, :allow_force_pushes, :required_conversation_resolution, :required_signatures,
|
2023-03-17 16:20:25 +05:30
|
|
|
:required_pull_request_reviews, :require_code_owner_reviews, :allowed_to_push_users
|
2022-10-11 01:57:18 +05:30
|
|
|
|
|
|
|
# Builds a Branch Protection info from a GitHub API response.
|
|
|
|
# Resource structure details:
|
|
|
|
# https://docs.github.com/en/rest/branches/branch-protection#get-branch-protection
|
2022-11-25 23:54:43 +05:30
|
|
|
# branch_protection - An instance of `Hash` containing the protection details.
|
2022-10-11 01:57:18 +05:30
|
|
|
def self.from_api_response(branch_protection, _additional_object_data = {})
|
2022-11-25 23:54:43 +05:30
|
|
|
branch_name = branch_protection[:url].match(%r{/branches/(\S{1,255})/protection$})[1]
|
2022-10-11 01:57:18 +05:30
|
|
|
|
2023-03-17 16:20:25 +05:30
|
|
|
allowed_to_push_users = branch_protection.dig(:required_pull_request_reviews,
|
|
|
|
:bypass_pull_request_allowances,
|
|
|
|
:users)
|
|
|
|
allowed_to_push_users &&= allowed_to_push_users.map do |u|
|
|
|
|
Representation::User.from_api_response(u)
|
|
|
|
end
|
2022-10-11 01:57:18 +05:30
|
|
|
hash = {
|
|
|
|
id: branch_name,
|
2022-11-25 23:54:43 +05:30
|
|
|
allow_force_pushes: branch_protection.dig(:allow_force_pushes, :enabled),
|
|
|
|
required_conversation_resolution: branch_protection.dig(:required_conversation_resolution, :enabled),
|
|
|
|
required_signatures: branch_protection.dig(:required_signatures, :enabled),
|
2023-01-13 00:05:48 +05:30
|
|
|
required_pull_request_reviews: branch_protection[:required_pull_request_reviews].present?,
|
|
|
|
require_code_owner_reviews: branch_protection.dig(:required_pull_request_reviews,
|
2023-03-17 16:20:25 +05:30
|
|
|
:require_code_owner_reviews).present?,
|
|
|
|
allowed_to_push_users: allowed_to_push_users.to_a
|
2022-10-11 01:57:18 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
new(hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Builds a new Protection using a Hash that was built from a JSON payload.
|
|
|
|
def self.from_json_hash(raw_hash)
|
2023-03-17 16:20:25 +05:30
|
|
|
hash = Representation.symbolize_hash(raw_hash)
|
|
|
|
|
|
|
|
hash[:allowed_to_push_users].map! do |u|
|
|
|
|
Representation::User.from_json_hash(u)
|
|
|
|
end
|
|
|
|
|
|
|
|
new(hash)
|
2022-10-11 01:57:18 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
# attributes - A Hash containing the raw Protection details. The keys of this
|
|
|
|
# Hash (and any nested hashes) must be symbols.
|
|
|
|
def initialize(attributes)
|
|
|
|
@attributes = attributes
|
|
|
|
end
|
|
|
|
|
|
|
|
def github_identifiers
|
|
|
|
{ id: id }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|