Module: GitHub::Payload::Helpers::Push

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.sample_payloadObject



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/github/payload/helpers/push.rb', line 173

def self.sample_payload
  {
    "after"  => "a47fd41f3aa4610ea527dcc1669dfdb9c15c5425",
    "ref"    => "refs/heads/master",
    "before" => "4c8124ffcf4039d292442eeccabdeca5af5c5017",
    "compare" => "http://github.com/mojombo/grit/compare/4c8124f...a47fd41",

    "repository" => {
      "name"  => "grit",
      "url"   => "http://github.com/mojombo/grit",
      "owner" => { "name" => "mojombo", "email" => "[email protected]" }
    },

    "pusher" => {
      "name" => "rtomayko"
    },

    "commits" => [
      {
        "distinct"  => true,
        "removed"   => [],
        "message"   => "stub git call for Grit#heads test f:15 Case#1",
        "added"     => [],
        "timestamp" => "2007-10-10T00:11:02-07:00",
        "modified"  => ["lib/grit/grit.rb", "test/helper.rb", "test/test_grit.rb"],
        "url"       => "http://github.com/mojombo/grit/commit/06f63b43050935962f84fe54473a7c5de7977325",
        "author"    => { "name" => "Tom Preston-Werner", "email" => "[email protected]" },
        "id"        => "06f63b43050935962f84fe54473a7c5de7977325"
      },
      {
        "distinct"  => true,
        "removed"   => [],
        "message"   => "clean up heads test f:2hrs",
        "added"     => [],
        "timestamp" => "2007-10-10T00:18:20-07:00",
        "modified"  => ["test/test_grit.rb"],
        "url"       => "http://github.com/mojombo/grit/commit/5057e76a11abd02e83b7d3d3171c4b68d9c88480",
        "author"    => { "name" => "Tom Preston-Werner", "email" => "[email protected]" },
        "id"        => "5057e76a11abd02e83b7d3d3171c4b68d9c88480"
      },
      {
        "distinct"  => true,
        "removed"   => [],
        "message"   => "add more comments throughout",
        "added"     => [],
        "timestamp" => "2007-10-10T00:50:39-07:00",
        "modified"  => ["lib/grit.rb", "lib/grit/commit.rb", "lib/grit/grit.rb"],
        "url"       => "http://github.com/mojombo/grit/commit/a47fd41f3aa4610ea527dcc1669dfdb9c15c5425",
        "author"    => { "name" => "Tom Preston-Werner", "email" => "[email protected]" },
        "id"        => "a47fd41f3aa4610ea527dcc1669dfdb9c15c5425"
      }
    ]
  }
end

Instance Method Details

#after_shaObject



43
44
45
# File 'lib/github/payload/helpers/push.rb', line 43

def after_sha
  payload['after'][0..6]
end

#after_sha_urlObject



143
144
145
# File 'lib/github/payload/helpers/push.rb', line 143

def after_sha_url
  repo_url + "/commit/#{after_sha}"
end

#base_refObject



21
22
23
# File 'lib/github/payload/helpers/push.rb', line 21

def base_ref
  payload['base_ref']
end

#base_ref_nameObject



35
36
37
# File 'lib/github/payload/helpers/push.rb', line 35

def base_ref_name
  payload['base_ref_name'] ||= base_ref.sub(/\Arefs\/(heads|tags)\//, '')
end

#before_shaObject



39
40
41
# File 'lib/github/payload/helpers/push.rb', line 39

def before_sha
  payload['before'][0..6]
end

#before_sha_urlObject



139
140
141
# File 'lib/github/payload/helpers/push.rb', line 139

def before_sha_url
  repo_url + "/commit/#{before_sha}"
end

#branch_urlObject



135
136
137
# File 'lib/github/payload/helpers/push.rb', line 135

def branch_url
  repo_url + "/commits/#{branch_name}"
end

#commit_messagesObject



52
53
54
55
56
# File 'lib/github/payload/helpers/push.rb', line 52

def commit_messages
  distinct_commits.map do |commit|
    format_commit_message(commit)
  end
end

#commitsObject



163
164
165
# File 'lib/github/payload/helpers/push.rb', line 163

def commits
  Array(payload['commits'])
end

#compare_urlObject



131
132
133
# File 'lib/github/payload/helpers/push.rb', line 131

def compare_url
  payload['compare']
end

#created?Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/github/payload/helpers/push.rb', line 5

def created?
  payload['created'] || !!(payload['before'] =~ /0{40}/)
end

#deleted?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/github/payload/helpers/push.rb', line 9

def deleted?
  payload['deleted'] || !!(payload['after'] =~ /0{40}/)
end

#distinct_commitsObject



167
168
169
170
171
# File 'lib/github/payload/helpers/push.rb', line 167

def distinct_commits
  payload['distinct_commits'] ||= Array(commits.select do |commit|
    commit['distinct'] and !commit['message'].to_s.strip.empty?
  end)
end

#forced?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/github/payload/helpers/push.rb', line 13

def forced?
  payload['forced']
end

#format_commit_message(commit) ⇒ Object



47
48
49
50
# File 'lib/github/payload/helpers/push.rb', line 47

def format_commit_message(commit)
  short = commit['message'].split("\n", 2).first
  "[#{repo_name}/#{branch_name}] #{short} - #{commit['author']['name']}"
end

#name_with_ownerObject



159
160
161
# File 'lib/github/payload/helpers/push.rb', line 159

def name_with_owner
  File.join(owner_name, repo_name)
end

#owner_nameObject



151
152
153
# File 'lib/github/payload/helpers/push.rb', line 151

def owner_name
  payload['repository']['owner']['name']
end

#pusher_nameObject



147
148
149
# File 'lib/github/payload/helpers/push.rb', line 147

def pusher_name
  payload.include?('pusher') ? payload['pusher']['name'] : "somebody"
end

#refObject



17
18
19
# File 'lib/github/payload/helpers/push.rb', line 17

def ref
  payload['ref'].to_s
end

#ref_nameObject Also known as: tag_name, branch_name



29
30
31
# File 'lib/github/payload/helpers/push.rb', line 29

def ref_name
  payload['ref_name'] ||= ref.sub(/\Arefs\/(heads|tags)\//, '')
end

#repo_nameObject



155
156
157
# File 'lib/github/payload/helpers/push.rb', line 155

def repo_name
  payload['repository']['name']
end

#repo_urlObject



127
128
129
# File 'lib/github/payload/helpers/push.rb', line 127

def repo_url
  payload['repository']['url']
end

#summary_messageObject



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
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/github/payload/helpers/push.rb', line 58

def summary_message
  message = []
  message << "[#{repo_name}] #{pusher_name}"

  if created?
    if tag?
      message << "tagged #{tag_name} at"
      message << (base_ref ? base_ref_name : after_sha)
    else
      message << "created #{branch_name}"

      if base_ref
        message << "from #{base_ref_name}"
      elsif distinct_commits.empty?
        message << "at #{after_sha}"
      end

      if distinct_commits.any?
        num = distinct_commits.size
        message << "(+#{num} new commit#{num > 1 ? 's' : ''})"
      end
    end

  elsif deleted?
    message << "deleted #{branch_name} at #{before_sha}"

  elsif forced?
    message << "force-pushed #{branch_name} from #{before_sha} to #{after_sha}"

  elsif commits.any? and distinct_commits.empty?
    if base_ref
      message << "merged #{base_ref_name} into #{branch_name}"
    else
      message << "fast-forwarded #{branch_name} from #{before_sha} to #{after_sha}"
    end

  elsif distinct_commits.any?
    num = distinct_commits.size
    message << "pushed #{num} new commit#{num > 1 ? 's' : ''} to #{branch_name}"

  else
    message << "pushed nothing"
  end

  message.join(' ')
end

#summary_urlObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/github/payload/helpers/push.rb', line 105

def summary_url
  if created?
    if distinct_commits.empty?
      branch_url
    else
      compare_url
    end

  elsif deleted?
    before_sha_url

  elsif forced?
    branch_url

  elsif distinct_commits.size == 1
    distinct_commits.first['url']

  else
    compare_url
  end
end

#tag?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/github/payload/helpers/push.rb', line 25

def tag?
  !!(ref =~ %r|^refs/tags/|)
end