Class: GitRid

Inherits:
Object
  • Object
show all
Defined in:
lib/git_rid.rb

Class Method Summary collapse

Class Method Details

.perform!(options) ⇒ Object



4
5
6
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
# File 'lib/git_rid.rb', line 4

def self.perform!(options)
  updated_name = options[:updated]
  outdated_name = options[:outdated]

  Gitlab.configure do |config|
    config.endpoint       = options[:url] || ENV['GITLAB_API_ENDPOINT']
    config.private_token  = options[:token] || ENV['GITLAB_API_PRIVATE_TOKEN']
  end

  projects = Gitlab.projects(min_access_level: 40)

  if projects.empty?
    puts "Looks like you aren't a maintainer on any projects :("
    exit 1
  end

  projects.auto_paginate do |project|
    unless options[:yes]
      confirm = ask("Update #{project.name}? [Y/N] ") { |yn| yn.limit = 1 }
      next if confirm.downcase == 'n'
      exit unless confirm.downcase == 'y'
    end
    mb = Gitlab.branch(project.id, outdated_name)

    Gitlab.create_branch(project.id, updated_name, mb.commit.id)

    if mb.protected
      Gitlab.protect_branch(project.id, updated_name)
      Gitlab.unprotect_branch(project.id, outdated_name)
    end

    if project.default_branch == outdated_name
      Gitlab.edit_project(project.id, {default_branch: updated_name})
    end

    Gitlab.delete_branch(project.id, outdated_name)
    puts "Updated #{project.name} and replaced #{outdated_name} with #{updated_name}"
  rescue Gitlab::Error::NotFound => e
    puts "Skipping #{project.name}, it doesn't have a #{outdated_name} branch!"
  rescue Gitlab::Error::BadRequest
    puts "Skipping #{project.name} because it already had a branch called #{updated_name}... you'll have to sort that out yourself."
  end
end