Class: GerritSeed::Gerrit

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host:, log:, port:, user:) ⇒ Gerrit

Returns a new instance of Gerrit.



5
6
7
8
9
10
11
# File 'lib/gerrit_seed/gerrit.rb', line 5

def initialize(host:, log:, port:, user:)
  @host = host
  @log = log
  @port = port
  @shell = Shell.new
  @user = user
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



3
4
5
# File 'lib/gerrit_seed/gerrit.rb', line 3

def host
  @host
end

#logObject (readonly)

Returns the value of attribute log.



3
4
5
# File 'lib/gerrit_seed/gerrit.rb', line 3

def log
  @log
end

#portObject (readonly)

Returns the value of attribute port.



3
4
5
# File 'lib/gerrit_seed/gerrit.rb', line 3

def port
  @port
end

#shellObject (readonly)

Returns the value of attribute shell.



3
4
5
# File 'lib/gerrit_seed/gerrit.rb', line 3

def shell
  @shell
end

#userObject (readonly)

Returns the value of attribute user.



3
4
5
# File 'lib/gerrit_seed/gerrit.rb', line 3

def user
  @user
end

Instance Method Details

#create_change(change, changes:, git:, users:) ⇒ Object



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
# File 'lib/gerrit_seed/gerrit.rb', line 76

def create_change(change, changes:, git:, users:, **)
  branch_name = branch_name_of(change[:name])
  parent_change = changes.detect do |other_change|
    other_change[:project] == change[:project] &&
    other_change[:name].start_with?(change[:parent])
  end

  parent_branch_name = if parent_change
    branch_name_of(parent_change[:name])
  else
    change[:parent]
  end

  author = users.detect { |x| x[:username] == change[:author] }

  unless author
    fail "Author '#{change[:author]}' for change '#{change[:name]}' could not be found"
  end

  git.checkout(
    branch: branch_name,
    commit: parent_branch_name,
  )

  if change[:files]
    change[:files].map do |file| File.expand_path(file, git.dir) end.each do |file|
      FileUtils.mkdir_p(File.dirname(file)) unless File.exist?(File.dirname(file))
      FileUtils.touch(file)
    end

    git.shell.("git add .")
  else
    git.shell.("touch #{branch_name}.in")
    git.shell.("git add #{branch_name}.in")
  end

  git.commit(
    author: author[:full_name],
    email: author[:email],
    subject: change[:name],
  )

  unless (change)
    git.push(
      user: author[:username],
      reviewers: %w[admin] + users.map { |x| x[:username] }
    )

    log.ok("change: #{change[:project]} - #{change[:name]}")
  end
end

#create_project(name:) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/gerrit_seed/gerrit.rb', line 13

def create_project(name:, **)
  unless exec('gerrit ls-projects').match(name)
    exec('gerrit create-project',
      name,
      '--change-id', 'TRUE',
      '--empty-commit',
      '--require-change-id'
    )

    log.ok("project: #{name}")
  end
end

#create_user(email:, group:, full_name:, username:, ssh_key:) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/gerrit_seed/gerrit.rb', line 49

def create_user(email:, group:, full_name:, username:, ssh_key:, **)
  begin
    exec('gerrit create-account', '--group', sq(group), username)
    log.ok("user: #{username}")
  rescue Shell::CommandError => e
    raise unless e.output.strip == "fatal: username '#{username}' already exists"
  end

  exec(
    'gerrit', 'set-account',
      '--active',
      '--full-name',    sq(full_name),
      '--add-email',    sq(email),
      '--add-ssh-key',  '-',
      '--preferred-email', sq(email),
      username,
    stdin: File.read(File.expand_path(ssh_key))
  )
end

#delete_project(name:) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/gerrit_seed/gerrit.rb', line 26

def delete_project(name:, **)
  if has_project?(name)
    begin
      exec('delete-project', 'delete', '--force', '--yes-really-delete', name)
    rescue Shell::CommandError => e
      raise if has_project?(name) || e.output.strip != 'fatal: internal server error'
    end

    log.ok("project: #{name}")
  end
end

#delete_user(username:) ⇒ Object



69
70
71
72
73
74
# File 'lib/gerrit_seed/gerrit.rb', line 69

def delete_user(username:, **)
  exec('gerrit set-account', '--inactive', username)
  log.ok("user: #{username}")
rescue Shell::CommandError => e
  raise unless e.output.strip == 'fatal: account not active'
end

#has_project?(name) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/gerrit_seed/gerrit.rb', line 38

def has_project?(name)
  exec('gerrit ls-projects').lines.map(&:strip).include?(name)
end

#install_plugin(name:, url:) ⇒ Object



42
43
44
45
46
47
# File 'lib/gerrit_seed/gerrit.rb', line 42

def install_plugin(name:, url:)
  unless JSON.parse(exec('gerrit plugin ls --format JSON')).key?(name.to_s)
    exec('gerrit', 'plugin', 'install', "'#{url}'")
    log.ok("plugin: #{name}")
  end
end