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:



26
27
28
29
30
31
32
33
34
35
# 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

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)


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

def enabled?
  enabled = @options[:enabled]
  enabled = true if enabled.nil?
  enabled
end

#lock(path) ⇒ Object



64
65
66
67
68
69
# 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) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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) ⇒ Object



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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# 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