class GitHubWebHooksReceiver::Repository

Public Class Methods

new(domain, owner_name, name, payload, options) click to toggle source
# File lib/github-web-hooks-receiver/repository.rb, line 26
def initialize(domain, owner_name, name, payload, options)
  @domain = domain
  @owner_name = owner_name
  @name = name
  @payload = payload
  @options = options
  @to = @options[:to]
  @max_n_retries = (@options[:n_retries] || 3).to_i
  raise Error.new("mail receive address is missing: <#{@name}>") if @to.nil?
end

Public Instance Methods

enabled?() click to toggle source
# File lib/github-web-hooks-receiver/repository.rb, line 37
def enabled?
  enabled = @options[:enabled]
  enabled = true if enabled.nil?
  enabled
end
lock(path) { || ... } click to toggle source
# File lib/github-web-hooks-receiver/repository.rb, line 64
def lock(path)
  File.open(path, "w") do |file|
    file.flock(File::LOCK_EX)
    yield
  end
end
process(before, after, reference) click to toggle source
# File lib/github-web-hooks-receiver/repository.rb, line 43
def process(before, after, reference)
  FileUtils.mkdir_p(File.dirname(mirror_path))
  n_retries = 0
  lock("#{mirror_path}.lock") do
    begin
      if File.exist?(mirror_path)
        git("--git-dir", mirror_path, "fetch", "--quiet", "--prune")
      else
        git("clone", "--quiet",
            "--mirror", @payload.repository_url,
            mirror_path)
      end
    rescue Error
      n_retries += 1
      retry if n_retries <= @max_n_retries
      raise
    end
  end
  send_commit_email(before, after, reference)
end
send_commit_email(before, after, reference) click to toggle source
# File lib/github-web-hooks-receiver/repository.rb, line 71
def send_commit_email(before, after, reference)
  options = [
    "--repository", mirror_path,
    "--max-size", "1M"
  ]
  if @payload.gitlab?
    if @payload.gitlab_wiki?
      add_option(options, "--repository-browser", "gitlab-wiki")
      gitlab_project_uri = @payload["project"]["homepage"]
    else
      add_option(options, "--repository-browser", "gitlab")
      gitlab_project_uri = @payload["repository"]["homepage"]
    end
    add_option(options, "--gitlab-project-uri", gitlab_project_uri)
  else
    if @payload.github_gollum?
      add_option(options, "--repository-browser", "github-wiki")
    else
      add_option(options, "--repository-browser", "github")
    end
    add_option(options, "--github-user", @owner_name)
    add_option(options, "--github-repository", @name)
    name = "#{@owner_name}/#{@name}"
    name << ".wiki" if @payload.github_gollum?
    add_option(options, "--name", name)
  end
  add_option(options, "--from", from)
  add_option(options, "--from-domain", from_domain)
  add_option(options, "--sender", sender)
  add_option(options, "--sleep-per-mail", sleep_per_mail)
  options << "--send-per-to" if send_per_to?
  options << "--add-html" if add_html?
  error_to.each do |_error_to|
    options.concat(["--error-to", _error_to])
  end
  if @to.is_a?(Array)
    options.concat(@to)
  else
    options << @to
  end
  command_line = [ruby, git_commit_mailer, *options].collect do |component|
    Shellwords.escape(component)
  end.join(" ")
  change = "#{before} #{after} #{reference}"
  status = nil
  output = capture_output do
    IO.popen(command_line, "w") do |io|
      io.puts(change)
    end
    status = $?
  end
  unless status.success?
    raise Error.new("failed to run git-commit-mailer: " +
                    "<#{command_line}>:<#{change}>:<#{output}>")
  end
end

Private Instance Methods

add_html?() click to toggle source
# File lib/github-web-hooks-receiver/repository.rb, line 195
def add_html?
  @options[:add_html]
end
add_option(options, name, value) click to toggle source
# File lib/github-web-hooks-receiver/repository.rb, line 209
def add_option(options, name, value)
  return if value.nil?
  value = value.to_s
  return if value.empty?
  options.concat([name, value])
end
capture_output() { || ... } click to toggle source
# File lib/github-web-hooks-receiver/repository.rb, line 216
def capture_output
  output = StringIO.new
  stdout = $stdout
  stderr = $stderr
  begin
    $stdout = output
    $stderr = output
    yield
  ensure
    $stdout = stdout
    $stderr = stderr
  end
  output.string
end
commit_email() click to toggle source
# File lib/github-web-hooks-receiver/repository.rb, line 161
def commit_email
  @commit_email ||=
    @options[:commit_email] ||
    path("..", "commit-email.rb")
end
error_to() click to toggle source
# File lib/github-web-hooks-receiver/repository.rb, line 187
def error_to
  @error_to ||= force_array(@options[:error_to])
end
force_array(value) click to toggle source
# File lib/github-web-hooks-receiver/repository.rb, line 199
def force_array(value)
  if value.is_a?(Array)
    value
  elsif value.nil?
    []
  else
    [value]
  end
end
from() click to toggle source
# File lib/github-web-hooks-receiver/repository.rb, line 171
def from
  @from ||= @options[:from]
end
from_domain() click to toggle source
# File lib/github-web-hooks-receiver/repository.rb, line 175
def from_domain
  @from_domain ||= @options[:from_domain]
end
git(*arguments) click to toggle source
# File lib/github-web-hooks-receiver/repository.rb, line 129
def git(*arguments)
  arguments = arguments.collect {|argument| argument.to_s}
  command_line = [git_command, *arguments]
  unless system(*command_line)
    raise Error.new("failed to run command: <#{command_line.join(' ')}>")
  end
end
git_command() click to toggle source
# File lib/github-web-hooks-receiver/repository.rb, line 137
def git_command
  @git ||= @options[:git] || "git"
end
git_commit_mailer() click to toggle source
# File lib/github-web-hooks-receiver/repository.rb, line 167
def git_commit_mailer
  @git_commit_mailer ||= @options[:git_commit_mailer] || commit_email
end
mirror_path() click to toggle source
# File lib/github-web-hooks-receiver/repository.rb, line 147
def mirror_path
  components = [mirrors_directory, @domain, @owner_name]
  if @payload.github_gollum? or @payload.gitlab_wiki?
    components << "#{@name}.wiki"
  else
    components << @name
  end
  File.join(*components)
end
mirrors_directory() click to toggle source
# File lib/github-web-hooks-receiver/repository.rb, line 141
def mirrors_directory
  @mirrors_directory ||=
    @options[:mirrors_directory] ||
    path("mirrors")
end
ruby() click to toggle source
# File lib/github-web-hooks-receiver/repository.rb, line 157
def ruby
  @ruby ||= @options[:ruby] || RbConfig.ruby
end
send_per_to?() click to toggle source
# File lib/github-web-hooks-receiver/repository.rb, line 191
def send_per_to?
  @options[:send_per_to]
end
sender() click to toggle source
# File lib/github-web-hooks-receiver/repository.rb, line 179
def sender
  @sender ||= @options[:sender]
end
sleep_per_mail() click to toggle source
# File lib/github-web-hooks-receiver/repository.rb, line 183
def sleep_per_mail
  @sleep_per_mail ||= @options[:sleep_per_mail]
end