Class: GitHubWebHooksReceiver::Repository

Inherits:
Object
  • Object
show all
Includes:
PathResolver
Defined in:
lib/github-web-hooks-receiver/repository.rb

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Methods included from PathResolver

#base_dir, #path

Constructor Details

#initialize(domain, owner_name, name, payload, options) ⇒ Repository

Returns a new instance of Repository.

Raises:



24
25
26
27
28
29
30
31
32
33
# File 'lib/github-web-hooks-receiver/repository.rb', line 24

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

Instance Method Details

#process(before, after, reference) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/github-web-hooks-receiver/repository.rb', line 41

def process(before, after, reference)
  FileUtils.mkdir_p(mirrors_directory)
  n_retries = 0
  begin
    if File.exist?(mirror_path)
      git("--git-dir", mirror_path, "fetch", "--quiet")
    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
  send_commit_email(before, after, reference)
end

#send_commit_email(before, after, reference) ⇒ Object



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
# File 'lib/github-web-hooks-receiver/repository.rb', line 60

def send_commit_email(before, after, reference)
  options = [
    "--repository", mirror_path,
    "--max-size", "1M"
  ]
  if @payload.gitlab?
    add_option(options, "--repository-browser", "gitlab")
    gitlab_project_uri = @payload["repository"]["homepage"]
    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, commit_email, *options].collect do |component|
    Shellwords.escape(component)
  end.join(" ")
  change = "#{before} #{after} #{reference}"
  IO.popen(command_line, "w") do |io|
    io.puts(change)
  end
  unless $?.success?
    raise Error.new("failed to run commit-email.rb: " +
                    "<#{command_line}>:<#{change}>")
  end
end

#target?Boolean

Returns:

  • (Boolean)


35
36
37
38
39
# File 'lib/github-web-hooks-receiver/repository.rb', line 35

def target?
  (@options[:targets] || [/\A[a-z\d_.\-]+\z/i]).any? do |target|
    target === @name
  end
end