Class: Flowdock::Git

Inherits:
Object
  • Object
show all
Defined in:
lib/flowdock/git.rb,
lib/flowdock/git/builder.rb

Defined Under Namespace

Classes: Builder, TokenError

Constant Summary collapse

API_ENDPOINT =
"https://api.flowdock.com/v1/git"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Git

Returns a new instance of Git.



24
25
26
27
28
29
30
# File 'lib/flowdock/git.rb', line 24

def initialize(options = {})
  @options = options
  @token = options[:token] || config["flowdock.token"] || raise(TokenError.new("Flowdock API token not found"))
  @commit_url = options[:commit_url] || config["flowdock.commit-url-pattern"] || nil
  @diff_url = options[:diff_url] || config["flowdock.diff-url-pattern"] || nil
  @repo_url = options[:repo_url] || config["flowdock.repository-url"] || nil
end

Class Method Details

.background_post(ref, from, to, options = {}) ⇒ Object



18
19
20
21
# File 'lib/flowdock/git.rb', line 18

def background_post(ref, from, to, options = {})
  git = Git.new(options)
  git.background_post(Git::Builder.new(git.repo, ref, from, to))
end

.post(ref, from, to, options = {}) ⇒ Object



13
14
15
16
# File 'lib/flowdock/git.rb', line 13

def post(ref, from, to, options = {})
  git = Git.new(options)
  git.post(Git::Builder.new(git.repo, ref, from, to))
end

Instance Method Details

#background_post(data) ⇒ Object

Create and post notification in background process. Avoid blocking the push notification.



62
63
64
65
66
67
68
69
70
71
# File 'lib/flowdock/git.rb', line 62

def background_post(data)
  pid = Process.fork
  if pid.nil?
    Grit::Git.with_timeout(600) do
      post(data) # Child
    end
  else
    Process.detach(pid) # Parent
  end
end

#post(data) ⇒ Object

Send git push notification to Flowdock



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/flowdock/git.rb', line 33

def post(data)
  uri = URI.parse("#{API_ENDPOINT}/#{([@token] + tags).join('+')}")
  req = Net::HTTP::Post.new(uri.path)

  payload_hash = data.to_hash
  if @repo_url
    payload_hash[:repository][:url] = @repo_url
  end
  if @commit_url
    payload_hash[:commits].each do |commit|
      commit[:url] = @commit_url % commit[:id]
    end
  end
  if @diff_url
    payload_hash[:compare] = @diff_url % [payload_hash[:before], payload_hash[:after]]
  end

  req.set_form_data(:payload => MultiJson.encode(payload_hash))
  http = Net::HTTP.new(uri.host, uri.port)

  if uri.scheme == 'https'
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    http.use_ssl = true
  end

  http.start { |http| http.request(req) }
end

#repoObject



73
74
75
# File 'lib/flowdock/git.rb', line 73

def repo
  @repo ||= Grit::Repo.new(@options[:repo] || Dir.pwd)
end