# frozen_string_literal: true require_relative "base" module Neetob class CLI module Github class ProtectBranch < Base attr_accessor :branch_name, :required_rules_json_file_path, :apps, :sandbox, :all_neeto_repos def initialize(branch_name, apps, required_rules_json_file_path = "", sandbox = false, all_neeto_repos = false) super() @branch_name = branch_name @required_rules_json_file_path = required_rules_json_file_path @apps = apps @sandbox = sandbox @all_neeto_repos = all_neeto_repos end def run check_for_apps_and_all_neeto_repos_option(apps, all_neeto_repos) matching_apps = find_all_matching_apps(apps, :github, sandbox, false, all_neeto_repos) inform_about_default_rules_file matching_apps.each do |app| ui.info("\n Working on \"#{app}\" repo") ui.info(" Updating \"#{branch_name}\" branch protection rules") rules = read_json_file(required_rules_json_file_path || default_rules_file_path) rules_with_symbol_keys = rules.transform_keys(&:to_sym) client.protect_branch(app, branch_name, rules_with_symbol_keys) ui.success("Branch protection rules updated successfully") end end private def default_rules_file_path File.expand_path("../../../../data/branch-protection-rules.json", __dir__) end def inform_about_default_rules_file if required_rules_json_file_path.nil? ui.info("Updating protection rules from the \"neetob/data/branch-protection-rules.json\" file") end end end end end end