Class: Gitlab::GithubImport::Representation::ProtectedBranch

Inherits:
Object
  • Object
show all
Includes:
ExposeAttribute, ToHash
Defined in:
lib/gitlab/github_import/representation/protected_branch.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ExposeAttribute

#[]

Methods included from ToHash

#convert_value_for_to_hash, #to_hash

Constructor Details

#initialize(attributes) ⇒ ProtectedBranch

attributes - A Hash containing the raw Protection details. The keys of this

Hash (and any nested hashes) must be symbols.


55
56
57
# File 'lib/gitlab/github_import/representation/protected_branch.rb', line 55

def initialize(attributes)
  @attributes = attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



10
11
12
# File 'lib/gitlab/github_import/representation/protected_branch.rb', line 10

def attributes
  @attributes
end

Class Method Details

.from_api_response(branch_protection, _additional_object_data = {}) ⇒ Object

Builds a Branch Protection info from a GitHub API response. Resource structure details: docs.github.com/en/rest/branches/branch-protection#get-branch-protection branch_protection - An instance of ‘Hash` containing the protection details.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/gitlab/github_import/representation/protected_branch.rb', line 19

def self.from_api_response(branch_protection, _additional_object_data = {})
  branch_name = branch_protection[:url].match(%r{/branches/(\S{1,255})/protection$})[1]

  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
  hash = {
    id: branch_name,
    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),
    required_pull_request_reviews: branch_protection[:required_pull_request_reviews].present?,
    require_code_owner_reviews: branch_protection.dig(:required_pull_request_reviews,
                                                      :require_code_owner_reviews).present?,
    allowed_to_push_users: allowed_to_push_users.to_a
  }

  new(hash)
end

.from_json_hash(raw_hash) ⇒ Object

Builds a new Protection using a Hash that was built from a JSON payload.



43
44
45
46
47
48
49
50
51
# File 'lib/gitlab/github_import/representation/protected_branch.rb', line 43

def self.from_json_hash(raw_hash)
  hash = Representation.symbolize_hash(raw_hash)

  hash[:allowed_to_push_users].map! do |u|
    Representation::User.from_json_hash(u)
  end

  new(hash)
end

Instance Method Details

#github_identifiersObject



59
60
61
# File 'lib/gitlab/github_import/representation/protected_branch.rb', line 59

def github_identifiers
  { id: id }
end