Class: Fastlane::Actions::SetBranchProtectionAction

Inherits:
Action
  • Object
show all
Defined in:
lib/fastlane/plugin/wpmreleasetoolkit/actions/common/set_branch_protection_action.rb

Class Method Summary collapse

Class Method Details

.authorsObject



133
134
135
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/set_branch_protection_action.rb', line 133

def self.authors
  ['Automattic']
end

.available_optionsObject



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
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/set_branch_protection_action.rb', line 83

def self.available_options
  [
    FastlaneCore::ConfigItem.new(key: :repository,
                                 env_name: 'GHHELPER_REPOSITORY',
                                 description: 'The slug of the GH repository on which we work',
                                 optional: false,
                                 type: String),
    # NOTE: GitHub branch protection API doesn't allow wildcard characters for the branch parameter
    FastlaneCore::ConfigItem.new(key: :branch,
                                 env_name: 'GHHELPER_BRANCH',
                                 description: 'The branch to protect',
                                 optional: false,
                                 type: String),
    FastlaneCore::ConfigItem.new(key: :keep_existing_settings_unchanged,
                                 description: 'If set to true, will only change the settings that are explicitly provided to the action, ' \
                                 + 'while keeping the values of other existing protection settings (if any) unchanged. If false, it will ' \
                                 + 'discard any existing branch protection setting if any before setting just the ones provided ' \
                                 + '(and leaving the rest with default GitHub values)',
                                 default_value: true,
                                 type: Boolean),
    FastlaneCore::ConfigItem.new(key: :required_ci_checks,
                                 description: 'If provided, specifies the list of CI status checks to mark as required. If not provided (nil), will keep existing ones',
                                 optional: true,
                                 default_value: nil,
                                 type: Array),
    FastlaneCore::ConfigItem.new(key: :required_approving_review_count,
                                 description: 'If not nil, change the number of approving reviews required to merge the PR. ' \
                                 + 'Acceptable values are `nil` (do not change), 0 (disable) or a number between 1–6',
                                 optional: true,
                                 default_value: nil,
                                 type: Integer),
    FastlaneCore::ConfigItem.new(key: :enforce_admins,
                                 description: 'If provided, will update the setting of whether admins can bypass restrictions (false) or not (true)',
                                 optional: true,
                                 default_value: nil,
                                 type: Boolean),
    FastlaneCore::ConfigItem.new(key: :allow_force_pushes,
                                 description: 'If provided, will update the setting of whether to allow force pushes on the branch',
                                 optional: true,
                                 default_value: nil,
                                 type: Boolean),
    FastlaneCore::ConfigItem.new(key: :lock_branch,
                                 description: 'If provided, will update the locked (aka readonly) state of the branch',
                                 optional: true,
                                 default_value: nil,
                                 type: Boolean),
    Fastlane::Helper::GithubHelper.github_token_config_item,
  ]
end

.descriptionObject



71
72
73
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/set_branch_protection_action.rb', line 71

def self.description
  'Sets the protection state for the specified branch'
end

.detailsObject



75
76
77
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/set_branch_protection_action.rb', line 75

def self.details
  description
end

.is_supported?(platform) ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/set_branch_protection_action.rb', line 137

def self.is_supported?(platform)
  true
end

.return_valueObject



79
80
81
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/set_branch_protection_action.rb', line 79

def self.return_value
  'The hash corresponding to the response returned by the API request, and containing the applied protection settings'
end

.run(params) ⇒ Object



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
# File 'lib/fastlane/plugin/wpmreleasetoolkit/actions/common/set_branch_protection_action.rb', line 7

def self.run(params)
  repository = params[:repository]
  branch_name = params[:branch]
  github_helper = Fastlane::Helper::GithubHelper.new(github_token: params[:github_token])

  settings = if params[:keep_existing_settings_unchanged]
               Fastlane::Helper::GithubHelper.branch_protection_api_response_to_normalized_hash(
                 github_helper.get_branch_protection(repository: repository, branch: branch_name)
               )
             else
               {}
             end

  # `required_status_checks` field — only override existing `checks` subfield if param provided
  unless params[:required_ci_checks].nil?
    if params[:required_ci_checks].empty?
      settings[:required_status_checks] = nil # explicitly completely delete existing check requirement
    else
      settings[:required_status_checks] ||= { strict: false }
      settings[:required_status_checks][:checks] = params[:required_ci_checks].map { |ctx| { context: ctx } }
    end
  end

  # `enforce_admins` field — only override existing value if param provided
  if params[:enforce_admins].nil?
    settings[:enforce_admins] ||= nil # parameter is required to be provided, even if nil (aka false) value
  else
    settings[:enforce_admins] = params[:enforce_admins]
  end

  # `required_pull_request_reviews` field — only override `required_approving_review_count` subfield if param provided
  settings[:required_pull_request_reviews] ||= {
    dismiss_stale_reviews: false,
    require_code_owner_reviews: false
  }
  unless params[:required_approving_review_count].nil?
    settings[:required_pull_request_reviews][:required_approving_review_count] = params[:required_approving_review_count]
  end

  # `restrictions` field
  settings[:restrictions] ||= { users: [], teams: [] }

  # `allow_force_pushes` field — only override existing value if param provided
  unless params[:allow_force_pushes].nil?
    settings[:allow_force_pushes] = params[:allow_force_pushes]
  end

  # `lock_branch` field — only override existing value if param provided
  unless params[:lock_branch].nil?
    settings[:lock_branch] = params[:lock_branch]
  end

  # API Call - See https://docs.github.com/en/rest/branches/branch-protection#update-branch-protection
  response = github_helper.set_branch_protection(
    repository: repository,
    branch: branch_name,
    **settings
  )

  Fastlane::Helper::GithubHelper.branch_protection_api_response_to_normalized_hash(response)
rescue Octokit::NotFound => e
  UI.user_error!("Branch `#{branch_name}` of repository `#{repository}` was not found.\n#{e.message}")
end