Class: GitlabBranchRename::Operation
- Inherits:
-
Object
- Object
- GitlabBranchRename::Operation
- Defined in:
- lib/gitlab_branch_rename/operation.rb
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(configuration, logger) ⇒ Operation
constructor
A new instance of Operation.
Constructor Details
#initialize(configuration, logger) ⇒ Operation
Returns a new instance of Operation.
5 6 7 8 |
# File 'lib/gitlab_branch_rename/operation.rb', line 5 def initialize(configuration, logger) @configuration = configuration @logger = logger end |
Instance Method Details
#execute ⇒ Object
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 70 71 72 73 74 75 76 77 78 79 80 81 82 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/gitlab_branch_rename/operation.rb', line 10 def execute projects = Gitlab.projects(min_access_level: @configuration.minimum_access_level, owned: true, per_page: 5) projects.auto_paginate do |project| @logger.info("Evaluating #{project.path_with_namespace}") # Verify this project meets criteria for renaming unless @configuration.has_visibility.include?(project.visibility) @logger.debug("Project has visibility #{project.visibility}, skipping") next end if project.archived @logger.debug("Project is archived") next end # Feature: Skip forks? # Skip projects you don"t have proper permission to change # Verify it meets your criteria for renaming default branch if @configuration.is_default_branch and project.default_branch != @configuration.branch_to_rename @logger.debug("Project default branch does not match is_default_branch criteria") next end # Feature: verify new branch doesn't already exist # Verify new branch doesn't exist already begin Gitlab.branch(project.id, @configuration.new_branch_name) @logger.debug("Destination branch #{@configuration.new_branch_name} already exists") next rescue Gitlab::Error::NotFound => e @logger.debug("Destination branch #{@configuration.new_branch_name} does not exist yet") end # Save branch protection parameters old_branch = begin Gitlab.branch(project.id, @configuration.branch_to_rename) rescue Gitlab::Error::NotFound => e @logger.warn("Project #{project.path_with_namespace} doesn't have branch #{@configuration.branch_to_rename}") next end branch_protection_parameters = nil if old_branch.protected # Save branch protection information bpp_raw = Gitlab.protected_branch(project.id, old_branch.name) bpp_hash = bpp_raw.to_hash branch_protection_parameters = bpp_hash.slice("push_access_levels", "merge_access_levels", "unprotect_access_levels", "code_owner_approval_required") @logger.debug("Will transfer branch protection parameters: #{branch_protection_parameters}") end # Confirm or pretend msg = "Rename #{project.path_with_namespace} branch #{@configuration.branch_to_rename} to #{@configuration.new_branch_name} " + (branch_protection_parameters.nil? ? "without" : "with") + " protection" if @configuration.pretend puts(msg) @logger.info(msg) next end unless @configuration.skip_confirm puts(msg) print("\tContinue with rename and delete? (y/N) ") input = gets.chomp if input != "y" msg = "Skipping branch rename for #{project.path_with_namespace} based on user input #{input}" puts(msg) @logger.info(msg) next end @logger.info("User confirmed branch rename for #{project.path_with_namespace}") end # Create new branch begin Gitlab.create_branch(project.id, @configuration.new_branch_name, old_branch.name) @logger.debug("Created new branch with name #{@configuration.new_branch_name}") rescue StandardError => e @logger.error("Error creating new branch: #{e}") next end # Protect branch unless branch_protection_parameters.nil? begin Gitlab.protect_branch(project.id, @configuration.new_branch_name, branch_protection_parameters) @logger.debug("Protected branch with parameters #{branch_protection_parameters}") rescue StandardError => e @logger.error("Error protecting new branch: #{e} with #{branch_protection_parameters}") end end # Feature: Update PRs? Issues? # Change default branch begin Gitlab.edit_project(project.id, {default_branch: @configuration.new_branch_name}) @logger.debug("Changed default branch") rescue StandardError => e @logger.error("Error changing default branch: #{e}") end # Unprotect protected branches if branch_protection_parameters begin Gitlab.unprotect_branch(project.id, old_branch.name) @logger.debug("Unprotected old branch") rescue StandardError => e @logger.error("Error unprotecting old branch: #{e}") end end # Delete old branch begin Gitlab.delete_branch(project.id, old_branch.name) @logger.debug("Deleted old branch") rescue StandardError => e @logger.error("Error deleting old branch: #{e}") end end end |