Class: Yanapiri::Bot

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(organization, gh_client) ⇒ Bot

Returns a new instance of Bot.



5
6
7
8
# File 'lib/yanapiri/bot.rb', line 5

def initialize(organization, gh_client)
  @organization = organization
  @gh_client = gh_client
end

Instance Attribute Details

#organizationObject (readonly)

Returns the value of attribute organization.



3
4
5
# File 'lib/yanapiri/bot.rb', line 3

def organization
  @organization
end

Instance Method Details

#aplanar_commits!(repo) ⇒ Object



73
74
75
76
77
78
79
80
81
# File 'lib/yanapiri/bot.rb', line 73

def aplanar_commits!(repo)
  repo.chdir do
    `git checkout --orphan new-master master`
    commit! repo, 'Enunciado preparado por Yanapiri'
    `git branch -M new-master master`
  end

  repo.branches.local.reject {|b| b.name == 'master'}.each(&:delete)
end

#clonar_entrega!(nombre) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/yanapiri/bot.rb', line 10

def clonar_entrega!(nombre)
  result = @gh_client.search_repositories "org:#{@organization} #{nombre}\\-", {per_page: 200}
  puts "Encontrados #{result.total_count} repositorios."
  FileUtils.mkdir_p nombre
  Dir.chdir(nombre) do
    result.items.each do |repo|
      if File.exist? repo.name
        puts "Actualizando #{repo.name}..."
        actualizar! repo.name
      else
        puts "Clonando #{repo.name}..."
        clonar! repo.full_name
      end
    end
  end
end

#commit!(repo, mensaje) ⇒ Object



66
67
68
69
70
71
# File 'lib/yanapiri/bot.rb', line 66

def commit!(repo, mensaje)
  repo.add
  repo.commit mensaje, author: git_author
rescue Git::GitExecuteError
  # no se pudo commitear porque no habĂ­a cambios
end

#emailObject



54
55
56
# File 'lib/yanapiri/bot.rb', line 54

def email
  '[email protected]'
end

#git_authorObject



58
59
60
# File 'lib/yanapiri/bot.rb', line 58

def git_author
  "#{nombre} <#{email}>"
end

#github_userObject



62
63
64
# File 'lib/yanapiri/bot.rb', line 62

def github_user
  @gh_client.user
end

#nombreObject



50
51
52
# File 'lib/yanapiri/bot.rb', line 50

def nombre
  'Yanapiri Bot'
end

#preparar_correccion!(entrega, transformaciones = []) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/yanapiri/bot.rb', line 27

def preparar_correccion!(entrega, transformaciones = [])
  if not entrega.hay_cambios?
    crear_issue_advertencia! entrega
  else
    if entrega.ya_preparada?
      entrega.actualizar!
      publicar_cambios! entrega
    else
      entrega.crear_branch_base!
      entrega.crear_branch_entrega!
      transformaciones.select {|t| t.aplica? entrega}.each {|t| t.transformar! entrega, self}
      publicar_cambios! entrega
      crear_pull_request! entrega
    end
  end
end

#preparar_entrega!(nombre, repo_base) ⇒ Object



44
45
46
47
48
# File 'lib/yanapiri/bot.rb', line 44

def preparar_entrega!(nombre, repo_base)
  repo = clonar! repo_base
  aplanar_commits! repo
  publicar_repo! nombre, repo
end