Skip to content

Commit

Permalink
Merge 2e5ccf4 into dd3fa70
Browse files Browse the repository at this point in the history
  • Loading branch information
amaltson committed Nov 25, 2014
2 parents dd3fa70 + 2e5ccf4 commit 798e15d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
19 changes: 17 additions & 2 deletions lib/lita/handlers/github_commits.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,34 @@ def notify_rooms(repo, payload)

def format_message(payload)
commits = payload['commits']
branch = branch_from_ref(payload['ref'])
if commits.size > 0
author = committer_and_author(commits.first)
commit_pluralization = commits.size > 1 ? 'commits' : 'commit'
"[GitHub] Got #{commits.size} new #{commit_pluralization} from #{commits.first['author']['name']} on #{payload['repository']['owner']['name']}/#{payload['repository']['name']}"
"[GitHub] Got #{commits.size} new #{commit_pluralization} #{author} on #{payload['repository']['owner']['name']}/#{payload['repository']['name']} on the #{branch} branch"
elsif payload['created']
"[GitHub] #{payload['pusher']['name']} created: #{payload['ref']}: #{payload['base_ref']}"
elsif payload['deleted']
"[GitHub] #{payload['pusher']['name']} deleted: #{payload['ref']}"
end
rescue
Lita.logger.warn "Error formatting message for #{repo} repo. Payload: #{payload}"
Lita.logger.warn "Error formatting message for payload: #{payload}"
return
end

def branch_from_ref(ref)
ref.split('/').last
end

def committer_and_author(commit)
if commit['author']['username'] != commit['committer']['username']
"authored by #{commit['author']['name']} and committed by " +
"#{commit['committer']['name']}"
else
"from #{commit['author']['name']}"
end
end

def rooms_for_repo(repo)
rooms = Lita.config.handlers.github_commits.repos[repo]

Expand Down
19 changes: 17 additions & 2 deletions spec/lita/handlers/github_commits_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
expect(robot).to receive(:send_message) do |target, message|
expect(target.room).to eq("#baz")
expect(message).to eq(
"[GitHub] Got 3 new commits from Garen Torikian on octokitty/testing")
"[GitHub] Got 3 new commits from Garen Torikian on octokitty/testing on the master branch")
end
subject.receive(request, response)
end
Expand All @@ -47,12 +47,27 @@
expect(robot).to receive(:send_message) do |target, message|
expect(target.room).to eq("#baz")
expect(message).to eq(
"[GitHub] Got 1 new commit from Garen Torikian on octokitty/testing")
"[GitHub] Got 1 new commit from Garen Torikian on octokitty/testing on the master branch")
end
subject.receive(request, response)
end
end

context "request with commits" do
before do
Lita.config.handlers.github_commits.repos["octokitty/testing"] = "#baz"
allow(params).to receive(:[]).with("payload").and_return(valid_payload_diff_committer)
end

it "sends a notification message to the applicable rooms" do
expect(robot).to receive(:send_message) do |target, message|
expect(message).to eq(
"[GitHub] Got 3 new commits authored by Garen Torikian and " +
"committed by Repository Owner on octokitty/testing on the master branch")
end
subject.receive(request, response)
end
end


context "create payload" do
Expand Down
13 changes: 13 additions & 0 deletions spec/lita/handlers/payload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,19 @@ def valid_payload_one_commit
payload.to_json
end

def valid_payload_diff_committer
payload = MultiJson.load(valid_payload, symbolize_keys: true)
payload[:commits] = payload[:commits].map do |commit|
commit[:committer] = {
email: "committer@noway.biz",
name: "Repository Owner",
username: "owner"
}
commit
end
payload.to_json
end

def valid_payload
<<-JSON.chomp
{
Expand Down

0 comments on commit 798e15d

Please sign in to comment.