Module: PushHandler

Extended by:
PushHandler
Included in:
PushHandler
Defined in:
lib/push_handler.rb,
lib/push_handler/config.rb

Defined Under Namespace

Classes: Config

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

Returns the value of attribute config.



12
13
14
# File 'lib/push_handler.rb', line 12

def config
  @config
end

Instance Method Details

#configure {|self.config| ... } ⇒ Object

this configure method implementation is taken from github.com/andrew/split/blob/master/lib/split.rb

Yields:



16
17
18
19
# File 'lib/push_handler.rb', line 16

def configure
  self.config ||= Config.new
  yield(self.config)
end

#construct_payload(old_commit, new_commit, ref_name) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
# File 'lib/push_handler.rb', line 36

def construct_payload(old_commit, new_commit, ref_name)
  hash_to_return = Hash.new({})
  hash_to_return = {
    'after' => new_commit,
    'before' => old_commit,
    'ref' => ref_name,
    'branch_url' => config.urls['branch'] % ref_name[/\/([^\/]+)$/, 1]
  }

  hash_to_return['repository'] = {
    'url' => config.repo['url'],
    'name' => config.repo['name'],
    'owner' => config.repo['owner']
  }
  repo = Grit::Repo.new(config.repo['working_dir'], :is_bare => config.repo['is_bare'])
  commits = Grit::Commit.find_all(
    repo,
    "#{old_commit}..#{new_commit}",
    :'no-merges' => true
  ).reverse

  unless commits.empty?
    hash_to_return['pusher'] = {
      'name' => commits.first.author.name
    }
    hash_to_return['commits'] = commits.collect do |commit|
      Hash.new.tap do |commit_hash|
        commit_hash['distinct'] = true
        commit_hash['id'] = commit.sha
        commit_hash['message'] = commit.message
        commit_hash['url'] = (config.urls['commit'] || config.commit_url) % commit.sha
        commit_hash['author'] = {
          'name'  => commit.author.name,
          'email' => commit.author.email
        }

        # accomodate the odd formatting they have in their timestamp
        timestamp = commit.authored_date.gmtime.strftime('%FT%T%z')
        commit_hash['timestamp'] = timestamp[0..-3] + ':' + timestamp[-2..-1]

        # figure out what, if any files should be added to the 
        # removed, added, or modified arrays
        commit_hash['removed']  = []
        commit_hash['added']    = []
        commit_hash['modified'] = []
        commit.diffs.each do |diff|
          commit_hash['removed']  << diff.a_path if diff.deleted_file
          commit_hash['added']    << diff.b_path if diff.new_file
          commit_hash['modified'] << diff.a_path if !diff.new_file && !diff.deleted_file
        end
      end
    end
  end

  hash_to_return
end

#send_to_services(old_commit, new_commit, ref_name) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/push_handler.rb', line 21

def send_to_services(old_commit, new_commit, ref_name)
  payload = construct_payload(old_commit, new_commit, ref_name)
  config.services['data'].each_pair do |service, data|
    Typhoeus::Request.post(
      config.services['url'] + '/' + service + '/push',
      :method => :post,
      :params => {
        'data' => data.to_json,
        'payload' => payload.to_json
      },
      :disable_ssl_peer_verification => true
    )
  end
end